├── README.md
├── server
├── .gitignore
├── README.md
├── index.js
└── package.json
└── socketIO.ino
/README.md:
--------------------------------------------------------------------------------
1 | # esp32_SocketIO
2 |
3 | Simple example using the [socketIO](https://socket.io/) library on ESP 32 boards.
4 |
5 | ## How to run
6 | After setting up Arduino IDE to accept ESP32 devices (if you didn't, [try this tutorial](https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/)):
7 |
8 | 1. Install the `WebSockets by Markus Settler` library on your IDE with _Sketch > Include Library > Manage Libraries..._
9 | 2. Install the `Socket IO Client` library available on [this repository](https://github.com/timum-viw/socket.io-client). Note: I've tried to use the version available on the library manager of Arduino IDE but I got some error messages on console. So, I had to download and install the .zip from the last commit of the library repo (I've used [this commit](https://github.com/timum-viw/socket.io-client/tree/5f5ccd94c38b5b6a5284500dc9d3e95f92298d3b)).
10 |
11 | After those two steps you are ready to go!
12 |
13 | If you want to make a quick test, you can use the server folder! [Check that out!](https://github.com/Valgueiro/esp32_SocketIO/tree/master/server)
14 |
--------------------------------------------------------------------------------
/server/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
3 | package-lock.json
--------------------------------------------------------------------------------
/server/README.md:
--------------------------------------------------------------------------------
1 | # Server
2 |
3 | Simple Node.js (express) server eaxmple to receive and send data to ESP 32. Some importante informations:
4 | * It will be listening on port `3000`.
5 | * You can send a POST request to this server (at `/sendData`) and it will send a message to connect devices.
6 |
7 | ## How to Build and Run
8 | ### Download Dependencys
9 | `$ npm install`
10 |
11 | ### Run server
12 | `$ npm start`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/server/index.js:
--------------------------------------------------------------------------------
1 | var app = require('express')();
2 | var http = require('http').createServer(app);
3 | var io = require('socket.io')(http);
4 |
5 | app.get('/', function (req, res) {
6 | res.send('
Hello world
');
7 | });
8 |
9 | // When the server receives a post request on /sendData
10 | app.post('/sendData', function (req, res) {
11 |
12 | //send data to sockets.
13 | io.sockets.emit('event', { message: "Hello from server!" })
14 |
15 | res.send({});
16 | });
17 |
18 | // When a new connection is requested
19 | io.on('connection', function (socket) {
20 | console.log('User Connected!');
21 |
22 | // Send to the connected user
23 | socket.emit('event', { message: 'Connected !!!!' });
24 |
25 | // On each "status", run this function
26 | socket.on('status', function (data) {
27 | console.log(data);
28 | });
29 | });
30 |
31 | // Listen to port 3000
32 | http.listen(3000, function () {
33 | console.log('listening on *:3000');
34 | });
35 |
36 |
--------------------------------------------------------------------------------
/server/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "esp32_socketio_server",
3 | "version": "0.0.1",
4 | "description": "my first socket.io app",
5 | "dependencies": {
6 | "express": "^4.17.1",
7 | "socket.io": "^2.2.0"
8 | },
9 | "scripts": {
10 | "start": "node index.js"
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/socketIO.ino:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 |
6 | #include
7 |
8 | #define USE_SERIAL Serial
9 |
10 | WiFiMulti WiFiMulti;
11 | SocketIoClient webSocket;
12 |
13 | // CONST VARIABLES
14 | const char *ssid = "";
15 | const char *pass = "";
16 | const char *HOST = "";
17 |
18 | void event(const char *payload, size_t length){
19 | USE_SERIAL.printf("got message: %s\n", payload);
20 | }
21 |
22 | void setup(){
23 | USE_SERIAL.begin(9600);
24 |
25 | USE_SERIAL.setDebugOutput(true);
26 |
27 | USE_SERIAL.println();
28 | USE_SERIAL.println();
29 | USE_SERIAL.println();
30 |
31 | for (uint8_t t = 4; t > 0; t--){
32 | USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
33 | USE_SERIAL.flush();
34 | delay(1000);
35 | }
36 |
37 |
38 | // Connect to WIFI
39 | WiFiMulti.addAP(ssid, pass);
40 |
41 | while (WiFiMulti.run() != WL_CONNECTED){
42 | delay(100);
43 | }
44 |
45 | // Receive events from server
46 | webSocket.on("event", event);
47 |
48 | webSocket.begin(HOST);
49 | }
50 |
51 | int count = 0;
52 |
53 | void loop(){
54 | webSocket.loop();
55 | count++;
56 | if (count == 18000){
57 | count = 0;
58 |
59 | // Send data to Server
60 | webSocket.emit("status", "Hello from esp32!");
61 | }
62 | }
--------------------------------------------------------------------------------