-
-
Save hongbo-miao/19a3bceaccbf5dfa6516a2ad0fbe82ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(ESP32) | |
#include <WiFiMulti.h> | |
WiFiMulti wifiMulti; | |
#define DEVICE "ESP32" | |
#elif defined(ESP8266) | |
#include <ESP8266WiFiMulti.h> | |
ESP8266WiFiMulti wifiMulti; | |
#define DEVICE "ESP8266" | |
#endif | |
#include <InfluxDbClient.h> | |
#include <InfluxDbCloud.h> | |
#include "DHT.h" | |
#define DHTPIN 4 // Digital pin connected to the DHT sensor | |
#define DHTTYPE DHT11 // DHT 11 | |
// Set up your WiFi connection here | |
// WiFi AP SSID | |
#define WIFI_SSID "<your ssid>" | |
// WiFi password | |
#define WIFI_PASSWORD "<your password>" | |
// Set up your InfluxDB details here | |
// InfluxDB v2 server url (Use: InfluxDB UI -> Load Data -> Client Libraries) | |
#define INFLUXDB_URL "http://<your ip>:8086" | |
// InfluxDB v2 server or cloud API authentication token (Use: InfluxDB UI -> Data -> Tokens -> <select token>) | |
#define INFLUXDB_TOKEN "O24__XgbcJyoctWgsEjot6lW2Eh_xX-Jrw54cJ5YLssz8EIYAEd62Xgj_ulSeBeH4w-4o5PpLGbWeE7dpM8tcg==" | |
// InfluxDB v2 organization id (Use: InfluxDB UI -> User -> About -> Common Ids ) | |
#define INFLUXDB_ORG "<your org>" | |
// InfluxDB v2 bucket name (Use: InfluxDB UI -> Data -> Buckets) | |
#define INFLUXDB_BUCKET "<your bucket>" | |
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | |
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3" | |
// InfluxDB client instance with preconfigured InfluxCloud certificate | |
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); | |
// Create your Data Point here | |
Point sensor("climate"); | |
// Set up the DHT connection | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
// Start Serial for monitoring | |
Serial.begin(115200); | |
// Init the DHT sensor | |
dht.begin(); | |
// Setup wifi | |
WiFi.mode(WIFI_STA); | |
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); | |
Serial.print("Connecting to wifi"); | |
while (wifiMulti.run() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(100); | |
} | |
Serial.println(); | |
// Add tags. Here we will track which device our data is coming from | |
sensor.addTag("device", DEVICE); | |
// Accurate time is necessary for certificate validation and writing in batches | |
// For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/ | |
// Syncing progress and the time will be printed to Serial. | |
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov"); | |
// 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()); | |
} | |
} | |
void loop() { | |
// Clear fields for reusing the point. Tags will remain untouched | |
sensor.clearFields(); | |
// Read the temperature and humidity from the sensor, and add them to your data point, along with a calculated heat index | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
sensor.addField("humidity", h); | |
sensor.addField("temperature", t); | |
sensor.addField("heat_index", dht.computeHeatIndex(t, h, false)); | |
// Print what are we exactly writing | |
Serial.print("Writing: "); | |
Serial.println(sensor.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(sensor)) { | |
Serial.print("InfluxDB write failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
//Wait 10s | |
Serial.println("Wait 10s"); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment