Professional Documents
Culture Documents
24
ROLL NO: 21EG104A11 l PAGE:
EXPERIMENT 12
Write a program on Arduino/Raspberry Pi to subscribe to MQTT
broker for temperature data.
AIM: To subscribe using MQTT broker for temperature data with the help of NODEMCU.
PROCEDURE:
1. Open io.adafruit.com website and create the account.
2. Open devices and go to new devices select the board with esp8266.
3. Connect DHT11 sensor to D4 and similarly all the pins to respectively
4. Go to new dashboard create exp11 and click setting and then disable the privacy.
5. Create new block as guage or slider so that output will be displayed in that guage. Write the code
in the Arduino UNO and then upload it. Connect to mobile hotspot.
6. Observe the output in that window.
SOURCE CODE:
#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <DHT.h>
// WiFi credentials
#define WIFI_SSID "Vaishnavii"
#define WIFI_PASS "vaish1201"
// Adafruit IO credentials
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "Aakanksha17"
#define AIO_KEY "aio_zuUk213Tk4HA1au976ak9WNqn0zm"
INTERFACING DIAGRAM:
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Connect to Adafruit IO
Serial.println("Connecting to Adafruit IO...");
mqtt.connect();
mqtt.subscribe(&Value2);
}
void loop() {
// Ensure the connection to Adafruit IO is alive
mqtt.connect();
// Read temperature from DHT sensor
float temperatureValue = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(temperatureValue)) {
Serial.println("Failed to read temperature from DHT sensor!");
delay(2000);
OUTPUT:
return;
}
Serial.print(F("Got: "));
Serial.println(Tempstatus);
}}
// 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.
CONCLUSION:
Subscribed temperature data using MQTT breaker with the help of applications.
RESULT:
Temp: 31.8mv