// Proyecto IoT con UBIDOTS
// https://wokwi.com/projects/383245783729315841
// https://help.ubidots.com/en/articles/748067-connect-an-esp32-devkitc-to-ubidots-over-mqtt
#include "UbidotsEsp32Mqtt.h"
const char *UBIDOTS_TOKEN = ""; // colocar su TOKEN
const char *WIFI_SSID = ""; // usuario de wifi
const char *WIFI_PASS = ""; // Clave de wifi
const char *DEVICE_LABEL = "esp32_smart_home-ramiro"; // Nombre del dispositivo
const char *VARIABLE_TEMP = "temperatura"; // Put here your Variable label to which data will be published
const char *VARIABLE_LAMPARA1 = "lampara";
const char *VARIABLE_CONTA = "contador";
const char *VARIABLE_HUME = "humedad";
const int PUBLISH_FREQUENCY = 5000; // Update rate in milliseconds
unsigned long timer;
uint8_t analogPin = 34; // Pin used to read data from GPIO34 ADC_CH6.
uint8_t PinLed = 27;
uint8_t conta = 0;
Ubidots ubidots(UBIDOTS_TOKEN);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Mensaje Recibido[");
Serial.print(topic);
Serial.print("] ");
String msg="" ;
for (int i = 0; i < length; i++)
{
// Serial.print((char)payload[i]);
msg += (char)payload[i];
}
Serial.println(msg);
if ((char)payload[0] == '1')
{
digitalWrite(PinLed, HIGH);
}
else
{
digitalWrite(PinLed, LOW);
}
}
/****************************************
* Main Functions
****************************************/
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
// ubidots.setDebug(true); // uncomment this to make debug messages available
ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
ubidots.setCallback(callback);
ubidots.setup();
ubidots.reconnect();
pinMode(PinLed, OUTPUT);
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LAMPARA1);
timer = millis();
}
void loop()
{
// put your main code here, to run repeatedly:
if (!ubidots.connected())
{
ubidots.reconnect();
ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LAMPARA1);
}
if (millis() - timer > PUBLISH_FREQUENCY) // triggers the routine every 5 seconds
{
float value = random(2000, 3000) / 100.0;
float hume = random(1000, 10000) / 100.0;
float conta = random(1, 100);
ubidots.add(VARIABLE_TEMP, value); // Insert your variable Labels and the value to be sent
ubidots.add(VARIABLE_CONTA, conta);
ubidots.add(VARIABLE_HUME, hume);
// conta++;
ubidots.publish(DEVICE_LABEL);
timer = millis();
}
ubidots.loop();
}