//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 02_ESP32_RobotDyn_AC_Dimmer_Module_Web_Server_SM
//----------------------------------------Include Library.
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Arduino_JSON.h>
#include <dimmable_light.h>
#include "PageIndex.h"
//----------------------------------------
// Defining Zero-Cross (Z-C) PINs.
#define Z_C_PIN 13
// Defines the Dimmer signal PIN.
#define DIM_1_PIN 12
//----------------------------------------Variable declaration for your network credentials.
const char* ssid = "Wimara";
const char* password = "DCOTOR-60178889/milo";
//----------------------------------------
//----------------------------------------The variables used to check the parameters passed in the URL.
// Look in the "PageIndex.h" file.
// xhr.open("GET", "set_LightBulb?LightBulb_Num=" + light_num + "&LightBulb_Val=" + value, true);
// For example :
// set_LightBulb?LightBulb_Num=LB1&LightBulb_Val=50
// PARAM_INPUT_1 = LB1
// PARAM_INPUT_2 = 50
const char* PARAM_INPUT_1 = "LightBulb_Num";
const char* PARAM_INPUT_2 = "LightBulb_Val";
//----------------------------------------
//----------------------------------------Variable declaration to hold data from the web server to control the Light Bulb.
String LightBulb_Number = "";
String LightBulb_Value = "";
String sendLB_val = "";
int LightBulb_1_Value = 0;
int rslt_LightBulb_1_Value = 0;
//----------------------------------------
//----------------------------------------Variable for the bulb controller "Timer".
unsigned long previousMillis = 0;
const long interval = 100;
byte cnt = 0;
//----------------------------------------
bool processing_Received_Data = false;
// Initialize DimmableLight.
DimmableLight light1(DIM_1_PIN);
// Initialize JSONVar
JSONVar JSON_All_Data_Received;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Create an Event Source on /events
AsyncEventSource events("/events");
//________________________________________________________________________________ Send_Data_to_WS()
// Subroutine for sending data to the web server.
//
// This subroutine is used when the web server page is opened, the light bulb brightness data will be sent to the web server.
// So the server web page will get the last bulb brightness data, which data is then applied to the slider on the web page.
void Send_Data_to_WS() {
//:::::::::::::::::: Enter the received data into JSONVar(JSON_All_Data_Received).
// Look at the file "PageIndex.h". Then look at the section "Processes the data received from the ESP32 Master."
// "LB1" is for "obj.LB1".
// "LB2" is for "obj.LB2".
JSON_All_Data_Received["LB1"] = LightBulb_1_Value;
//::::::::::::::::::
//:::::::::::::::::: Create a JSON String to hold all data received from the sender.
String jsonString_Send_All_Data_received = JSON.stringify(JSON_All_Data_Received);
//::::::::::::::::::
//:::::::::::::::::: Sends all data received from the sender to the browser as an event ('allDataJSON').
events.send(jsonString_Send_All_Data_received.c_str(), "allDataJSON", millis());
//::::::::::::::::::
Serial.println();
Serial.println("Send data to Web Server");
}
//________________________________________________________________________________
//________________________________________________________________________________VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000);
DimmableLight::setSyncPin(Z_C_PIN);
DimmableLight::begin();
delay(500);
light1.setBrightness(rslt_LightBulb_1_Value);
//----------------------------------------Set Wifi to STA mode
Serial.println();
Serial.println("-------------");
Serial.println("WIFI mode : STA");
WiFi.mode(WIFI_STA);
Serial.println("-------------");
//----------------------------------------
delay(100);
//----------------------------------------Connect to Wi-Fi (STA).
Serial.println("------------");
Serial.println("WIFI STA");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
//:::::::::::::::::: The process of connecting ESP32 with WiFi Hotspot / WiFi Router.
// The process timeout of connecting ESP32 with WiFi Hotspot / WiFi Router is 20 seconds.
// If within 20 seconds the ESP32 has not been successfully connected to WiFi, the ESP32 will restart.
// I made this condition because on my ESP32, there are times when it seems like it can't connect to WiFi, so it needs to be restarted to be able to connect to WiFi.
int connecting_process_timed_out = 20; //--> 20 = 20 seconds.
connecting_process_timed_out = connecting_process_timed_out * 2;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
if(connecting_process_timed_out > 0) connecting_process_timed_out--;
if(connecting_process_timed_out == 0) {
delay(1000);
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("------------");
//::::::::::::::::::
//----------------------------------------
delay(500);
//----------------------------------------Handle Web Server
Serial.println();
Serial.println("Setting Up the Main Page on the Server.");
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", MAIN_page);
});
//----------------------------------------
//----------------------------------------Handle Web Server Events
Serial.println();
Serial.println("Setting up event sources on the Server.");
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 10 second
client->send("hello!", NULL, millis(), 10000);
});
//----------------------------------------
//----------------------------------------Send a GET request to <ESP_IP>/set_LightBulb?LightBulb_Num=<inputMessage1>&LightBulb_Val=<inputMessage2>
server.on("/set_LightBulb", HTTP_GET, [] (AsyncWebServerRequest *request) {
//::::::::::::::::::
// GET input value on <ESP_IP>/set_LightBulb?LightBulb_Num=<inputMessage1>&LightBulb_Val=<inputMessage2>
// PARAM_INPUT_1 = inputMessage1
// PARAM_INPUT_2 = inputMessage2
// LightBulb_Number = PARAM_INPUT_1
// LightBulb_Value = PARAM_INPUT_2
//::::::::::::::::::
if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
LightBulb_Number = request->getParam(PARAM_INPUT_1)->value();
LightBulb_Value = request->getParam(PARAM_INPUT_2)->value();
}
else {
LightBulb_Number = "No message sent";
LightBulb_Value = "No message sent";
}
request->send(200, "text/plain", "OK");
processing_Received_Data = true;
});
//----------------------------------------
//----------------------------------------Adding event sources on the Server.
Serial.println();
Serial.println("Adding event sources on the Server.");
server.addHandler(&events);
//----------------------------------------
//----------------------------------------Starting the Server.
Serial.println();
Serial.println("Starting the Server.");
server.begin();
//----------------------------------------
Serial.println();
Serial.println("------------");
Serial.print("ESP32 IP address : ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.println("Visit the IP Address above in your browser to open the main page.");
Serial.println("------------");
Serial.println();
}
//________________________________________________________________________________
//________________________________________________________________________________VOID LOOP()
void loop() {
// put your main code here, to run repeatedly:
//----------------------------------------
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (processing_Received_Data == true) {
//..................
if (LightBulb_Number == "LB1") {
LightBulb_1_Value = LightBulb_Value.toInt();
}
if (LightBulb_Number == "getLB_val") {
sendLB_val = LightBulb_Number;
cnt = 0;
}
//..................
//..................Convert brightness value in percent (0-100) to PWM value (0-255).
rslt_LightBulb_1_Value = map(LightBulb_1_Value, 0, 100, 0, 255);
//..................
//..................
light1.setBrightness(rslt_LightBulb_1_Value);
//..................
Serial.println();
Serial.print("LightBulb_Number : ");
Serial.print(LightBulb_Number);
Serial.print(" || LightBulb_Value : ");
Serial.println(LightBulb_Value);
Serial.print("LightBulb_1_Value : ");
Serial.print(LightBulb_1_Value);
Serial.println(" %");
Serial.print("LightBulb_1 PWM : ");
Serial.print(rslt_LightBulb_1_Value);
processing_Received_Data = false;
}
// When the web server page is opened, the last bulb brightness data will be sent to the web server 1 second later.
cnt++;
if (cnt > 9) {
cnt = 0;
if (sendLB_val == "getLB_val") {
Send_Data_to_WS();
sendLB_val = "";
}
}
}
//----------------------------------------
}
//________________________________________________________________________________
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<