/* Project: default */
#include <WiFi.h>
#include "PubSubClient.h"
String Pedroddffled;
const char espwifi_ssid[]= "Wokwi-GUEST";
const char espwifi_pass[]= "";
void espwifi_setup(){
WiFi.mode(WIFI_STA);
WiFi.begin(espwifi_ssid,espwifi_pass);
Serial.println("Conectando a la red Wifi");
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);}
Serial.println("");
Serial.println("conexión realizada con éxito");
Serial.print("Dirección IP: ");
Serial.println(WiFi.localIP());
}
bool conectado;
unsigned long impr_conect_mqtt=0;
const char espmqtt_broker[]= "io.adafruit.com";
const int espmqtt_port= 1883;
const char espmqtt_user[]= "Pedroddf";
const char espmqtt_pass[]= "aio_rWhv77gCqaDnSkIWXTruaYNF8Mt0";
const char espmqtt_clientid[]= "idenghjtificativo_unico";
WiFiClient espmqtt_wifiClient;
PubSubClient espmqtt_client(espmqtt_wifiClient);
String espmqtt_topic="";
String espmqtt_msg_str="";
double espmqtt_msg_number=0;
char espmqtt_payload[128];
void espmqtt_onreceive(){};
void espmqtt_setup(){
delay(10);
randomSeed(micros());
espmqtt_client.setServer(espmqtt_broker, espmqtt_port);
espmqtt_client.setCallback(espmqtt_callback);
espmqtt_subscribe();
}
void espmqtt_loop(){
if (!espmqtt_client.connected()) {
espmqtt_client.connect(espmqtt_clientid,espmqtt_user,espmqtt_pass);
if((millis()-impr_conect_mqtt)>=1000){
Serial.println("Conectando al servidor mqtt");
impr_conect_mqtt=millis();}
conectado = false;
espmqtt_subscribe();}
if (espmqtt_client.connected()) {
if (!conectado) {
Serial.println("Conectado con éxito");
conectado = true;}
espmqtt_client.loop();}
}
double espmqtt_payload2double(unsigned char *_payload, int _length){
int i;
for (i = 0; i<_length && i<128; i++){
espmqtt_payload[i] = _payload[i];}
espmqtt_payload[i] = 0;
return atof(espmqtt_payload);
}
String espmqtt_payload2string(unsigned char *_payload, int _length){
int i;
for (i = 0; i<_length && i<128; i++){
espmqtt_payload[i] = _payload[i];}
espmqtt_payload[i] = 0;
return String(espmqtt_payload);
}
boolean Pedroddffled_fun(){
boolean Pedroddffled_loc;
if(Pedroddffled == ""){Pedroddffled_loc = 0;}
if(Pedroddffled == "true"){Pedroddffled_loc = 1;}
if(Pedroddffled == "TRUE"){Pedroddffled_loc = 1;}
if(Pedroddffled == "ON"){Pedroddffled_loc = 1;}
if(Pedroddffled == "on"){Pedroddffled_loc = 1;}
if(Pedroddffled == "1"){Pedroddffled_loc = 1;}
if(Pedroddffled == "false"){Pedroddffled_loc = 0;}
if(Pedroddffled == "FALSE"){Pedroddffled_loc = 0;}
if(Pedroddffled == "off"){Pedroddffled_loc = 0;}
if(Pedroddffled == "OFF"){Pedroddffled_loc = 0;}
if(Pedroddffled == "0"){Pedroddffled_loc = 0;}
return Pedroddffled_loc;}
bool imprEtapa2_;
#include "DHT.h";
DHT dht22(22,DHT22);
unsigned long frecuenciaPubl_ = 0;
void espmqtt_subscribe()
{
espmqtt_client.subscribe(String("Pedroddf/f/led").c_str());
}
void espmqtt_callback(char* _topic, unsigned char* _payload, unsigned int _payloadlength)
{
espmqtt_topic=String(_topic);
espmqtt_msg_str=espmqtt_payload2string(_payload,_payloadlength);
espmqtt_msg_number=espmqtt_payload2double(_payload,_payloadlength);
if(espmqtt_topic==String("Pedroddf/f/led"))Pedroddffled = espmqtt_msg_str;
espmqtt_onreceive();
}
void setup()
{
Serial.begin(115200);
espwifi_setup();
espmqtt_setup();
pinMode(15, OUTPUT);
pinMode(22, INPUT);
dht22.begin();
}
void loop()
{
yield();
espmqtt_loop();
if((Pedroddffled_fun())){
digitalWrite(15,HIGH);
}else{
digitalWrite(15,LOW);
}
if(millis() - frecuenciaPubl_ > 4000){
frecuenciaPubl_ = millis();
espmqtt_client.publish(String("Pedroddf/f/tem").c_str(),String(((dht22.readTemperature()))).c_str());}
}