Components and supplies
Arduino UNO
Jumper wires (generic)
DHT11 Temperature & Humidity Sensor (3 pins)
SparkFun Breadboard Power Supply 5V/3.3V
ESP8266 ESP-01
Breadboard (generic)
Apps and platforms
Windows 10
ThingSpeak API
Arduino IDE
Project description
Code
CODE
arduino
1String myAPIkey = "Your Write API key here"; 2 3#include <SoftwareSerial.h> 4#include <DHT.h>; 5SoftwareSerial ESP8266(2, 3); // Rx, Tx 6 7#define DHTTYPE DHT11 8#define DHTPIN A0 9 10DHT dht(DHTPIN, DHTTYPE,11); 11float humidity, temp_f; 12long writingTimer = 17; 13long startTime = 0; 14long waitTime = 0; 15 16 17boolean relay1_st = false; 18boolean relay2_st = false; 19unsigned char check_connection=0; 20unsigned char times_check=0; 21boolean error; 22 23void setup() 24{ 25 Serial.begin(9600); 26 ESP8266.begin(9600); 27 dht.begin(); 28 startTime = millis(); 29 ESP8266.println("AT+RST"); 30 delay(2000); 31 Serial.println("Connecting to Wifi"); 32 while(check_connection==0) 33 { 34 Serial.print("."); 35 ESP8266.print("AT+CWJAP=\\"Wifi Network's name\\",\\"PassWord\\"\ \ 36"); 37 ESP8266.setTimeout(5000); 38 if(ESP8266.find("WIFI CONNECTED\ \ 39")==1) 40 { 41 Serial.println("WIFI CONNECTED"); 42 break; 43 } 44 times_check++; 45 if(times_check>3) 46 { 47 times_check=0; 48 Serial.println("Trying to Reconnect.."); 49 } 50 } 51} 52 53void loop() 54{ 55 waitTime = millis()-startTime; 56 if (waitTime > (writingTimer*1000)) 57 { 58 readSensors(); 59 writeThingSpeak(); 60 startTime = millis(); 61 } 62} 63 64 65void readSensors(void) 66{ 67 temp_f = dht.readTemperature(); 68 humidity = dht.readHumidity(); 69} 70 71 72void writeThingSpeak(void) 73{ 74 startThingSpeakCmd(); 75 // preparacao da string GET 76 String getStr = "GET /update?api_key="; 77 getStr += myAPIkey; 78 getStr +="&field1="; 79 getStr += String(temp_f); 80 getStr +="&field2="; 81 getStr += String(humidity); 82 getStr += "\ \ 83\ \ 84"; 85 GetThingspeakcmd(getStr); 86} 87 88void startThingSpeakCmd(void) 89{ 90 ESP8266.flush(); 91 String cmd = "AT+CIPSTART=\\"TCP\\",\\""; 92 cmd += "184.106.153.149"; // api.thingspeak.com IP address 93 cmd += "\\",80"; 94 ESP8266.println(cmd); 95 Serial.print("Start Commands: "); 96 Serial.println(cmd); 97 98 if(ESP8266.find("Error")) 99 { 100 Serial.println("AT+CIPSTART error"); 101 return; 102 } 103} 104 105String GetThingspeakcmd(String getStr) 106{ 107 String cmd = "AT+CIPSEND="; 108 cmd += String(getStr.length()); 109 ESP8266.println(cmd); 110 Serial.println(cmd); 111 112 if(ESP8266.find(">")) 113 { 114 ESP8266.print(getStr); 115 Serial.println(getStr); 116 delay(500); 117 String messageBody = ""; 118 while (ESP8266.available()) 119 { 120 String line = ESP8266.readStringUntil('\n'); 121 if (line.length() == 1) 122 { 123 messageBody = ESP8266.readStringUntil('\n'); 124 } 125 } 126 Serial.print("MessageBody received: "); 127 Serial.println(messageBody); 128 return messageBody; 129 } 130 else 131 { 132 ESP8266.println("AT+CIPCLOSE"); 133 Serial.println("AT+CIPCLOSE"); 134 } 135}
CODE
arduino
1String myAPIkey = "Your Write API key here"; 2 3#include <SoftwareSerial.h> 4#include <DHT.h>; 5SoftwareSerial ESP8266(2, 3); // Rx, Tx 6 7#define DHTTYPE DHT11 8#define DHTPIN A0 9 10DHT dht(DHTPIN, DHTTYPE,11); 11float humidity, temp_f; 12long writingTimer = 17; 13long startTime = 0; 14long waitTime = 0; 15 16 17boolean relay1_st = false; 18boolean relay2_st = false; 19unsigned char check_connection=0; 20unsigned char times_check=0; 21boolean error; 22 23void setup() 24{ 25 Serial.begin(9600); 26 ESP8266.begin(9600); 27 dht.begin(); 28 startTime = millis(); 29 ESP8266.println("AT+RST"); 30 delay(2000); 31 Serial.println("Connecting to Wifi"); 32 while(check_connection==0) 33 { 34 Serial.print("."); 35 ESP8266.print("AT+CWJAP=\\"Wifi Network's name\\",\\"PassWord\\"\ \ 36"); 37 ESP8266.setTimeout(5000); 38 if(ESP8266.find("WIFI CONNECTED\ \ 39")==1) 40 { 41 Serial.println("WIFI CONNECTED"); 42 break; 43 } 44 times_check++; 45 if(times_check>3) 46 { 47 times_check=0; 48 Serial.println("Trying to Reconnect.."); 49 } 50 } 51} 52 53void loop() 54{ 55 waitTime = millis()-startTime; 56 if (waitTime > (writingTimer*1000)) 57 { 58 readSensors(); 59 writeThingSpeak(); 60 startTime = millis(); 61 } 62} 63 64 65void readSensors(void) 66{ 67 temp_f = dht.readTemperature(); 68 humidity = dht.readHumidity(); 69} 70 71 72void writeThingSpeak(void) 73{ 74 startThingSpeakCmd(); 75 // preparacao da string GET 76 String getStr = "GET /update?api_key="; 77 getStr += myAPIkey; 78 getStr +="&field1="; 79 getStr += String(temp_f); 80 getStr +="&field2="; 81 getStr += String(humidity); 82 getStr += "\ \ 83\ \ 84"; 85 GetThingspeakcmd(getStr); 86} 87 88void startThingSpeakCmd(void) 89{ 90 ESP8266.flush(); 91 String cmd = "AT+CIPSTART=\\"TCP\\",\\""; 92 cmd += "184.106.153.149"; // api.thingspeak.com IP address 93 cmd += "\\",80"; 94 ESP8266.println(cmd); 95 Serial.print("Start Commands: "); 96 Serial.println(cmd); 97 98 if(ESP8266.find("Error")) 99 { 100 Serial.println("AT+CIPSTART error"); 101 return; 102 } 103} 104 105String GetThingspeakcmd(String getStr) 106{ 107 String cmd = "AT+CIPSEND="; 108 cmd += String(getStr.length()); 109 ESP8266.println(cmd); 110 Serial.println(cmd); 111 112 if(ESP8266.find(">")) 113 { 114 ESP8266.print(getStr); 115 Serial.println(getStr); 116 delay(500); 117 String messageBody = ""; 118 while (ESP8266.available()) 119 { 120 String line = ESP8266.readStringUntil('\ 121'); 122 if (line.length() == 1) 123 { 124 messageBody = ESP8266.readStringUntil('\ 125'); 126 } 127 } 128 Serial.print("MessageBody received: "); 129 Serial.println(messageBody); 130 return messageBody; 131 } 132 else 133 { 134 ESP8266.println("AT+CIPCLOSE"); 135 Serial.println("AT+CIPCLOSE"); 136 } 137}
Downloadable files
Code
https://github.com/imalwaysontheinternet/Weather-Stasion
Code
https://github.com/imalwaysontheinternet/Weather-Stasion
Circuit
Circuit
Comments
Only logged in users can leave comments
neverofftheinternet
0 Followers
•0 Projects
Table of contents
Intro
10
0