Created
July 30, 2025 12:15
-
-
Save jondkelley/bb340c89d8f806e4fa42ab6c828eefde to your computer and use it in GitHub Desktop.
Esp32 Voltage monitor to influx
This file contains hidden or 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 <WiFi.h> | |
| #include <HTTPClient.h> | |
| #include <time.h> | |
| #include <esp_sleep.h> | |
| const char* ssid = "your_ssid"; | |
| const char* password = "your_password"; | |
| const char* influx_url = "http://192.168.1.100:8086/api/v2/write?org=myorg&bucket=mybucket&precision=s"; | |
| const char* influx_token = "your_token"; | |
| // Timing parameters | |
| const int maxSamples = 12; // 12 × 5s = 60s | |
| const int sampleIntervalSec = 5; // 5-second sampling | |
| const int ntpSyncTimeoutSec = 10; | |
| // RTC memory | |
| RTC_DATA_ATTR int sampleCount = 0; | |
| RTC_DATA_ATTR char lineBuffer[2048] = {0}; // Persisted across deep sleep | |
| // ========== Sensor Mock (replace with actual reading) ========== | |
| float readSensor() { | |
| return analogRead(34) * (3.3 / 4095.0); // Replace with real sensor | |
| } | |
| // ========== Time Sync ========== | |
| void syncTime() { | |
| configTime(0, 0, "pool.ntp.org"); | |
| int retries = 0; | |
| time_t now = time(nullptr); | |
| while (now < 100000 && retries++ < (ntpSyncTimeoutSec * 2)) { | |
| delay(500); | |
| now = time(nullptr); | |
| } | |
| if (now < 100000) { | |
| Serial.println("NTP sync failed"); | |
| } else { | |
| Serial.printf("NTP time synced: %ld\n", now); | |
| } | |
| } | |
| // ========== InfluxDB Upload ========== | |
| void flushToInfluxDB() { | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) delay(500); | |
| HTTPClient http; | |
| http.begin(influx_url); | |
| http.addHeader("Authorization", "Token " + String(influx_token)); | |
| http.addHeader("Content-Type", "text/plain"); | |
| int httpCode = http.POST(String(lineBuffer)); | |
| Serial.printf("InfluxDB POST code: %d\n", httpCode); | |
| http.end(); | |
| WiFi.disconnect(true); | |
| } | |
| // ========== Add Line Protocol ========== | |
| void appendLineProtocol(float value, time_t ts) { | |
| char line[128]; | |
| snprintf(line, sizeof(line), "temperature,sensor=esp32 value=%.3f %ld\n", value, ts); | |
| strncat(lineBuffer, line, sizeof(lineBuffer) - strlen(lineBuffer) - 1); | |
| } | |
| // ========== Main Entry ========== | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(100); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) delay(500); | |
| syncTime(); | |
| WiFi.disconnect(true); | |
| float value = readSensor(); | |
| time_t ts = time(nullptr); | |
| appendLineProtocol(value, ts); | |
| sampleCount++; | |
| Serial.printf("Sample %d: %.2f at %ld\n", sampleCount, value, ts); | |
| if (sampleCount >= maxSamples) { | |
| flushToInfluxDB(); | |
| sampleCount = 0; | |
| memset(lineBuffer, 0, sizeof(lineBuffer)); | |
| } | |
| Serial.println("Entering deep sleep..."); | |
| esp_sleep_enable_timer_wakeup(sampleIntervalSec * 1000000ULL); | |
| esp_deep_sleep_start(); | |
| } | |
| void loop() { | |
| // Never used due to deep sleep | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment