Using multi wifi and fully working using secureclient
This commit is contained in:
parent
496ba9510a
commit
0ce1b6c557
59
src/main.cpp
59
src/main.cpp
@ -1,17 +1,13 @@
|
|||||||
|
#include <DHT.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
|
#include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
|
||||||
#include <DHT.h>
|
|
||||||
#include <Wire.h>
|
|
||||||
#include <WiFiClientSecure.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.
|
// WiFi connect timeout per AP. Increase when connecting takes longer.
|
||||||
// const uint32_t connectTimeoutMs = 5000;
|
const uint32_t connectTimeoutMs = 5000;
|
||||||
|
|
||||||
// Replace with your network credentials
|
|
||||||
const char* ssid = "Tanshu";
|
|
||||||
const char* password = "openwrt15";
|
|
||||||
|
|
||||||
// Replace with your server's address
|
// Replace with your server's address
|
||||||
const char* serverAddress = "sensors.tanshu.com";
|
const char* serverAddress = "sensors.tanshu.com";
|
||||||
@ -21,13 +17,13 @@ const int serverPort = 443;
|
|||||||
const int dhtPin = D4;
|
const int dhtPin = D4;
|
||||||
|
|
||||||
// Replace with your device name
|
// Replace with your device name
|
||||||
const char* deviceName = "s1-orb";
|
const char* deviceName = "s1-gwb";
|
||||||
|
|
||||||
// Initialize the DHT sensor
|
// Initialize the DHT sensor
|
||||||
DHT dht(dhtPin, DHT22);
|
DHT dht(dhtPin, DHT22);
|
||||||
|
|
||||||
// Function declaration
|
// Function declaration
|
||||||
void connectToWiFi();
|
void setupWiFi();
|
||||||
void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device);
|
void sendSensorData(float temperature, float humidity, unsigned long timestamp, const char* device);
|
||||||
void sendBufferedData();
|
void sendBufferedData();
|
||||||
|
|
||||||
@ -47,13 +43,19 @@ const unsigned long interval = 30000;
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// Serial.setDebugOutput(true);
|
|
||||||
connectToWiFi();
|
setupWiFi();
|
||||||
|
|
||||||
dht.begin();
|
dht.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
// Maintain WiFi connection
|
||||||
|
if (wifiMulti.run(connectTimeoutMs) != WL_CONNECTED) {
|
||||||
|
Serial.print(".");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long currentTimestamp = millis();
|
unsigned long currentTimestamp = millis();
|
||||||
|
|
||||||
if (currentTimestamp - previousTimestamp >= interval) {
|
if (currentTimestamp - previousTimestamp >= interval) {
|
||||||
@ -86,21 +88,18 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectToWiFi() {
|
void setupWiFi() {
|
||||||
delay(2000);
|
Serial.println("Setting up WiFi Connection");
|
||||||
Serial.println("Starting WiFi Connection");
|
|
||||||
|
|
||||||
// Set in station mode
|
// Set in station mode
|
||||||
// WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
|
||||||
|
|
||||||
// Register multi WiFi networks
|
// Register multi WiFi networks
|
||||||
// wifiMulti.addAP("Tanshu", "openwrt15");
|
wifiMulti.addAP("Tanshu", "openwrt15");
|
||||||
WiFi.setPhyMode(WIFI_PHY_MODE_11G);
|
wifiMulti.addAP("Mozimo", "bestchocever");
|
||||||
WiFi.begin(ssid, password);
|
|
||||||
|
|
||||||
// while (wifiMulti.run() != WL_CONNECTED) {
|
while (wifiMulti.run(connectTimeoutMs) != WL_CONNECTED) {
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,30 +113,17 @@ void sendSensorData(float temperature, float humidity, unsigned long age, const
|
|||||||
String url = "/upload";
|
String url = "/upload";
|
||||||
|
|
||||||
WiFiClientSecure client;
|
WiFiClientSecure client;
|
||||||
client.setInsecure();//skip verification
|
client.setInsecure(); // skip verification
|
||||||
|
|
||||||
if (!client.connect(serverAddress, serverPort)) {
|
if (!client.connect(serverAddress, serverPort)) {
|
||||||
Serial.println("Failed to connect to server!");
|
Serial.println("Failed to connect to server!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (client.verifyCertChain(serverAddress)) {
|
|
||||||
// Serial.println("Certificate matches!");
|
|
||||||
// } else {
|
|
||||||
// Serial.println("Certificate doesn't match!");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Calculate the length of the payload
|
// Calculate the length of the payload
|
||||||
String payload = "temp=" + String(temperature) + "&humidity=" + String(humidity) + "&age=" + String(millis() - age) + "&device=" + device;
|
String payload = "temp=" + String(temperature) + "&humidity=" + String(humidity) + "&age=" + String(millis() - age) + "&device=" + device;
|
||||||
int payloadLength = payload.length();
|
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" +
|
client.print("POST " + url + " HTTP/1.1\r\n" +
|
||||||
"Host: " + String(serverAddress) + "\r\n" +
|
"Host: " + String(serverAddress) + "\r\n" +
|
||||||
"Connection: close\r\n" +
|
"Connection: close\r\n" +
|
||||||
@ -167,7 +153,6 @@ void sendBufferedData() {
|
|||||||
unsigned long age = buffer[dataIndex].timestamp;
|
unsigned long age = buffer[dataIndex].timestamp;
|
||||||
|
|
||||||
// Attempt to send the data
|
// Attempt to send the data
|
||||||
// Serial.println("temp: " + String(temperature) + " // humidity: " + String(humidity) + " // age: " + String(millis() - age) + " // device: " + deviceName);
|
|
||||||
sendSensorData(temperature, humidity, age, deviceName);
|
sendSensorData(temperature, humidity, age, deviceName);
|
||||||
|
|
||||||
// Wait a short time before sending the next data
|
// Wait a short time before sending the next data
|
||||||
|
Loading…
Reference in New Issue
Block a user