Advertisement
DrAungWinHtut

ESP01WebScClient.ino

Aug 30th, 2022
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5.  
  6. #include <WebSocketsClient.h>
  7.  
  8. #include <Hash.h>
  9.  
  10. #define SSID "GreenHackers"
  11. #define Password "1234567890a"
  12.  
  13. #define DeviceAPIWebsocketIP "192.168.0.102"
  14. #define DeviceAPIWebsocketPort 80
  15.  
  16. #define Token "token"
  17.  
  18. #define USE_SERIAL Serial
  19.  
  20. ESP8266WiFiMulti WiFiMulti;
  21. WebSocketsClient webSocket;
  22.  
  23. int webflag = 0;
  24.  
  25.  
  26. void setup() {
  27.   // USE_SERIAL.begin(921600);
  28.   USE_SERIAL.begin(115200);
  29.  
  30.   //Serial.setDebugOutput(true);
  31.   USE_SERIAL.setDebugOutput(true);
  32.  
  33.   USE_SERIAL.println();
  34.   USE_SERIAL.println();
  35.   USE_SERIAL.println();
  36.  
  37.   for(uint8_t t = 4; t > 0; t--) {
  38.     USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  39.     USE_SERIAL.flush();
  40.     delay(1000);
  41.   }
  42.  
  43.   WiFiMulti.addAP(SSID, Password);
  44.   while(WiFiMulti.run() != WL_CONNECTED) delay(100);
  45.  
  46.   webSocket.begin(DeviceAPIWebsocketIP, DeviceAPIWebsocketPort, "/connectdevice");
  47.   webSocket.onEvent(webSocketEvent);
  48.   webSocket.setReconnectInterval(5000);
  49.  
  50.   // start heartbeat (optional)
  51.   // ping server every 15000 ms
  52.   // expect pong from server within 3000 ms
  53.   // consider connection disconnected if pong is not received 2 times
  54.   webSocket.enableHeartbeat(15000, 3000, 2);
  55. }
  56.  
  57. void loop() {  
  58.   webSocket.loop();
  59.   USE_SERIAL.println("Loop again!!!!!");
  60.   sendData();
  61. }
  62.  
  63. void sendData() {
  64.   //if websocket is not connected, exit immediately
  65.   if(webflag==0){return ;}
  66.  
  67.   //else connected!
  68.   const String humidityID = String("6306024fd2a6f70702fe46c8");
  69.   const String tempID = String("63060250d2a6f70702fe46c9");
  70.  
  71.   char humidityMessage[30] = { 0, 0, 0, 2, 24 };
  72.  
  73.   strcat(&humidityMessage[5], humidityID.c_str());
  74.   humidityMessage[29] = random(70, 80);
  75.   webSocket.sendBIN((uint8_t*)humidityMessage, 30);
  76.  
  77.   char tempMessage[30] = { 0, 0, 0, 2, 24 };
  78.  
  79.   strcat(&tempMessage[5], tempID.c_str());
  80.   tempMessage[29] = random(20, 30);
  81.   webSocket.sendBIN((uint8_t*)tempMessage, 30);
  82.   USE_SERIAL.printf("[SEND DATA] accomplished!\n");
  83.   delay(2000);
  84.  
  85. }
  86.  
  87. void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
  88.   switch(type) {
  89.     case WStype_DISCONNECTED:
  90.       USE_SERIAL.printf("[WSc] Disconnected!\n");
  91.        webflag = 0;
  92.       break;
  93.     case WStype_CONNECTED: {
  94.       USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  95.        webflag = 1;
  96.       const String token = String(Token);
  97.       char* message = new char[4 + token.length()] { 0, 0, 0, 0 };
  98.       strcat(&message[4], token.c_str());
  99.  
  100.       webSocket.sendBIN((uint8_t*)message, 4 + token.length());
  101.       delete [] message;
  102.      
  103.     }
  104.       break;
  105.     case WStype_TEXT:
  106.       USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  107.  
  108.       // send message to server
  109.       // webSocket.sendTXT("message here");
  110.       break;
  111.     case WStype_BIN:
  112.       USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  113.       hexdump(payload, length);
  114.  
  115.       // send data to server
  116.       // webSocket.sendBIN(payload, length);
  117.       break;
  118.         case WStype_PING:
  119.           // pong will be send automatically
  120.           USE_SERIAL.printf("[WSc] get ping\n");
  121.           break;
  122.         case WStype_PONG:
  123.           // answer to a ping we send
  124.           USE_SERIAL.printf("[WSc] get pong\n");
  125.           break;
  126.          
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement