Advertisement
Guest User

Untitled

a guest
Feb 17th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266HTTPClient.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7. LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
  8.  
  9. const char *ssid = "ssid";
  10. const char *password = "password";
  11. bool isRecording;
  12. bool muted;
  13. bool streaming;
  14.  
  15. boolean toBool(String value);
  16.  
  17. #define mutedLED D8
  18. #define recordingLED D7
  19. #define debug true
  20.  
  21. String serverName = "http://myip:1880";
  22.  
  23. unsigned long lastTime = 0;
  24. unsigned long timerDelay = 1000;
  25.  
  26. void setup()
  27. {
  28.   lcd.init();
  29.   lcd.backlight();
  30.   lcd.setCursor(0, 0);
  31.   pinMode(mutedLED, OUTPUT);
  32.   digitalWrite(mutedLED, LOW);
  33.   pinMode(recordingLED, OUTPUT);
  34.   digitalWrite(recordingLED, LOW);
  35.   Serial.begin(115200);
  36.   for (int i = 0; i < 10; i++)
  37.   {
  38.     Serial.println("");
  39.   }
  40.  
  41.   WiFi.begin(ssid, password);
  42.   String connectingTo = {"Connecting to: "};
  43.   connectingTo += ssid;
  44.   Serial.println(connectingTo);
  45.   while (WiFi.status() != WL_CONNECTED)
  46.   {
  47.     delay(500);
  48.     Serial.print(".");
  49.   }
  50.   Serial.println("Connected!\n\n\n");
  51. }
  52.  
  53. void loop()
  54. {
  55.   if ((millis() - lastTime) > timerDelay)
  56.   {
  57.     if (WiFi.status() == WL_CONNECTED)
  58.     {
  59.       HTTPClient http;
  60.       http.useHTTP10(true);
  61.       String path = serverName + "/muted";
  62.       http.begin(path.c_str());
  63.       int httpResponseCode = http.GET();
  64.       if (httpResponseCode > 0)
  65.       {
  66.         String response = http.getString();
  67.         unsigned int valueIndex = response.indexOf(":") + 1;
  68.         response = response.substring(valueIndex);
  69.         muted = !toBool(response);
  70.         digitalWrite(mutedLED, muted);
  71.       }
  72.       http.end();
  73.       path = serverName + "/isRecording";
  74.       http.begin(path.c_str());
  75.       httpResponseCode = http.GET();
  76.       if (httpResponseCode > 0)
  77.       {
  78.         String response = http.getString();
  79.         unsigned int valueIndex = response.indexOf(":") + 1;
  80.         response = response.substring(valueIndex);
  81.         isRecording = toBool(response);
  82.         digitalWrite(recordingLED, isRecording);
  83.         http.end();
  84.       }
  85.     }
  86.     else
  87.       Serial.println("WiFi Disconnected");
  88.     lastTime = millis();
  89.   }
  90. }
  91.  
  92. boolean toBool(String value)
  93. {
  94.   value.toLowerCase();
  95.   if(value == "true" || value == "TRUE" || value == "True") return true;
  96.   if(value == "false" || value == "FALSE" || value == "False") return false;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement