-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp-and-lcd-dht22-connect.ino
181 lines (135 loc) · 3.56 KB
/
esp-and-lcd-dht22-connect.ino
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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include "MQ7.h"
MQ7 mq7(A0,5.0);
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
#define DHTPIN 2
#define DHTTYPE DHT22
String AP = "Arjuna"; // CHANGE ME
String PASS = "arjuna12345"; // CHANGE ME
String API = "ELIB1XW18XYTXE2W"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
String sensorDataString = "";
String COvalue = "";
//dht sensor
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial esp8266(RX,TX);
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
displayText("Hello from Air!","Connecting..wifi",true);
Serial.begin(9600);
dhtSetup();
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void mq7read(){
// Serial.println("CO value");
// Serial.println(mq7.getPPM());
COvalue = (String)mq7.getPPM();
delay(2000);
}
void dhtSetup(){
Serial.println("DHTxx test!");
dht.begin();
}
void dhtRead(){
delay(2000); // 2 seconds to load
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
displayText("RH:" + (String)h + "%" + ",T:" + (String)t + " C", "CO:"+ COvalue+" ppm",true);
sensorDataString = getSensorData((String)h,(String)t,COvalue);
Serial.print(sensorDataString);
delay(1000);
}
void displayText(String row1, String row2, boolean backlightStatus){
lcd.init();
if(backlightStatus == false){
lcd.noBacklight();
}
else{
lcd.backlight();
}
lcd.backlight();
lcd.print(row1);
lcd.setCursor(0,1);
lcd.print(row2);
}
void loop()
{
syncSensorData();
mq7read();
dhtRead();
//delay(15000);
}
String getSensorData(String val1,String val2,String val3){
String x = "field1="+val1+"&" + "field2="+val2 + "&" + "field3="+val3;
return x;
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
//displayText("Server connected","Data sent", true);
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
//displayText("Server not connected","Data fail", true);
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
//function to sync data with server
void syncSensorData(){
String getData = "GET /update?api_key="+ API +"&"+ sensorDataString;
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);
delay(1500);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}