Working with MQTT up to Grafana
This commit is contained in:
parent
f20d25ffb6
commit
83a5171fb6
81
src/main.cpp
81
src/main.cpp
@ -1,6 +1,6 @@
|
||||
#include <DHT.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
|
||||
#include <ESP8266WiFiMulti.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Wire.h>
|
||||
|
||||
@ -9,41 +9,24 @@ ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class
|
||||
// WiFi connect timeout per AP. Increase when connecting takes longer.
|
||||
const uint32_t connectTimeoutMs = 5000;
|
||||
|
||||
// MQTT broker's address and port
|
||||
// MQTT settings
|
||||
const char* mqttServer = "iot.tanshu.com";
|
||||
const int mqttPort = 1883;
|
||||
|
||||
// MQTT broker username and password
|
||||
const char* mqttClientId = "s3-roy";
|
||||
const char* mqttUser = "esp8266";
|
||||
const char* mqttPassword = "esp8266";
|
||||
|
||||
// DHT sensor pin
|
||||
const int dhtPin = D4;
|
||||
|
||||
// Device name
|
||||
const char* deviceName = "s1-gwb";
|
||||
|
||||
// Initialize the DHT sensor
|
||||
DHT dht(dhtPin, DHT22);
|
||||
|
||||
// Function declaration
|
||||
void setupWiFi();
|
||||
void connectToMQTT();
|
||||
void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device);
|
||||
void sendBufferedData();
|
||||
void sendSensorData(float temperature, float humidity, const char* device);
|
||||
|
||||
// Data buffer
|
||||
struct SensorData {
|
||||
float temperature;
|
||||
float humidity;
|
||||
unsigned long timestamp;
|
||||
};
|
||||
|
||||
const int BUFFER_SIZE = 10;
|
||||
SensorData buffer[BUFFER_SIZE];
|
||||
int bufferIndex = 0;
|
||||
|
||||
unsigned long previousTimestamp = 0;
|
||||
const unsigned long interval = 30000;
|
||||
|
||||
WiFiClient espClient;
|
||||
@ -66,14 +49,18 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned long currentTimestamp = millis();
|
||||
if (!client.connected()) {
|
||||
connectToMQTT();
|
||||
}
|
||||
|
||||
client.loop();
|
||||
|
||||
if (currentTimestamp - previousTimestamp >= interval) {
|
||||
float temperature = dht.readTemperature();
|
||||
float humidity = dht.readHumidity();
|
||||
|
||||
if (isnan(temperature) || isnan(humidity)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
delay(1000);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -83,25 +70,10 @@ void loop() {
|
||||
Serial.print(humidity);
|
||||
Serial.println(" %");
|
||||
|
||||
// Buffer the data
|
||||
buffer[bufferIndex].temperature = temperature;
|
||||
buffer[bufferIndex].humidity = humidity;
|
||||
buffer[bufferIndex].timestamp = currentTimestamp;
|
||||
// Send the data
|
||||
sendSensorData(temperature, humidity, mqttClientId);
|
||||
|
||||
// Increment the buffer index
|
||||
bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
|
||||
|
||||
// Attempt to send buffered data when connectivity is restored
|
||||
sendBufferedData();
|
||||
|
||||
previousTimestamp = currentTimestamp;
|
||||
}
|
||||
|
||||
if (!client.connected()) {
|
||||
connectToMQTT();
|
||||
}
|
||||
|
||||
client.loop();
|
||||
delay(interval); // Delay between readings
|
||||
}
|
||||
|
||||
void setupWiFi() {
|
||||
@ -125,11 +97,11 @@ void setupWiFi() {
|
||||
Serial.println("IP address: " + WiFi.localIP().toString());
|
||||
}
|
||||
|
||||
void sendSensorData(float temperature, float humidity, unsigned long age, const char* device) {
|
||||
void sendSensorData(float temperature, float humidity, const char* device) {
|
||||
char topic[50];
|
||||
snprintf(topic, sizeof(topic), "sensor/%s", device);
|
||||
char payload[100];
|
||||
snprintf(payload, sizeof(payload), "{\"temperature\":%.2f,\"humidity\":%.2f,\"age\":%lu}", temperature, humidity, millis() - age);
|
||||
snprintf(payload, sizeof(payload), "{\"temperature\":%.2f,\"humidity\":%.2f}", temperature, humidity);
|
||||
client.publish(topic, payload);
|
||||
Serial.println("Sent sensor data to MQTT broker!");
|
||||
}
|
||||
@ -138,7 +110,7 @@ void connectToMQTT() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Connecting to MQTT broker...");
|
||||
|
||||
if (client.connect("ESP8266Client", mqttUser, mqttPassword)) {
|
||||
if (client.connect(mqttClientId, mqttUser, mqttPassword)) {
|
||||
Serial.println("connected");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
@ -148,24 +120,3 @@ void connectToMQTT() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sendBufferedData() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
// Send buffered data if connectivity is restored
|
||||
for (int dataIndex = bufferIndex - 1; dataIndex >= 0; dataIndex--) {
|
||||
// Extract temperature, humidity, and timestamp from the buffer
|
||||
float temperature = buffer[dataIndex].temperature;
|
||||
float humidity = buffer[dataIndex].humidity;
|
||||
unsigned long age = buffer[dataIndex].timestamp;
|
||||
|
||||
// Attempt to send the data
|
||||
sendSensorData(temperature, humidity, age, deviceName);
|
||||
|
||||
// Wait a short time before sending the next data
|
||||
delay(100);
|
||||
}
|
||||
|
||||
// Clear the buffer
|
||||
bufferIndex = 0;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user