Using multi wifi and fully working using secureclient

This commit is contained in:
Amritanshu Agrawal 2023-07-11 17:15:27 +05:30
parent 496ba9510a
commit 0ce1b6c557

View File

@ -1,17 +1,13 @@
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
#include <DHT.h>
#include <Wire.h>
#include <WiFiClientSecure.h>
#include <Wire.h>
// ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
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";
const uint32_t connectTimeoutMs = 5000;
// Replace with your server's address
const char* serverAddress = "sensors.tanshu.com";
@ -21,13 +17,13 @@ const int serverPort = 443;
const int dhtPin = D4;
// Replace with your device name
const char* deviceName = "s1-orb";
const char* deviceName = "s1-gwb";
// Initialize the DHT sensor
DHT dht(dhtPin, DHT22);
// Function declaration
void connectToWiFi();
void setupWiFi();
void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device);
void sendBufferedData();
@ -47,13 +43,19 @@ const unsigned long interval = 30000;
void setup() {
Serial.begin(9600);
// Serial.setDebugOutput(true);
connectToWiFi();
setupWiFi();
dht.begin();
}
void loop() {
// Maintain WiFi connection
if (wifiMulti.run(connectTimeoutMs) != WL_CONNECTED) {
Serial.print(".");
return;
}
unsigned long currentTimestamp = millis();
if (currentTimestamp - previousTimestamp >= interval) {
@ -86,21 +88,18 @@ void loop() {
}
}
void connectToWiFi() {
delay(2000);
Serial.println("Starting WiFi Connection");
void setupWiFi() {
Serial.println("Setting up WiFi Connection");
// Set in station mode
// WiFi.mode(WIFI_STA);
WiFi.mode(WIFI_STA);
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
// Register multi WiFi networks
// wifiMulti.addAP("Tanshu", "openwrt15");
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
WiFi.begin(ssid, password);
wifiMulti.addAP("Tanshu", "openwrt15");
wifiMulti.addAP("Mozimo", "bestchocever");
// while (wifiMulti.run() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
while (wifiMulti.run(connectTimeoutMs) != WL_CONNECTED) {
Serial.print(".");
}
@ -114,30 +113,17 @@ void sendSensorData(float temperature, float humidity, unsigned long age, const
String url = "/upload";
WiFiClientSecure client;
client.setInsecure();//skip verification
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" +
@ -167,7 +153,6 @@ void sendBufferedData() {
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