Socket.io Library for Arduino
REST API added: getREST(path)
, postREST(path, type, data)
, putREST(path, type, data)
, deleteREST(path)
#define W5100
#include <SocketIOClient.h>
SocketIOClient client;
byte mac[] = { 0xAA, 0x00, 0xBE, 0xEF, 0xFE, 0xEE };
char hostname[] = "192.168.0.5";
int port = 1234;
extern String RID;
extern String Rname;
extern String Rcontent;
unsigned long previousMillis = 0;
long interval = 10000;
void setup() {
pinMode(10, OUTPUT); //for some ethernet shield on the mega : disables the SD and enables the W5100
digitalWrite(10, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
Ethernet.begin(mac);
if (!client.connect(hostname, port))
Serial.println(F("Not connected."));
if (client.connected()){
client.send("connection", "message", "Connected !!!!");
}else{
Serial.println("Connection Error");
while(1);
}
delay(1000);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
//client.heartbeat(0);
client.send("atime", "message", "Time please?");
}
if (client.monitor())
{
Serial.println(RID);
if (RID == "atime" && Rname == "time")
{
Serial.print("Time is ");
Serial.println(Rcontent);
}
}
}
#define ESP8266
#include <SocketIOClient.h>
SocketIOClient client;
const char* ssid = "SSID";
const char* password = "WPA KEY";
char host[] = "192.168.0.5";
int port = 1234;
extern String RID;
extern String Rname;
extern String Rcontent;
unsigned long previousMillis = 0;
long interval = 5000;
unsigned long lastreply = 0;
unsigned long lastsend = 0;
String JSON;
JsonObject& root = jsonBuffer.createObject();
void setup() {
root["sensor"] = "gps";
root["time"] = 1351824120;
JsonArray& data = root.createNestedArray("data");
data.add(double_with_n_digits(48.756080, 6));
data.add(double_with_n_digits(2.302038, 6));
root.printTo(JSON);
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
if (client.connected()){
client.send("connection", "message", "Connected !!!!");
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
//client.heartbeat(0);
Serial.println(JSON);
client.send("atime", "message", "Time please?");
client.sendJSON("JSON", JSON);
lastsend = millis();
}
if (client.monitor())
{
lastreply = millis();
Serial.println(RID);
if (RID == "atime" && Rname == "time")
{
Serial.print("Il est ");
Serial.println(Rcontent);
}
}
}
#define ESP32
#include <SocketIOClient.h>
SocketIOClient client;
const char* ssid = "xxxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxx";
char host[] = "192.168.1.1";
int port = 1234;
extern String RID;
extern String Rname;
extern String Rcontent;
unsigned long previousMillis = 0;
long interval = 5000;
unsigned long lastreply = 0;
unsigned long lastsend = 0;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
if (client.connected()){
client.send("connection", "message", "Connected !!!!");
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
//client.heartbeat(0);
Serial.print("************************************** ");
Serial.println(lastsend);
client.send("atime", "message", "Time please?");
lastsend = millis();
Serial.print("************************************** ");
Serial.println(lastsend);
}
if (client.monitor())
{
lastreply = millis();
Serial.println(RID);
if (RID == "atime" && Rname == "time")
{
Serial.print("Il est ");
Serial.println(Rcontent);
}
}
}