This is a theoretical visualization of my project, and an actual photo. The only ones that work are the DHT(11-22), IR led, LCD, the rest don't return any reading. Considering that everything else has already been tested, I theorize that the problem may be in the pinout. How can I resolve it? The DSM501A is disconnected because it is causing some kind of corruption in the execution of the software, as well as the two ZMPT101B.
// ESP32
extern void log_register(int key);
extern void begin_watch_dog();
extern void watch_dog();
extern void air_control(bool force = false);
extern void att_lcd(bool force = false);
extern void initialize_lcd();
extern void client_verify();
extern void DHCP_connect();
extern void DHCP_reconnect();
extern void initialize_sensors();
extern void Irsend();
extern void handle_client();
extern void sensor_reader(bool force = false);
void read_data(const char* key);
extern String nameOfModule;
void setup() {
read_data("nameOfModule");
Serial.begin(115200);
Serial.println(F("Ligado"));
Irsend();
DHCP_connect();
initialize_sensors();
initialize_lcd();
begin_watch_dog();
air_control(true);
sensor_reader(true);
}
void loop() {
watch_dog();
DHCP_reconnect();
air_control();
att_lcd();
sensor_reader(); // v3 chamada assíncrona
}
#include <DHT.h>
// caso precise, defina cabeçalho de função
#define DHTPIN 23 // pino que estamos conectado
#define DHTTYPE DHT11 // DHT
DHT dht(DHTPIN, DHTTYPE);
void initialize_dht() {
log_register(601);
dht.begin();
}
float read_temperature() {
log_register(111);
return dht.readTemperature();
}
float read_humidity() {
log_register(112);
return dht.readHumidity();
}
#define pin2 14 // Vout2 PM10
#define pin1 12 // Vout1 PM2.5
#define sampletime_ms 3000 //sampe 1s ;
float read_pm2p5() {
log_register(113);
unsigned long duration1 = pulseIn(pin1, LOW);
unsigned long lowpulseoccupancy1 = lowpulseoccupancy1+duration1;
float ratio1 = lowpulseoccupancy1/(sampletime_ms*10.0); // Integer percentage 0=>100
float pm2p5 = 1.1*pow(ratio1,3)-3.8*pow(ratio1,2)+520*ratio1+0.62; // using spec sheet curve
return pm2p5;
}
float read_pm10() {
log_register(114);
unsigned long duration2 = pulseIn(pin2, LOW);
unsigned long lowpulseoccupancy2 = lowpulseoccupancy2+duration2;
float ratio2 = lowpulseoccupancy2/(sampletime_ms*10.0); // Integer percentage 0=>100
float pm10 = 1.1*pow(ratio2,3)-3.8*pow(ratio2,2)+520*ratio2+0.62;
return pm10;
}
// AC Voltage Sensor without LCD By Solarduino (Revision 2)
#include <EmonLib.h>
EnergyMonitor tensao;
float read_voltage(int PIN, float VOLT_CAL) {
log_register(115);
tensao.voltage(PIN, VOLT_CAL, 1.7);
tensao.calcVI(20,2000);
return tensao.Vrms;
}
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
//#define emitterPin 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2).
#define emitterPin 17
#define size_raw 198
bool ar_condicionado = false;
bool dehumidify = false;
bool automatic = true;
// caso precise, defina cabeçalho de função aqui
IRsend irsend(emitterPin);
void Irsend() {
log_register(3);
irsend.begin();
}
uint16_t rawDataOff[size_raw]={
5994, 7398, 506, 1662, 478, 1710, 498, 1690,
458, 1706, 506, 1686, 538, 1630, 462, 1722,
546, 1626, 546, 610, 518, 586, 498, 638,
518, 586, 498, 634, 522, 586, 498, 634,
522, 586, 498, 1666, 546, 1642, 546, 1622,
442, 1742, 546, 1646, 526, 1634, 550, 1642,
546, 1622, 514, 642, 518, 590, 494, 638,
518, 586, 494, 642, 518, 586, 498, 634,
522, 586, 498, 1662, 546, 1646, 542, 1626,
482, 1702, 538, 1650, 534, 1630, 542, 1646,
538, 1650, 462, 674, 510, 598, 490, 642,
510, 594, 530, 602, 514, 594, 534, 598,
514, 618, 514, 1626, 442, 1742, 542, 594,
486, 1674, 546, 1642, 546, 590, 494, 1666,
498, 1690, 494, 614, 502, 630, 494, 1674,
466, 682, 478, 630, 446, 1734, 490, 626,
442, 682, 486, 630, 466, 1714, 462, 670,
482, 1686, 394, 1790, 486, 626, 446, 1738,
454, 1730, 486, 1686, 414, 714, 482, 1702,
486, 626, 478, 650, 482, 1690, 414, 710,
482, 630, 394, 730, 486, 1702, 486, 622,
482, 1706, 486, 626, 394, 1786, 486, 630,
466, 658, 486, 1702, 482, 630, 450, 1734,
482, 634, 462, 1718, 450, 678, 486, 1686,
446, 1734, 486, 7422, 482, 1000};
uint16_t rawDataOn[size_raw]={
5998, 7474, 466, 1718, 470, 1730, 454, 1746,
466, 1766, 394, 1770, 466, 1726, 434, 1774,
466, 1718, 470, 670, 446, 702, 446, 670,
398, 746, 470, 646, 482, 666, 446, 670,
470, 646, 442, 1750, 494, 1710, 422, 1770,
494, 1698, 414, 1790, 498, 1694, 470, 1734,
498, 1690, 542, 622, 478, 642, 478, 638,
498, 646, 506, 614, 434, 710, 506, 610,
486, 658, 486, 1678, 538, 1670, 506, 1678,
478, 1754, 478, 1706, 482, 1726, 454, 1734,
482, 1706, 474, 666, 482, 642, 442, 694,
482, 638, 446, 694, 454, 666, 450, 694,
450, 666, 450, 1762, 454, 662, 478, 642,
470, 1738, 450, 1762, 446, 670, 450, 1738,
446, 1762, 450, 670, 446, 1762, 450, 1738,
442, 698, 450, 670, 446, 1762, 446, 674,
446, 694, 450, 670, 446, 1762, 446, 674,
446, 1762, 446, 1738, 446, 698, 442, 1742,
446, 1742, 466, 1742, 442, 678, 462, 1746,
442, 678, 438, 702, 442, 1746, 438, 706,
438, 682, 434, 730, 414, 1746, 438, 730,
414, 1746, 438, 730, 410, 1750, 434, 734,
410, 706, 414, 1794, 414, 706, 414, 1794,
414, 706, 414, 1770, 438, 706, 414, 1770,
414, 1794, 414, 7550, 414, 1000};
uint16_t rawDataDehumidify[size_raw]={
6062, 7422, 458, 1750, 510, 1678, 506, 1702,
506, 1678, 478, 1730, 506, 1682, 474, 1730,
510, 1682, 414, 722, 506, 614, 446, 690,
510, 614, 474, 662, 510, 610, 510, 630,
510, 610, 510, 1698, 506, 1682, 534, 1670,
506, 1682, 442, 1758, 514, 1682, 446, 1754,
510, 1698, 474, 642, 510, 634, 470, 646,
510, 614, 462, 674, 510, 614, 414, 718,
510, 638, 394, 1782, 510, 1702, 470, 1714,
506, 1702, 506, 1678, 478, 1726, 510, 1682,
414, 1786, 510, 634, 442, 674, 510, 614,
490, 646, 506, 634, 398, 718, 510, 614,
490, 642, 510, 1702, 470, 646, 510, 1698,
474, 642, 510, 630, 478, 638, 510, 634,
474, 1710, 510, 630, 502, 1686, 498, 638,
506, 1682, 474, 1726, 482, 1726, 446, 1738,
482, 662, 446, 670, 482, 1726, 482, 1702,
454, 686, 506, 1678, 474, 1730, 514, 610,
470, 1730, 514, 1694, 478, 638, 518, 626,
474, 1710, 482, 658, 506, 610, 550, 1658,
510, 610, 450, 686, 510, 1678, 466, 670,
510, 1682, 462, 670, 510, 1698, 474, 642,
510, 634, 470, 1714, 502, 638, 506, 1678,
470, 670, 502, 1690, 462, 670, 502, 1706,
442, 1738, 502, 7478, 470, 1000};
#define khz 36
#define size_raw 198
void off_ac() {
log_register(301);
irsend.sendRaw(rawDataOff, size_raw, khz);
ar_condicionado = false;
dehumidify = false;
}
void on_ac() {
log_register(302);
irsend.sendRaw(rawDataOn, size_raw, khz);
ar_condicionado = true;
dehumidify = false;
}
void dehumidify_ac() {
log_register(303);
irsend.sendRaw(rawDataDehumidify, size_raw, khz);
ar_condicionado = false;
dehumidify = true;
}
void change_ac(bool mode){
log_register(304);
automatic = mode;
}
//void sendRAW_Flash(const unsigned int * signalArray, unsigned int signalLength, unsigned char carrierFreq) {
// irsend.enableIROut(carrierFreq); //initialise the carrier frequency for each signal to be sent
// for (unsigned int i=0; i < signalLength; i++) {
// if (i & 1) irsend.space(pgm_read_word_near(&signalArray[i]));
// else irsend.mark(pgm_read_word_near(&signalArray[i]));
// }
// irsend.space(1);//make sure IR is turned off at end of signal
//}
// sendRAW_Flash(rawDataOff, sizeof(rawDataOff)/sizeof(int),khz);
void air_control(bool force) {
static unsigned long previouMillis = 0;
static unsigned long previouMillisOne = 0;
log_register(9);
if (force) {
off_ac();
}
if (automatic == false || force) {
previouMillisOne = millis();
}
if (automatic || force) {
float temperatura = read_temperature();
float umidade = read_humidity();
if ((millis() - previouMillisOne) >= 3600000) { // in case of 1 hour without action
off_ac();
force = true;
previouMillisOne = millis();
}
if (((millis() - previouMillis) >= 60000) || force) {
if (temperatura > 26 && ar_condicionado == false) {
on_ac();
previouMillisOne = millis();
}
if (temperatura > 28){
on_ac();
previouMillisOne = millis();
}
if (temperatura <= 23 && ar_condicionado) {
off_ac();
previouMillisOne = millis();
}
if (umidade > 60 && dehumidify == false && ar_condicionado == false) {
dehumidify_ac();
previouMillisOne = millis();
} else {
if (umidade < 55 && dehumidify) {
off_ac();
previouMillisOne = millis();
}
}
previouMillis = millis();
}
}
}
char ac_status() {
log_register(118);
char ac;
if (ar_condicionado) ac = '1';
else if (dehumidify) ac = '2';
else ac = '0'; // status do ar condicionado, 1 ar gelado, 2 desumidificação, 0 desligado
return ac;
}
#define NOBREAK_ENTRADA_PIN 13
#define NOBREAK_SAIDA_PIN 15
#define VOLT_CAL_ENTRADA 458
#define VOLT_CAL_SAIDA 503
extern String nameOfModule;
struct JSON_data{
float temperature;
float humidity;
int smoke;
bool smokebin;
float pm2p5;
float pm10;
float volt_in;
float volt_out;
char ac_status;
String time_;
String date;
};
extern void initialize_mq2();
extern bool read_smokebin();
extern int read_smoke();
extern float read_temperature();
extern float read_humidity();
extern float read_voltage(int PIN);
extern void inicialize_dht();
extern float read_pm2p5();
extern float read_pm10();
extern char ac_status();
extern String read_time();
extern String read_date();
extern void read_data(const char* key);
extern bool automatic;
JSON_data json;
void sensor_reader(bool force){
static unsigned long previouMillis = 0;
log_register(11);
if(millis() - previouMillis >= 15000 || force){
json.temperature = read_temperature();
json.humidity = read_humidity();
json.smoke = read_smoke();
json.smokebin = read_smokebin();
json.pm2p5 = read_pm2p5();
json.pm10 = read_pm10();
json.volt_in = read_voltage(NOBREAK_ENTRADA_PIN,VOLT_CAL_ENTRADA);
json.volt_out = read_voltage(NOBREAK_SAIDA_PIN,VOLT_CAL_SAIDA);
json.ac_status = ac_status();
json.time_ = read_time();
json.date = read_date();
previouMillis = millis();
}
}
String json_generator(){
log_register(214);
String request = "";
request += "{\"temperatura\":\"";
request += json.temperature;
request += "\",\"umidade\":\"";
request += json.humidity;
request += "\",\"mq2\":{\"fumaca\":\"";
request += json.smoke;
request += "\",\"fumaca_bin\":\"";
request += json.smokebin;
request += "\"}";
request += ",\"pm\":{\"pm2p5\":\"";
request += json.pm2p5;
request += "\",\"pm10\":\"";
request += json.pm10;
request += "\"}";
request += ",\"volt_in\":\"";
request += json.volt_in;
request += "\",\"volt_out\":\"";
request += json.volt_out;
request += "\",\"ac_status\":\"";
request += json.ac_status;
request += "\",\"time\":\"";
request += json.time_;
request += "\",\"date\":\"";
request += json.date;
request += "\"}";
return request;
}
String json_config_generator() {
log_register(216);
String request = "";
request += "{\"automatic\":\"";
request += automatic;
request += "\",\"ac_status\":\"";
request += ac_status();
request += "\",\"name\":\"";
request += nameOfModule;
request += "\",\"sizeRAM\":\"";
request += ESP.getHeapSize();
request += "\",\"freeRAM\":\"";
request += ESP.getFreeHeap();
request += "\",\"log\":\"";
request += ac_status();
request += "\"}";
return request;
}
void initialize_sensors() {
log_register(6);
initialize_dht();
initialize_mq2();
}
#include <LiquidCrystal_I2C.h>
#ifndef WiFi.h
#include <WiFi.h>
#endif
// GPIO 22 SCL
// GPIO 21 SDA
// caso precise, defina cabeçalho de função aqui
//void att_lcd(bool force = false);
byte grau[8] = {
B11100,
B10100,
B11100,
B00000,
B00000,
B00000,
B00000,
B00000
};
LiquidCrystal_I2C lcd(0x27, 16, 2);
void initialize_lcd() {
log_register(7);
lcd.init();
lcd.backlight();
lcd.createChar(0, grau);
att_lcd(true);
}
void att_lcd(bool force) {
static uint8_t mode_ = -1;
static unsigned long previouMillis = 0;
log_register(701);
if (millis() - previouMillis >= 3000 || force) {
mode_++;
lcd.clear();
if (mode_ == 0) {
lcd.print("IP local:");
lcd.setCursor(0,1);
lcd.print(WiFi.localIP());
} else if (mode_ == 1) {
lcd.print("Temperatura: ");
lcd.print(int(dht.readTemperature()));
lcd.write((byte)0);
lcd.setCursor(0, 1);
lcd.print("Umidade: ");
lcd.print(int(dht.readHumidity()));
lcd.print("%");
} else {
mode_ = -1;
if (nameOfModule.length() < 16)
lcd.print(nameOfModule);
else {
lcd.setCursor(0,0);
lcd.print(nameOfModule.substring(0, 16));
lcd.setCursor(0,1);
lcd.print(nameOfModule.substring(16, nameOfModule.length()));
}
}
previouMillis = millis();
}
}
#define MQ2PIN 4
//#define MQ2PIN_DIGITAL 16
#define MQ2PIN_DIGITAL 2
void initialize_mq2() {
pinMode(MQ2PIN, INPUT);
pinMode(MQ2PIN_DIGITAL, INPUT);
}
//if too sensitive, set to 400 in ZABBIX
int read_smoke() {
log_register(116);
return analogRead(MQ2PIN);
}
bool read_smokebin() {
log_register(117);
return !digitalRead(MQ2PIN_DIGITAL);
}
#include <Preferences.h>
// EEPROM is deprecated in esp32, use this: https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/
// https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/preferences.html
String nameOfModule;
Preferences preferences;
//infos {};
void read_data(const char* key) {
log_register(1);
preferences.begin("infos", true); // false (read and write), true (read only)
nameOfModule = preferences.getString(key);
preferences.end();
}
void write_data(const char* key, String value) {
log_register(2);
preferences.begin("infos");
preferences.putString(key, value);
nameOfModule = value;
preferences.end();
}
// watchdog serve para reiniciar o esp32 caso sofra uma falha e gere um travamento, durante o desenvolvimento isso foi observado.
hw_timer_t *timer = NULL;
void IRAM_ATTR resetModule() {
log_register(802);
ESP.restart();
}
void begin_watch_dog() {
//hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp)
// log_register(8);
timer = timerBegin(0, 80, true); // timerID, clock processor, enable
timerAttachInterrupt(timer, &resetModule, true); // timer, callback function interruption, enable
timerAlarmWrite(timer, 15000000, true); // timer, 15s in microseconds, enable
timerAlarmEnable(timer); // enable
}
void watch_dog() {
log_register(801);
timerWrite(timer, 0);
}
String web_page = R"***(
<!DOCTYPE html>
<html lang="pt-br">
<head>
<title id="title">esp32</title>
<meta charset="UTF-8">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.temp_umid {
font-size: 50px;
}
.grid {
display: grid;
grid-template-columns: repeat(5, 1fr);
}
#grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
#buttons {
text-align: center;
}
#buttons h3 {
margin-bottom: 0;
}
#end {
display: flex;
align-items: flex-end;
flex-direction: column;
}
.border {
background-color: rgb(228, 228, 228);
border-radius: 10%;
padding: 5%;
margin: 1%;
}
.border > div {
background-color: rgb(201, 201, 201);
border-radius: 1vw;
padding: 1vw;
margin: 1%;
}
</style>
<script>
function percentage(base, partial) {
return (100 * partial) / base;
}
function getHost() {
return window.location.hostname;
}
async function getJSON() {
let response = await fetch(`http://` + getHost() + `/json`);
let data = await response.json();
return data;
}
async function getConfJSON() {
let response = await fetch(`http://` + getHost() + `/config_json`);
let data = await response.json();
return data;
}
async function receptData() {
let confJson = await getConfJSON().then();
let perc = percentage(confJson.sizeRAM, confJson.freeRAM);
let ram = document.getElementById('ram');
ram.innerText = parseInt(perc) + '%';
if (perc <= 20) {
ram.style.color = "#00b71b";
} else if (perc <= 40) {
ram.style.color = "#ffb600";
} else if (perc <= 60) {
ram.style.color = "#ffec02";
} else if (perc <= 80) {
ram.style.color = "#83c502";
} else {
ram.style.color = "#00b71e";
}
if (confJson.ac_status === '0') {
document.getElementById('acst').innerText = "Desligado";
document.getElementById('acst').style.color = "#FF3300";
}
else if (confJson.ac_status === '1') {
document.getElementById('acst').innerText = "Ligado";
document.getElementById('acst').style.color = "#00CC00";
}
else if (confJson.ac_status === '2') {
document.getElementById('acst').innerText = "Ligado em modo desumidificação";
document.getElementById('acst').style.color = "#7ddc09";
}
let json = await getJSON().then();
document.getElementById('temp').innerText = json.temperatura;
document.getElementById('umid').innerText = json.umidade;
document.getElementById('fumc').innerText = json.mq2.fumaca;
document.getElementById('fumb').innerText = json.mq2.fumaca_bin;
document.getElementById('pm25').innerText = json.pm.pm2p5;
document.getElementById('pm10').innerText = json.pm.pm10;
document.getElementById('vinn').innerText = json.volt_in;
document.getElementById('vout').innerText = json.volt_out;
document.getElementById('time').innerText = json.time + " " + json.date.split('-').reverse().join('/');
}
async function check() {
let confJson = await getConfJSON().then();
if (confJson.automatic === '1') {
document.getElementById('auto').innerText = 'Modo automático: ON';
document.getElementById('auto').style.backgroundColor = "#00CC00";
}
else {
document.getElementById('auto').innerText = 'Modo automático: OFF';
document.getElementById('auto').style.backgroundColor = "#FF3300";
}
document.getElementById('name').innerText = confJson.name;
document.getElementById('title').innerText = confJson.name;
document.getElementById('ip').innerText = getHost();
}
</script>
</head>
<body>
<div id="grid">
<div>
<h1>MCST(<span id="name">-</span>), IPv4: <span id="ip">0.0.0.0</span></h1>
<h2>Última leitura em: <span id="time">-</span></h2>
</div>
<div id="end">
<h2>RAM: <span id="ram">-</span></h2>
</div>
</div>
<div>
<div class="grid">
<div class="border">
<h3>Temperatura</h3>
<div class="temp_umid"><span id="temp">-</span>°C</div>
</div>
<div class="border">
<h3>Umidade</h3>
<div class="temp_umid"><span id="umid">-</span>%</div>
</div>
<div class="border">
<h3>Fumaça</h3>
<div>
<h4>Analógico</h4>
<div class="temp_umid"><span id="fumc">-</span>ppm</div>
</div>
<div>
<h3>Binário</h3>
<div class="temp_umid"><span id="fumb">-</span></div>
</div>
</div>
<div class="border">
<h3>Partículas</h3>
<div>
<h4>pm2.5</h4>
<div class="temp_umid"><span id="pm25">-</span>ppm</div>
</div>
<div>
<h4>pm10</h4>
<div class="temp_umid"><span id="pm10">-</span>ppm</div>
</div>
</div>
<div class="border">
<h3>Tensão no nobreak</h3>
<div>
<h4>Tomada de entrada</h4>
<div class="temp_umid"><span id="vinn">-</span>v</div>
</div>
<div>
<h4>Tomada de saída</h4>
<div class="temp_umid"><span id="vout">-</span>v</div>
</div>
</div>
</div>
<div id="buttons">
<h3>Status do Ar condicionado (AC)</h3>
<div class="temp_umid"><span id="acst">-</span></div>
<form action="/get" method="get" class="buttons" id="form">
<button name="ac" value="1" id="ac1">Ligar AC</button>
<button name="ac" value="2" id="ac3">Modo desumidificação AC</button>
<button name="ac" value="0" id="ac0">Desligar AC</button>
<button name="auto" value="on" id="auto">Modo automático: -</button>
</form>
</div>
</div>
<script>
check();
try {
receptData();
} catch {
setTimeout('receptData()', 1000);
} finally {
setInterval('receptData()', 15000);
}
</script>
</body>
</html>
)***";
// ESP32
#include <ESPAsyncWebServer.h>
#ifndef WiFi.h
#include <WiFi.h>
#endif
#ifndef WiFiUdp.h
#include <WiFiUdp.h>
#endif
#ifndef NTPClient.h
#include <NTPClient.h>
#endif
//#include <ESP32ZabbixSender.h>
//#define DHCP_connect 1
// caso precise, defina cabeçalho de função aqui
extern String json_generator();
extern String json_config_generator();
extern void off_ac();
extern void on_ac();
extern void dehumidify_ac();
extern void change_ac(bool mode);
extern bool automatic;
extern String nameOfModule;
extern void read_data(const char* key);
extern void write_data(const char* key, String value);
extern void IRAM_ATTR resetModule();
const char* ssid = "UFFS-Equipamentos";
const char* password = "********";
const char* http_username = "admin";
const char* http_password = "admin";
AsyncWebServer server(80);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
//ESP32ZabbixSender zSender;
extern String web_page;
// void (*Reset)() = 0; para Arduino, para esp: ESP.reset(); ou ESP.restart(); já implementado. Para entender as diferenças, leia em: https://www.pieterverhees.nl/prototyping-builds/esp8266/130-difference-between-esp-reset-and-esp-restart
void handleRoot();
void handleJson();
void handleCommand();
void handleConfigs();
void notFound(AsyncWebServerRequest *request);
void log_register(int key);
void DHCP_connect() { //inicializa o WiFi
// UFFS-Equipamentos
// 5PnatQDd
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(F("Connecting to "));
Serial.println(ssid);
unsigned long current_time = millis() + 5000;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
if (millis() >= current_time) resetModule();
}
Serial.println(F("WiFi connected"));
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
handleJson();
handleCommand();
handleRoot();
handleConfigs();
server.onNotFound(notFound);
server.begin();
timeClient.begin();
timeClient.setTimeOffset(-10800);
// zSender.Init(IPAddress(192,168,253,149), 10051, "ESP32MCSTHomologacao");
log_register(0);
log_register(4);
}
void DHCP_reconnect() { //reconecta o WiFi em caso de perda
log_register(5);
if ((WiFi.status() != WL_CONNECTED) || (WiFi.localIP() == IPAddress(0, 0, 0, 0)) || (WiFi.localIP() == IPAddress(255, 255, 255, 255))) {
WiFi.disconnect();
WiFi.reconnect();
Serial.print(F("Connecting to "));
Serial.println(nameOfModule);
unsigned long current_time = millis() + 5000;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
if (millis() >= current_time) {
Serial.print(F("Failed!"));
resetModule();
}
}
}
}
void notFound(AsyncWebServerRequest *request) {
log_register(12);
request->send(404, "text/plain", "Not found");
}
void handleRoot() {
// log_register(13);
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
// log_register(13);
if (!request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send(200, "text/html", web_page);
});
}
void handleJson() {
// log_register(14);
server.on("/json", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(200, "text/json", json_generator());
if (request->hasParam("name")) {
AsyncWebParameter* p = request->getParam("name");
if (p->value() != nameOfModule)
write_data("nameOfModule", p->value());
}
});
}
void handleCommand() {
log_register(15);
server.on("/get", HTTP_GET, [](AsyncWebServerRequest * request) {
int paramsNr = request->params();
for (int i = 0; i < paramsNr; i++) {
AsyncWebParameter* p = request->getParam(i);
// p->name(); p->value();
if (p->name() == "ac") {
if (p->value() == "0")
off_ac();
else if (p->value() == "1")
on_ac();
else if (p->value() == "2")
dehumidify_ac();
change_ac(false);
}
if (p->name() == "auto") {
if (p->value() == "on")
change_ac(!automatic);
}
}
request->send(200, "text/html", web_page);
});
}
void handleConfigs() {
log_register(16);
server.on("/config_json", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(200, "text/json", json_config_generator());
});
}
String att_timestamp() {
log_register(10);
while (!timeClient.update()) {
timeClient.forceUpdate();
}
return timeClient.getFormattedDate();
}
String read_date() {
log_register(120);
String formattedDate = att_timestamp();
int splitT = formattedDate.indexOf("T");
return formattedDate.substring(0, splitT);
}
String read_time() {
log_register(119);
String formattedDate = att_timestamp();
int splitT = formattedDate.indexOf("T");
return formattedDate.substring(splitT + 1, formattedDate.length() - 1);
}
//void log_register(int key) {
// static bool onn = false;
// if (onn) {
// zSender.ClearItem();
// zSender.AddItem("mcst.log", key);
//
// if (zSender.Send() == EXIT_FAILURE){ // send packet
// DHCP_reconnect();
// }
// } else {
// if (key == 0) {
// onn = true;
// }
// }
//}
void log_register(int key) {
switch (key) {
case 1:
Serial.println("read_data");
break;
case 2:
Serial.println("write_data");
break;
case 3:
Serial.println("Irsend begin");
break;
case 301:
Serial.println("off_ac");
break;
case 302:
Serial.println("onn_ac");
break;
case 303:
Serial.println("dehumidify_ac");
break;
case 304:
Serial.println("change_ac");
break;
case 4:
Serial.println("DHCP_connect");
break;
case 5:
Serial.println("DHCP_reconnect");
break;
case 6:
Serial.println("initialize_sensors");
break;
case 601:
Serial.println("initialize_dht");
break;
case 7:
Serial.println("initialize_lcd");
break;
case 701:
Serial.println("att_lcd");
break;
case 8:
Serial.println("begin_watch_dog");
break;
case 801:
Serial.println("watch_dog");
break;
case 802:
Serial.println("reset_module");
break;
case 9:
Serial.println("air_control");
break;
case 10:
Serial.println("att_timestamp");
break;
case 11:
Serial.println("senosr_reader");
break;
case 111:
Serial.println("read_temperature");
break;
case 112:
Serial.println("read_humidity");
break;
case 113:
Serial.println("read_pm2p5");
break;
case 114:
Serial.println("read_pm10");
break;
case 115:
Serial.println("read_voltage");
break;
case 116:
Serial.println("read_smoke");
break;
case 117:
Serial.println("read_smokebin");
break;
case 118:
Serial.println("ac_status");
break;
case 119:
Serial.println("read_time");
break;
case 120:
Serial.println("read_date");
break;
case 12:
Serial.println("notFound");
break;
case 13:
Serial.println("handleRoot");
break;
case 14:
Serial.println("handleJson");
break;
case 214:
Serial.println("json_generator");
break;
case 15:
Serial.println("handleCommand");
break;
case 16:
Serial.println("handleConfings");
break;
case 216:
Serial.println("json_config_generator");
break;
default:
Serial.println("log_register");
break;
}
}
It appears you did a lot of work. However without an annotated schematic I and many others will not help you. I will not spend the time looking up the parts as there are many out there with the same name and or number that are different. Then trying to figure out how things are connected is very timeconsumning. Posting links to "Technical Information" on the parts you use helps a lot and saves us a lot of time in getting you an answer. Links to sales market places such as azon are useless as some of the technical information is not present.
1 Like
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.