/*
visit www.ArduinoYard.com for more details
*/
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
/************************* DHT Sensor Setup *********************************/
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // Type of DHT Sensor, DHT11 or DHT22
DHT dht(DHTPIN, DHTTYPE);
//#define ledPin D3 // Connect LED on pin D3
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "Wokwi-GUEST" // Replace with your Wi-Fi SSID
#define WLAN_PASS "" // Replace with your Wi-Fi Password
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "vongocquy002" // Replace with your Adafruit IO Username
#define AIO_KEY "aio_Luij89D8wMsStfTgAPDaPHaM2nOW" // Replace with your Adafruit IO Key
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup feeds called 'temp' & 'hum' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
// feedname should be the same as set in dashboard for temp and hum gauges
Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); // feedname
Adafruit_MQTT_Publish hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");
// Setup a feed called 'onoff' for controlling LED.
//Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
void MQTT_connect();
int counter;
void setup() {
Serial.begin(115200);
delay(500);
//pinMode(ledPin, OUTPUT);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed.
//mqtt.subscribe(&onoffbutton);
dht.begin();
delay(500);
}
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
delay(500);
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
delay(2000);
// Wait 2000 milliseconds, while we wait for data from subscription feed. After this wait, next code will be executed
//while ((subscription = mqtt.readSubscription(2000))) {
//if (subscription == &onoffbutton) {
//Serial.print(F("Got: "));
//Serial.println((char *)onoffbutton.lastread);
//char *value = (char *)onoffbutton.lastread;
// Uncomment Serial print sections for debugging
//Serial.print(F("Received: ")); // Display the value received from dashboard
//Serial.println(value);
// Apply message to OnOff
//String message = String(value);
//message.trim();
//if (message == "ON") {
// digitalWrite(ledPin, HIGH);
//Serial.println("LED ON");
//}
//if (message == "OFF") {
//digitalWrite(ledPin, LOW);
//Serial.println("LED OFF");
// }
//}
// }
// DHT11 Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
delay(1000);
Serial.print(t);
Serial.print(F("°C "));
delay(1000);
// Now we can publish stuff!
Serial.print(F("\nSending Humidity val "));
if (! hum.publish(h)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
Serial.print(F("\nSending Temperature val "));
if (! temp.publish(t)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(10000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}