//industrial workers health and safety system based on IoT
#include <WiFi.h>//library for WiFi
#include <PubSubClient.h>//library for MQTT
#include "DHT.h"//Library for DHT11
#include <NewPing.h>//Library for ultrasonic sensor
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // define type of sensor DHT 11
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
DHT dht (DHTPIN, DHTTYPE);// creating the instance by passing pin and type of DHT connected
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Creating an instance for the ultrasonic sensor
void callback(char* subscribetopic, byte* payload, unsigned int payloadLength);
//-------credentials of IBM Accounts------
#define ORG "j1q0lx"//IBM ORGANIZATION ID
#define DEVICE_TYPE "abcd"//Device type mentioned in IBM Watson IoT Platform
#define DEVICE_ID "1234"//Device ID mentioned in IBM Watson IoT Platform
#define TOKEN "12345678" //Token
String data3;
float t;
int p;
int distance;
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";// Server Name
char publishTopic[] = "iot-2/evt/Data/fmt/json";// Topic name and type of event performed and format in which data is sent
char subscribetopic[] = "iot-2/cmd/test/fmt/String";// Command represents the command type, and the command is "test" of format "String"
char authMethod[] = "use-token-auth";// Authentication method
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;// Client ID
//-----------------------------------------
WiFiClient wifiClient; // Creating an instance for WiFiClient
PubSubClient client(server, 1883, callback, wifiClient); // Calling the predefined client ID by passing parameters like server ID, port, and WiFi credentials
void setup()// Configuring the ESP32
{
Serial.begin(115200);
dht.begin();
delay(10);
Serial.println();
wificonnect();
mqttconnect();
}
void loop()// Recursive function
{
t = dht.readTemperature();
p = random(60, 150);
distance = sonar.ping_cm();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Pulse: ");
Serial.println(p);
Serial.print("Distance: ");
Serial.println(distance);
PublishData(t, p, distance);
delay(1000);
if (!client.loop()) {
mqttconnect();
}
}
/*.....................................retrieving to Cloud...............................*/
void PublishData(float temp, int pulse, int dist) {
mqttconnect(); // Function call for connecting to IBM
/*
Creating the JSON payload to update the data to IBM Cloud
*/
String payload = "{\"temperature\":";
payload += temp;
payload += "," "\"pulse\":";
payload += pulse;
payload += "," "\"distance\":";
payload += dist;
payload += "}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(publishTopic, (char*)payload.c_str())) {
Serial.println("Publish OK"); // If it successfully uploads data to the cloud, it will print "Publish OK" in the Serial monitor; otherwise, it will print "Publish failed"
}
else {
Serial.println("Publish failed");
}
}
void mqttconnect() {
if (!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
initManagedDevice();
Serial.println();
}
}
void wificonnect() // Function definition for WiFi connect
{
Serial.println();
Serial.print("Connecting to ");
WiFi.begin("Wokwi-GUEST", "", 6); // Passing the WiFi credentials to establish the connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void initManagedDevice() {
if (client.subscribe(subscribetopic)) {
Serial.println(subscribetopic);
Serial.println("Subscribe to cmd OK");
}
else {
Serial.println("Subscribe to cmd FAILED");
}
}
void callback(char* subscribetopic, byte* payload, unsigned int payloadLength)
{
Serial.print("Callback invoked for topic: ");
Serial.println(subscribetopic);
for (int i = 0; i < payloadLength; i++) {
data3 += (char)payload[i];
}
Serial.println("Data: " + data3);
/*
if (data3 == "lighton") {
Serial.println(data3);
digitalWrite(LED, HIGH);
}
else {
Serial.println(data3);
digitalWrite(LED, LOW);
}
*/
data3 = "";
}