如何将ESP8266的Socket确认信息发送给Node JS Server?

时间:2019-07-16 07:04:00

标签: socket.io callback arduino esp8266 ack

基本上,我使用SocketIO在Node JS Server和ESP8266 Client之间创建通信通道。我通过使用正常工作的事件来进行上下通讯。.但是我无法触发服务器从ESP8266端向客户端发送的确认。.我目前正在使用此库在客户端上使用socketio侧面

https://github.com/Links2004/arduinoWebSockets/blob/master/examples/esp8266/WebSocketClientSocketIO/WebSocketClientSocketIO.ino

建议我查看套接字协议以找出答案。.https://github.com/socketio/socket.io-protocol

任何帮助将不胜感激

#include <ESP8266WiFi.h>
#include <WebSocketsClient.h>
#include <SocketIOclient.h>
#include <ArduinoJson.h>

const char *IP = "192.168.1.8";
const int PORT = 3000;

const char *SSID_ = "XXXXXXXXXXXXX";
const char *PASSWORD = "xxxxxxxxxxx";

const String DEVICE_ID = "RACK C2";
const int TIERS = 10;
float CODE_VERSION = 1.1;

SocketIOclient SocketIO;

// Parses the messsage that was received and returns the body
void commandAtmel(uint8_t *payload)
{
  String text = ((const char *)&payload[0]);
  int pos = text.indexOf('{');
  String json = text.substring(pos, text.length() - 1);

  // DISABLE ALL SERIAL WHEN COMMUNICATING WITH ATMEL AND ONLY KEEP MESSAGES TO SEND
  // Sends message to Atmel chip
  Serial.print(json);
  // Decoding JSON instructions
  // DynamicJsonDocument doc(1024);
  // deserializeJson(doc, json);
  // JsonObject obj = doc.as<JsonObject>();
  // int waitingTime = obj["waitingTime"];
  // return obj;
}

// Emits event to server through socket io
void emitEvent(DynamicJsonDocument doc)
{
  // JSON to String (serialization)
  String output;
  serializeJson(doc, output);

  // Send event
  SocketIO.sendEVENT(output);

  // Print JSON for debugging
  Serial.println(output);
}

// Creates the JSON object for registration
void emitAuthEvent()
{
  // Create JSON message
  DynamicJsonDocument doc(1024);
  JsonArray array = doc.to<JsonArray>();

  // Event name
  array.add("auth");

  // Add payload
  JsonObject object = array.createNestedObject();
  object["did"] = DEVICE_ID;
  object["codeVersion"] = CODE_VERSION;

  emitEvent(doc);
}


void socketIOEvent(socketIOmessageType_t type, uint8_t *payload, size_t length)
{
  switch (type)
  {
  case sIOtype_DISCONNECT:
    Serial.printf("[IOc] Disconnected!\n");
    break;
  case sIOtype_CONNECT:
    Serial.printf("[IOc] Connected to url: %s\n", payload);
    emitAuthEvent();
    break;
  case sIOtype_EVENT:
    Serial.printf("[IOc] get event: %s\n", payload);
    commandAtmel(payload);
    delay(2000);
    emitCompleteEvent("true", "false", "true");
    break;
  case sIOtype_ACK:
    Serial.printf("[IOc] get ack: %u\n", length);
    hexdump(payload, length);
    break;
  case sIOtype_ERROR:
    Serial.printf("[IOc] get error: %u\n", length);
    hexdump(payload, length);
    break;
  case sIOtype_BINARY_EVENT:
    Serial.printf("[IOc] get binary: %u\n", length);
    hexdump(payload, length);
    break;
  case sIOtype_BINARY_ACK:
    Serial.printf("[IOc] get binary ack: %u\n", length);
    hexdump(payload, length);
    break;
  }
}

void connectWifi()
{
  WiFi.begin(SSID_, PASSWORD);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
}

void setup()
{
  Serial.begin(115200);
  connectWifi();
  SocketIO.begin(IP, PORT);
  SocketIO.onEvent(socketIOEvent);
}

void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    connectWifi();
  }
  else
  {
    SocketIO.loop();
  }
}

  console.log(`Updating Light DID | ${light.did}`)
  socket.emit(`update`, data, (callback) => {
    console.log("CALLED BACK")
  })
}

0 个答案:

没有答案