/**************
* Include Libraries
**************/
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
/**************
* Define Constants
**************/
#define TOKEN "BBUS-r6bqeWeNRQewrhBOCTFPfIQeOXec2R" // Put your Ubidots' TOKEN
#define VARIABLE_LABEL1 "Motion"
#define VARIABLE_LABEL2 "Temperature"
#define VARIABLE_LABEL3 "Humidity"
#define VARIABLE_LABEL4 "Distance"
#define DEVICE_LABEL "Industrial_automation" // Assig the device label
#define MQTT_CLIENT_NAME "0001" // MQTT client Name, put a Random ASCII
const int PIR_sensor = 34;
#define DHTTYPE DHT22 //Define the model for DHT sensor
#define DHTPIN 27 // Defining GPIO pin no 27 to DHt22
#define trigPin 2
#define echoPin 16
long duration;
int distanceCm;
const char* WIFISSID = "Wokwi-GUEST";
const char* PASSWORD = "";
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[700];
char payload1[700];
char payload2[700];
char payload3[700];char payload4[700];
char topic[150];
char topicControl1[150];
char topicControl2[150];
unsigned long ReadTime = 0;
int delayTime = 10000;
// Space to store values to send
char str_val[6];
char str_val2[6];
/**************
* Initializate constructors for objects
**************/
//ESP8266WiFiMulti WiFiMulti;
WiFiClient ubidots;
PubSubClient client(ubidots);
DHT dht(DHTPIN, DHTTYPE, 22);
/**************
* Auxiliar Functions
**************/
void callback(char* topic, byte* payload, unsigned int length) {}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
Serial.println("connected");
client.subscribe(topicControl1);
client.subscribe(topicControl2);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/**************
* Main Functions
**************/
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(PIR_sensor, INPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
WiFi.begin(WIFISSID, PASSWORD);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
int PIR_value = digitalRead(PIR_sensor);
Serial.print("PIR value: ");
Serial.println(PIR_value);
float hum_value = dht.readHumidity(); //reads the umidity data and stores it in hum
float temp_value = dht.readTemperature(); //reads the temperature value and stores it in temp
Serial.print("Temperature");
Serial.print(temp_value);
Serial.print("Humidity");
Serial.print("hum_value");
// Measure the distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * 0.034 / 2;
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
//inicio
sprintf(payload, "%s", ""); // Cleans the payload content
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, String(PIR_value)); // Adds the value
//intermedio
sprintf(payload, "%s },\"%s\":", payload, VARIABLE_LABEL2); // Adds the variable label2
sprintf(payload, "%s {\"value\": %s", payload, String(temp_value)); // Adds the value2
sprintf(payload, "%s },\"%s\":", payload, VARIABLE_LABEL3); // Adds the variable label2
sprintf(payload, "%s {\"value\": %s", payload, String(hum_value)); // Adds the value2
sprintf(payload, "%s },\"%s\":", payload, VARIABLE_LABEL4); // Adds the variable label2
sprintf(payload, "%s {\"value\": %s", payload, String(distanceCm)); // Adds the value2
//cierre
sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
Serial.println(String(payload));
client.publish(topic, payload);
client.loop();
}