122 lines
2.6 KiB
Arduino
122 lines
2.6 KiB
Arduino
|
#include <stdlib.h>
|
||
|
#include <ArduinoJson.h>
|
||
|
|
||
|
#define SSID "hangar_polivalent" // insert your SSID
|
||
|
#define PASS "p0l1v4l3nc14s" // insert your password
|
||
|
|
||
|
#define WeatherWS_IP "188.226.224.148" //api.openweathermap.org
|
||
|
#define TimeWS_IP "208.113.226.171" //earthtools.org
|
||
|
|
||
|
#define LOCATION "Barcelona" // location
|
||
|
#define LOCATION_LL "41.3871/2.1685" // location longitude/latitude
|
||
|
|
||
|
StaticJsonBuffer<200> jsonBuffer;
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
Serial1.begin(9600);
|
||
|
Serial1.setTimeout(5000);
|
||
|
Serial.begin(9600); // for debuging
|
||
|
while (!Serial) {
|
||
|
; // wait for serial port to connect. Needed for Leonardo only
|
||
|
}
|
||
|
Serial.println("Init");
|
||
|
|
||
|
do{
|
||
|
Serial1.println("AT+RST");
|
||
|
Serial.println("WiFi - Module is missing");
|
||
|
delay(100);
|
||
|
}
|
||
|
while(!Serial1.find("ready"));
|
||
|
Serial.println("WiFi - Module is ready");
|
||
|
|
||
|
// try to connect to wifi
|
||
|
boolean connected = false;
|
||
|
for (int i = 0; i < 5; i++) {
|
||
|
if (connectWiFi()) {
|
||
|
connected = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (!connected) {
|
||
|
while (1);
|
||
|
}
|
||
|
delay(5000);
|
||
|
Serial1.println("AT+CIPMUX=0"); // set to single connection mode
|
||
|
}
|
||
|
void loop()
|
||
|
{
|
||
|
String cmd = "AT+CIPSTART=\"TCP\",\"";
|
||
|
cmd += WeatherWS_IP;
|
||
|
cmd += "\",80";
|
||
|
Serial1.println(cmd);
|
||
|
Serial.println(cmd);
|
||
|
|
||
|
if (Serial1.find("Error"))
|
||
|
return;
|
||
|
|
||
|
cmd = "GET /data/2.5/weather?q=";
|
||
|
cmd += LOCATION;
|
||
|
cmd += " HTTP/1.0\r\nHost: api.openweathermap.org\r\n\r\n";
|
||
|
|
||
|
Serial1.print("AT+CIPSEND=");
|
||
|
Serial1.println(cmd.length());
|
||
|
|
||
|
if (Serial1.find(">")) {
|
||
|
Serial.print(">");
|
||
|
} else {
|
||
|
Serial1.println("AT+CIPCLOSE");
|
||
|
Serial.println("connection timeout");
|
||
|
delay(1000);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Serial1.print(cmd);
|
||
|
unsigned int i = 0; //timeout counter
|
||
|
int n = 1; // char counter
|
||
|
char json[100] = "{";
|
||
|
while (!Serial1.find("\"main\":{")) {} // find the part we are interested in.
|
||
|
while (i < 60000) {
|
||
|
if (Serial1.available()) {
|
||
|
char c = Serial1.read();
|
||
|
json[n] = c;
|
||
|
if (c == '}') break;
|
||
|
n++;
|
||
|
i = 0;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
Serial.println(json);
|
||
|
JsonObject& root = jsonBuffer.parseObject(json);
|
||
|
if (!root.success()) {
|
||
|
Serial.println("parseObject() failed");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
double temp = root["temp"];
|
||
|
temp -= 273.15;
|
||
|
|
||
|
Serial.println(temp,6);
|
||
|
|
||
|
}
|
||
|
|
||
|
boolean connectWiFi()
|
||
|
{
|
||
|
Serial1.println("AT+CWMODE=1");
|
||
|
String cmd = "AT+CWJAP=\"";
|
||
|
cmd += SSID;
|
||
|
cmd += "\",\"";
|
||
|
cmd += PASS;
|
||
|
cmd += "\"";
|
||
|
Serial.println(cmd);
|
||
|
Serial1.println(cmd);
|
||
|
delay(2000);
|
||
|
if (Serial1.find("OK")) {
|
||
|
Serial.println("OK, Connected to WiFi.");
|
||
|
return true;
|
||
|
} else {
|
||
|
Serial.println("Can not connect to the WiFi.");
|
||
|
return false;
|
||
|
}
|
||
|
}
|