ESP8266 Home Automation Projects - Errata

After the ESP8266 Home Automation Projects book was published, the wunderground.com service is not offering free services anymore, so the code presented in the Getting data from the internet part need to be reviewed.

There are two options in here: either you are subscribing to the wunderground.com service, paying a monthly subscription or you can choose other provider like OpenWeatherMap.org

Go to openweathermap.org an create an account. After login you will find your own API Key that will be used later in the code.

Now the code for getting current data is:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const String OPENWEATHERMAP_API_KEY = "YOUR_OPENWEATHERMAP_API_KEY";
const String OPENWEATHERMAP_COUNTRY = "us";
const String OPENWEATHERMAP_CITY = "New York";
const String dataURL = "http://api.openweathermap.org/data/2.5/weather?q="+OPENWEATHERMAP_CITY+","+OPENWEATHERMAP_COUNTRY+"&APPID="+OPENWEATHERMAP_API_KEY;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(dataURL);

  if(WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;
    http.begin(dataURL);
    int httpCode = http.GET();
    if(httpCode> 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
}

To check if the formed link is working, get the output from:

Serial.println(dataURL);

and check it in a browser. You should see the response in JSON format. I encourage you to test the links first in browser and later on the ESP8266.


For more information on how to get data you can access the link: https://openweathermap.org/current

At this time the free account has:

OpenWeatherMap Free Account



If you need to get the data every minute you can use the following code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const String OPENWEATHERMAP_API_KEY = "YOUR_OPENWEATHERMAP_API_KEY";
const String OPENWEATHERMAP_COUNTRY = "us";
const String OPENWEATHERMAP_CITY = "New York";
const String dataURL = "http://api.openweathermap.org/data/2.5/weather?q="+OPENWEATHERMAP_CITY+","+OPENWEATHERMAP_COUNTRY+"&APPID="+OPENWEATHERMAP_API_KEY;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(dataURL);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;
    http.begin(dataURL);
    int httpCode = http.GET();
    if(httpCode> 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    }

  }
delay(10*6000)
}

To make use of the results you will need to parse the JSON response and extract the data you need.



0 Response to "ESP8266 Home Automation Projects - Errata"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel