@marcus you got it all
include "HttpClient/HttpClient.h"
iinclude "LiquidCrystal/LiquidCrystal.h"
String token = "--------------------------------------";
//button
int button = D6;
//LEDs
int RedLED = A3;
int GreenLED = A5;
int BlueLED = A6;
//parameters
int AQI;
String num1;
//libraries
////LCD
LiquidCrystal lcd(D0, D1, D2, D3, D4, D5);
////HttpClient
HttpClient http;
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL }
};
http_request_t request;
http_response_t response;
//String search_on = String("\"breezometer_aqi\": ");//To check if a switch is pressed or not
void setup() {
Serial.begin(9600);
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Hello!");
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(3, 0);
//lcd.print("Hello, Itay!");
lcd.print("Breezometer");
lcd.setCursor(3, 1);
lcd.print("AQI Station");
//PINs SET
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
pinMode(button, INPUT_PULLDOWN);
}
void loop() {
//delay(5*1000); //cant establish http get after deep sleep or power on
if (!digitalRead(button)) AQI_Check();
//delay(20*1000);
}
void AQI_Check(){
//reset the LEDS
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, LOW);
digitalWrite(BlueLED, LOW);
Serial.println("Asking Breezometer about the air pollution NOW ");
Serial.println(AQI);
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print("Breezometer AQI:");
//Serial.println("Application>\tStart of Loop.");
request.hostname = "api-beta.breezometer.com";
request.port = 80;
request.path = "/baqi/?location=Tel+Aviv-Yafo&key=" + token ;
// Get request
http.get(request, response, headers);
String raw_response(response.body);
////////changing the AQI in the response msg to int//////
String statusCode = raw_response.substring(43,45); //the place of the number.
char tensC = raw_response.charAt(43+3); ///FIX the change in answer from USA(3) to Israel(6)
char onesC = raw_response.charAt(44+3); ///FIX the change in answer from USA(3) to Israel(6)
String color = raw_response.substring(43+3+27,44+3+27+5);
Serial.println(color);
String Tcolor;
int Rcolor;
int Gcolor;
int Bcolor;
int temp;
char C_color;
C_color = color[0];
Rcolor = hexToDec(C_color,1);
C_color = color[1];
Rcolor = (hexToDec(C_color,0)) + Rcolor;
Serial.println(Rcolor);
C_color = color[2];
Gcolor = hexToDec(C_color,1);
C_color = color[3];
Gcolor = (hexToDec(C_color,0)) + Gcolor;
Serial.println(Gcolor);
C_color = color[4];
Bcolor = hexToDec(C_color,1);
C_color = color[5];
Bcolor = (hexToDec(C_color,0)) + Bcolor;
Serial.println(Bcolor);
//String num2c = raw_response.substring(44,45);
int tensI = tensC - '0';
int onesI = onesC - '0';
int AQI = tensI*10+onesI;
//int Statei = atoi(State);
//Serial.print("INT:");
/////Write in Serial and LCD
Serial.println("Breezometer AQI:");
Serial.println(AQI);
lcd.clear();
delay(500);
lcd.setCursor(0, 0);
lcd.print("Breezometer AQI:");
if (response.status==200){ //if the status is good write to the LCD
Serial.println("response.status is GOOD");
lcd.setCursor(7, 1);
Serial.println(statusCode);
lcd.print(AQI);
}
else Serial.println("response.status is NOT good");
analogWrite(RedLED, Rcolor);
analogWrite(GreenLED, Gcolor);
analogWrite(BlueLED, Bcolor);
///Trun ON the RGB LIGHT accordingly to the air pollution
if (AQI>70){
delay(3*1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(AQI);
lcd.setCursor(0, 1);
lcd.print("Time To Go Out!");
}
else if (AQI>50){
delay(3*1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(AQI);
lcd.setCursor(3, 1);
lcd.print("Comsi Comsa");
}
else {
delay(3*1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(AQI);
lcd.setCursor(0, 1);
lcd.print("Stay Home...");
}
//Serial.print("Application>\tResponse status: ");
//Serial.println(response.status);
//Serial.print("Application>\tHTTP Response Body: ");
//Serial.println(response.body);
}
int hexToDec(char hexString,int x) {
int decValue = 0;
int tempvalue =0;
if ((hexString != 'A') && (hexString != 'B') && (hexString != 'C') && (hexString != 'D') && (hexString != 'E') && (hexString != 'F')){
int tempvalue = hexString - '0';
if (x==1){
decValue=tempvalue *16;
} else {
decValue=tempvalue + decValue;
}
}
else {
if (hexString == 'A'){
tempvalue=10; }
if (hexString == 'B'){
tempvalue=11; }
if (hexString == 'C'){
tempvalue=12; }
if (hexString == 'D'){
tempvalue=13; }
if (hexString == 'E'){
tempvalue=14; }
if (hexString == 'F'){
tempvalue=15; }
if (x==1){
decValue=tempvalue *16;
} else {
decValue=tempvalue + decValue;
}
}
return decValue;
}
Iāve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy