171 lines
5.4 KiB
C++
171 lines
5.4 KiB
C++
#include <Arduino.h>
|
|
#include <WiFiClient.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <EEPROM.h>
|
|
#include <DHT.h>
|
|
|
|
#define DHTPIN 0
|
|
#define DHTTYPE DHT22
|
|
DHT dht22(DHTPIN, DHTTYPE); // <-- Global object
|
|
#define ledPin 2
|
|
int wifiConnectStatus = 0; //WiFi connection status
|
|
|
|
WiFiServer server(80);
|
|
|
|
void loadWiFiSettings(char* ssid, char* password) { //Read WiFi settings from EEPROM
|
|
EEPROM.begin(64); //Initialize EEPROM with size 64 bytes
|
|
for (int i = 0; i < 32; i++) {
|
|
char c = EEPROM.read(i);
|
|
// Serial.print(c);
|
|
// Serial.print(" ");
|
|
// Serial.print((int)c);
|
|
// Serial.print(" ");
|
|
ssid[i] = c;
|
|
if(c =='\0'){
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < 32; i++) {
|
|
char c = EEPROM.read(i + 32);
|
|
// Serial.print(c);
|
|
// Serial.print(" ");
|
|
// Serial.print((int)c);
|
|
// Serial.print(" ");
|
|
password[i] = c;
|
|
if(c == '\0'){
|
|
break;
|
|
}
|
|
}
|
|
EEPROM.end();
|
|
}
|
|
|
|
void saveWiFiSettings(const char ssid[32], const char password[32]) {
|
|
EEPROM.begin(64); //Initialize EEPROM with size 64 bytes
|
|
for(int i=0; i<32; i++) {
|
|
// Serial.print(ssid[i]);
|
|
// Serial.print(" ");
|
|
// Serial.print((int)ssid[i]);
|
|
EEPROM.write(i, ssid[i]); //Write SSID to EEPROM
|
|
// Serial.print(ssid[i]);
|
|
if(ssid[i] == '\0') {
|
|
break;
|
|
}
|
|
}
|
|
for(int i=0; i<32; i++) {
|
|
// Serial.print(password[i]);
|
|
// Serial.print(" ");
|
|
// Serial.print((int)password[i]);
|
|
EEPROM.write(i+32, password[i]); //Write Password to EEPROM
|
|
// Serial.print(password[i]);
|
|
if(password[i] == '\0') {
|
|
break;
|
|
}
|
|
}
|
|
Serial.println("EEPROM.end");
|
|
EEPROM.end();
|
|
}
|
|
|
|
int connectWiFi(const char* ssid, const char* password) { //Connect to WiFi
|
|
WiFi.begin(ssid,password);
|
|
while(WiFi.status() !=WL_CONNECTED){ // Wait for WiFi connection || Blink LED while connecting
|
|
digitalWrite(ledPin,LOW); // Turn on LED
|
|
delay(250);
|
|
Serial.println(".");
|
|
digitalWrite(ledPin,HIGH);// Turn off LED
|
|
delay(250);
|
|
}
|
|
wifiConnectStatus = 1;
|
|
return (wifiConnectStatus); // Return Connected status when WiFi is connected
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("");
|
|
Serial.println("Environment Monitoring System - ESP8266 Firmware Ver.1.0.0.stable");
|
|
Serial.println("Developed by Madobi Nanami");
|
|
Serial.println("Personal site: https://nanami.tech");
|
|
Serial.println("Tech support & Feedback: yzt114@yeah.net");
|
|
dht22.begin();
|
|
EEPROM.begin(64);
|
|
pinMode(ledPin, OUTPUT);
|
|
digitalWrite(ledPin, LOW);
|
|
char ssid[32];
|
|
char password[32];
|
|
loadWiFiSettings(ssid, password); //Load WiFi settings from EEPROM
|
|
if(strlen (ssid) == 0 || strlen (password) == 0) { //If no WiFi settings found, set default values
|
|
Serial.println("No WiFi settings found in EEPROM, please set WiFi settings.");
|
|
Serial.println("Please enter WiFi SSID:");
|
|
while(!Serial.available());
|
|
String input_ssid = Serial.readStringUntil('\n');
|
|
input_ssid.trim(); // Remove any leading/trailing whitespace
|
|
if (input_ssid.length() > 32) {
|
|
Serial.println("SSID is too long, please enter a valid SSID (max 32 characters).");
|
|
return;
|
|
}
|
|
Serial.println("Please enter WiFi Password:");
|
|
while(!Serial.available());
|
|
String input_password = Serial.readStringUntil('\n');
|
|
input_password.trim(); // Remove any leading/trailing whitespace
|
|
if (input_password.length() > 32) {
|
|
Serial.println("Password is too long, please enter a valid password (max 32 characters).");
|
|
return;
|
|
}
|
|
if (int(connectWiFi(input_ssid.c_str(), input_password.c_str())) == 1) {
|
|
saveWiFiSettings(input_ssid.c_str(), input_password.c_str()); //Save WiFi settings to EEPROM
|
|
Serial.println("WiFi settings saved to EEPROM.");
|
|
} else {
|
|
Serial.println("Failed to connect to WiFi, please check your SSID and Password.");
|
|
return;
|
|
}
|
|
} else {
|
|
Serial.print("Loaded WiFi SSID: ");
|
|
Serial.println(ssid);
|
|
Serial.print("Loaded WiFi Password: ");
|
|
Serial.println(password);
|
|
if (connectWiFi(ssid, password) == 1){
|
|
Serial.println("Connected to WiFi successfully.");
|
|
Serial.print("IP Address: ");
|
|
Serial.println(WiFi.localIP());
|
|
server.begin();
|
|
digitalWrite(ledPin, LOW); // Turn on LED after connection
|
|
} //Connect to WiFi with loaded settings
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
String user_command;
|
|
WiFiClient client; // Declare client at the start
|
|
if(server.hasClient()) { // Check if there is a client connected
|
|
Serial.println("A new client connected to web server.");
|
|
server.setNoDelay(true);
|
|
client = server.available(); // Assign client here
|
|
if(client) {
|
|
Serial.println("New client connected.");
|
|
String currentLine = "";
|
|
while(client.connected()) {
|
|
if(client.available()) {
|
|
char c = client.read();
|
|
Serial.write(c);
|
|
if(c == '\n') {
|
|
currentLine.trim(); // Remove whitespace
|
|
user_command = currentLine;
|
|
Serial.printf("Received command: %s\n", user_command.c_str());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (user_command == "temp.read") {
|
|
float temperature = dht22.readTemperature();
|
|
client.println(temperature);
|
|
Serial.printf("Temperature: %.2f °C\n", temperature);
|
|
user_command = ""; // Clear command after processing
|
|
}
|
|
if (user_command == "hum.read") {
|
|
float humidity = dht22.readHumidity();
|
|
client.println(humidity);
|
|
Serial.printf("Humidity: %.2f %%\n", humidity);
|
|
user_command = ""; // Clear command after processing
|
|
}
|
|
} |