sweetboicz

wemos_d1_mini_02

Feb 22nd, 2021
470
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3.  
  4. #include <Wire.h>
  5. #include "HTU21D.h"
  6.  
  7. HTU21D myHTU21D(HTU21D_RES_RH12_TEMP14); //-- resolution HTU21D_RES_RH12_TEMP14 - RH: 12Bit, Temperature: 14Bit, by default
  8.  
  9. #ifndef STASSID
  10. #define STASSID "***"
  11. #define STAPSK  "***"
  12. #endif
  13.  
  14. String sURL = "http://data.peezee.eu/index.php";
  15. String sTemp = "";
  16. String sHumidity = "";
  17. String sBattery = "";
  18.  
  19. void setup()
  20. {
  21.   Serial.begin(115200);
  22.   Serial.println();
  23.  
  24.   while (myHTU21D.begin() != true)
  25.   {
  26.     Serial.println(F("HTU21D, SHT21 sensor je vadny nebo nepripojeny")); //(F()) saves string to flash & keeps dynamic memory free
  27.     delay(5000);
  28.   }
  29.   Serial.println(F("HTU21D, SHT21 sensor je aktivni"));
  30.  
  31.   WiFi.begin(STASSID, STAPSK);
  32.  
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.   }
  37.   Serial.println("");
  38.   Serial.print("Connected! IP address: ");
  39.   Serial.println(WiFi.localIP());
  40.  
  41. }
  42.  
  43. void loop()
  44. {
  45.  
  46.   sHumidity = String(myHTU21D.readHumidity()) + F(" (+-2%)");
  47.   sTemp = String(myHTU21D.readTemperature()) + F(" (+-0.3C)");
  48.   if   (myHTU21D.batteryStatus() == true) sBattery = F("OK.  Level > 2.25v");
  49.   else                                    sBattery = F("Slabá. Level < 2.25v");
  50.  
  51.   Serial.print(F("MAC: ")); Serial.println(getMacAddress());
  52.  
  53.   Serial.print(F("Teplota: ")); Serial.println(sTemp);
  54.   Serial.print(F("Vlhkost: ")); Serial.println(sHumidity);
  55.   Serial.print(F("Baterie: ")); Serial.println(sBattery);
  56.   Serial.println(F("--------- a nyni posleme data ----------------------------------------------------------"));
  57.  
  58.   if ((WiFi.status() == WL_CONNECTED)) {
  59.  
  60.     WiFiClient client;
  61.     HTTPClient http;
  62.  
  63.     Serial.print("[HTTP] begin...");
  64.     Serial.println(sURL);
  65.     http.begin(client, sURL);
  66.     http.addHeader("Content-Type", "text/plain");
  67.     //Serial.print("[HTTP] POST...\n");
  68.     int httpCode = http.POST("aaa");
  69.     if (httpCode > 0) {
  70.       Serial.printf("[HTTP] POST... code: %d\n", httpCode);
  71.       if (httpCode == HTTP_CODE_OK) {
  72.         String payload = http.getString();
  73.         Serial.println("received payload:\n<<");
  74.         Serial.println(payload);
  75.         Serial.println(">>");
  76.       }
  77.     } else {
  78.       Serial.printf("[HTTPS] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
  79.     }
  80.  
  81.     http.end();
  82.   }
  83.  
  84.   Serial.println(F("--------- za 20 sec. znovu ----------------------------------------------------------"));
  85.  
  86.   delay(20000);
  87.  
  88. }
  89. //---------------------------------------------------------------------------------------------------------------------------------
  90. String getMacAddress()
  91. {
  92.   byte mac[6];
  93.   WiFi.macAddress(mac);
  94.   String cMac = "";
  95.   for (int i = 0; i < 6; ++i)
  96.   {
  97.     cMac += String(mac[i],HEX);
  98.     if(i < 5)
  99.       cMac += "-";
  100.   }
  101.   cMac.toUpperCase();
  102.   return cMac;
  103. }
RAW Paste Data