Websocket connection drops after some time
Posted: Sat Nov 26, 2016 5:37 am
Dear All,
I am currently working on a IOT project to control a machine remotely and able to transmit and receive
data from SAP HANA trial server(WSS-websocket secured) also able to get a disconnect and connect notifications.The problem is suddenly after two or 4 hours iam not able to see any notification from my client(node-Mcu 1.0), mean to say it goes offline without notification. At that time server creates "Device not connected" logs.
Code below:
#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
define AUTH_BEARER = "XXXXX ";
define FIRST_DEVICE_OAUTH = "XXXXXXXXXXXXXXXX";
#define WIFI_SSID "XXXXXX"
#define WIFI_PASSWORD "XXXXXXXXX"
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
int i=0;
long nextCmdId = 1;
bool connected = false;
void sendData() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["mode"] = "async-ack";
root["sequence"] = "4811";
root["messageType"] = "7989a08ff2b083d7b906";
JsonArray& array = root.createNestedArray("messages");
JsonObject& object = array.createNestedObject();
object["measured_at"]=1479101139;
object["temperature"]=48.87;
object["pressure"]=29.34;
object["load"]= load1;
object["kilometer"]=7485.87;
String json;
root.printTo(json);
Serial.println(json);
webSocket.sendTXT(json);
}
void processSlackMessage(String receivedpayload)
{
char json[] = "{"hhhhhhh":\xxxxxxxxxx}]}";
receivedpayload.toCharArray(json, 1024);
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
JsonArray& list = root ["messages"];
if (!root.success()) {
Serial.println("root failed");
return;
}
JsonArray& nestedArray = root["messages"].asArray();
if (root.containsKey("messages"))
{
// double temperature = root["messages"]["temperature"];
int temperature= list[0] ["temperature"];
double pressure= list[0] ["pressure"];
double load= list[0] ["load"];
double km= list[0] ["kilometer"];
// double temperature= list[0] ["temperature"];
// String data = root["messages"];
Serial.println(temperature);
Serial.println(pressure);
Serial.println(load);
Serial.println(km);
if(temperature >= 1)
{
digitalWrite(16, LOW);
}
else
{
digitalWrite(16, HIGH);
}
}
}
void webSocketEvent(WStype_t type, uint8_t *payload, size_t len) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[WebSocket] Disconnected \n");
connected = false;
break;
case WStype_CONNECTED:
Serial.printf("[WebSocket] Connected to: %s\n", payload);
sendData();
break;
case WStype_TEXT:
Serial.printf("[WebSocket] Message: %s\n", payload);
String receivedpayload;
for (int i = 0; i < len; i++)
{
receivedpayload += (char)payload[i];
}
if (receivedpayload.startsWith("{\"messageType\":\"xxxxxxxx"))
{
Serial.printf("[WebSocket cooooooooooooooooooooooL");
processSlackMessage(receivedpayload);
}
break;
}
}
bool connectToSlack() {
webSocket.beginSSL("XXXXXXXXXXXXXXXX", 443, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
webSocket.setAuthorization("XXXXXXXXXXXXXXXXXXXXXX");
webSocket.onEvent(webSocketEvent);
return true;
}
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
Serial.println();
Serial.println();
Serial.println();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
connectToSlack();
}
void loop() {
webSocket.loop()
}
I am currently working on a IOT project to control a machine remotely and able to transmit and receive
data from SAP HANA trial server(WSS-websocket secured) also able to get a disconnect and connect notifications.The problem is suddenly after two or 4 hours iam not able to see any notification from my client(node-Mcu 1.0), mean to say it goes offline without notification. At that time server creates "Device not connected" logs.
Code below:
#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
define AUTH_BEARER = "XXXXX ";
define FIRST_DEVICE_OAUTH = "XXXXXXXXXXXXXXXX";
#define WIFI_SSID "XXXXXX"
#define WIFI_PASSWORD "XXXXXXXXX"
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
int i=0;
long nextCmdId = 1;
bool connected = false;
void sendData() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["mode"] = "async-ack";
root["sequence"] = "4811";
root["messageType"] = "7989a08ff2b083d7b906";
JsonArray& array = root.createNestedArray("messages");
JsonObject& object = array.createNestedObject();
object["measured_at"]=1479101139;
object["temperature"]=48.87;
object["pressure"]=29.34;
object["load"]= load1;
object["kilometer"]=7485.87;
String json;
root.printTo(json);
Serial.println(json);
webSocket.sendTXT(json);
}
void processSlackMessage(String receivedpayload)
{
char json[] = "{"hhhhhhh":\xxxxxxxxxx}]}";
receivedpayload.toCharArray(json, 1024);
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
JsonArray& list = root ["messages"];
if (!root.success()) {
Serial.println("root failed");
return;
}
JsonArray& nestedArray = root["messages"].asArray();
if (root.containsKey("messages"))
{
// double temperature = root["messages"]["temperature"];
int temperature= list[0] ["temperature"];
double pressure= list[0] ["pressure"];
double load= list[0] ["load"];
double km= list[0] ["kilometer"];
// double temperature= list[0] ["temperature"];
// String data = root["messages"];
Serial.println(temperature);
Serial.println(pressure);
Serial.println(load);
Serial.println(km);
if(temperature >= 1)
{
digitalWrite(16, LOW);
}
else
{
digitalWrite(16, HIGH);
}
}
}
void webSocketEvent(WStype_t type, uint8_t *payload, size_t len) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[WebSocket] Disconnected \n");
connected = false;
break;
case WStype_CONNECTED:
Serial.printf("[WebSocket] Connected to: %s\n", payload);
sendData();
break;
case WStype_TEXT:
Serial.printf("[WebSocket] Message: %s\n", payload);
String receivedpayload;
for (int i = 0; i < len; i++)
{
receivedpayload += (char)payload[i];
}
if (receivedpayload.startsWith("{\"messageType\":\"xxxxxxxx"))
{
Serial.printf("[WebSocket cooooooooooooooooooooooL");
processSlackMessage(receivedpayload);
}
break;
}
}
bool connectToSlack() {
webSocket.beginSSL("XXXXXXXXXXXXXXXX", 443, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
webSocket.setAuthorization("XXXXXXXXXXXXXXXXXXXXXX");
webSocket.onEvent(webSocketEvent);
return true;
}
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
Serial.println();
Serial.println();
Serial.println();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
connectToSlack();
}
void loop() {
webSocket.loop()
}