Advertisement
Makerino

NodeMCU Messung Smarthome

Jul 28th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <ESP8266WiFi.h>
  3. #define CYCLE 1800000                               //Hier die Zeit in Millisekunden angeben (1800000 sind 30 minuten)
  4. #include <Bme280BoschWrapper.h>
  5. Bme280BoschWrapper bme280(true);
  6.  
  7. const char* ssid     = "Hier_dein_Wlanname";        //Wlan Name eingeben
  8. const char* password = "Hier_dein_Wlanpasswort";    //Wlan Passwort eingeben
  9. const char* host = "192.168.0.xxx";                 //IP Adresse des Raspi eingeben
  10. unsigned long value = 0;
  11. unsigned int lastcall = CYCLE;
  12. int conn_time;
  13.  
  14. void setup() {
  15.   Serial.begin(115200);
  16.   delay(10);
  17.  
  18.     while(!bme280.beginI2C(0x76))                   //I2C adresse des BME280 standartmäsig 0x76
  19.     {
  20.       Serial.println("Cannot find sensor.");
  21.       delay(1000);
  22.     }
  23.  
  24.   // We start by connecting to a WiFi network
  25.  
  26.   Serial.println();
  27.   Serial.println();
  28.   Serial.print("Connecting to ");
  29.   Serial.println(ssid);
  30.  
  31.   WiFi.begin(ssid, password);
  32.  
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     delay(500);
  35.     Serial.print(".");
  36.     conn_time++;
  37.     if (conn_time > 20) {
  38.       break;
  39.     }
  40.   }
  41.   if (WiFi.status() == WL_CONNECTED) {
  42.     Serial.println("");
  43.     Serial.println("WiFi connected");
  44.     Serial.println("IP address: ");
  45.     Serial.println(WiFi.localIP());
  46.   }
  47.   else {
  48.     Serial.println("");
  49.     Serial.println("no WiFi connection");
  50.     ESP.deepSleep(100000);
  51.     Serial.println("gone to sleep");
  52.   }
  53. }
  54.  
  55. void loop() {
  56.  
  57.   bool ok = bme280.measure();
  58.  
  59.   while (millis() - lastcall < CYCLE) {
  60.     delay(1);
  61.   }
  62.   lastcall = millis();
  63.   ++value;
  64.  
  65.   Serial.print("connecting to ");
  66.   Serial.println(host);
  67.  
  68.   // Use WiFiClient class to create TCP connections
  69.   WiFiClient client;
  70.   const int httpPort = 80;
  71.   if (!client.connect(host, httpPort)) {
  72.     Serial.println("connection failed");
  73.     return;
  74.   }
  75.  
  76.   // We now create a URI for the request
  77.   String url = "/messung.php";
  78.   url += "?temp=";
  79.   url += (bme280.getTemperature()/100.0);
  80.   url += "_*C";
  81.   url += "&hum=";
  82.   url += (bme280.getHumidity()/1024.0);
  83.   url += "_%Luftfeuchtigkeit";
  84.   url += "&druck=";
  85.   url += (bme280.getPressure()/100.0);
  86.   url += "_hPa";
  87.  
  88.  
  89.   Serial.print("Requesting URL: ");
  90.   Serial.println(url);
  91.  
  92.   // This will send the request to the server
  93.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  94.                "Host: " + host + "\r\n" +
  95.                "Connection: close\r\n\r\n");
  96.   delay(1000);
  97.  
  98.   // Read all the lines of the reply from server and print them to Serial
  99.   while (client.available()) {
  100.     String line = client.readStringUntil('\r');
  101.     Serial.print(line);
  102.   }
  103.  
  104.   Serial.println();
  105.   Serial.println("closing connection");
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement