uwezi

wemos_d1_mini_ds18b20_influx

Jan 21st, 2021
929
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <InfluxDbClient.h>
  2. #include <ESP8266WiFiMulti.h>
  3. ESP8266WiFiMulti wifiMulti;
  4.  
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. #define ONE_WIRE_BUS D5
  9. OneWire oneWire(ONE_WIRE_BUS);
  10. DallasTemperature sensors(&oneWire);
  11.  
  12. union
  13. {
  14.  uint64_t the_long_long;
  15.  uint32_t the_longs[2];
  16.  uint8_t the_shorts[8];
  17. } devAddr;
  18.  
  19. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  20.  
  21. // fill in the blanks...
  22. #define WIFI_SSID ""
  23. #define WIFI_PASSWORD ""
  24. #define INFLUXDB_URL ""
  25. #define INFLUXDB_DB_NAME ""
  26.  
  27. // Single InfluxDB instance
  28. InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
  29.  
  30. // Define data point with measurement name 'device_status`
  31. Point point("temperature");
  32.  
  33. int16_t i, j;
  34. char query[160];
  35.  
  36. uint32_t lasttime;
  37.    
  38. void setup()
  39. {
  40.   char dummytext[20];
  41.  
  42.   Serial.begin(115200); //Begin serial communication
  43.   Serial.println("Arduino Digital Temperature // Serial Monitor Version");
  44.   pinMode(D2, OUTPUT);
  45.  
  46.   // Connect WiFi
  47.   Serial.println("Connecting to WiFi");
  48.   WiFi.mode(WIFI_STA);
  49.   wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  50.   while (wifiMulti.run() != WL_CONNECTED)
  51.   {
  52.     Serial.print(".");
  53.     delay(100);
  54.   }
  55.   Serial.print ( "IP address: " );
  56.   Serial.println ( WiFi.localIP() );
  57.  
  58.   // Check server connection
  59.   if (client.validateConnection())
  60.   {
  61.     Serial.print("Connected to InfluxDB: ");
  62.     Serial.println(client.getServerUrl());
  63.   }
  64.   else
  65.   {
  66.     Serial.print("InfluxDB connection failed: ");
  67.     Serial.println(client.getLastErrorMessage());
  68.   }  
  69.  
  70.   // check the real time on the internet
  71.   timeSync("UTC", "se.pool.ntp.org", "pool.ntp.org");
  72.  
  73.   oneWire.reset() ;
  74.   lasttime = millis();
  75.  
  76.   // try to find all one-wire sensors in the system
  77.   sensors.begin();  
  78.   Serial.print(sensors.getDS18Count());
  79.   Serial.println(" devices found");
  80.   for (i=0; i<sensors.getDS18Count(); i++)
  81.   {
  82.     sensors.getAddress(devAddr.the_shorts, i);
  83.     sprintf(query,"-x 0x%8lx%8lx",devAddr.the_longs[1],devAddr.the_longs[0]);
  84.     Serial.println(query);
  85.     sprintf(query,"-ll %llu",devAddr.the_long_long);
  86.     Serial.println(query);
  87.   }  
  88. }
  89.  
  90. void loop()
  91. {
  92.   digitalWrite(D2, LOW);     // turn the LED on
  93.   delay(10);                 // wait for a second
  94.   digitalWrite(D2, HIGH);    // turn the LED off
  95.  
  96.   Serial.print(".");
  97.   if ((millis()-lasttime) > 10000)
  98.   {
  99.     lasttime = millis();
  100.  
  101.     // start conversion on all attached sensors at the same time
  102.     sensors.requestTemperatures();  
  103.  
  104.     for (i=0; i<sensors.getDS18Count(); i++)
  105.     {
  106.       sensors.getAddress(devAddr.the_shorts, i);
  107.  
  108.       // Add data
  109.       point.clearTags();
  110.       point.clearFields();
  111.       sprintf(query,"%llu", devAddr.the_long_long);
  112.       point.addTag("sensorID", query);
  113.       point.addField("value", sensors.getTempCByIndex(i));
  114.  
  115.       // Write data
  116.       Serial.print("Writing: ");
  117.       Serial.println(point.toLineProtocol());
  118.    
  119.       // If no Wifi signal: reconnect
  120.       if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED))
  121.       {
  122.         Serial.println("Wifi connection lost");
  123.       }
  124.  
  125.       // Write point
  126.       if (!client.writePoint(point))
  127.       {
  128.         Serial.print("InfluxDB write failed: ");
  129.         Serial.println(client.getLastErrorMessage());
  130.       }
  131.     }
  132.   }
  133.   delay(500);              // wait for a second
  134. }
  135.  
RAW Paste Data