Created
October 23, 2023 22:35
-
-
Save pyrho/f33b2af318531d85957d3af5bb07c08a 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
#include <Wire.h> | |
#include "AHT20.h" | |
#include <ESP8266WiFi.h> | |
#include <InfluxDbClient.h> | |
#include <InfluxDbCloud.h> | |
#define ONE_MINUTE 60 * 1000 | |
#define ONE_HOUR ONE_MINUTE * 60 | |
#define INFLUXDB_URL "http://influxdb2.lan:8086" | |
#define INFLUXDB_TOKEN "RhSqRBPW11u7UYYWLV94VSC07HOcinqrdoWMZoaxFU3pYE94pIOW-Nu2_EjZOgOJXLba3MHkSPM0YoJaQ2SPdg==" | |
#define INFLUXDB_ORG "98dbe379e534ae7b" | |
#define INFLUXDB_BUCKET "sensors" | |
// WiFi AP SSID | |
#define WIFI_SSID "Ono-Sendai Cyberspace" | |
// WiFi password | |
#define WIFI_PASSWORD "rene coty est ton ami" | |
// Time zone info | |
#define TZ_INFO "UTC0" | |
#define DEVICE "ESP8266" | |
#define LOCATION "FILAMENT_BOX" | |
// Temp sensor | |
AHT20 aht20; | |
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate | |
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); | |
// Declare Data point | |
Point sensor("atmospheric_data"); | |
// const char* ssid = "iot"; | |
// const char* password = "!42# this is my iot password !42#"; | |
// const char* ssid = "Ono-Sendai Cyberspace"; | |
// const char* password = "rene coty est ton ami"; | |
void setup() { | |
Serial.begin(115200); | |
Serial.print("\n\n"); | |
Wire.begin(); // Start the I2C Controller bus on the NodeMCU | |
// Check that the sensor is online | |
if (!aht20.begin()) { | |
Serial.println("Could not find AHT20 sensor, resetting"); | |
while (1) | |
; // This will cause a crash and a reset (apparently) | |
} | |
Serial.println("AHT20 sensor online"); | |
// Connect to Wi-Fi | |
Serial.print("Connecting to "); | |
Serial.println(WIFI_SSID); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println(WiFi.status()); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("Wi-Fi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
// Accurate time is necessary for certificate validation and writing in batches | |
// We use the NTP servers in your area as provided by: 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()); | |
} | |
// Add tags to the data point | |
sensor.addTag("device", DEVICE); | |
sensor.addTag("location", LOCATION); | |
} | |
void loop() { | |
// Clear fields for reusing the point. Tags will remain the same as set above. | |
sensor.clearFields(); | |
float humidity = aht20.getHumidity(); | |
float temperature = aht20.getTemperature(); | |
if (isnan(humidity) || isnan(temperature)) { | |
Serial.println("Failed to read from AHT20 sensor!"); | |
return; | |
} | |
// Store measured value into point | |
sensor.addField("temperature", temperature); | |
sensor.addField("humidity", humidity); | |
Serial.print("Humidity: "); | |
Serial.print(humidity); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(temperature); | |
Serial.println(" *C"); | |
// Print what are we exactly writing | |
Serial.print("Writing: "); | |
Serial.println(sensor.toLineProtocol()); | |
// Check WiFi connection and reconnect if needed | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("Wifi connection lost"); | |
} | |
// Write point | |
if (!client.writePoint(sensor)) { | |
Serial.print("InfluxDB write failed: "); | |
Serial.println(client.getLastErrorMessage()); | |
} | |
delay(ONE_MINUTE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment