Assalam-o-Alaikum!
Once again welcome to my Temperature sensor tutorials series. As you know we are working on DHT11 temperature sensor, previously we successfully completed our sensor programming with MySQL and PHP programming. We added our sensor data into database and then retrieved it using PHP. In today’s tutorial we are going to read multiple sensors and then we will save it into database. If you not read my previous tutorial, please read it frist from here:
Now lets start our lesson.
We need to connect two DHT11 temperature sensors on two pins (data pin). It is your choice to select pins, I selected D1 and D2, D1 for reading first sensor D2 for second sensor. Now second thing you have to do is connecting + and – wires. You both wire should connect with same pin. Now your hardware requirement is OK. Here is code for arduino:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
//include libraries #include <ESP8266HTTPClient.h> #include <ESP8266WiFi.h> #include <dht.h> #define dht_apin D1 // Analog Pin sensor is connected to #define dht_apin2 D2 // Analog Pin sensor is connected to dht DHT,DHT2; float get_temperature,get_humidity,get_temperature2,get_humidity2; //Access point credentials const char* ssid = "My WiFi"; const char* pwd = "Iam@007"; const char* host = "http://192.168.8.102"; String get_host = "http://192.168.8.102"; int read_data = 0; int live_delay = 3000; // Its mean 3 seconds wait to enter first record int actual_delay_times = 5; // Its mean that 5 time of live delay to enter record for actual data WiFiServer server(80); // open port 80 for server connection void setup() { Serial.begin(115200); //initialise the serial communication delay(20); //defining the pins, i/p and o/p pinMode(dht_apin, OUTPUT); pinMode(dht_apin, INPUT); digitalWrite(dht_apin, LOW); //connecting to wifi Serial.print("Connecting to wifi "); Serial.println(ssid); WiFi.begin(ssid, pwd); //attempt to connect to wifi while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); //progress with dots until the wifi is connected } //starting the server server.begin(); Serial.print("This is your ip address: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { WiFiClient client = server.available(); if (!client) { return; } //wait for the data to be sent from client while(!client.available()) { delay(1); } DHT.read11(dht_apin); DHT2.read11(dht_apin2); get_temperature = DHT.temperature; get_humidity = DHT.humidity; get_temperature2 = DHT2.temperature; get_humidity2 = DHT2.humidity; //Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); int value = LOW; // set the thermometer off initially //check the index of the browser and act accordingly if (request.indexOf("/Thermo=ON") != -1) { digitalWrite(dht_apin, HIGH); digitalWrite(dht_apin2, HIGH); value = HIGH; //save the data to mysql, access the php file to write HTTPClient http; String url = get_host+"/temperature/add.php?temp="+String(get_temperature)+"&humidity="+String(get_humidity)+"&home_temp="+String(get_temperature2)+"&home_humidity="+String(get_humidity2); read_data++; if(read_data > actual_delay_times) { String url = get_host+"/temperature/add_real_data.php?temp="+String(get_temperature)+"&humidity="+String(get_humidity)+"&home_temp="+String(get_temperature2)+"&home_humidity="+String(get_humidity2); Serial.println(url); http.begin(url); //GET method int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET...code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } read_data = 0; } Serial.println(url); http.begin(url); //GET method int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET...code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); delay(live_delay); } if (request.indexOf("/Thermo=OFF") != -1) { digitalWrite(dht_apin, LOW); digitalWrite(dht_apin2, LOW); value = LOW; } //Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); if (request.indexOf("/AlwaysOn") != -1) { digitalWrite(dht_apin, HIGH); digitalWrite(dht_apin2, HIGH); value = HIGH; client.println("Refresh: 5"); } client.println(""); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("<body style=background-color:skyblue> </body>"); client.println("<style> h1 {text-align: center}</style>"); client.println("<style> h3 {text-align: center}</style>"); client.println("<style> b {text-align: center}</style>"); client.println("<style> p {text-align: center}</style>"); client.println("<head><style>div.relative{position:relative; left:200px; height:20px; width:350px; border:2px solid #73AD21;}</style></head>"); client.println(); if(request.indexOf("/AlwaysOn") != -1) { client.print("<h3>Always On<h3>"); client.println("<h3>Current Temperature: <h3>"+String(get_temperature)+"<b> deg C.</b>"); client.println("<h3>Current Humidity: <h3>"+String(get_humidity)+"<b> %.</b>"); //save the data to mysql, with http client HTTPClient http; String url = get_host+"/temperature/add.php?temp="+String(get_temperature)+"&humidity="+String(get_humidity)+"&home_temp="+String(get_temperature2)+"&home_humidity="+String(get_humidity2); read_data++; if(read_data > actual_delay_times) { String url = get_host+"/temperature/add_real_data.php?temp="+String(get_temperature)+"&humidity="+String(get_humidity)+"&home_temp="+String(get_temperature2)+"&home_humidity="+String(get_humidity2); Serial.println(url); http.begin(url); //using GET method to write to sql int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET...code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } read_data = 0; } Serial.println(url); http.begin(url); //using GET method to write to sql int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET...code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); //delay(3000); } else if(value == HIGH) { client.print("<h3>On Once<h3>"); // client.println("<br>"); client.println("<h3>Current Temperature: <h3>"+String(get_temperature)+"<b> deg C.</b>"); client.println("<h3>Current Humidity: <h3>"+String(get_humidity)+"<b> %.</b>"); } else if(value == LOW) { client.print("<h3>Off<h3>"); // client.println("<br>"); client.println("<h3>Last Temperature: <h3>"+String(get_temperature)+"<b> deg C.</b>"); client.println("<h3>Last Humidity: <h3>"+String(get_humidity)+"<b> %.</b>"); } client.println("<br><br>"); //create buttons client.println("<a href=\"/Thermo=ON\"\"><button>Turn On Once </button></a>"); client.println("<a href=\"/AlwaysOn\"\"><button>Keep It On </button></a>"); client.println("<a href=\"/Thermo=OFF\"\"><button>Turn Off </button></a><br />"); client.println("<p>"+get_host+"/temperature/show.php</p>"); // if there are incoming bytes available from the server, read them and print them: if (client.available()) { char c = client.read(); client.print(c); } client.println("</html>"); } |
As you can see in above code that we are getting two sensors data and then calling two tables. Now you need code to handle these requests. Here is PHP code for first sensor:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php include_once('inc.inc'); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } date_default_timezone_set("Asia/Karachi"); $val = $_GET['temp']; $humd = $_GET['humidity']; $home_val = $_GET['home_temp']; $home_humd = $_GET['home_humidity']; $date = date('Y-m-d'); $time = date('H:i:s'); $sql = "INSERT INTO temps(temp,humidity,home_temp,home_humidity,dated,time) VALUES ($val,$humd,$home_val,$home_humd,'$date','$time');"; if ($conn->query($sql) === TRUE) { echo "Saved Successfully!"; } else { echo "Error:" . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
and here is code for second sensor:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php include_once('inc.inc'); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } date_default_timezone_set("Asia/Karachi"); $val = $_GET['temp']; $humd = $_GET['humidity']; $home_val = $_GET['home_temp']; $home_humd = $_GET['home_humidity']; $date = date('Y-m-d'); $time = date('H:i:s'); $sql = "INSERT INTO temps_real(temp,humidity,home_temp,home_humidity,dated,time) VALUES ($val,$humd,$home_val,$home_humd,'$date','$time');"; if ($conn->query($sql) === TRUE) { echo "Saved Successfully!"; } else { echo "Error:" . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
As you can see there is no difference except $_GET[] values. You can write single page code for both sensors. To store data of these sensors you also need 2 tables, here is MySQL table schema:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
DROP TABLE IF EXISTS `temps`; CREATE TABLE `temps` ( `id` int(11) NOT NULL AUTO_INCREMENT, `temp` int(11) NOT NULL, `humidity` int(11) DEFAULT NULL, `home_temp` int(11) DEFAULT NULL, `home_humidity` int(11) DEFAULT NULL, `time` time DEFAULT NULL, `dated` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `temps_real`; CREATE TABLE `temps_real` ( `id` int(11) NOT NULL AUTO_INCREMENT, `temp` int(11) NOT NULL, `humidity` int(11) DEFAULT NULL, `home_temp` int(11) DEFAULT NULL, `home_humidity` int(11) DEFAULT NULL, `time` time DEFAULT NULL, `dated` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Now test you code.
Note:- Everything is discussed in detail in previous tutorials, not no need to explain it here that how to test the code.
Complete source code is here for download:
Hi mohammad
Thank you very much for the good training
Can this be extended to 4 sensors?
Yes, offcourse, you can.