#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;
// WiFi AP SSID
#define WIFI_SSID ""
// WiFi password
#define WIFI_PASSWORD ""
int status = WL_IDLE_STATUS; // the Wifi radio's status
// InfluxDB server url, e.g. http://192.168.1.48:8086 (don't use localhost, always server name or ip address)
#define INFLUXDB_URL ""
// InfluxDB database name
#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"); //Print a message
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());
}
// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
//NTPClient timeClient(ntpUDP, "se.pool.ntp.org", 0, 120000);
timeSync("UTC", "se.pool.ntp.org", "pool.ntp.org");
oneWire.reset() ;
lasttime = millis();
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 (HIGH is the voltage level)
delay(10); // wait for a second
digitalWrite(D2, HIGH); // turn the LED off by making the voltage LOW
Serial.print(".");
if ((millis()-lasttime) > 10000)
{
lasttime = millis();
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, try to reconnect it
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
}
Comments