Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <InfluxDbClient.h>
- #include <ESP8266WiFiMulti.h>
- ESP8266WiFiMulti wifiMulti;
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS D5
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- union
- {
- uint64_t the_long_long;
- uint32_t the_longs[2];
- uint8_t the_shorts[8];
- } devAddr;
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- // fill in the blanks...
- #define WIFI_SSID ""
- #define WIFI_PASSWORD ""
- #define INFLUXDB_URL ""
- #define INFLUXDB_DB_NAME ""
- // Single InfluxDB instance
- InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
- // Define data point with measurement name 'device_status`
- Point point("temperature");
- int16_t i, j;
- char query[160];
- uint32_t lasttime;
- void setup()
- {
- char dummytext[20];
- Serial.begin(115200); //Begin serial communication
- Serial.println("Arduino Digital Temperature // Serial Monitor Version");
- pinMode(D2, OUTPUT);
- // Connect WiFi
- Serial.println("Connecting to WiFi");
- WiFi.mode(WIFI_STA);
- wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
- while (wifiMulti.run() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(100);
- }
- Serial.print ( "IP address: " );
- Serial.println ( WiFi.localIP() );
- // Check server connection
- if (client.validateConnection())
- {
- Serial.print("Connected to InfluxDB: ");
- Serial.println(client.getServerUrl());
- }
- else
- {
- Serial.print("InfluxDB connection failed: ");
- Serial.println(client.getLastErrorMessage());
- }
- // check the real time on the internet
- timeSync("UTC", "se.pool.ntp.org", "pool.ntp.org");
- oneWire.reset() ;
- lasttime = millis();
- // try to find all one-wire sensors in the system
- sensors.begin();
- Serial.print(sensors.getDS18Count());
- Serial.println(" devices found");
- for (i=0; i<sensors.getDS18Count(); i++)
- {
- sensors.getAddress(devAddr.the_shorts, i);
- sprintf(query,"-x 0x%8lx%8lx",devAddr.the_longs[1],devAddr.the_longs[0]);
- Serial.println(query);
- sprintf(query,"-ll %llu",devAddr.the_long_long);
- Serial.println(query);
- }
- }
- void loop()
- {
- digitalWrite(D2, LOW); // turn the LED on
- delay(10); // wait for a second
- digitalWrite(D2, HIGH); // turn the LED off
- Serial.print(".");
- if ((millis()-lasttime) > 10000)
- {
- lasttime = millis();
- // start conversion on all attached sensors at the same time
- sensors.requestTemperatures();
- for (i=0; i<sensors.getDS18Count(); i++)
- {
- sensors.getAddress(devAddr.the_shorts, i);
- // Add data
- point.clearTags();
- point.clearFields();
- sprintf(query,"%llu", devAddr.the_long_long);
- point.addTag("sensorID", query);
- point.addField("value", sensors.getTempCByIndex(i));
- // Write data
- Serial.print("Writing: ");
- Serial.println(point.toLineProtocol());
- // If no Wifi signal: reconnect
- if ((WiFi.RSSI() == 0) && (wifiMulti.run() != WL_CONNECTED))
- {
- Serial.println("Wifi connection lost");
- }
- // Write point
- if (!client.writePoint(point))
- {
- Serial.print("InfluxDB write failed: ");
- Serial.println(client.getLastErrorMessage());
- }
- }
- }
- delay(500); // wait for a second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement