相关文章推荐
火爆的手术刀  ·  ardrone_autonomy - ...·  8 月前    · 
善良的番茄  ·  悦读|一生不长,要和人品好的人交往_新华报业网·  1 年前    · 
快乐的显示器  ·  中华人民共和国农业机械化促进法_中国人大网·  1 年前    · 
讲道义的小熊猫  ·  在“马王堆学”园地中耕耘—写在《长沙马王堆汉 ...·  1 年前    · 
果断的甘蔗  ·  茶啊二中人物原型是哪个-抖音·  1 年前    · 
小百科  ›  NodeMCU + socket.io | Codementor
NodeMCU printf payload socket nodemcu
着急的小马驹
1 年前
  • Find Developers & Mentors
    • Web Development
      • Angular
      • ASP.NET
      • Django
      • Express
      • HTML/CSS
      • jQuery
      • Laravel
      • Node.js
      • Rails
      • React
      • Redux
      • Vue.js
    • Mobile App Development
      • Android
      • iOS
      • Ionic
      • Kotlin
      • React Native
      • Swift
      • Xcode
    • Programming Languages
      • C++
      • C#
      • C
      • Golang
      • Java
      • JavaScript
      • PHP
      • Python
      • R
      • Ruby
      • TypeScript
    • Data Science /Engineering
      • AI
      • Machine Learning
      • Matlab
      • Tableau
      • Tensorflow
    • Database /Operations
      • AWS
      • Database
      • Docker
      • GCP
      • Heroku
      • Linux
      • MongoDB
      • MySQL
      • Postgres
      • SQL
    • Others
      • Arduino
      • Bash
      • Electron
      • Firebase
      • Game
      • Git
      • Rasberry Pi
      • Selenium WebDriver
      • Stripe
      • Unity 3D
      • Visual Studio
      • WordPress
  • Learning Center
    Blog
    Get insights on scaling, management, and product development for founders and engineering managers.
    Community Posts
    Read programming tutorials, share your knowledge, and become better developers together.
    Hot Topics
    • Android
    • Angular
    • iOS
    • JavaScript
    • Node.js
    • Python
    • React
    • Blockchain
    • Ethereum
SIGN UP
LOG IN
Find Developers & Mentors Community Post Blog SIGN UP LOG IN
Alex Polymath
Follow
Let me solve some troubles

NodeMCU + socket.io

Published Sep 26, 2022
NodeMCU + socket.io

Hello, my name is Alex. And I'm making different cool stuff.
This article is just kind of a note for myself, so it was easier to recall stuff later. But if you'll find code examples userfull - great!

So the idea is very simple. I want to build small car with remote control via internet. So I 3d printed what you can see on the picture, placed cheap photo and added nodeMCU.

Nodemcu will receive move commands via sockets. And phone will just be standalone thing with video-chat opened on it.

Part 1 - socket.io server

It's very stupid, but it will help you to understand if nodeMCU is connected and it receives commands 💪

index.js

const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer, {
var i = 0;
io.on("connection", (socket) => {
        console.log('connected')
        setInterval(()=>{
                io.emit("hello", "world " + i);
        }, 50)
httpServer.listen(3000);

package.json

"name": "socket_server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" "author": "", "license": "ISC", "dependencies": { "cors": "^2.8.5", "express": "^4.18.1", "socket.io": "^4.5.2"

Part 2 - nodeMCU

Be carefull, you'll need to install bunch of libraries in arduino IDE.
Sketch -> include library -> ...

And maybe some other libraries as well :3

#include <ESP8266WiFi.h> // Include the Wi-Fi library #include <ESP8266WiFiMulti.h> #include <Arduino.h> #include <ArduinoJson.h> const char* ssid = "WIFINAME"; // The SSID (name) of the Wi-Fi network you want to connect 2.4ghz! const char* password = "your password!"; // The password of the Wi-Fi network #include <WebSocketsClient.h> #include <SocketIOclient.h> #include <Hash.h> SocketIOclient socketIO; #define USE_SERIAL Serial void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) { switch (type) { case sIOtype_DISCONNECT: USE_SERIAL.printf("[IOc] Disconnected!\n"); break; case sIOtype_CONNECT: USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload); // join default namespace (no auto join in Socket.IO V3) socketIO.send(sIOtype_CONNECT, "/"); break; case sIOtype_EVENT: USE_SERIAL.printf("[IOc] get event: %s\n", payload); break; case sIOtype_ACK: USE_SERIAL.printf("[IOc] get ack: %u\n", length); hexdump(payload, length); break; case sIOtype_ERROR: USE_SERIAL.printf("[IOc] get error: %u\n", length); hexdump(payload, length); break; case sIOtype_BINARY_EVENT: USE_SERIAL.printf("[IOc] get binary: %u\n", length); hexdump(payload, length); break; case sIOtype_BINARY_ACK: USE_SERIAL.printf("[IOc] get binary ack: %u\n", length); hexdump(payload, length); break; void setup() { Serial.begin(115200); // Start the Serial communication to send messages to the computer delay(10); Serial.println('\n'); WiFi.begin(ssid, password); // Connect to the network Serial.print("Connecting to "); Serial.print(ssid); Serial.println(" ..."); int i = 0; while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect delay(1000); Serial.print(++i); Serial.print(' '); Serial.println('\n'); Serial.println("Connection established!"); Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer //put your server ip address socketIO.begin("xx.xx.xx.xx", 3000, "/socket.io/?EIO=4"); // event handler socketIO.onEvent(socketIOEvent); void loop() { socketIO.loop();

I'll post some videos when it will finally work.
I need to set up website with sockets to control car and video-chat to see what's happening.

Node.js Arduino Internet of things Video streaming Socket.io
Report

Enjoy this post? Give Alex Polymath a like if it's helpful.

1
1
Share
Alex Polymath
Let me solve some troubles
Hello, my name is Alex Polymath! I'm indie-hacker, creator of 📷 http://colorize.cc (NuxtJS + FastAI NN) 🎥 http://youstreamer.com (NodeJS + VueJS + liveStreaming)
 
推荐文章
火爆的手术刀  ·  ardrone_autonomy - ROS Wiki
8 月前
善良的番茄  ·  悦读|一生不长,要和人品好的人交往_新华报业网
1 年前
快乐的显示器  ·  中华人民共和国农业机械化促进法_中国人大网
1 年前
讲道义的小熊猫  ·  在“马王堆学”园地中耕耘—写在《长沙马王堆汉墓简帛集成》出版之际 ...
1 年前
果断的甘蔗  ·  茶啊二中人物原型是哪个-抖音
1 年前
Link管理   ·   51好读   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南