Lab_interaccio/2016/weatherlamp/timetemp/timetemp.ino

212 lines
4.7 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
#include <stdlib.h>
#include <ArduinoJson.h>
//____________
//int rgb[3] = {255,255,255}; // RELLENAR COLOR FRIO
//____
#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_NAME "Barcelona,es" // location
#define LOCATION_LL "41.3871/2.1685" // location latitude/longitude/
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");
delay(1000);
// 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()
{
int h = getTime();
Serial.print("time = "); Serial.println(h);
//for(int i=0;i<20;i++){Serial.print(i);delay(1000);}
int t = getTemp();
Serial.print("temp = "); Serial.println(t);
for(int i=0;i<20;i++){Serial.print(i);delay(1000);}
}
int getTime(){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += TimeWS_IP;
cmd += "\",80";
Serial1.println(cmd);
Serial.println(cmd);
if (Serial1.find("Error"))
return -1;
cmd = "GET /timezone/";
cmd += LOCATION_LL;
cmd += " HTTP/1.0\r\nHost: www.earthtools.org\r\n\r\n";
Serial1.print("AT+CIPSEND=");
Serial1.println(cmd.length());
if (Serial1.find(">")) {
Serial.println(">");
} else {
Serial1.println("AT+CIPCLOSE");
Serial.println("connection timeout");
delay(1000);
return -1;
}
Serial1.print(cmd);
unsigned int i = 0; //timeout counter
//char json[100] = "{";
while (!Serial1.find("<localtime>")) {} // find the part we are interested in.
while (!Serial1.find("2015 ")) {} // find the part we are interested in.
char h[2];
Serial1.readBytes(h, 2);
int hour = atoi(h);
//Serial.println(hour);
// flush
while(Serial1.available()){
char c = Serial1.read();
}
return hour;
}
int getTemp(){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += WeatherWS_IP;
cmd += "\",80";
Serial1.println(cmd);
Serial.println(cmd);
if (Serial1.find("Error")){
Serial.println("error");
return -1;
}
cmd = "GET /data/2.5/weather?q=";
cmd += LOCATION_NAME;
cmd += "&mode=xml";
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.println(">");
} else {
Serial1.println("AT+CIPCLOSE");
Serial.println("connection timeout");
//delay(1000);
return -1;
}
Serial1.println(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 -1;
// }
//
// double temp = root["temp"];
// temp -= 273.15;
int temperature = -111;
while (!Serial1.find("<temperature value=")) {} // find the part we are interested in.
//Serial.println("seguimos");
//char c0 = Serial1.read();
//char c1 = Serial1.read();
//Serial.print("c = "); Serial.println(c);
char space[1];
Serial1.readBytes(space, 1);
char temp[3];
Serial1.readBytes(temp, 3);
Serial.print("read temp = "); Serial.print(temp[0]); Serial.print(temp[1]); Serial.print(temp[2]); //Serial.println(temp[3]);
temperature = atoi(temp)-273;
//Serial.println(hour);
// flush
while(Serial1.available()){
Serial.print(Serial1.read());
}
//Serial.println(temp,6);
return temperature;
}
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;
}
}