Skip to content

Instantly share code, notes, and snippets.

@iddar
Created January 29, 2016 00:32
Show Gist options
  • Select an option

  • Save iddar/3f13440d5b35afe96a67 to your computer and use it in GitHub Desktop.

Select an option

Save iddar/3f13440d5b35afe96a67 to your computer and use it in GitHub Desktop.
// password of your WiFi network.
#define mySSID "513570"
#define myPSK "H21208269B2B"
// openweathermap Constants
#define HTTP_PORT 80
#define WEATHER_SERVER "api.openweathermap.org"
#define ID_CITY "4019233"
#define APP_ID "44db6a862fba0b067b1930da0d769e98"
#define HTTP_HEADER "GET /data/2.5/weather?id=" ID_CITY \
"&appid=" APP_ID "&units=metric" " HTTP/1.1\r\n" \
"Host: " WEATHER_SERVER "\r\n" \
"Content-Type: application/x-www-form-urlencoded\r\n" \
"Connection: close\r\n\r\n"
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <avr/pgmspace.h>
#include <Servo.h>
#include <json_arduino.h>
#include "sabas.h"
Servo myservo;
ESP8266Client client;
char json_string[512];
token_list_t *token_list = NULL;
// "{\"coord\":{\"lon\":-102.3,\"lat\":21.88},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"sky is clear\",\"icon\":\"02n\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":5,\"pressure\":1025,\"humidity\":56,\"temp_min\":5,\"temp_max\":5},\"wind\":{\"speed\":7.7,\"deg\":20},\"clouds\":{\"all\":5},\"dt\":1453956180,\"sys\":{\"type\":1,\"id\":3966,\"message\":0.0026,\"country\":\"MX\",\"sunrise\":1453987717,\"sunset\":1454027754},\"id\":4019233,\"name\":\"Aguascalientes\",\"cod\":200}"
void setup()
{
int status;
Serial.begin(9600);
myservo.attach(3); // attaches the servo on pin 5 to the servo object
// To turn the MG2639 shield on, and verify communication
// always begin a sketch by calling cell.begin().
status = esp8266.begin();
if (status <= 0)
{
Serial.println(F("Unable to communicate with shield. Looping"));
while(1) ;
}
esp8266.setMode(ESP8266_MODE_STA); // Set WiFi mode to station
if (esp8266.status() <= 0) // If we're not already connected
{
if (esp8266.connect(mySSID, myPSK) < 0)
{
Serial.println(F("Error connecting"));
while (1) ;
}
}
// Get our assigned IP address and print it:
Serial.print(F("My IP address is: "));
Serial.println(esp8266.localIP());
Serial.println(F("Press any key to post to IFTTT!"));
}
void loop()
{
// If a character has been received over serial:
if (Serial.available())
{
// !!! Make sure we haven't posted recently
// Post to IFTTT!
getData();
// Then clear the serial buffer:
while (Serial.available())
Serial.read();
}
}
void getData()
{
if (client.connect(WEATHER_SERVER, HTTP_PORT) <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}
Serial.println(F("Connected."));
client.print(F(HTTP_HEADER));
// read response
while(client.available()){
client.readStringUntil('\r').toCharArray(json_string, 512);
// Serial.print(json_string);
}
if (client.connected()) client.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment