Devices & Components
Arduino Uno Rev3
USB-A to Mini-USB Cable
Jumper wires (generic)
IR receiver (generic)
DHT11 Temperature & Humidity Sensor (3 pins)
NodeMcu
GPS Module (Generic)
Breadboard (generic)
IR receiver (generic)
USB-A to B Cable
Software & Tools
Arduino IDE
ThingSpeak API
Project description
Code
MATLAB Visualization with scaling
matlab
This is a different version of the previous one. The marks on the map are as big as the value of a variable that we choose. In this case, the mark is as big as the value of temperature. For easy testing we could send data manually to our ThingSpeak variables through the browser: https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=?
1% MATLAB Visualization with scaling 2%This is a different version of the previous one. The marks on the map are as big as the value of a variable that we choose. In this case the mark is as big as the value of temperature. 3%For easy testing we could send data manually to our ThingSpeak variables through the browser: (in my case field4 was the longitude, field3 the latitude and field1 the temperature) 4%https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=? */ 5 6% the <'NumPoints',3> indicates that the last 3 values will be shown 7lat = thingSpeakRead(yourChannelID,'Fields',3,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 8lon = thingSpeakRead(yourChannelID,'Fields',4,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 9temp = thingSpeakRead(yourChannelID,'Fields',1,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 10%indicate how map will look like 11geobasemap('streets'); 12geobubble(lat,lon,temp,zoom=5); 13
Sending email alerts in case of wrong temperature.
arduino
We will add the function of sending email in case the temperature is different than 20 ° C. To perform this function we will create a new gmail account which will be the sender. So, in case we have a different value than 20 ° C in the loop we have created, an email will be sent from the new email alert account through the NodeMCU.
1//Sending email alerts in case of wrong temperature 2//We will add the function of sending email in case the temperature is different than 20 ° C. To perform this function we will create a new gmail account which will be the sender. //So, in case we have a different value than 20 ° C in the loop we have created, an email will be sent from the new email alert account through the NodeMCU. 3 4#include <ESP_Mail_Client.h> 5#include <ESP_Mail_FS.h> 6#include <SDK_Version_Common.h> 7 8#include <ESP8266WiFi.h> 9#include <dht.h> 10#include <ThingSpeak.h> 11//Defining the WiFi credentials 12#define WIFI_SSID "Your SSID" 13#define WIFI_PASSWORD "Your pass" 14//Defining the host we will use for the emails, I use gmail 15#define SMTP_HOST "smtp.gmail.com" 16#define SMTP_PORT 465 17 18/* The sign in credentials of the new created email */ 19#define AUTHOR_EMAIL "your new gmail account" 20#define AUTHOR_PASSWORD "your new password" 21 22/* Defining recipient's email*/ 23#define RECIPIENT_EMAIL "your preferred recipient" 24 25/* The SMTP Session object used for Email sending */ 26SMTPSession smtp; 27 28/* Declare the message class */ 29 SMTP_Message message; 30 31/* Callback function to get the Email sending status */ 32void smtpCallback(SMTP_Status status); 33 34dht DHT; 35#define DHT11_PIN D4 36 37WiFiClient client; 38 39long myChannelNumber = yourChannelID; 40const char myWriteAPIKey[] = "yourWriteAPIKey"; 41 42void setup() { 43 44 Serial.begin(9600); 45 // Connect to WiFi 46 WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 47 while(WiFi.status() != WL_CONNECTED) 48 { 49 delay(200); 50 Serial.print(".."); 51 } 52 Serial.println(); 53 Serial.println("NodeMCU is connected!"); 54 Serial.println(WiFi.localIP()); 55 //DHT.begin(); 56 ThingSpeak.begin(client); 57 58 /* Set the callback function to get the sending results */ 59 smtp.callback(smtpCallback); 60 61 /* Declare the email session config data */ 62 ESP_Mail_Session session; 63 64 /* Set the session config */ 65 session.server.host_name = SMTP_HOST; 66 session.server.port = SMTP_PORT; 67 session.login.email = AUTHOR_EMAIL; 68 session.login.password = AUTHOR_PASSWORD; 69 session.login.user_domain = ""; 70 71 /* Set the message headers */ 72 message.sender.name = "ESP"; 73 message.sender.email = AUTHOR_EMAIL; 74 message.subject = "ESP Alert Email"; 75 message.addRecipient("Vasileios", RECIPIENT_EMAIL); 76 77 /* Connect to server with the session config */ 78 if (!smtp.connect(&session)) 79 return; 80} 81 82void loop() { 83 84 int chk = DHT.read11(DHT11_PIN); 85 float h = DHT.humidity; 86 float t = DHT.temperature; 87 88 if(t!=20){ 89 /*Defining the HTML message of the email*/ 90 String htmlMsg = "<div style=\\"color:#2f4468;\\"><h1>Hello World!</h1><p>- Sent from ESP board: Temperature needs caution:!</p></div>" + (String)t + "° C"; 91 message.html.content = htmlMsg.c_str(); 92 message.html.content = htmlMsg.c_str(); 93 message.text.charSet = "us-ascii"; 94 message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; 95 96 /* Start sending Email and close the session */ 97 if (!MailClient.sendMail(&smtp, &message)) 98 Serial.println("Error sending Email, " + smtp.errorReason()); 99 } 100 101 Serial.println("Temperature: " + (String) t); 102 Serial.println("Humidity: " + (String) h); 103 104 ThingSpeak.setField(1, t); 105 ThingSpeak.setField(2, h); 106 int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 107 if(x == 200){ 108 Serial.println("Channel update successful."); 109 } 110 else{ 111 Serial.println("Problem updating channel. HTTP error code " + String(x)); 112 } 113 delay(20000); 114} 115 116/* Callback function to get the Email sending status */ 117void smtpCallback(SMTP_Status status){ 118 /* Print the current status */ 119 Serial.println(status.info()); 120 121 /* Print the sending result */ 122 if (status.success()){ 123 Serial.println("----------------"); 124 ESP_MAIL_PRINTF("Message sent success: %d\ 125", status.completedCount()); 126 ESP_MAIL_PRINTF("Message sent failed: %d\ 127", status.failedCount()); 128 Serial.println("----------------\ 129"); 130 struct tm dt; 131 132 for (size_t i = 0; i < smtp.sendingResult.size(); i++){ 133 /* Get the result item */ 134 SMTP_Result result = smtp.sendingResult.getItem(i); 135 time_t ts = (time_t)result.timestamp; 136 localtime_r(&ts, &dt); 137 //printing info regarding the email sent 138 ESP_MAIL_PRINTF("Message No: %d\ 139", i + 1); 140 ESP_MAIL_PRINTF("Status: %s\ 141", result.completed ? "success" : "failed"); 142 ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\ 143", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); 144 ESP_MAIL_PRINTF("Recipient: %s\ 145", result.recipients); 146 ESP_MAIL_PRINTF("Subject: %s\ 147", result.subject); 148 } 149 Serial.println("----------------\ 150"); 151 } 152}
GPS Tracking with NodeMCU and MATLAB Visualization
matlab
In ThingSpeak we can create Map Visualizations in order to track the location of a cargo at a specific time. We create MATLAB Visualization and through code we can choose which variables to show(e.g. Temperature), including the Latitude and Longitude variables for printing a mark on the map. For easy testing we could send data manually to our ThingSpeak variables through the browser: https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=?
1% GPS Tracking with NodeMCU and MATLAB Visualization 2%In ThingSpeak we can create Map Visualizations in order to track the location of a cargo at a specific time. We create MATLAB Visualization and through code we can choose which variables to show(e.g. Temperature), including the Latitude and Longitude variables for printing a mark on the map. 3%For easy testing we could send data manually to our ThingSpeak variables through the browser: (in my case field4 was the longitude, field3 the latitude and field1 the temperature) 4%https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=? 5 6% the <'NumPoints',3> indicates that the last 3 values will be shown 7lat = thingSpeakRead(yourChannelID,'Fields',3,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 8lon = thingSpeakRead(yourChannelID,'Fields',4,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 9temp = thingSpeakRead(yourChannelID,'Fields',1,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 10%red indicated the color of marks and filled indicates how the mark will look like 11geoscatter(lat,lon,temp,'red','filled'); 12geobasemap('streets');
Connecting NodeMcu to WiFi and sending data to the Cloud.
arduino
This code first connects NodeMcu to WiFi and then sends the data to the ThingSpeak page. At https://thingspeak.com/ we can create a Channel that will have its own fields, graphs and API keys so we can do our own updates. So in the code we need to add the WiFi name and code as well as the Channel ID and the Write API Key of ThingSpeak. Finally, we upload the code to NodeMcu by selecting "NodeMCU 1.0 (ESP-12E Module)" as a board tool.
1//Connecting NodeMcu to WiFi and sending data to the Cloud 2//This code first connects NodeMcu to WiFi and then sends the data to the ThingSpeak page. At https://thingspeak.com/ we can create a Channel that will have its own fields, graphs //and API keys so we can do our own updates. So, in the code we need to add the WiFi name and code as well as the Channel ID and the Write API Key of ThingSpeak. Finally, we upload //the code to NodeMcu by selecting "NodeMCU 1.0 (ESP-12E Module)" as a board tool. 3 4#include <ESP8266WiFi.h> 5#include <dht.h> 6#include <ThingSpeak.h> 7 8dht DHT; 9#define DHT11_PIN D4 10/* creating object for connecting on WiFi and variables for Channel ID and Write API Key in ThingSpeak */ 11WiFiClient client; 12 13long myChannelNumber = yourChannelID; 14const char myWriteAPIKey[] = "yourAPIKey"; 15 16void setup() { 17 Serial.begin(9600); 18 //connect on WiFi 19 WiFi.begin("Your SSID", "Your Pass"); 20 while(WiFi.status() != WL_CONNECTED) 21 { 22 delay(200); 23 Serial.print(".."); 24 } 25 Serial.println(); 26 Serial.println("NodeMCU is connected!"); 27 Serial.println(WiFi.localIP()); 28 //Start ThingSpeak with WiFi credentials 29 ThingSpeak.begin(client); 30} 31 32void loop() { 33 int chk = DHT.read11(DHT11_PIN); 34 float h = DHT.humidity; 35 float t = DHT.temperature; 36 Serial.println("Temperature: " + (String) t); 37 Serial.println("Humidity: " + (String) h); 38 /*sending temperature and humidity values to field1 and field2 of ThingSpeak*/ 39 ThingSpeak.setField(1, t); 40 ThingSpeak.setField(2, h); 41 int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 42 if(x == 200){ 43 Serial.println("Channel update successful."); 44 } 45 else{ 46 Serial.println("Problem updating channel. HTTP error code " + String(x)); 47 } 48 delay(20000); 49}
Handling the room temperature by sending IR signal to turn on/off the air-conditioner
arduino
We set specific temperature values and check the ambient conditions through the DHT temperature and humidity sensor. We have set as desired room temperature the 20 ° C and the signal is sent per minute. If the conditions are not what we need, the Arduino board via IR Transmitter sends the appropriate signal either to turn on or off the air conditioning.
1//Handling the room temperature by sending IR signal to turn on/off the air-conditioner 2 3//We set specific temperature values and check the ambient conditions through the DHT temperature and humidity sensor. We have set as desired room temperature the 20 ° C and the //signal is sent per minute. If the conditions are not what we need, the Arduino board via IR Transmitter sends the appropriate signal either to turn on or off the air //conditioning. 4 5//The libraries we need for handling IR singals and DHT11 sensor 6#include <IRremote.hpp> 7#include <dht.h> 8 9//defining the Arduino pin where we connect DHT11 sensor 10#define dht_apin A0 11//defining the Arduino pin where we connect IR Transmitter 12#define IRledPin 13 13#define NumIRsignals 200 14 15dht DHT; 16 17int IRsignalON[] = { 18 19//put your own signal here 20/* 21456, 454, 2258, 164, 2358, 52, 2458, 166, 2556, 164, 2658, 54, 2758, 52, 2858, 164, 2958, 54, 3058, 52, 3158, 164, 3258, 54, 3356, 54, 3458, 164, 3558, 164, 3658, 54, 3756, 166, 3858, 164, 3958, 52, 4058, 164, 4158, 164, 4258, 164, 4358, 164, 4458, 164, 4558, 164, 4658, 54, 4758, 164, 4858, 52, 4958, 54, 5058, 52, 5158, 54, 5256, 54, 5358, 52, 5458, 166, 5556, 54, 5658, 164, 5758, 164, 5858, 164, 5958, 164, 6058, 54, 6156, 54, 6258, 54, 6356, 164, 6458, 54, 6558, 52, 6658, 54, 6758, 52, 6858, 164, 6958, 164, 7058, 536, 71456, 456, 7256, 166, 7356, 54, 7458, 164, 7558, 164, 7658, 52, 7758, 54, 7858, 164, 7958, 52, 8058, 54, 8156, 166, 8258, 52, 8358, 54, 8456, 166, 8558, 164, 8658, 52, 8758, 164, 8858, 164, 8958, 54, 9058, 164, 9158, 164, 9258, 164, 9358, 164, 9458, 164, 9558, 164, 9658, 54, 9756, 164, 9858, 54, 9958, 52, 10058, 54, 10158, 52, 10258, 54, 10358, 52, 10458, 164, 10558, 54, 10656, 166, 10756, 166, 10856, 166, 10956, 166, 11056, 54, 11158, 52, 11258, 54, 11358, 164, 11458, 52, 11558, 54, 11658, 52, 11758, 54, 11856, 166, 11958, 164, 12058, 0 121*/ 122}; 123 124int IRsignalOFF[] = { 125 126//put your own signal here 127/* 128458, 454, 12958, 164, 13058, 52, 13158, 164, 13258, 164, 13358, 54, 13458, 52, 13558, 164, 13658, 54, 13756, 54, 13858, 164, 13958, 54, 14056, 54, 14158, 164, 14258, 164, 14358, 52, 14458, 164, 14558, 54, 14658, 164, 14758, 164, 14858, 164, 14958, 164, 15058, 54, 15156, 166, 15256, 166, 15358, 164, 15458, 52, 15558, 54, 15658, 52, 15758, 54, 15856, 166, 15958, 52, 16058, 54, 16156, 166, 16256, 166, 16356, 166, 16456, 54, 16558, 54, 16656, 54, 16758, 52, 16858, 54, 16958, 52, 17058, 54, 17158, 52, 17258, 164, 17358, 164, 17458, 164, 17558, 164, 17658, 164, 17758, 536, 178456, 454, 17958, 164, 18058, 54, 18158, 164, 18258, 164, 18358, 52, 18458, 54, 18558, 164, 18658, 52, 18758, 54, 18858, 164, 18958, 52, 19058, 54, 19158, 164, 19258, 164, 19358, 52, 19458, 164, 19558, 54, 19658, 164, 19756, 166, 19856, 166, 19958, 164, 20058, 52, 20158, 164, 20258, 164, 20358, 164, 20458, 54, 20558, 52, 20658, 54, 20758, 52, 20858, 164, 20958, 54, 21056, 54, 21158, 164, 21258, 164, 21358, 164, 21458, 52, 21558, 54, 21658, 52, 21758, 54, 21858, 52, 21958, 54, 22058, 52, 22158, 54, 22258, 164, 22356, 166, 22458, 164, 22558, 164, 22658, 164, 22758, 0 228*/ 229}; 230 231void setup() { 232 //Confirming that IR is off in the beginning 233 digitalWrite(IRledPin, LOW); 234 pinMode(IRledPin, OUTPUT); 235 Serial.begin(9600); 236} 237 238void loop() { 239 //reading from sensor 240 DHT.read11(dht_apin); 241 //printing values from sensor 242 Serial.print("Current humidity = "); 243 Serial.print(DHT.humidity); 244 Serial.print("% "); 245 Serial.print("temperature = "); 246 Serial.print(DHT.temperature); 247 Serial.print("C ~ "); 248 Serial.print(DHT.temperature*1.8 + 32); 249 Serial.println("F"); 250 /*If temperature is less than 19°C --> send IR signal for turning on the air-conditioner*/ 251 if(DHT.temperature < 19) 252 { 253 Serial.println("Temperature low --> AC powered on"); 254 for (int i = 0; i < NumIRsignals; i+=2) 255 { 256 //send IR signal of turning on the air-conditioner 257 pulseIR(IRsignalON[i]*10); 258 //turning off the IR Transmitter 259 delayMicroseconds(IRsignalON[i+1]*10); 260 } 261 } 262 263 /*If temperature is more than 19°C --> send IR signal for turning off the air-conditioner*/ 264 if(DHT.temperature > 19) 265 { 266 Serial.println("Temperature high --> AC powered off"); 267 for (int i = 0; i < NumIRsignals; i+=2) 268 { 269 //send IR signal of turning off the air-conditioner 270 pulseIR(IRsignalOFF[i]*10); 271 //turning off the IR Transmitter 272 delayMicroseconds(IRsignalOFF[i+1]*10); 273 } 274 } 275 //wait for 60 secs and repeat 276 delay(60000); 277} 278 279//method for sending the IR signal 280void pulseIR(long microsecs) { 281 282 //turns off any background interrupts 283 cli(); 284 285 while (microsecs > 0) { 286 287 digitalWrite(IRledPin, HIGH); 288 delayMicroseconds(10); 289 digitalWrite(IRledPin, LOW); 290 delayMicroseconds(10); 291 292 microsecs -= 26; 293 } 294 //turns on background interrupts 295 sei(); 296}
Decoding the IR signal for air-conditioner
arduino
We decode the IR signal sent by the remote controller of air-conditioner. We press the buttons ON and OFF and the IR Receiver will receive this signal which after that is decoded in order to send it back again afterwards.
1//Decoding the IR signal for air-conditioner 2//We decode the IR signal sent by the remote controller of air-conditioner. We press the buttons ON and OFF and the IR Receiver will receive this signal which after that is //decoded in order to send it back again afterwards. 3 4#define IRpin_PIN PIND 5#define IRpin 6 6#define MAXPULSE 65000 7#define RESOLUTION 20 8 9//the array where the IR signal will be saved 10uint16_t pulses[100][2]; 11uint8_t currentpulse = 0; 12 13void setup(void) { 14 Serial.begin(9600); 15 Serial.println("Ready to decode IR!"); 16} 17 18void loop(void) { 19 uint16_t highpulse, lowpulse; 20 highpulse = lowpulse = 0; 21 22 //reading the signal 23 while (IRpin_PIN & (1 << IRpin)) { 24 25 highpulse++; 26 delayMicroseconds(RESOLUTION); 27 28 if ((highpulse >= MAXPULSE) && (currentpulse != 0)) { 29 printpulses(); //method for printing results 30 currentpulse=0; 31 return; 32 } 33 } 34 35 pulses[currentpulse][0] = highpulse; 36 //reading of signal 37 while (! (IRpin_PIN & _BV(IRpin))) { 38 39 lowpulse++; 40 delayMicroseconds(RESOLUTION); 41 if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) { 42 printpulses(); //method for printing results 43 currentpulse=0; 44 return; 45 } 46 } 47 pulses[currentpulse][1] = lowpulse; 48 49 currentpulse++; 50} 51//method for printing the decoding of IR signal 52void printpulses(void) { 53 54 Serial.println("int IRsignal[] = {"); 55 for (uint8_t i = 0; i < currentpulse-1; i++) { 56 Serial.print("\ "); 57 Serial.print(pulses[i][1] * RESOLUTION / 10, DEC); 58 Serial.print(", "); 59 Serial.print(pulses[i+1][0] * RESOLUTION / 10, DEC); 60 Serial.println(","); 61 } 62 Serial.print("\ "); 63 Serial.print(pulses[currentpulse-1][1] * RESOLUTION / 10, DEC); 64 Serial.print(", 0};"); 65}
Handling the room temperature by sending IR signal to turn on/off the air-conditioner
arduino
We set specific temperature values and check the ambient conditions through the DHT temperature and humidity sensor. We have set as desired room temperature the 20 ° C and the signal is sent per minute. If the conditions are not what we need, the Arduino board via IR Transmitter sends the appropriate signal either to turn on or off the air conditioning.
1//Handling the room temperature by sending IR signal to turn on/off the air-conditioner 2 3//We set specific temperature values and check the ambient conditions through the DHT temperature and humidity sensor. We have set as desired room temperature the 20 ° C and the //signal is sent per minute. If the conditions are not what we need, the Arduino board via IR Transmitter sends the appropriate signal either to turn on or off the air //conditioning. 4 5//The libraries we need for handling IR singals and DHT11 sensor 6#include <IRremote.hpp> 7#include <dht.h> 8 9//defining the Arduino pin where we connect DHT11 sensor 10#define dht_apin A0 11//defining the Arduino pin where we connect IR Transmitter 12#define IRledPin 13 13#define NumIRsignals 200 14 15dht DHT; 16 17int IRsignalON[] = { 18 19//put your own signal here 20/* 21456, 454, 2258, 164, 2358, 52, 2458, 166, 2556, 164, 2658, 54, 2758, 52, 2858, 164, 2958, 54, 3058, 52, 3158, 164, 3258, 54, 3356, 54, 3458, 164, 3558, 164, 3658, 54, 3756, 166, 3858, 164, 3958, 52, 4058, 164, 4158, 164, 4258, 164, 4358, 164, 4458, 164, 4558, 164, 4658, 54, 4758, 164, 4858, 52, 4958, 54, 5058, 52, 5158, 54, 5256, 54, 5358, 52, 5458, 166, 5556, 54, 5658, 164, 5758, 164, 5858, 164, 5958, 164, 6058, 54, 6156, 54, 6258, 54, 6356, 164, 6458, 54, 6558, 52, 6658, 54, 6758, 52, 6858, 164, 6958, 164, 7058, 536, 71456, 456, 7256, 166, 7356, 54, 7458, 164, 7558, 164, 7658, 52, 7758, 54, 7858, 164, 7958, 52, 8058, 54, 8156, 166, 8258, 52, 8358, 54, 8456, 166, 8558, 164, 8658, 52, 8758, 164, 8858, 164, 8958, 54, 9058, 164, 9158, 164, 9258, 164, 9358, 164, 9458, 164, 9558, 164, 9658, 54, 9756, 164, 9858, 54, 9958, 52, 10058, 54, 10158, 52, 10258, 54, 10358, 52, 10458, 164, 10558, 54, 10656, 166, 10756, 166, 10856, 166, 10956, 166, 11056, 54, 11158, 52, 11258, 54, 11358, 164, 11458, 52, 11558, 54, 11658, 52, 11758, 54, 11856, 166, 11958, 164, 12058, 0 121*/ 122}; 123 124int IRsignalOFF[] = { 125 126//put your own signal here 127/* 128458, 454, 12958, 164, 13058, 52, 13158, 164, 13258, 164, 13358, 54, 13458, 52, 13558, 164, 13658, 54, 13756, 54, 13858, 164, 13958, 54, 14056, 54, 14158, 164, 14258, 164, 14358, 52, 14458, 164, 14558, 54, 14658, 164, 14758, 164, 14858, 164, 14958, 164, 15058, 54, 15156, 166, 15256, 166, 15358, 164, 15458, 52, 15558, 54, 15658, 52, 15758, 54, 15856, 166, 15958, 52, 16058, 54, 16156, 166, 16256, 166, 16356, 166, 16456, 54, 16558, 54, 16656, 54, 16758, 52, 16858, 54, 16958, 52, 17058, 54, 17158, 52, 17258, 164, 17358, 164, 17458, 164, 17558, 164, 17658, 164, 17758, 536, 178456, 454, 17958, 164, 18058, 54, 18158, 164, 18258, 164, 18358, 52, 18458, 54, 18558, 164, 18658, 52, 18758, 54, 18858, 164, 18958, 52, 19058, 54, 19158, 164, 19258, 164, 19358, 52, 19458, 164, 19558, 54, 19658, 164, 19756, 166, 19856, 166, 19958, 164, 20058, 52, 20158, 164, 20258, 164, 20358, 164, 20458, 54, 20558, 52, 20658, 54, 20758, 52, 20858, 164, 20958, 54, 21056, 54, 21158, 164, 21258, 164, 21358, 164, 21458, 52, 21558, 54, 21658, 52, 21758, 54, 21858, 52, 21958, 54, 22058, 52, 22158, 54, 22258, 164, 22356, 166, 22458, 164, 22558, 164, 22658, 164, 22758, 0 228*/ 229}; 230 231void setup() { 232 //Confirming that IR is off in the beginning 233 digitalWrite(IRledPin, LOW); 234 pinMode(IRledPin, OUTPUT); 235 Serial.begin(9600); 236} 237 238void loop() { 239 //reading from sensor 240 DHT.read11(dht_apin); 241 //printing values from sensor 242 Serial.print("Current humidity = "); 243 Serial.print(DHT.humidity); 244 Serial.print("% "); 245 Serial.print("temperature = "); 246 Serial.print(DHT.temperature); 247 Serial.print("C ~ "); 248 Serial.print(DHT.temperature*1.8 + 32); 249 Serial.println("F"); 250 /*If temperature is less than 19°C --> send IR signal for turning on the air-conditioner*/ 251 if(DHT.temperature < 19) 252 { 253 Serial.println("Temperature low --> AC powered on"); 254 for (int i = 0; i < NumIRsignals; i+=2) 255 { 256 //send IR signal of turning on the air-conditioner 257 pulseIR(IRsignalON[i]*10); 258 //turning off the IR Transmitter 259 delayMicroseconds(IRsignalON[i+1]*10); 260 } 261 } 262 263 /*If temperature is more than 19°C --> send IR signal for turning off the air-conditioner*/ 264 if(DHT.temperature > 19) 265 { 266 Serial.println("Temperature high --> AC powered off"); 267 for (int i = 0; i < NumIRsignals; i+=2) 268 { 269 //send IR signal of turning off the air-conditioner 270 pulseIR(IRsignalOFF[i]*10); 271 //turning off the IR Transmitter 272 delayMicroseconds(IRsignalOFF[i+1]*10); 273 } 274 } 275 //wait for 60 secs and repeat 276 delay(60000); 277} 278 279//method for sending the IR signal 280void pulseIR(long microsecs) { 281 282 //turns off any background interrupts 283 cli(); 284 285 while (microsecs > 0) { 286 287 digitalWrite(IRledPin, HIGH); 288 delayMicroseconds(10); 289 digitalWrite(IRledPin, LOW); 290 delayMicroseconds(10); 291 292 microsecs -= 26; 293 } 294 //turns on background interrupts 295 sei(); 296}
MATLAB Visualization with scaling
matlab
This is a different version of the previous one. The marks on the map are as big as the value of a variable that we choose. In this case, the mark is as big as the value of temperature. For easy testing we could send data manually to our ThingSpeak variables through the browser: https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=?
1% MATLAB Visualization with scaling 2%This is a different version of the previous one. The marks on the map are as big as the value of a variable that we choose. In this case the mark is as big as the value of temperature. 3%For easy testing we could send data manually to our ThingSpeak variables through the browser: (in my case field4 was the longitude, field3 the latitude and field1 the temperature) 4%https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=? */ 5 6% the <'NumPoints',3> indicates that the last 3 values will be shown 7lat = thingSpeakRead(yourChannelID,'Fields',3,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 8lon = thingSpeakRead(yourChannelID,'Fields',4,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 9temp = thingSpeakRead(yourChannelID,'Fields',1,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 10%indicate how map will look like 11geobasemap('streets'); 12geobubble(lat,lon,temp,zoom=5); 13
Printing Temperature and Humidity
arduino
Printing the room Temperature and Humidity values from DHT11 sensor in the Arduino IDE serial monitor.
1//Printing Temperature and Humidity 2//Printing the room Temperature and Humidity values from DHT11 sensor in the Arduino IDE serial monitor. 3 4// The library we need for sensor DHT11 5#include <dht.h> 6 7#define dht_apin A0 // The Arduino analog pin in which the DHT11 Sensor is connected to 8 9dht DHT; //The object that will handle the values of temperature and humidity 10 11void setup(){ 12 13 Serial.begin(9600); 14 delay(500); //Delay 0.5 sec to let system boot 15 Serial.println("DHT11 Humidity & temperature Sensor\ 16\ 17"); 18 delay(1000);//Wait 1 sec before accessing Sensor 19 20} 21 22void loop(){ 23 //Start of Program 24 25 DHT.read11(dht_apin); //read from the A0 analog pin of Arduino 26 27 Serial.print("Current humidity = "); 28 Serial.print(DHT.humidity); 29 Serial.print("% "); 30 Serial.print("temperature = "); 31 Serial.print(DHT.temperature); 32 Serial.print("C and "); 33 //We convert the temperature to fahrenheit 34 Serial.print(DHT.temperature*1.8 + 32); 35 Serial.println("F"); 36 37 delay(2000);//Wait 2 seconds before accessing the sensor again. 38}
Printing Temperature and Humidity
arduino
Printing the room Temperature and Humidity values from DHT11 sensor in the Arduino IDE serial monitor.
1//Printing Temperature and Humidity 2//Printing the room Temperature and Humidity values from DHT11 sensor in the Arduino IDE serial monitor. 3 4// The library we need for sensor DHT11 5#include <dht.h> 6 7#define dht_apin A0 // The Arduino analog pin in which the DHT11 Sensor is connected to 8 9dht DHT; //The object that will handle the values of temperature and humidity 10 11void setup(){ 12 13 Serial.begin(9600); 14 delay(500); //Delay 0.5 sec to let system boot 15 Serial.println("DHT11 Humidity & temperature Sensor\ 16\ 17"); 18 delay(1000);//Wait 1 sec before accessing Sensor 19 20} 21 22void loop(){ 23 //Start of Program 24 25 DHT.read11(dht_apin); //read from the A0 analog pin of Arduino 26 27 Serial.print("Current humidity = "); 28 Serial.print(DHT.humidity); 29 Serial.print("% "); 30 Serial.print("temperature = "); 31 Serial.print(DHT.temperature); 32 Serial.print("C and "); 33 //We convert the temperature to fahrenheit 34 Serial.print(DHT.temperature*1.8 + 32); 35 Serial.println("F"); 36 37 delay(2000);//Wait 2 seconds before accessing the sensor again. 38}
Sending email alerts in case of wrong temperature.
arduino
We will add the function of sending email in case the temperature is different than 20 ° C. To perform this function we will create a new gmail account which will be the sender. So, in case we have a different value than 20 ° C in the loop we have created, an email will be sent from the new email alert account through the NodeMCU.
1//Sending email alerts in case of wrong temperature 2//We will add the 3 function of sending email in case the temperature is different than 20 ° C. To perform 4 this function we will create a new gmail account which will be the sender. //So, 5 in case we have a different value than 20 ° C in the loop we have created, an email 6 will be sent from the new email alert account through the NodeMCU. 7 8#include 9 <ESP_Mail_Client.h> 10#include <ESP_Mail_FS.h> 11#include <SDK_Version_Common.h> 12 13#include 14 <ESP8266WiFi.h> 15#include <dht.h> 16#include <ThingSpeak.h> 17//Defining the 18 WiFi credentials 19#define WIFI_SSID "Your SSID" 20#define WIFI_PASSWORD "Your 21 pass" 22//Defining the host we will use for the emails, I use gmail 23#define 24 SMTP_HOST "smtp.gmail.com" 25#define SMTP_PORT 465 26 27/* The sign in credentials 28 of the new created email */ 29#define AUTHOR_EMAIL "your new gmail account" 30#define 31 AUTHOR_PASSWORD "your new password" 32 33/* Defining recipient's email*/ 34#define 35 RECIPIENT_EMAIL "your preferred recipient" 36 37/* The SMTP Session object used 38 for Email sending */ 39SMTPSession smtp; 40 41/* Declare the message class */ 42 43 SMTP_Message message; 44 45/* Callback function to get the Email sending status 46 */ 47void smtpCallback(SMTP_Status status); 48 49dht DHT; 50#define DHT11_PIN 51 D4 52 53WiFiClient client; 54 55long myChannelNumber = yourChannelID; 56const 57 char myWriteAPIKey[] = "yourWriteAPIKey"; 58 59void setup() { 60 61 Serial.begin(9600); 62 63 // Connect to WiFi 64 WiFi.begin(WIFI_SSID, WIFI_PASSWORD); 65 while(WiFi.status() 66 != WL_CONNECTED) 67 { 68 delay(200); 69 Serial.print(".."); 70 } 71 72 Serial.println(); 73 Serial.println("NodeMCU is connected!"); 74 Serial.println(WiFi.localIP()); 75 76 //DHT.begin(); 77 ThingSpeak.begin(client); 78 79 /* Set the callback function 80 to get the sending results */ 81 smtp.callback(smtpCallback); 82 83 /* Declare 84 the email session config data */ 85 ESP_Mail_Session session; 86 87 /* Set 88 the session config */ 89 session.server.host_name = SMTP_HOST; 90 session.server.port 91 = SMTP_PORT; 92 session.login.email = AUTHOR_EMAIL; 93 session.login.password 94 = AUTHOR_PASSWORD; 95 session.login.user_domain = ""; 96 97 /* Set the message 98 headers */ 99 message.sender.name = "ESP"; 100 message.sender.email = AUTHOR_EMAIL; 101 102 message.subject = "ESP Alert Email"; 103 message.addRecipient("Vasileios", 104 RECIPIENT_EMAIL); 105 106 /* Connect to server with the session config */ 107 if 108 (!smtp.connect(&session)) 109 return; 110} 111 112void loop() { 113 114 int 115 chk = DHT.read11(DHT11_PIN); 116 float h = DHT.humidity; 117 float t = DHT.temperature; 118 119 120 if(t!=20){ 121 /*Defining the HTML message of the email*/ 122 String 123 htmlMsg = "<div style=\\"color:#2f4468;\\"><h1>Hello World!</h1><p>- Sent from 124 ESP board: Temperature needs caution:!</p></div>" + (String)t + "° C"; 125 message.html.content 126 = htmlMsg.c_str(); 127 message.html.content = htmlMsg.c_str(); 128 message.text.charSet 129 = "us-ascii"; 130 message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit; 131 132 133 /* Start sending Email and close the session */ 134 if (!MailClient.sendMail(&smtp, 135 &message)) 136 Serial.println("Error sending Email, " + smtp.errorReason()); 137 138 } 139 140 Serial.println("Temperature: " + (String) t); 141 Serial.println("Humidity: 142 " + (String) h); 143 144 ThingSpeak.setField(1, t); 145 ThingSpeak.setField(2, 146 h); 147 int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 148 if(x 149 == 200){ 150 Serial.println("Channel update successful."); 151 } 152 else{ 153 154 Serial.println("Problem updating channel. HTTP error code " + String(x)); 155 156 } 157 delay(20000); 158} 159 160/* Callback function to get the Email sending 161 status */ 162void smtpCallback(SMTP_Status status){ 163 /* Print the current status 164 */ 165 Serial.println(status.info()); 166 167 /* Print the sending result */ 168 169 if (status.success()){ 170 Serial.println("----------------"); 171 ESP_MAIL_PRINTF("Message 172 sent success: %d\ 173", status.completedCount()); 174 ESP_MAIL_PRINTF("Message 175 sent failed: %d\ 176", status.failedCount()); 177 Serial.println("----------------\ 178"); 179 180 struct tm dt; 181 182 for (size_t i = 0; i < smtp.sendingResult.size(); i++){ 183 184 /* Get the result item */ 185 SMTP_Result result = smtp.sendingResult.getItem(i); 186 187 time_t ts = (time_t)result.timestamp; 188 localtime_r(&ts, &dt); 189 190 //printing info regarding the email sent 191 ESP_MAIL_PRINTF("Message 192 No: %d\ 193", i + 1); 194 ESP_MAIL_PRINTF("Status: %s\ 195", result.completed 196 ? "success" : "failed"); 197 ESP_MAIL_PRINTF("Date/Time: %d/%d/%d %d:%d:%d\ 198", 199 dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); 200 201 ESP_MAIL_PRINTF("Recipient: %s\ 202", result.recipients); 203 ESP_MAIL_PRINTF("Subject: 204 %s\ 205", result.subject); 206 } 207 Serial.println("----------------\ 208"); 209 210 } 211}
GPS Tracking with NodeMCU and MATLAB Visualization
matlab
In ThingSpeak we can create Map Visualizations in order to track the location of a cargo at a specific time. We create MATLAB Visualization and through code we can choose which variables to show(e.g. Temperature), including the Latitude and Longitude variables for printing a mark on the map. For easy testing we could send data manually to our ThingSpeak variables through the browser: https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=?
1% GPS Tracking with NodeMCU and MATLAB Visualization 2%In ThingSpeak 3 we can create Map Visualizations in order to track the location of a cargo at a 4 specific time. We create MATLAB Visualization and through code we can choose which 5 variables to show(e.g. Temperature), including the Latitude and Longitude variables 6 for printing a mark on the map. 7%For easy testing we could send data manually 8 to our ThingSpeak variables through the browser: (in my case field4 was the longitude, 9 field3 the latitude and field1 the temperature) 10%https://api.thingspeak.com/update?api_key=yourWriteAPIKey&field4=?&field3=?&field1=? 11 12% 13 the <'NumPoints',3> indicates that the last 3 values will be shown 14lat = thingSpeakRead(yourChannelID,'Fields',3,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 15lon 16 = thingSpeakRead(yourChannelID,'Fields',4,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 17temp 18 = thingSpeakRead(yourChannelID,'Fields',1,'ReadKey','yourReadKey','NumPoints',3,'Timeout',50); 19%red 20 indicated the color of marks and filled indicates how the mark will look like 21geoscatter(lat,lon,temp,'red','filled'); 22geobasemap('streets');
Connecting NodeMcu to WiFi and sending data to the Cloud.
arduino
This code first connects NodeMcu to WiFi and then sends the data to the ThingSpeak page. At https://thingspeak.com/ we can create a Channel that will have its own fields, graphs and API keys so we can do our own updates. So in the code we need to add the WiFi name and code as well as the Channel ID and the Write API Key of ThingSpeak. Finally, we upload the code to NodeMcu by selecting "NodeMCU 1.0 (ESP-12E Module)" as a board tool.
1//Connecting NodeMcu to WiFi and sending data to the Cloud 2//This code 3 first connects NodeMcu to WiFi and then sends the data to the ThingSpeak page. At 4 https://thingspeak.com/ we can create a Channel that will have its own fields, graphs 5 //and API keys so we can do our own updates. So, in the code we need to add the 6 WiFi name and code as well as the Channel ID and the Write API Key of ThingSpeak. 7 Finally, we upload //the code to NodeMcu by selecting "NodeMCU 1.0 (ESP-12E Module)" 8 as a board tool. 9 10#include <ESP8266WiFi.h> 11#include <dht.h> 12#include 13 <ThingSpeak.h> 14 15dht DHT; 16#define DHT11_PIN D4 17/* creating object for 18 connecting on WiFi and variables for Channel ID and Write API Key in ThingSpeak 19 */ 20WiFiClient client; 21 22long myChannelNumber = yourChannelID; 23const char 24 myWriteAPIKey[] = "yourAPIKey"; 25 26void setup() { 27 Serial.begin(9600); 28 29 //connect on WiFi 30 WiFi.begin("Your SSID", "Your Pass"); 31 while(WiFi.status() 32 != WL_CONNECTED) 33 { 34 delay(200); 35 Serial.print(".."); 36 } 37 38 Serial.println(); 39 Serial.println("NodeMCU is connected!"); 40 Serial.println(WiFi.localIP()); 41 42 //Start ThingSpeak with WiFi credentials 43 ThingSpeak.begin(client); 44} 45 46void 47 loop() { 48 int chk = DHT.read11(DHT11_PIN); 49 float h = DHT.humidity; 50 51 float t = DHT.temperature; 52 Serial.println("Temperature: " + (String) t); 53 54 Serial.println("Humidity: " + (String) h); 55 /*sending temperature and humidity 56 values to field1 and field2 of ThingSpeak*/ 57 ThingSpeak.setField(1, t); 58 59 ThingSpeak.setField(2, h); 60 int x = ThingSpeak.writeFields(myChannelNumber, 61 myWriteAPIKey); 62 if(x == 200){ 63 Serial.println("Channel update successful."); 64 65 } 66 else{ 67 Serial.println("Problem updating channel. HTTP error code 68 " + String(x)); 69 } 70 delay(20000); 71}
Downloadable files
A closer look to the connections.
A closer look to the connections.

Circuit Diagram
Some hardware is shown different in the image from what I used. For example my DHT11 Sensor has three nodes instead of four like in the image and the NodeMCU I used is different than the "Photon" board in the image. But the connections are the same, just in a different position. You can easily just connect the wires in your corresponding Vcc or GND. What's more, you don't need to make all the connections of the NodeMCU like I did because the DHT11 sensor already is connected to GND and Vcc. I did all the three connections needed though, in case someone wanted to use the DHT11 sensor without sending the values to the Arduino board. I provide more explanations in the code section.
Circuit Diagram

A closer look to the connections.
A closer look to the connections.

A closer look to the connections.
A closer look to the connections.

A closer look to the connections.
A closer look to the connections.

A closer look to the connections.
A closer look to the connections.

Circuit Diagram
Some hardware is shown different in the image from what I used. For example my DHT11 Sensor has three nodes instead of four like in the image and the NodeMCU I used is different than the "Photon" board in the image. But the connections are the same, just in a different position. You can easily just connect the wires in your corresponding Vcc or GND. What's more, you don't need to make all the connections of the NodeMCU like I did because the DHT11 sensor already is connected to GND and Vcc. I did all the three connections needed though, in case someone wanted to use the DHT11 sensor without sending the values to the Arduino board. I provide more explanations in the code section.
Circuit Diagram

A closer look to the connections.
A closer look to the connections.

Comments
Only logged in users can leave comments