You are on page 1of 6

NAME:G.AAKANKSHA DATE: 19.02.

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.

APPARATUS: PC, NODEMCU, DHT11 sensor, Adafruit, USB cable.

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"

// Define DHT sensor type and pin


#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Create an ESP8266 WiFiClient
WiFiClient client;

// Setup the Adafruit IO MQTT client

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,


AIO_USERNAME, AIO_KEY);

Anurag University Embedded Systems and IOT Lab ECE Department


NAME:G.AAKANKSHA DATE: 19.02.24
ROLL NO: 21EG104A11 l PAGE:

INTERFACING DIAGRAM:

Anurag University Embedded Systems and IOT Lab ECE Department


NAME:G.AAKANKSHA DATE: 19.02.24
ROLL NO: 21EG104A11 l PAGE:

Adafruit_MQTT_Publish value1= Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME


"/feeds/value1");

Adafruit_MQTT_Subscribe Value2 = Adafruit_MQTT_Subscribe(&mqtt,


AIO_USERNAME "/feeds/value1");
// Define temperature feed

void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);

Serial.println("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Wait for connection


while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Initialize DHT sensor


dht.begin();

// 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);

Anurag University Embedded Systems and IOT Lab ECE Department


NAME:G.AAKANKSHA DATE: 19.02.24
ROLL NO: 21EG104A11 l PAGE:

OUTPUT:

Anurag University Embedded Systems and IOT Lab ECE Department


NAME:G.AAKANKSHA DATE: 19.02.24
ROLL NO: 21EG104A11 l PAGE:

return;
}

// Publish temperature to Adafruit IO


Serial.print("Publishing temperature to Adafruit IO: ");
Serial.println(temperatureValue);
value1.publish(temperatureValue);
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &Value2) {

String Tempstatus = (char *)Value2.lastread;

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.

Serial.print("Connecting to MQTT... ");


Serial.println("MQTT Connected!");
}

CONCLUSION:
Subscribed temperature data using MQTT breaker with the help of applications.

RESULT:
Temp: 31.8mv

Anurag University Embedded Systems and IOT Lab ECE Department

You might also like