-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266-OWM-i2c.ino
155 lines (131 loc) · 3.97 KB
/
esp8266-OWM-i2c.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
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // If it doesn't work change "0x27" with another address according to your i2c lcd.
const char* ssid = "your_ssid"; // SSID of local network
const char* password = "your_password"; // Password on network
String APIKEY = "your_openweathermap_api_key";
String CityID = "746881"; //Your city id. search your city in OpenWeatherMap.org and just copy the numbers from URL to find your cityID
WiFiClient client;
char servername[]="api.openweathermap.org"; // remote server we will connect to
String result;
int counter = 30; // refresh every 5 min.
String weatherDescription ="";
String weatherLocation = "";
String Country;
float Temperature;
float Humidity;
float Pressure;
void setup() {
Serial.begin(115200);
int cursorPosition=0;
lcd.begin();
lcd.backlight();
lcd.print(" Connecting");
Serial.println("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
lcd.setCursor(cursorPosition,2);
lcd.print(".");
cursorPosition++;
}
lcd.clear();
lcd.print(" Connected!");
Serial.println("Connected");
delay(1000);
}
void loop() {
if(counter == 30) //Get new data every 10 minutes
{
counter = 0;
displayGettingData();
delay(1000);
getWeatherData();
}else
{
counter++;
displayWeather(weatherLocation,weatherDescription);
delay(5000);
displayConditions(Temperature,Humidity,Pressure);
delay(5000);
}
}
void getWeatherData() //client function to send/receive GET request data.
{
if (client.connect(servername, 80)) { //starts client connection, checks for connection
client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
client.stop(); //stop client
result.replace('[', ' ');
result.replace(']', ' ');
Serial.println(result);
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';
StaticJsonBuffer<1024> json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
String location = root["name"];
String country = root["sys"]["country"];
float temperature = root["main"]["temp"];
float humidity = root["main"]["humidity"];
String weather = root["weather"]["main"];
String description = root["weather"]["description"];
float pressure = root["main"]["pressure"];
weatherDescription = description;
weatherLocation = location;
Country = country;
Temperature = temperature;
Humidity = humidity;
Pressure = pressure;
}
void displayWeather(String location,String description)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(location);
lcd.print(", ");
lcd.print(Country);
lcd.setCursor(0,1);
lcd.print(description);
}
void displayConditions(float Temperature,float Humidity, float Pressure)
{
lcd.clear();
lcd.print("S: ");
lcd.print(Temperature,1);
lcd.print((char)223);
lcd.print("C ");
//Printing Humidity
lcd.print(" H:");
lcd.print(Humidity,0);
lcd.print(" %");
//Printing Pressure
lcd.setCursor(0,1);
lcd.print("P: ");
lcd.print(Pressure,1);
lcd.print(" hPa");
}
void displayGettingData()
{
lcd.clear();
lcd.print("Getting Data");
}