#include <PubSubClient.h>
#include <WiFi.h>
#include <DHT.h>
#define DHT_TYPE DHT22
// initial WIFI
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClient espClient;
// initial MQTT client
const char* mqttServer = "mqtt.netpie.io";
const int mqttPort = 1883;
const char* clientID = "1a29f63e-65e1-4eec-9453-d4843059438c";
const char* mqttUser = "Vc4Bso1ZUXY2twvT9Dj41Gkh7Z9R1NDo";
const char* mqttPassword = "5xbcBUzDEMMXhxNX6ZgKhfZxg7cLpZz6";
const char* topic_pub = "@msg/lab_nodered_ict_kps/weather/data";
const char* topic_sub = "@msg/lab_nodered_ict_kps/command";
// send buffer
String publishMessage;
PubSubClient client(espClient);
const int DHT_PIN = 32;
const int RED_PIN = 12;
const int GREEN_PIN = 14;
const int BLUE_PIN = 27;
DHT dht(DHT_PIN, DHT_TYPE);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnectNETPIE() {
// Loop until we're reconnected
char mqttinfo[80];
snprintf (mqttinfo, 75, "Attempting MQTT connection at %s:%d (%s/%s)...", mqttServer, mqttPort, mqttUser,
mqttPassword);
while (!client.connected()) {
Serial.println(mqttinfo);
String clientId = clientID;
if (client.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("...Connected");
client.subscribe(topic_sub);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void messageReceivedCallback(char* topic, byte* payload, unsigned int length) {
char payloadMsg[80];
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
payloadMsg[i] = (char) payload[i];
}
payloadMsg[length] = '\0'; // put end of string to buffer
Serial.println();
Serial.println("-----------------------");
processMessage(payloadMsg);
}
void processMessage(String recvCommand) {
}
void setColor(int R, int G, int B) {
analogWrite(RED_PIN, R);
analogWrite(GREEN_PIN, G);
analogWrite(BLUE_PIN, B);
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(messageReceivedCallback);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
dht.begin();
}
void loop() {
if (!client.connected()) {
reconnectNETPIE();
}
client.loop();
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("ERR: fail to read data from DHT sensor...!");
delay(5000);
return;
}
publishMessage = "{\"data\": {\"temperature\": " + String(t) + ", \"humidity\": " + String(h) + " }}";
Serial.println(publishMessage);
client.publish(topic_pub, publishMessage.c_str());
// color red (R = 255, G = 0, B = 0)
setColor(255, 0, 0);
delay(1000); // keep the color 1 second
// color green (R = 0, G = 255, B = 0)
setColor(0, 255, 0);
delay(1000); // keep the color 1 second
// color blue (R = 0, G = 0, B = 255)
setColor(0, 0, 255);
}