#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define RELAY 14 //LED PIN
#define DHTPIN 32 //DHT11 PIN
#define DHTTYPE DHT22
//#define LED_PIN 23 //RGB PIN
DHT dht(DHTPIN, DHTTYPE);
//Adafruit_NeoPixel pixels(1, LED_PIN, NEO_GRB + NEO_KHZ800); //1 are number of LED
const char* ssid = "Wokwi-GUEST"; //SSID WIFI
const char* password = ""; //PASSWORD WIFI
const char* mqtt_server = "broker.netpie.io";
const int mqtt_port = 1883;
const char* mqtt_Client = "2ec25fb4-8782-4832-a11e-1e466408688d"; //ClientID
const char* mqtt_username = "9Ec5Vrg5kYYLTtQMCx5Naezkon4Fv6nR"; //Token
const char* mqtt_password = "HFU4G6t7EEexJVmi7AbsGntHEETFLsot"; //Secret
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
int value = 0;
char msg[100];
String DataString;
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(mqtt_Client, mqtt_username, mqtt_password)) { //Connect to MQTT BROKER
Serial.println("connected");
client.subscribe("@msg/operator");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message;
for (int i = 0; i < length; i++) {
message = message + char(payload[i]);
}
if (String(topic) == "@msg/operator") {
if (message == "ON") {
digitalWrite(RELAY, HIGH);
client.publish("@shadow/data/update", "{\"data\" : {\"led\":1}}");
Serial.println("LED ON");
//pixels.setPixelColor(0, pixels.Color(60, 0, 100));
//pixels.show();
} else if (message == "OFF") {
digitalWrite(RELAY, LOW);
client.publish("@shadow/data/update", "{\"data\" : {\"led\":0}}");
Serial.println("LED OFF");
//pixels.setPixelColor(0, pixels.Color(0, 0, 0));
//pixels.show();
}
}
}
void setup() {
//pixels.begin();
//pixels.clear();
Serial.begin(115200);
pinMode(RELAY, OUTPUT);
dht.begin();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //Connect WIFI
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //connect WIFI success show ---> IP
client.setServer(mqtt_server, mqtt_port); //Define MQTT BROKER, PORT used.
client.setCallback(callback); //Set the function that will run when data comes in through Subscribe
client.subscribe("@msg/operator");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 5000) { //Timer transmits data every 5 seconds
lastMsg = now;
++value;
float h = dht.readHumidity();
float t = dht.readTemperature();
float ldr = analogRead(36);
DataString = "{\"data\" : \"temperature\" : " + (String)t + ", \"humidity\" : " + (String)h + ", \"ldr\" : " + (String)ldr + "}";
// Example of data : {"data":{"temperature":25 , "humidity": 60, "led": 50 }}
DataString.toCharArray(msg, 100);
Serial.println("Hello NETPIE2020");
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("@shadow/data/update", msg); //Sent Data ----> Real Time Database (Shadow)
}
delay(1);
}