uwezi

logg_ds18b20_wemos_d1

Dec 31st, 2021 (edited)
1,788
0
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.  uint64_t the_long_long;
  14.  uint32_t the_longs[2];
  15.  uint8_t the_shorts[8];
  16. } devAddr;
  17.  
  18. // WiFi AP SSID
  19. #define WIFI_SSID ""
  20. // WiFi password
  21. #define WIFI_PASSWORD ""
  22. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  23.  
  24. // InfluxDB server url, e.g. http://192.168.1.48:8086 (don't use localhost, always server name or ip address)
  25. #define INFLUXDB_URL ""
  26. // InfluxDB database name
  27. #define INFLUXDB_DB_NAME ""
  28.  
  29. // Single InfluxDB instance
  30. InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
  31.  
  32. // Define data point with measurement name 'device_status`
  33. Point point("temperature");
  34.  
  35.   int16_t i, j;
  36.   char query[160];
  37.  
  38. uint32_t lasttime;
  39.    
  40. void setup()
  41. {
  42.   char dummytext[20];
  43.  
  44.   Serial.begin(115200); //Begin serial communication
  45.   Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message  
  46.   pinMode(D2, OUTPUT);
  47.  
  48.   // Connect WiFi
  49.   Serial.println("Connecting to WiFi");
  50.   WiFi.mode(WIFI_STA);
  51.   wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  52.   while (wifiMulti.run() != WL_CONNECTED)
  53.   {
  54.     Serial.print(".");
  55.     delay(100);
  56.   }
  57.   Serial.print ( "IP address: " );
  58.   Serial.println ( WiFi.localIP() );
  59.  
  60.  
  61.   // Check server connection
  62.   if (client.validateConnection())
  63.   {
  64.     Serial.print("Connected to InfluxDB: ");
  65.     Serial.println(client.getServerUrl());
  66.   }
  67.   else
  68.   {
  69.     Serial.print("InfluxDB connection failed: ");
  70.     Serial.println(client.getLastErrorMessage());
  71.   }  
  72.  
  73.   // You can specify the time server pool and the offset, (in seconds)
  74.   // additionaly you can specify the update interval (in milliseconds).
  75.   //NTPClient timeClient(ntpUDP, "se.pool.ntp.org", 0, 120000);
  76.   timeSync("UTC", "se.pool.ntp.org", "pool.ntp.org");
  77.  
  78.   oneWire.reset() ;
  79.   lasttime = millis();
  80.  
  81.   sensors.begin();  
  82.   Serial.print(sensors.getDS18Count());
  83.   Serial.println(" devices found");
  84.   for (i=0; i<sensors.getDS18Count(); i++)
  85.   {
  86.     sensors.getAddress(devAddr.the_shorts, i);
  87.     sprintf(query,"-x 0x%8lx%8lx",devAddr.the_longs[1],devAddr.the_longs[0]);
  88.     Serial.println(query);
  89.     sprintf(query,"-ll %llu",devAddr.the_long_long);
  90.     Serial.println(query);
  91.   }  
  92. }
  93.  
  94. void loop()
  95. {
  96.   digitalWrite(D2, LOW);   // turn the LED on (HIGH is the voltage level)
  97.   delay(10);              // wait for a second
  98.   digitalWrite(D2, HIGH);    // turn the LED off by making the voltage LOW
  99.   Serial.print(".");
  100.   if ((millis()-lasttime) > 10000)
  101.   {
  102.     lasttime = millis();
  103.  
  104.     sensors.requestTemperatures();  
  105.  
  106.     for (i=0; i<sensors.getDS18Count(); i++)
  107.     {
  108.       sensors.getAddress(devAddr.the_shorts, i);
  109.  
  110.       // Add data
  111.       point.clearTags();
  112.       point.clearFields();
  113.       sprintf(query,"%llu", devAddr.the_long_long);
  114.       point.addTag("sensorID", query);
  115.       point.addField("value", sensors.getTempCByIndex(i));
  116.  
  117.       // Write data
  118.       Serial.print("Writing: ");
  119.       Serial.println(point.toLineProtocol());
  120.    
  121.       // If no Wifi signal, try to reconnect it
  122.       if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED))
  123.         Serial.println("Wifi connection lost");
  124.       // Write point
  125.       if (!client.writePoint(point))
  126.       {
  127.         Serial.print("InfluxDB write failed: ");
  128.         Serial.println(client.getLastErrorMessage());
  129.       }
  130.     }
  131.   }
  132.   delay(500);              // wait for a second
  133.  
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment