Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. /*
  2.  *  This sketch sends a temperature data from a BMP180 temp sensor to a raspberry pi running influxdb/grafana
  3.  *
  4.  */
  5.  
  6. #include <ESP8266WiFi.h>
  7. #include <ESP8266WiFiMulti.h>
  8. #include <WiFiUdp.h>
  9. #include <SFE_BMP180.h>
  10. #include <Wire.h>
  11.  
  12. ESP8266WiFiMulti WiFiMulti;
  13.  
  14. SFE_BMP180 pressure;
  15.  
  16. void setup() {
  17.     Serial.begin(115200);
  18.     delay(10);
  19.     if (pressure.begin())
  20.     Serial.println("BMP180 init success");
  21.     else
  22.     {
  23.       Serial.println("BMP180 init fail. Check cables.\n\n");
  24.       while(1); // Pause forever.
  25.     }
  26.  
  27.    
  28.     WiFiMulti.addAP("SSID****", "Password***");
  29.  
  30.     Serial.println();
  31.     Serial.println();
  32.     Serial.print("Wait for WiFi... ");
  33.  
  34.     while(WiFiMulti.run() != WL_CONNECTED) {
  35.         Serial.print(".");
  36.         delay(500);
  37.     }
  38.  
  39.     Serial.println("");
  40.     Serial.println("WiFi connected");
  41.     Serial.println("IP address: ");
  42.     Serial.println(WiFi.localIP());
  43.    
  44.  
  45.     delay(500);
  46. }
  47.  
  48.  
  49. void loop() {
  50.  
  51.     delay(5000);
  52.     const uint16_t port = 8888;
  53.     const char * host = "192.168.1.33"; // IP for influxdb
  54.     char status;
  55.     double T;
  56.    
  57.    
  58.     Serial.print("connecting to ");
  59.     Serial.println(host);
  60.  
  61.     // Use WiFiClient class to create TCP connections
  62.    WiFiUDP udp;
  63.  
  64.    
  65.   status = pressure.startTemperature();
  66.   if (status != 0)
  67.   {
  68.     // Wait for the measurement to complete:
  69.     delay(status);
  70.  
  71.     // Retrieve the completed temperature measurement:
  72.     // Note that the measurement is stored in the variable T.
  73.     // Function returns 1 if successful, 0 if failure.
  74.  
  75.     status = pressure.getTemperature(T);
  76.   }
  77.  
  78.    
  79.     String line;
  80.    
  81.     // Edit below line to set device ID
  82.     line = String("temperature,device=downstairs value=" + String(T));
  83.    
  84.  
  85.     udp.beginPacket(host,port);
  86.     udp.print(line);
  87.     udp.endPacket();
  88.  
  89.     Serial.println("closing connection");
  90.     udp.stop();
  91.    
  92.     Serial.println("wait 8 min...");
  93.     delay(500000);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement