202 lines
5 KiB
C++
202 lines
5 KiB
C++
#include <stdlib.h>
|
|
#include "Adafruit_NeoPixel.h" // https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library
|
|
|
|
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
|
|
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
|
|
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
|
|
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
|
|
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, 9, NEO_GRB + NEO_KHZ800);
|
|
|
|
// C O N F I G ------------------------------------------------
|
|
//-------------------------------------------------------------
|
|
int cold[3] = {255,0,0};
|
|
int warm[3] = {0,0,255};
|
|
|
|
int mint = 5;
|
|
int maxt = 25;
|
|
|
|
#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/
|
|
|
|
void setup(){
|
|
strip.begin();
|
|
strip.show(); // Initialize all pixels to 'off'
|
|
|
|
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);}
|
|
|
|
setColor(map(t, mint, maxt, cold[0], warm[0]), map(t, mint, maxt, cold[1], warm[1]), map(t, mint, maxt, cold[0], warm[0]));
|
|
}
|
|
|
|
void setColor(int r, int g, int b){
|
|
for(int i=0; i<strip.numPixels(); i++) {
|
|
strip.setPixelColor(i, r, g, b);
|
|
}
|
|
strip.show();
|
|
}
|
|
|
|
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);
|
|
|
|
int temperature = -111;
|
|
|
|
while (!Serial1.find("<temperature value=")) {} // find the part we are interested in.
|
|
|
|
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;
|
|
}
|
|
}
|