Hello, I am starting a MQTT project using esp32 and humidity sensor, I want to be able to send data from my sensor to my Ubidots dashboard, however it is really confusing for me as im a beginner. I would appreciate any form of help to help integrate the Ubidots code to my arduino code.
Heres my progress so far without adding the Ubidots code yet.
const int buzzer = A5;
const int RELAY_PIN = A18;
const int led = A17;
void setup()
{
pinMode(buzzer, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A4);
float voltage = sensorValue;
Serial.println(voltage);
if (sensorValue < 2700)
{
tone(buzzer, 100);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(led, HIGH);
}
else
{
noTone(buzzer);
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(led, LOW);
}
}
What I have tried:
As this is a school project, my teacher has only provided a previous exercise that uses light sensor and ubidots. However in this case I am using a different sensor.
I would be extremely grateful for whatever help I get. Thank you.
This is the school exercise.
#include <WiFi.h>
#include <PubSubClient.h>
#define Builtin_LED 13
WiFiClient ubidots;
PubSubClient client(ubidots);
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[100];
char topic_pub[150];
char topic_sub[150];
char str_sensor[10];
unsigned long timer = 0;
float sub_payload;
float sensor;
#define WIFISSID "xyz" // Line1: Wifi SSID
#define PASSWORD "xyz" // Line2: Wifi password
#define TOKEN "BBFF-u123456789vbZYzPO3RcN86E9Qm3j7" // Line3: Ubidots TOKEN
#define MQTT_CLIENT_NAME "esp32irp3" // Line4: Set unique device client Name; 8-12 alphanumeric string
#define VARIABLE_LABEL "volts" // Line5: Set variable label; lowercase only
#define DEVICE_LABEL "lightsensor" // Line6: Set device label; lowercase only
#define SENSOR 32 // Line7: Assign SENSOR to pin32
#define LED 33 // Line8: Assign LED to pin33
#define PB 25 // Line9: Assign Pushbutton to pin25
#define RELAY 27 // Line10: Assign Relay control to pin27
float Threshold = 2;
unsigned long interval= 10000;
void setup() {
Serial.begin(115200);
pinMode(Builtin_LED, OUTPUT);
pinMode(SENSOR, INPUT);
pinMode(LED, OUTPUT);
pinMode(PB, INPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(Builtin_LED, HIGH);
digitalWrite(RELAY, HIGH);
digitalWrite(LED, HIGH);
}
void loop() {
if (millis() > timer){
sensor = analogRead(SENSOR)+ 150 ;
Serial.print("Sense binary code:");
Serial.print(sensor);
sensor = sensor * 0.0008;
Serial.print(" / Sense Voltage Va (V):");
Serial.print(sensor);
if (sensor > Threshold){
Serial.println(" / light is very dim");
}
else {
Serial.println("");
}
convert_json();
set_timer();
}
}
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
sub_payload = atof(p);
Serial.print(sub_payload);
Serial.println(topic);
if (sub_payload < Threshold) {
digitalWrite(Builtin_LED, LOW);
} else{
digitalWrite(Builtin_LED, HIGH);
}
}
void mqttconnect() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
delay(2000);
}
}
}
void blink_LED(){
digitalWrite(Builtin_LED, LOW);
delay(500);
digitalWrite(Builtin_LED, HIGH);
delay(500);
digitalWrite(Builtin_LED, LOW);
delay(500);
digitalWrite(Builtin_LED, HIGH);
delay(1000);
}
void iot_setup(){
WiFi.begin(WIFISSID, PASSWORD);
digitalWrite(Builtin_LED, HIGH);
Serial.print("Wait for WiFi...");
timer = millis() + 3000;
while (WiFi.status() != WL_CONNECTED) {
if (millis() > timer){
ESP.restart();
}
Serial.print(".");
delay(500);
}
Serial.println("WiFi Connected");
blink_LED();
sprintf(topic_pub, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(topic_sub, "%s%s/%s/lv", "/v1.6/devices/", DEVICE_LABEL,VARIABLE_LABEL);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void convert_json(){
dtostrf(sensor, 4, 2, str_sensor);
sprintf(payload, "%s", "");
sprintf(payload, "{\"%s\":{\"value\": %s}}", VARIABLE_LABEL,str_sensor );
}
void set_timer(){
if (interval < 9999){
interval = 10000;
}
timer = millis() + interval;
}