Components and supplies
DHT11 Temperature & Humidity Sensor (3 pins)
Gravity: Analog Soil Moisture Sensor For Arduino
Arduino UNO
ESP8266 ESP-01
Apps and platforms
ThingSpeak API
Arduino IDE
Project description
Code
ESP8266 Program
arduino
1#include "ThingSpeak.h" 2#include <ESP8266WiFi.h> 3 4//------- WI-FI details ----------// 5char ssid[] = " "; //SSID here 6char pass[] = " "; // Passowrd here 7//--------------------------------// 8 9//----------- Channel details ----------------// 10unsigned long Channel_ID = ; // Your Channel ID 11const char * myWriteAPIKey = " "; //Your write API key 12//-------------------------------------------// 13 14const int Field_Number_1 = 1; 15const int Field_Number_2 = 2; 16const int Field_Number_3 = 3; 17String value = ""; 18int value_1 = 0, value_2 = 0, value_3 = 0; 19int x, y, z; 20WiFiClient client; 21 22void setup() 23{ 24 Serial.begin(115200); 25 WiFi.mode(WIFI_STA); 26 ThingSpeak.begin(client); 27 internet(); 28} 29 30void loop() 31{ 32 internet(); 33 if (Serial.available() > 0) 34 { 35 delay(100); 36 while (Serial.available() > 0) 37 { 38 value = Serial.readString(); 39 if (value[0] == '*') 40 { 41 if (value[7] == '#') 42 { 43 value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30)); 44 value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30)); 45 value_3 = ((value[5] - 0x30) * 10 + (value[6] - 0x30)); 46 } 47 } 48 } 49 } 50 upload(); 51} 52 53void internet() 54{ 55 if (WiFi.status() != WL_CONNECTED) 56 { 57 while (WiFi.status() != WL_CONNECTED) 58 { 59 WiFi.begin(ssid, pass); 60 delay(5000); 61 } 62 } 63} 64 65void upload() 66{ 67 ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey); 68 delay(15000); 69 ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey); 70 delay(15000); 71 ThingSpeak.writeField(Channel_ID, Field_Number_3, value_3, myWriteAPIKey); 72 delay(15000); 73 value = ""; 74} 75
ThingSpeak-hum vs moist
matlab
1readID = ; 2readkey = ' '; 3hum = thingSpeakRead(readID, 'Field', 2, 'NumPoints', 60, 'ReadKey', readkey); 4moist= thingSpeakRead(readID, 'Field', 3, 'NumPoints', 60, 'ReadKey', readkey); 5hum = hum(~isnan(hum)); 6moist = moist(~isnan(moist)); 7stem(moist(end-10:end),hum(end-10:end)') 8xlabel('Moisture(%)') 9ylabel('Humidity(%)')
Arduino CODE
arduino
1#include <math.h> 2#include <dht.h> 3#include <SoftwareSerial.h> 4SoftwareSerial mySerial(10, 11); //(Tx,Rx) 5dht DHT; 6#define DHTxxPIN 5 7int op_value; 8 9int ack; 10void setup() 11{ 12 Serial.begin(9600); 13 mySerial.begin(115200); 14} 15void loop() 16{ 17 ack = 0; 18 int chk = DHT.read11(DHTxxPIN); 19 switch (chk) 20 { 21 case DHTLIB_ERROR_CONNECT: 22 ack = 1; 23 break; 24 } 25 if (ack == 0) 26 { 27 Serial.print("Temperature(*C) = "); 28 Serial.println(DHT.temperature); 29 Serial.print("Humidity(%) = "); 30 Serial.println(DHT.humidity); 31 op_value = analogRead(A0); 32 op_value = map(op_value,550,0,0,100); 33 Serial.print("Moisture(%): "); 34 Serial.print(op_value); 35 Serial.print("%"); 36 Serial.println("\ 37 ------------------------- \ 38"); 39 //------Sending Data to ESP8266--------// 40 mySerial.print('*'); // Starting char 41 mySerial.print(round(DHT.temperature)); //2 digit data 42 mySerial.print(round(DHT.humidity)); //2 digit data 43 mySerial.print(op_value); //2 digit data 44 mySerial.println('#'); // Ending char 45 //------------------------------------// 46 delay(2000); 47 } 48 if (ack == 1) 49 { 50 Serial.print("NO DATA"); 51 Serial.print("\ 52\ 53"); 54 delay(2000); 55 } 56} 57
Arduino CODE
arduino
1#include <math.h> 2#include <dht.h> 3#include <SoftwareSerial.h> 4SoftwareSerial mySerial(10, 11); //(Tx,Rx) 5dht DHT; 6#define DHTxxPIN 5 7int op_value; 8 9int ack; 10void setup() 11{ 12 Serial.begin(9600); 13 mySerial.begin(115200); 14} 15void loop() 16{ 17 ack = 0; 18 int chk = DHT.read11(DHTxxPIN); 19 switch (chk) 20 { 21 case DHTLIB_ERROR_CONNECT: 22 ack = 1; 23 break; 24 } 25 if (ack == 0) 26 { 27 Serial.print("Temperature(*C) = "); 28 Serial.println(DHT.temperature); 29 Serial.print("Humidity(%) = "); 30 Serial.println(DHT.humidity); 31 op_value = analogRead(A0); 32 op_value = map(op_value,550,0,0,100); 33 Serial.print("Moisture(%): "); 34 Serial.print(op_value); 35 Serial.print("%"); 36 Serial.println("\ 37 ------------------------- \ 38"); 39 //------Sending Data to ESP8266--------// 40 mySerial.print('*'); // Starting char 41 mySerial.print(round(DHT.temperature)); //2 digit data 42 mySerial.print(round(DHT.humidity)); //2 digit data 43 mySerial.print(op_value); //2 digit data 44 mySerial.println('#'); // Ending char 45 //------------------------------------// 46 delay(2000); 47 } 48 if (ack == 1) 49 { 50 Serial.print("NO DATA"); 51 Serial.print("\ 52\ 53"); 54 delay(2000); 55 } 56} 57
ESP8266 Program
arduino
1#include "ThingSpeak.h" 2#include <ESP8266WiFi.h> 3 4//------- WI-FI details ----------// 5char ssid[] = " "; //SSID here 6char pass[] = " "; // Passowrd here 7//--------------------------------// 8 9//----------- Channel details ----------------// 10unsigned long Channel_ID = ; // Your Channel ID 11const char * myWriteAPIKey = " "; //Your write API key 12//-------------------------------------------// 13 14const int Field_Number_1 = 1; 15const int Field_Number_2 = 2; 16const int Field_Number_3 = 3; 17String value = ""; 18int value_1 = 0, value_2 = 0, value_3 = 0; 19int x, y, z; 20WiFiClient client; 21 22void setup() 23{ 24 Serial.begin(115200); 25 WiFi.mode(WIFI_STA); 26 ThingSpeak.begin(client); 27 internet(); 28} 29 30void loop() 31{ 32 internet(); 33 if (Serial.available() > 0) 34 { 35 delay(100); 36 while (Serial.available() > 0) 37 { 38 value = Serial.readString(); 39 if (value[0] == '*') 40 { 41 if (value[7] == '#') 42 { 43 value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30)); 44 value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30)); 45 value_3 = ((value[5] - 0x30) * 10 + (value[6] - 0x30)); 46 } 47 } 48 } 49 } 50 upload(); 51} 52 53void internet() 54{ 55 if (WiFi.status() != WL_CONNECTED) 56 { 57 while (WiFi.status() != WL_CONNECTED) 58 { 59 WiFi.begin(ssid, pass); 60 delay(5000); 61 } 62 } 63} 64 65void upload() 66{ 67 ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey); 68 delay(15000); 69 ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey); 70 delay(15000); 71 ThingSpeak.writeField(Channel_ID, Field_Number_3, value_3, myWriteAPIKey); 72 delay(15000); 73 value = ""; 74} 75
ThingSpeak-hum vs moist
matlab
1readID = ; 2readkey = ' '; 3hum = thingSpeakRead(readID, 'Field', 2, 'NumPoints', 60, 'ReadKey', readkey); 4moist= thingSpeakRead(readID, 'Field', 3, 'NumPoints', 60, 'ReadKey', readkey); 5hum = hum(~isnan(hum)); 6moist = moist(~isnan(moist)); 7stem(moist(end-10:end),hum(end-10:end)') 8xlabel('Moisture(%)') 9ylabel('Humidity(%)')
ThingSpeak-temp vs moist
matlab
1readID = ; 2readkey = ' '; 3temp = thingSpeakRead(readID, 'Field', 1, 'NumMinutes', 60, 'ReadKey', readkey); 4moist= thingSpeakRead(readID, 'Field', 3, 'NumMinutes', 60, 'ReadKey', readkey); 5temp = temp(~isnan(temp)); 6moist = moist(~isnan(moist)); 7stem(moist(end-10:end),temp(end-10:end)') 8xlabel('Moisture (%)'); 9ylabel('Temperature (C)');
Downloadable files
project_bb_CkKjcAO14a.png
project_bb_CkKjcAO14a.png
project_bb_CkKjcAO14a.png
project_bb_CkKjcAO14a.png
Comments
Only logged in users can leave comments
Ninety99
0 Followers
•0 Projects
Table of contents
Intro
11
0