Initial Commit. It is working

This commit is contained in:
2023-07-07 09:47:54 +05:30
commit 496ba9510a
7 changed files with 308 additions and 0 deletions

180
src/main.cpp Normal file
View File

@ -0,0 +1,180 @@
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
#include <DHT.h>
#include <Wire.h>
#include <WiFiClientSecure.h>
// ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
// WiFi connect timeout per AP. Increase when connecting takes longer.
// const uint32_t connectTimeoutMs = 5000;
// Replace with your network credentials
const char* ssid = "Tanshu";
const char* password = "openwrt15";
// Replace with your server's address
const char* serverAddress = "sensors.tanshu.com";
const int serverPort = 443;
// Replace with your DHT sensor pin
const int dhtPin = D4;
// Replace with your device name
const char* deviceName = "s1-orb";
// Initialize the DHT sensor
DHT dht(dhtPin, DHT22);
// Function declaration
void connectToWiFi();
void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device);
void sendBufferedData();
// 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;
void setup() {
Serial.begin(9600);
// Serial.setDebugOutput(true);
connectToWiFi();
dht.begin();
}
void loop() {
unsigned long currentTimestamp = millis();
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!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
// Buffer the data
buffer[bufferIndex].temperature = temperature;
buffer[bufferIndex].humidity = humidity;
buffer[bufferIndex].timestamp = currentTimestamp;
// Increment the buffer index
bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
// Attempt to send buffered data when connectivity is restored
sendBufferedData();
previousTimestamp = currentTimestamp;
}
}
void connectToWiFi() {
delay(2000);
Serial.println("Starting WiFi Connection");
// Set in station mode
// WiFi.mode(WIFI_STA);
// Register multi WiFi networks
// wifiMulti.addAP("Tanshu", "openwrt15");
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
WiFi.begin(ssid, password);
// while (wifiMulti.run() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.println("IP address: " + WiFi.localIP().toString());
}
void sendSensorData(float temperature, float humidity, unsigned long age, const char* device) {
String url = "/upload";
WiFiClientSecure client;
client.setInsecure();//skip verification
if (!client.connect(serverAddress, serverPort)) {
Serial.println("Failed to connect to server!");
return;
}
// if (client.verifyCertChain(serverAddress)) {
// Serial.println("Certificate matches!");
// } else {
// Serial.println("Certificate doesn't match!");
// }
// Calculate the length of the payload
String payload = "temp=" + String(temperature) + "&humidity=" + String(humidity) + "&age=" + String(millis() - age) + "&device=" + device;
int payloadLength = payload.length();
Serial.println("POST " + url + " HTTP/1.1\r\n" +
"Host: " + String(serverAddress) + "\r\n" +
"Connection: close\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(payloadLength) + "\r\n\r\n" +
payload);
client.print("POST " + url + " HTTP/1.1\r\n" +
"Host: " + String(serverAddress) + "\r\n" +
"Connection: close\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + String(payloadLength) + "\r\n\r\n" +
payload);
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
Serial.println("Sent sensor data to server!");
client.stop();
}
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
// Serial.println("temp: " + String(temperature) + " // humidity: " + String(humidity) + " // age: " + String(millis() - age) + " // device: " + deviceName);
sendSensorData(temperature, humidity, age, deviceName);
// Wait a short time before sending the next data
delay(100);
}
// Clear the buffer
bufferIndex = 0;
}
}