Skip to content

Commit bfca8c7

Browse files
authored
refactor: improve code structure and formatting (#9)
* feat: add example environment configuration header file * feat: add example environment configuration header * feat: migrate env.h.example to include directory and remove old files * refactor: move InfluxDB functionality and logging utilities into separate header files * refactor: move utility functions into separate module * refactor: move weather data functions into separate modules * pre-commit run --all-files * fix: move InfluxDB function from header to source file * refactor InfluxDB integration and add docs * feat: add toggle for Weather Underground data sending * docs: add docstring for `send_to_wunderground` * refactor: standardize function naming to snake_case * docs: unifies naming and improves documentation for sensor data * docs: clarifies sensor variable naming * chore: clean up imports
1 parent 05ce8e6 commit bfca8c7

9 files changed

Lines changed: 325 additions & 219 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
#define INFLUXDB_HOSTNAME "actual_influxdb_hostname"
1717
#define INFLUXDB_ORG_ID "actual_influxdb_orgid"
18+
#define INFLUXDB_BUCKET "actual_influxdb_bucket"
1819
#define INFLUXDB_API_TOKEN "actual_influxdb_api_token"
1920

2021
#define WEATHER_UNDERGROUND_STATION_ID "station_ID"
2122
#define WEATHER_UNDERGROUND_API_KEY "API_key"
2223

24+
#define SEND_TO_WEATHER_UNDERGROUND 1
25+
2326
#endif

include/influxdb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef INFLUXDB_H
2+
#define INFLUXDB_H
3+
4+
void send_to_influx_db(float temperature, float humidity, float pressure, float dew_point,
5+
float illumination, float battery_voltage, float solar_panel_voltage);
6+
7+
#endif // INFLUXDB_H

include/utils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef UTILS_H
2+
#define UTILS_H
3+
4+
#include <Arduino.h>
5+
6+
extern String log_buffer;
7+
8+
void serial_log(String message);
9+
10+
float calculate_dew_point(float temperature, float humidity);
11+
12+
void isolate_all_rtc_gpio();
13+
14+
void connect_to_wifi();
15+
16+
void send_log();
17+
18+
void send_to_database(float temperature, float humidity, float pressure, float dew_point,
19+
float illumination, float battery_voltage, float solar_panel_voltage);
20+
21+
#endif // UTILS_H

include/wunderground.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef WUNDERGROUND_H
2+
#define WUNDERGROUND_H
3+
4+
void send_to_wunderground(float temperature, int humidity, float baromin, float dew_point);
5+
6+
#endif // WUNDERGROUND_H

src/env.h.example

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/influxdb.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "env.h"
2+
#include "utils.h"
3+
4+
#include <HTTPClient.h>
5+
#include <WiFi.h>
6+
7+
/**
8+
* Sends weather sensor data to InfluxDB using HTTP POST request
9+
*
10+
* This function constructs an InfluxDB line protocol payload with sensor readings
11+
* and transmits it to the configured InfluxDB instance via HTTP API v2.
12+
*
13+
* Link to InfluxDB API documentation:
14+
* https://docs.influxdata.com/influxdb3/cloud-serverless/get-started/write/?t=v2+API#write-line-protocol-to-influxdb
15+
*
16+
* @param temperature Temperature in Celsius
17+
* @param humidity Relative humidity percentage
18+
* @param pressure Atmospheric pressure
19+
* @param dew_point Dew point in Celsius
20+
* @param illumination Light intensity measurement
21+
* @param battery_voltage Battery voltage level
22+
* @param solar_panel_voltage Solar panel voltage output
23+
*
24+
* @note Requires active WiFi connection. Function will log error if WiFi disconnected.
25+
* @note Uses InfluxDB API v2 with token-based authentication.
26+
*/
27+
void send_to_influx_db(float temperature, float humidity, float pressure, float dew_point,
28+
float illumination, float battery_voltage, float solar_panel_voltage)
29+
{
30+
if (WiFi.status() == WL_CONNECTED) {
31+
HTTPClient http;
32+
33+
String request_url = String(INFLUXDB_HOSTNAME) + "/api/v2/write?"
34+
+ "bucket=" + String(INFLUXDB_BUCKET) + "&precision=ns";
35+
36+
serial_log("Sending data to InfluxDB...");
37+
38+
http.begin(request_url);
39+
http.setTimeout(10000); // 10s
40+
41+
http.addHeader("Authorization", "Token " + String(INFLUXDB_API_TOKEN));
42+
http.addHeader("Content-Type", "text/plain; charset=utf-8");
43+
http.addHeader("Accept", "application/json");
44+
45+
// Format: "weather temperature=XX.XX,humidity=XX.X,pressure=XX.XX,..."
46+
String payload = String("weather ") + "temperature=" + String(temperature, 2) + ","
47+
+ "humidity=" + String(humidity, 1) + "," + "pressure=" + String(pressure, 2) + ","
48+
+ "illumination=" + String(illumination, 1) + "," + "dew_point=" + String(dew_point, 1)
49+
+ "," + "battery_voltage=" + String(battery_voltage, 2) + ","
50+
+ "solar_panel_voltage=" + String(solar_panel_voltage, 2);
51+
52+
int response_code = http.POST(payload);
53+
serial_log(payload);
54+
55+
if (response_code > 0) {
56+
serial_log("HTTP Response Code: ");
57+
serial_log(String(response_code));
58+
} else {
59+
serial_log("Error in HTTP request: ");
60+
serial_log(String(response_code));
61+
}
62+
63+
http.end();
64+
} else {
65+
serial_log("WiFi not connected");
66+
}
67+
}

0 commit comments

Comments
 (0)