Close

CO2 Data fetch

A project log for ClimateChange IoT AwarenessHelper

The target of this project is to raise awareness about climate change.

h4ltsysh4ltsys 03/21/2018 at 21:100 Comments

the first step of the project is to obtained the co2 level data. To do this, I choose to use the HTTPThing app from thingspeak, that allow to fetch data from every site and make it disponible trough an API.

The MCU that i choose to use is the well know ESP8266. Thanks to the ESP8266HttpClient library i can simply send a GET request to the thinghttp link and receive the daily data from www.co2.earth.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
 
const char* ssid = "MYWIFI";
const char* password = "*******";
 
void setup () {
 
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
 
    delay(1000);
    Serial.println("Connecting..");
 
  }
 
}
 
void loop() {
 
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
    HTTPClient http;  //Declare an object of class HTTPClient
 
    http.begin("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=YAOX15VANMLMYH4S");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
 
    if (httpCode > 0) { //Check the returning code
 
      String payload = http.getString(); //Get the request response payload
      String todaydate = payload.substring(62,76);
      String todayco2 = payload.substring(118,128);
      Serial.print(todaydate);
      Serial.print(" CO2 Level: "); //Print the response payload
      Serial.println(todayco2);
      httpCode == 0;
      
 
    }

    http.begin("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=3TKGGW5Z5CHH7G0S");  //Specify request destination
    httpCode = http.GET();                                                                  //Send the request
 
    if (httpCode > 0) { //Check the returning code
 
      String payload = http.getString();
          String agodate = payload.substring(36,50);
      String agoco2 = payload.substring(92,99);//Get the request response payload
         Serial.print(agodate);
      Serial.print(" CO2 Level: "); //Print the response payload
      Serial.println(agoco2);                    //Print the response payload
 
    }
 
    http.end();   //Close connection
 
  }
 
  delay(30000);    //Send a request every 30 seconds
 
}

However co2.earth only maintain data histories about last year. So for the decade CO2 data i will use  a text file downloaded from noaa server that contain world CO2 data from start of 20th century.

Discussions