Git Product home page Git Product logo

socket.io-v1.x-v2.x-library's Introduction

Socket.io Library for v1.x and v2.x

Socket.io Library for Arduino

Navgation


News

  1. Works with W5100 & ENC28J60 as well as ESP8266 and ESP32 (beta)
  2. Library will not work when the ESP8266 is being driven by a Uno.
  3. Support for JSON was added using this library.
  4. Instead of using client.send, you can now use client.sendJSON
  5. Thanks to @funkyguy4000, users can now use #define ESP8266, #define W5100 or #define ENC28J60 no need to edit socketio.h anymore
  6. headers are not yet handled, soon
  7. incoming messages can now be longer than 125 bytes (at least up to 255 bytes)

Download Releases

Download in releases and include zip file in arduino ide. Release 1.0

REST API added: getREST(path), postREST(path, type, data), putREST(path, type, data), deleteREST(path)


Examples

W5100

  #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);
      }
    }
  }

ESP8266

  #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);
      }
    }
  }

ESP32

  #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);
      }
    }
  }

Contributors

  1. washo4evr
  2. all-lala
  3. andreghisleni

socket.io-v1.x-v2.x-library's People

Contributors

washo4evr avatar dclause avatar andreghisleni avatar all-lala avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.