Advertisement
DrAungWinHtut

ESP5.ino

Sep 6th, 2022
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Power External 5V , Gnd -> Gnd of ESP
  2. //All sensors and motors and relays VCC to 5V
  3. //All sensors Gnd to any Gnd
  4. //D1 - LCD SCL
  5. //D2 - LCD SDA
  6. //D3 - DHT22 Data
  7. //D4 - Servo Control to BreadBoard
  8. //Two Servo Control pins -> D4 from BreadBoard
  9. //D5 - Relay 1 (Active LOW) (Pump)
  10. //D6 - Relay 2 (FAN)
  11. //A0 - Soil Sensor Analog pin
  12.  
  13. #include <Arduino.h>
  14.  
  15. #include <ESP8266WiFi.h>
  16. #include <ESP8266WiFiMulti.h>
  17.  
  18. #include <WebSocketsClient.h>
  19.  
  20. #include <Hash.h>
  21. #include "DHT.h"
  22.  
  23. #include <Wire.h>
  24. #include <LiquidCrystal_I2C.h>
  25. LiquidCrystal_I2C lcd(0x3F,20,4);
  26.  
  27. #define SSID "GreenHackers"
  28. #define Password "1234567890a"
  29.  
  30. #define DeviceAPIWebsocketIP "192.168.0.102"
  31. #define DeviceAPIWebsocketPort 80
  32.  
  33. #define Token "token"
  34.  
  35. #define USE_SERIAL Serial
  36.  
  37. #define DHTPIN D3 //gpio?  d1
  38. #define DHTTYPE DHT22
  39. #include<Servo.h>
  40. ESP8266WiFiMulti WiFiMulti;
  41. WebSocketsClient webSocket;
  42. DHT dht(DHTPIN, DHTTYPE);
  43.  
  44. int webflag = 0;
  45. Servo servo1;
  46. Servo servo2;
  47.  
  48. void setup() {
  49.   pinMode(A0,INPUT);
  50.   // USE_SERIAL.begin(921600);
  51.   USE_SERIAL.begin(115200);
  52.   dht.begin();
  53.  
  54.   //Serial.setDebugOutput(true);
  55.   USE_SERIAL.setDebugOutput(true);
  56.  
  57.   USE_SERIAL.println();
  58.   USE_SERIAL.println();
  59.   USE_SERIAL.println();
  60.  
  61.   for(uint8_t t = 4; t > 0; t--) {
  62.     USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  63.     USE_SERIAL.flush();
  64.     delay(1000);
  65.   }
  66.  
  67.   WiFiMulti.addAP(SSID, Password);
  68.   while(WiFiMulti.run() != WL_CONNECTED) delay(100);
  69.  
  70.   webSocket.begin(DeviceAPIWebsocketIP, DeviceAPIWebsocketPort, "/connectdevice");
  71.   webSocket.onEvent(webSocketEvent);
  72.   webSocket.setReconnectInterval(5000);
  73.  
  74.   // start heartbeat (optional)
  75.   // ping server every 15000 ms
  76.   // expect pong from server within 3000 ms
  77.   // consider connection disconnected if pong is not received 2 times
  78.   webSocket.enableHeartbeat(15000, 3000, 2);
  79.   lcd.init();  
  80.   lcd.backlight();
  81.   lcd.setCursor(0,0);
  82.   lcd.print("Autobot!");
  83.   servo1.attach(D4);
  84.  
  85.   pinMode(D5,OUTPUT);
  86.   pinMode(D6,OUTPUT);
  87.   digitalWrite(D5,HIGH);
  88.   delay(1000);
  89.   digitalWrite(D6,HIGH);
  90.   delay(1000);
  91.   digitalWrite(D5,LOW);
  92.   delay(1000);
  93.   digitalWrite(D6,LOW);
  94.   delay(1000);
  95. }
  96.  
  97. void loop() {  
  98.   webSocket.loop();
  99.   USE_SERIAL.println("Loop again!!!!!");
  100.   sendData();
  101. }
  102.  
  103. void sendData() {
  104.   //if websocket is not connected, exit immediately
  105.   if(webflag==0){return ;}
  106.  
  107.   //else connected!
  108.   const String humidityID = String("6306024fd2a6f70702fe46c8");
  109.   const String tempID = String("63060250d2a6f70702fe46c9");
  110.   delay(300);
  111.   // Reading temperature or humidity takes about 250 milliseconds!
  112.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  113.   float h = dht.readHumidity();
  114.   // Read temperature as Celsius (the default)2.4
  115.   float t = dht.readTemperature();
  116.   // Read temperature as Fahrenheit (isFahrenheit = true)
  117.   float f = dht.readTemperature(true);
  118.   int soil = analogRead(A0);
  119.   USE_SERIAL.print("Soil : ");
  120.   USE_SERIAL.println(soil);
  121.   // Check if any reads failed and exit early (to try again).
  122.   if (isnan(h) || isnan(t) || isnan(f)) {
  123.     Serial.println(F("Failed to read from DHT sensor!"));
  124.     return;
  125.   }
  126.  
  127.  
  128.  
  129.   char humidityMessage[50] = { 0, 0, 0, 2, 24 };  //first byte 0 - 1 byte
  130.                                                   //cmd id 0
  131.                                                   //cmd context - payload 0,2
  132.                                                   //payload 24
  133.                                                  
  134.   USE_SERIAL.print("humidity : ");
  135.   USE_SERIAL.println(h);
  136.  
  137.   strcat(&humidityMessage[5], humidityID.c_str());
  138.   strcat(&humidityMessage[29], String(h).c_str());
  139.   webSocket.sendBIN((uint8_t*)humidityMessage, 30);
  140.   USE_SERIAL.print("humidity bin: ");
  141.   //USE_SERIAL.write(humidityMessage,50);
  142.   delay(2000);
  143.   char tempMessage[30] = { 0, 0, 0, 2, 24 };
  144.  
  145.   strcat(&tempMessage[5], tempID.c_str());
  146.   tempMessage[29] = random(20, 30);
  147.   webSocket.sendBIN((uint8_t*)tempMessage, 30);
  148.   USE_SERIAL.printf("[SEND DATA] accomplished!\n");
  149.   delay(2000);
  150.  
  151. }
  152.  
  153. void webSocketEvent(WStype_t type, uint8_t* payload, size_t length) {
  154.   switch(type) {
  155.     case WStype_DISCONNECTED:
  156.       USE_SERIAL.printf("[WSc] Disconnected!\n");
  157.        webflag = 0;
  158.       break;
  159.     case WStype_CONNECTED: {
  160.       USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  161.        webflag = 1;
  162.       const String token = String(Token);
  163.       char* message = new char[4 + token.length()] { 0, 0, 0, 0 };
  164.       strcat(&message[4], token.c_str());
  165.  
  166.       webSocket.sendBIN((uint8_t*)message, 4 + token.length());
  167.       delete [] message;
  168.      
  169.     }
  170.       break;
  171.     case WStype_TEXT:
  172.       USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  173.  
  174.       // send message to server
  175.       // webSocket.sendTXT("message here");
  176.       break;
  177.     case WStype_BIN:
  178.       USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  179.       hexdump(payload, length);
  180.  
  181.       // send data to server
  182.       // webSocket.sendBIN(payload, length);
  183.       break;
  184.         case WStype_PING:
  185.           // pong will be send automatically
  186.           USE_SERIAL.printf("[WSc] get ping\n");
  187.           break;
  188.         case WStype_PONG:
  189.           // answer to a ping we send
  190.           USE_SERIAL.printf("[WSc] get pong\n");
  191.           break;
  192.          
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement