Advertisement
auriza

sht30-thingspeak.ino

Jan 21st, 2022
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WEMOS_SHT3X.h>
  2. #include <Adafruit_SSD1306.h>
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266HTTPClient.h>
  5.  
  6. SHT3X             sht30(0x45);
  7. Adafruit_SSD1306  oled(-1);
  8. int               i = 0;
  9.  
  10. void setup() {
  11.   pinMode(LED_BUILTIN, OUTPUT);
  12.   Serial.begin(115200);
  13.   oled.begin();
  14.   oled.setTextColor(WHITE);
  15.   WiFi.begin("Ilkomerz_GreenHouse", "ilkomgr33n1P8");
  16.   while (WiFi.status() != WL_CONNECTED) delay(500);
  17. }
  18.  
  19. void loop() {
  20.   if (sht30.get() == 0)
  21.     show_temp();
  22.  
  23.   if (WiFi.status() == WL_CONNECTED && i%12 == 0) {
  24.     digitalWrite(LED_BUILTIN, LOW);
  25.     show_wifi();
  26.     send_data();
  27.     digitalWrite(LED_BUILTIN, HIGH);
  28.   }
  29.  
  30.   oled.display();
  31.   delay(5000); i++;
  32. }
  33.  
  34. void send_data() {
  35.   WiFiClient client;
  36.   HTTPClient http;
  37.   http.begin(client, "http://api.thingspeak.com/update");
  38.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  39.   int resp_code = http.POST("api_key=C6H0ZFXN0UK0NB4R&field1=" + String(sht30.cTemp) + "&field2=" + sht30.humidity + "&field3="+ WiFi.RSSI());
  40.   http.end();
  41.   Serial.print("[HTTP] "); Serial.println(resp_code);
  42.   oled.setCursor(0, 36); oled.print(resp_code);
  43.   if (resp_code == 302) {
  44.     http.begin(client, "http://1.1.1.3/ac_portal/login.php");
  45.     http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  46.     http.POST("opr=pwdLogin&userName=krs002&pwd=krs2015");
  47.     http.end();
  48.   }
  49. }
  50.  
  51. void show_temp() {
  52.   Serial.print("[SHT30] Temp: ");
  53.   Serial.print(sht30.cTemp);
  54.   Serial.print(", RH: ");
  55.   Serial.println(sht30.humidity);
  56.  
  57.   oled.clearDisplay();
  58.   oled.setCursor(0, 0);
  59.  
  60.   oled.setTextSize(2);
  61.   oled.print(int(sht30.cTemp));
  62.   oled.setTextSize(1);
  63.   oled.println("\"");
  64.   oled.print("    .");
  65.   oled.println(int((sht30.cTemp - int(sht30.cTemp)) * 10));
  66.   oled.println();
  67.   oled.println();
  68.   oled.print("     ");
  69.   oled.setTextSize(2);
  70.   oled.print(sht30.humidity, 0);
  71.   oled.setTextSize(1);
  72.   oled.println();
  73.   oled.print("         %");
  74. }
  75.  
  76. void show_wifi() {
  77.   Serial.print("[WiFi] IP: ");
  78.   Serial.print(WiFi.localIP());
  79.   Serial.print(", RSSI: ");
  80.   Serial.println(WiFi.RSSI());
  81.   //oled.setCursor(0, 20); oled.print(WiFi.localIP());
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement