Final Arduino Code
Final Arduino Code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> Final ESP32 MySQL Database
//======================================== Including the libraries.
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include "DHT.h"
//========================================
//________________________________________________________________________________
Subroutine to control LEDs after successfully fetching data from database.
void control_LEDs() {
Serial.println();
Serial.println("---------------control_LEDs()");
JSONVar myObject = JSON.parse(payload);
if (myObject.hasOwnProperty("LED_01")) {
Serial.print("myObject[\"LED_01\"] = ");
Serial.println(myObject["LED_01"]);
}
if (myObject.hasOwnProperty("LED_02")) {
Serial.print("myObject[\"LED_02\"] = ");
Serial.println(myObject["LED_02"]);
}
Serial.println("---------------");
}
//________________________________________________________________________________
// ________________________________________________________________________________
Subroutine to read and get data from the DHT11 sensor.
void get_DHT11_sensor_data() {
Serial.println();
Serial.println("-------------get_DHT11_sensor_data()");
// Read Humidity
send_Humd = dht11_sensor.readHumidity();
//________________________________________________________________________________
VOID SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //--> Initialize serial communications with the PC.
delay(2000);
Serial.println();
Serial.println("-------------");
Serial.print("Connecting");
//........................................ Countdown
"connecting_process_timed_out".
if(connecting_process_timed_out > 0) connecting_process_timed_out--;
if(connecting_process_timed_out == 0) {
delay(1000);
ESP.restart();
}
//........................................
}
//----------------------------------------
digitalWrite(ON_Board_LED, LOW); //--> Turn off the On Board LED when it is
connected to the wifi router.
delay(2000);
}
//________________________________________________________________________________
//________________________________________________________________________________
VOID LOOP()
void loop() {
// put your main code here, to run repeatedly
digitalWrite(ON_Board_LED, HIGH);
Serial.println();
Serial.println("---------------getdata.php");
// In this project I use local server or localhost with XAMPP application.
// So make sure all PHP files are "placed" or "saved" or "run" in the "htdocs"
folder.
// I suggest that you create a new folder for this project in the "htdocs"
folder.
// The "htdocs" folder is in the "xampp" installation folder.
// The order of the folders I recommend:
// xampp\htdocs\your_project_folder_name\phpfile.php
//
// ESP32 accesses the data bases at this line of code:
//
http.begin("http://REPLACE_WITH_YOUR_COMPUTER_IP_ADDRESS/REPLACE_WITH_PROJECT_FOLDE
R_NAME_IN_htdocs_FOLDER/getdata.php");
// REPLACE_WITH_YOUR_COMPUTER_IP_ADDRESS = there are many ways to see the IP
address, you can google it.
// But make sure that the IP address
used is "IPv4 address".
// Example :
http.begin("http://192.168.0.0/ESP32_MySQL_Database/Final/getdata.php");
http.begin("http://REPLACE_WITH_YOUR_COMPUTER_IP_ADDRESS/REPLACE_WITH_PROJECT_FOLDE
R_NAME_IN_htdocs_FOLDER/getdata.php"); //--> Specify request destination
http.addHeader("Content-Type", "application/x-www-form-
urlencoded"); //--> Specify content-type header
Serial.print("httpCode : ");
Serial.println(httpCode); //--> Print HTTP return code
Serial.print("payload : ");
Serial.println(payload); //--> Print request response payload
delay(1000);
postData = "id=esp32_01";
postData += "&temperature=" + String(send_Temp);
postData += "&humidity=" + String(send_Humd);
postData += "&status_read_sensor_dht11=" + send_Status_Read_DHT11;
postData += "&led_01=" + LED_01_State;
postData += "&led_02=" + LED_02_State;
payload = "";
digitalWrite(ON_Board_LED, HIGH);
Serial.println();
Serial.println("---------------updateDHT11data_and_recordtable.php");
// Example :
http.begin("http://192.168.0.0/ESP32_MySQL_Database/Final/updateDHT11data_and_recor
dtable.php");
http.begin("http://REPLACE_WITH_YOUR_COMPUTER_IP_ADDRESS/REPLACE_WITH_PROJECT_FOLDE
R_NAME_IN_htdocs_FOLDER/updateDHT11data_and_recordtable.php"); //--> Specify
request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //-->
Specify content-type header
delay(4000);
}
//----------------------------------------
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<