Summary:-
The previous blog discussed about the planned circuit and how the individual sensors work. Now let's see how the IoT feature operates.
First step was, testing individual sensor with arduino pro mini. One by one I tested all the sensors starting with DHT 11 (for temp and humidity), wind speed circuit, MQ7 (for CO level), then finally wind direction circuit. All the individual sensors worked perfectly with the arduino.
Now the next step was to combine all the individual sensor circuits into one circuit and replace arduino with ESP 32 . The same thing needed to be done with the individual codes that worked for the individual sensors.
Even though the ESP 32 is compatible with the arduino IDE. The program that runs on arduino pro mini needs to be modified, in order to work with ESP 32. The baud rate must be changed from 9600 to 115200. Also the pin no's has to be changed according to the ESP 32 pin diagram. If we are using the on board Wi-Fi then it will disable the some of the ADC pins, we can only use pin 32 to 39 for ADC.
Some components that didn't worked as expected were removed from the circuit. Below given is the Modified circuit diagram for the project:-
About IoT platform :-
This project uses IBM watson IoT platform. It uses MQTT protocol to send the data from client to server. It works on publish - subscribe model. The users need to subscribe to the topic. The messages published on that topic can be accessed by the user.
For making the ESP 32 to communicate with the IBM watson IoT platform and send temperature and humidity data , click on the link below and follow the steps given in the link.
https://iotdesignpro.com/projects/how-to-connect-esp32-with-ibm-watson-cloud-platform
Once the account is created on the IoT platform. We can sign in and create our own data visuals by adding graph, pi chart, bar graph , gauge, etc.
Project images:-
IBM watson IoT platform showing temp, humidity, wind speed and co level on guage and line graph. |
Changed layout shows wind direction in left , 315 is north-west. |
Project code and description:-
The code given the above mentioned link is only for temperature and humidity. Therefore it contains only 2 topics and 2 payload. To add more parameters like carbon monoxide level, wind speed and wind direction we need to combine their individual sensor codes by calling functions . Also we need to add more topics and payloads. These payloads are string payloads that contain the device ID , device name and the value of parameter. This information is very important for the data to reach the right location.
Project program:-
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include "DHT.h"
float RS_gas = 0; //variables for CO level//
float ratio = 0;
float sensorValue = 0;
float sensor_volt = 0;
float R0 =4500.0;
int apin=32;
int sensorpin=15; //variables for wind speed//
int a=0;
int lsta;
int b=0;
int lstb;
int c=0;
int s=0;
float m=0;
int h=0;
int k=0;
unsigned int e=0;
void count();
float windspeed();
int dpin=21; //variables for wind direction//
int dpinst=0;
int led[]={5,18,19};
byte x=0b00000000;
int i=0;
int windirection();
int directn[]={0,315,270,225,180,135,90,45};
#define DHTPIN 4
#define DHTTYPE DHT11
#define ORG "5zg8eg"
#define DEVICE_TYPE "ESP32"
#define DEVICE_ID "F4FD8CE350CC"
#define TOKEN "FU0sof*@G0*ju(e5OC"
const char* ssid = "prachet hire";
const char* password = "helloiit";
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char pubTopic1[] = "iot-2/evt/status1/fmt/json";
char pubTopic2[] = "iot-2/evt/status2/fmt/json";
char pubTopic3[] = "iot-2/evt/status3/fmt/json";
char pubTopic4[] = "iot-2/evt/status4/fmt/json";
char pubTopic5[] = "iot-2/evt/status5/fmt/json";
char authMethod[] = "use-token-auth";
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
WiFiClient wifiClient;
PubSubClient client(server, 1883, NULL, wifiClient);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
pinMode(sensorpin,INPUT); // wind speed hall sensor//
pinMode(dpin,INPUT);
pinMode(5,OUTPUT);
pinMode(18,OUTPUT);
pinMode(19,OUTPUT);
dht.begin();
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: ");
Serial.println(WiFi.localIP());
if (!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!client.connect(clientId, authMethod, TOKEN)) {
Serial.print(".");
delay(500);
}
Serial.println("Bluemix connected");
}
}
long lastMsg = 0;
void loop() {
client.loop();
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
sensorValue = analogRead(apin);
sensor_volt = sensorValue/4095*3.3;
RS_gas = (3.3-sensor_volt)/sensor_volt;
ratio = RS_gas/R0; //Replace R0 with the value found using the sketch above
float x = 1538.46 * ratio;
float ppm = pow(x,-1.709);
float ws = windspeed();
int wd = windirection();
String payload = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
payload += ",\"co level\":";
payload += ppm;
payload += "}}";
Serial.print("Sending payload: ");
Serial.println(payload);
if (client.publish(pubTopic1, (char*) payload.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
String payload1 = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
payload1 += ",\"temperature\":";
payload1 += temperature;
payload1 += "}}";
Serial.print("Sending payload1: ");
Serial.println(payload1);
if (client.publish(pubTopic2, (char*) payload1.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
String payload2 = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
payload2 += ",\"humidity\":";
payload2 += humidity;
payload2 += "}}";
Serial.print("Sending payload2: ");
Serial.println(payload2);
if (client.publish(pubTopic3, (char*) payload2.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
String payload3 = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
payload3 += ",\"wind speed\":";
payload3 += ws;
payload3 += "}}";
Serial.print("Sending payload3: ");
Serial.println(payload3);
if (client.publish(pubTopic4, (char*) payload3.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
String payload4 = "{\"d\":{\"Name\":\"" DEVICE_ID "\"";
payload4 += ",\"wind direction\":";
payload4 += wd;
payload4 += "}}";
Serial.print("Sending payload4: ");
Serial.println(payload4);
if (client.publish(pubTopic5, (char*) payload4.c_str())) {
Serial.println("Publish ok");
} else {
Serial.println("Publish failed");
}
}
}
float windspeed(){
b=digitalRead(sensorpin);
if(b!=lstb){
if(b==LOW ){
s++;
}
}
lstb=b;
if(s==1){
for(e=0;e<=310;e++){
delay(10);
if(e>=0 & e<=300){
count();
}
if(e>309){
break;
}
if(e>300 & e<310){
m=(c*0.18);
}
}}
c=0;
s=0;
Serial.println(m);
return m;
}
void count(){
a=digitalRead(sensorpin);
if(a!=lsta){
if(a==LOW ){
c++;
}
if(a==HIGH){
c++;
}
}
lsta=a;
}
int windirection(){
for(i=0;i<=3;i++){
digitalWrite(led[i],HIGH);
dpinst=digitalRead(dpin);
bitWrite(x,i-1,dpinst);
delay(100);
digitalWrite(led[i],LOW);
}
return directn[x];
}// code ends here
If you found this blog useful and want to support me, then you can donate me. The money you give will be used to make more advance electronics projects and setting up better lab.
