This tutorial explains the steps involved in building a virtual Arduino-based Device Integration Block to send temperature, humidity and fire status data from the Processor Block to the IoT-based Server Block in the Temperature & Fire Monitoring & Alerting Tinker Block Application. The Device Integration Block is nothing but the Arduino sketch that needs to be uploaded to the NodeMCU device within the Processor Block. The steps for building and activating the Device Integration Block for a Temperature & Fire Monitoring & Buzzer control Tinker Block application is given below. Please follow the below instructions:
Before building Device Integration Block, please make sure to build:
At least one physical Sensor Block, one Actuator Block and Processor Block
IoT-based Server Block
Please copy the below Arduino Sketch (code)
Paste the code into Arduino IDE or Arduino Cloud Editor
Edit the Arduino code as follows:
Change ABCDEF with your WiFi name.
Change 1234567890 with your WiFi password.
Change 999999 with your ThingSpeak Channel ID.
Change XXXXXXXXXXXXXXXX with your ThingSpeak Channel Write API Key.
Change 888888 with your ThingSpeak Talkback ID.
Change ZZZZZZZZZZZZZZZZ with your ThingSpeak TalkBack API Key.
Change field1, field2 and field6 to the corresponding field numbers defined by you in the ThingSpeak Channel settings.
After editng the above code, power up the NodeMCU board (within the Processor Block) and upload the Arduino code to NodeMCU board through a USB data cable.
/**********************************************************************************
* ESP8266 Remote Temperature and Fire Status Monitoring and Buzzer Control
* from ThingSpeak IoT Platform
* Add libraries:
* - DHT sensor library by Adafruit v1.3.6
* - Adafruit unified sensor by Adafruit 1.0.3
* Compile for board: NodeMCU 1.0 (ESP 12E Module)
* Create an account on ThingSpeak IoT Platform (https://thingspeak.com)
* Create a private channel on ThingSpeak
*********************************************************************************/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "DHT.h"
static char celsiusTemp[7];
// DHT Sensor type
#define DHTTYPE DHT11 // Change it to DHT11 or DHT22 depending on the sensor type used
// Device Pins
#define TempHumidityPin D1 // Change it to the NodeMCU pin to which the temp sensor is connected
#define FirePin D2 // Change it to the NodeMCU pin to which the fire sensor is connected
#define BuzzerPin D3 // Change it to the NodeMCU pin to which the buzzer device is connected
// Wi-Fi Settings
const char* ssid = "ABCDEF"; // Add your wireless network name (SSID)
const char* password = "1234567890"; // Add your Wi-Fi network password
WiFiClient client;
// ThingSpeak Settings
const int channelID = 999999; // Add your ThingSpeak Channel ID
String writeAPIKey = "XXXXXXXXXXXXXXXX"; // Add Write API key for your ThingSpeak Channel
const char* server = "api.thingspeak.com";
const int postingInterval = 5 * 1000; // post data every 5 seconds
// Variable Setup
long lastConnectionTime = 0;
String fireStatusString = "Not Detected";
// Initialize the program
void setup() {
pinMode(BuzzerPin, OUTPUT);
digitalWrite(BuzzerPin, LOW);
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
// Read temperature value from the DHT11 sensor
String getTemperatureData() {
// Initialize DHT sensor
DHT dht(TempHumidityPin, DHTTYPE);
dht.begin();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
return ((String)t);
}
// Read humidity value from the DHT11 sensor
String getHumidityData() {
// Initialize DHT sensor
DHT dht(TempHumidityPin, DHTTYPE);
dht.begin();
// Read temperature as Celsius (the default)
float h = dht.readHumidity();
return ((String)h);
}
// Read fire status from Flame Sensor
int getFireStatus() {
int fireStatus = digitalRead(FirePin);
if (fireStatus == LOW) {
// LOW means fire detected (depends on sensor module)
Serial.println("Fire Detected!");
} else {
Serial.println("No Fire");
}
return fireStatus;
delay(500); // Small delay for readability
}
// Process Buzzer Turn on / Turn off request
void checkBuzzerControlTalkBack()
{
String talkBackAPIKey = "ZZZZZZZZZZZZZZZZ";
String talkBackID = "888888";
Serial.println("Inside checkBuzzerControlTalkBack");
//HttpClient client;
HTTPClient http;
String thingSpeakAPI = "api.thingspeak.com";
char charIn;
String talkBackURL = "http://" + thingSpeakAPI + "/talkbacks/" + talkBackID + "/commands/execute?api_key=" + talkBackAPIKey;
// Make a HTTP GET request to the TalkBack API:
http.begin(talkBackURL);
int httpCode = http.GET();
if (httpCode > 0) { //Check the returning code
String talkBackCommand = http.getString(); //Get the request response payload
Serial.println("TalkbackCommand= ");
Serial.println(talkBackCommand.c_str()); //Print the response payload
http.end(); //Close connection
if (!strcmp(talkBackCommand.c_str(), "TURN_ON")) {
Serial.println("Turning on Buzzer for 5 seconds");
digitalWrite(BuzzerPin, HIGH);
}
else if (!strcmp(talkBackCommand.c_str(), "TURN_OFF")) {
Serial.println("Turning off Buzzer");
digitalWrite(BuzzerPin, LOW);
}
else {
Serial.println("Unknown command");
}
Serial.flush();
delay(1000);
}
}
// Package the HTTP message to be sent to ThingSpeak IoT Platform
void postHTTPMessage(String body) {
client.println("POST /update HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("User-Agent: ESP8266 (nothans)/1.0");
client.println("Connection: close");
client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: " + String(body.length()));
client.println("");
client.print(body);
}
// Main program looping forever
void loop() {
if (client.connect(server, 80)) {
String temperature = getTemperatureData();
// If temperature data is not valid, consider temperature value as 0
if (!strcmp(temperature.c_str(), "NaN") || !strcmp(temperature.c_str(), "nan")) {
temperature = "0";
}
delay(1000);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" degree Celsius");
String humidity = getHumidityData();
// If humidity data is not valid, consider humidity value as 0
if (!strcmp(humidity.c_str(), "NaN") || !strcmp(humidity.c_str(), "nan")) {
humidity = "0";
}
delay(1000);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
int fireStatus = getFireStatus();
Serial.println("Fire Status:");
Serial.print(fireStatus);
// Construct API request body
String body = "field1=";
body += String(temperature);
body += "&";
body += "field2=";
body += String(humidity);
body += "&";
body += "field6=";
body += String(fireStatus);
postHTTPMessage(body);
}
client.stop();
// wait and then post again
delay(postingInterval);
// Check ThingSpeak for TalkBack Commands
checkBuzzerControlTalkBack();
delay(1000);
}