Advertisement
DrAungWinHtut

ESP8266SmartGreenHouse.ino

Sep 2nd, 2022
1,265
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. #include "DHT.h"
  10.  
  11.  
  12. #define SSID "GreenHackers"
  13. #define Password "1234567890a"
  14.  
  15. #define DeviceAPIWebsocketIP "192.168.0.102"
  16. #define DeviceAPIWebsocketPort 80
  17.  
  18. #define Token "token"
  19.  
  20. #define USE_SERIAL Serial
  21.  
  22. #define DHTPIN D2 //gpio?  d1
  23. #define DHTTYPE DHT22
  24.  
  25. ESP8266WiFiMulti WiFiMulti;
  26. WebSocketsClient webSocket;
  27. DHT dht(DHTPIN, DHTTYPE);
  28.  
  29. int webflag = 0;
  30.  
  31.  
  32. void setup() {
  33.   pinMode(A0,INPUT);
  34.   // USE_SERIAL.begin(921600);
  35.   USE_SERIAL.begin(115200);
  36.   dht.begin();
  37.  
  38.   //Serial.setDebugOutput(true);
  39.   USE_SERIAL.setDebugOutput(true);
  40.  
  41.   USE_SERIAL.println();
  42.   USE_SERIAL.println();
  43.   USE_SERIAL.println();
  44.  
  45.   for(uint8_t t = 4; t > 0; t--) {
  46.     USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  47.     USE_SERIAL.flush();
  48.     delay(1000);
  49.   }
  50.  
  51.   WiFiMulti.addAP(SSID, Password);
  52.   while(WiFiMulti.run() != WL_CONNECTED) delay(100);
  53.  
  54.   webSocket.begin(DeviceAPIWebsocketIP, DeviceAPIWebsocketPort, "/connectdevice");
  55.   webSocket.onEvent(webSocketEvent);
  56.   webSocket.setReconnectInterval(5000);
  57.  
  58.   // start heartbeat (optional)
  59.   // ping server every 15000 ms
  60.   // expect pong from server within 3000 ms
  61.   // consider connection disconnected if pong is not received 2 times
  62.   webSocket.enableHeartbeat(15000, 3000, 2);
  63. }
  64.  
  65. void loop() {  
  66.   webSocket.loop();
  67.   USE_SERIAL.println("Loop again!!!!!");
  68.   sendData();
  69. }
  70.  
  71. void sendData() {
  72.   //if websocket is not connected, exit immediately
  73.   if(webflag==0){return ;}
  74.  
  75.   //else connected!
  76.   const String humidityID = String("6306024fd2a6f70702fe46c8");
  77.   const String tempID = String("63060250d2a6f70702fe46c9");
  78.   delay(300);
  79.   // Reading temperature or humidity takes about 250 milliseconds!
  80.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  81.   float h = dht.readHumidity();
  82.   // Read temperature as Celsius (the default)2.4
  83.   float t = dht.readTemperature();
  84.   // Read temperature as Fahrenheit (isFahrenheit = true)
  85.   float f = dht.readTemperature(true);
  86.   int soil = analogRead(A0);
  87.   USE_SERIAL.print("Soil : ");
  88.   USE_SERIAL.println(soil);
  89.   // Check if any reads failed and exit early (to try again).
  90.   if (isnan(h) || isnan(t) || isnan(f)) {
  91.     Serial.println(F("Failed to read from DHT sensor!"));
  92.     return;
  93.   }
  94.  
  95.  
  96.  
  97.   char humidityMessage[50] = { 0, 0, 0, 2, 24 };  //first byte 0 - 1 byte
  98.                                                   //cmd id 0
  99.                                                   //cmd context - payload 0,2
  100.                                                   //payload 24
  101.                                                  
  102.   USE_SERIAL.print("humidity : ");
  103.   USE_SERIAL.println(h);
  104.  
  105.   strcat(&humidityMessage[5], humidityID.c_str());
  106.   strcat(&humidityMessage[29], String(h).c_str());
  107.   webSocket.sendBIN((uint8_t*)humidityMessage, 30);
  108.   USE_SERIAL.print("humidity bin: ");
  109.   //USE_SERIAL.write(humidityMessage,50);
  110.   delay(2000);
  111.   char tempMessage[30] = { 0, 0, 0, 2, 24 };
  112.  
  113.   strcat(&tempMessage[5], tempID.c_str());
  114.   tempMessage[29] = random(20, 30);
  115.   webSocket.sendBIN((uint8_t*)tempMessage, 30);
  116.   USE_SERIAL.printf("[SEND DATA] accomplished!\n");
  117.   delay(2000);
  118.  
  119. }
  120.  
  121. void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
  122.   switch(type) {
  123.     case WStype_DISCONNECTED:
  124.       USE_SERIAL.printf("[WSc] Disconnected!\n");
  125.        webflag = 0;
  126.       break;
  127.     case WStype_CONNECTED: {
  128.       USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  129.        webflag = 1;
  130.       const String token = String(Token);
  131.       char* message = new char[4 + token.length()] { 0, 0, 0, 0 };
  132.       strcat(&message[4], token.c_str());
  133.  
  134.       webSocket.sendBIN((uint8_t*)message, 4 + token.length());
  135.       delete [] message;
  136.      
  137.     }
  138.       break;
  139.     case WStype_TEXT:
  140.       USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  141.  
  142.       // send message to server
  143.       // webSocket.sendTXT("message here");
  144.       break;
  145.     case WStype_BIN:
  146.       USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  147.       hexdump(payload, length);
  148.  
  149.       // send data to server
  150.       // webSocket.sendBIN(payload, length);
  151.       break;
  152.         case WStype_PING:
  153.           // pong will be send automatically
  154.           USE_SERIAL.printf("[WSc] get ping\n");
  155.           break;
  156.         case WStype_PONG:
  157.           // answer to a ping we send
  158.           USE_SERIAL.printf("[WSc] get pong\n");
  159.           break;
  160.          
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement