1° EMQx - MQTT Broker --> https://docs.emqx.io/tutorial/latest/en/quick_start/run_first.html
2° MQTT-WIFI-DALLAS DS18B20 .ino (adapt the wifi libraries from 8266 to esp32)
/********************************************
Smart Thermostat - Wrele
NODEMCU ESP8266-01s + Rele Shield
_____________________________________
OTA feature
Hostname: "Wrele"
GPIO 0 PIN DS18B20 // Bibliotecas OneWire y Dallastemperature
GPIO 2 PIN RELE
Webserver: "debug & State"
OTA Port: 8622
MQTT Client! PubSub von //https://github.com/knolleary/pubsubclient / https://pubsubclient.knolleary.net/api.html#connect1
Remote debug on TELNET: Wrele
______________________________________
---Pablo R. Pescador---
*********************************************/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
String ssid = "wifi_router";
String password = "pass_router";
int period=35000;
int period1=30000;
unsigned long time_now=0;
unsigned long time_now1=0;
long lastReconnectAttempt = 0;
//--4 x Dallassensor
const int pinDatosDQ = D3;
// Instancia a las clases OneWire y DallasTemperature
OneWire oneWireObjeto(pinDatosDQ);
DallasTemperature sensorDS18B20(&oneWireObjeto);
// Variables con las direcciones únicas de los 4 sensores DS18B20
DeviceAddress sensorVL = {0x28, 0xAA, 0xA9, 0xC2, 0x1D, 0x13, 0x02, 0x2C};
DeviceAddress sensorKW = {0x28, 0x13, 0xF4, 0xB5, 0x32, 0x14, 0x01, 0xAD};
DeviceAddress sensorWW = {0x28, 0x4A, 0xC1, 0xAD, 0x32, 0x14, 0x01, 0x67};
DeviceAddress sensorRL = {0x28, 0xAA, 0xD1, 0x81, 0x3B, 0x14, 0x01, 0xDE};
//---------MQTT Client
const char* mqtt_server = "ip_broker";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
String myIPrele;
byte willQoS = 3;
const char* willTopic = "ds1820b20";
const char* willMessage = "0";
boolean willRetain = true;
String received;
unsigned long lastmsg;
//end---------MQTT Client
//---------PIN RELE
const int Wrele = D4;
char buffer[20]={0};
//-------------------------------------------------------------------SETUP
void setup() {
Serial.begin(115200);
pinMode (Wrele, OUTPUT);
Serial.println("Booting");
delay(100);
digitalWrite(Wrele, LOW);
//----------------------------WIFI
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname("cli-rele");
Serial.println("Ready");
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
sprintf(buffer, WiFi.localIP().toString().c_str());
Serial.println(myIPrele);
//------------------------- MQTT Client
client.setServer(mqtt_server, 1893);
client.setCallback(callback);
client.connect("Cli-rele", "user_mqq", "pass_mqtt");
client.subscribe("broker/kessel/rele/estado");
client.subscribe("mcureset");
if(client.connected()){
client.publish("broker/kessel/estado", "1",true);
client.publish("client", "cli-rele");
}
//end------------------------- MQTT Client
//----------------------------OTA config
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("Cli-rele");
// No authentication by default
// ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
//End----------------------------OTA config
//----------Sensores Dallas
sensorDS18B20.begin();
//Serial.println("Buscando dispositivos...");
//Serial.println("Encontrados: ");
//Serial.print(sensorDS18B20.getDeviceCount());
//Serial.println(" sensores");
}
//-------------------------------------------------------------------end SETUP
//-------------------------------------------------------------------MQTT CLient Callback
void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Mensaje MQTT [");
// Serial.print(topic);
// Serial.print("] ");
for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
}
// Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '0') {
digitalWrite(Wrele, LOW);
}
if ((char)payload[0] == '1') {
digitalWrite(Wrele, HIGH);
// lastmsg = millis(); //es para apagar la caldera en caso de emergencia de no conexion.
}
if ((char)payload[0] == '2') {
valoresDS18B20();
}
if ((char)payload[0] == '9') {
client.publish("Rele", "RESET");
ESP.restart();
}
}
boolean reconnect() {
if (client.connect("Cli-rele", "mqtt_user", "mqtt_pass")) {
// Once connected, publish an announcement...
client.publish("client", "Cli-rele");
client.publish("broker/kessel/estado", "1",true);
// ... and resubscribe
client.subscribe("broker/kessel/rele/estado");
client.subscribe("mcureset");
}
return client.connected();
}
//-------------------------------------------------------------------MQTT CLient Reconnect
void loop() {
delay(100);
ArduinoOTA.handle();
//-------------------------MQTT Handle
if (!client.connected()) {
digitalWrite(Wrele, LOW);
Serial.println(" :( ");
long now = millis();
if (now - lastReconnectAttempt > 5000) {
lastReconnectAttempt = now;
Serial.println("cada 5 seg");
// Attempt to reconnect
if (reconnect()) {
Serial.println("Reconectando");
lastReconnectAttempt = 0;
}
}
} else {
// Client connected
client.loop();
}
// if(millis() > time_now + period)
// {
// time_now = millis();
// if (client.connected()){
// client.publish("broker/kessel/estado", "1");
// }
// }
if(millis() > time_now1 + period1)
{
time_now1 = millis();
valoresDS18B20();
}
// if(digitalRead(Wrele)==HIGH){
// if(millis() > lastmsg + 15000){
// Serial.println("Apage caldera");
// digitalWrite(Wrele, LOW);
// }
// }
}
void valoresDS18B20(){
// Mandamos comandos para toma de temperatura a los sensores
// Serial.println("Mandando TEmperaturas de sensores");
sensorDS18B20.requestTemperatures();
float tempVL = sensorDS18B20.getTempC(sensorVL);
float tempRL = sensorDS18B20.getTempC(sensorRL);
float tempWW = sensorDS18B20.getTempC(sensorWW);
float tempKW = sensorDS18B20.getTempC(sensorKW);
// Leemos y mostramos los datos de los sensores DS18B20 por dirección única
client.publish("broker/kessel/dallas/VL",String(tempVL).c_str());
client.publish("broker/kessel/dallas/RL",String(tempRL).c_str());
client.publish("broker/kessel/dallas/WW",String(tempWW).c_str());
client.publish("broker/kessel/dallas/KW",String(tempKW).c_str());
// Serial.print("Temperatura sensor VL: ");
// Serial.print(sensorDS18B20.getTempC(sensorVL));
// Serial.println(" C");
// Serial.print("Temperatura sensor RL: ");
// Serial.print(sensorDS18B20.getTempC(sensorRL));
// Serial.println(" C");
// Serial.print("Temperatura sensor WW: ");
// Serial.print(sensorDS18B20.getTempC(sensorWW));
// Serial.println(" C");
// Serial.print("Temperatura sensor KW: ");
// Serial.print(sensorDS18B20.getTempC(sensorKW));
// Serial.println(" C");
// delay(1000);
}
3° esp32 -->link
4°Lern HOW DeepSleep ESP32 -->link
5° Baterry–>link
6° Cafe!!!
7° Mqtt binding–>link, Bridge, and generic thing config