This implementation is derived from Bill Roy and washo4evr Work you should check out there libraries. the major difference is that this library is implemented using HTTPS instead of HTTP because it's 2016 😛
This library is not yet complete But it's features currently supports 2 way communication between the client and server.
Clone this repository into your Ardunio Sketchbook directory under libraries, then restart the Ardunio IDE so that it notices the new library. Now, under File\Examples you should see SocketIOClientSecure. You can run the esp_client with the node server running to test out the implementation. The server server a page to your local host that displays the messages being received form the Esp8266 you can view the page by going to the local host and the port you selected when running the server
cd .../.../socket_io_server
npm install \\Install the dependencies
node index -p 3000 \\Serve application on port 3000 or any other port you like
Note if you change the port number you have to change the port on the esp_client if you plan to run the sketch.
To send a message to the server you can use any of this methods, the RID specifies the identifier of the event the socket server listens too, Rname is the key and Rcontent is the value of the json object that will be sent.
//Sending data to the server
void send(String RID, String Rname, String Rcontent);
void sendJSON(String RID, String JSON);
In this case the RID='device status'
and the {Rname: "Rcontent"}
will be the data
that is received.
io.on('connection', function (socket) {
socket.on('device status', function (data) {
...
...
}
}
This variables are shared across the Sketch. when data is sent from the server the the monitor and parser method in the libiary capture the following information
- RID: the the id of the event,
- Rname and Rcontent the key and value pair of the data sent.
extern String RID;
extern String Rname;
extern String Rcontent;
...
...
sendPing = setInterval(function () {
console.log("emitting message...");
io.emit('server status', {
status: '100'
});
}, 1000);
...
...
This also send a ping to the server every 10 seconds
void loop(){
...
...
if (socketIOClient.monitor())
{
Serial.print("RID: ");
Serial.print(RID);
Serial.print(", Rname: ");
Serial.print(Rname);
Serial.print(", Rcontent: ");
Serial.println(Rcontent+" .");
//Send ping to server
if ((millis() - now) > 10000UL) {
now = millis();
Serial.println("Sending ping to server...");
socketIOClient.send("device status", "status", "100");
}
socketIOClient.send("message", "Power", "off");
}
else if(!socketIOClient.connected()) {
socketIOClient.connect(server, httpPort);
}
}
RID: server status, Rname: status, Rcontent: 100 .
Made with ❤️