Skip to content

Instantly share code, notes, and snippets.

@sabas1080
Last active January 26, 2016 22:38
Show Gist options
  • Save sabas1080/e006fdc25e75f937e1fa to your computer and use it in GitHub Desktop.
Save sabas1080/e006fdc25e75f937e1fa to your computer and use it in GitHub Desktop.
/************************************************************
Distributed as-is; no warranty is given.
************************************************************/
// The SparkFunESP8266WiFi library uses SoftwareSerial
// to communicate with the ESP8266 module. Include that
// library first:
#include <SoftwareSerial.h>
// Include the ESP8266 AT library:
#include <SparkFunESP8266WiFi.h>
// Include the ArduinoJSON library:
#include <ArduinoJson.h>
#include <Servo.h>
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "513570";
const char myPSK[] = "H21208269B2B";
/////////////////////
// Weather Constants //
/////////////////////
// Weather destination server:
const char* WeatherServer = "api.openweathermap.org";
// Weather http port:
const int httpPort = 80;
// Weather idCity:
const String id_City = "4019233"; //"4019233";
// Weather app id:
const String App_id = "44db6a862fba0b067b1930da0d769e98";
String httpHeader = "GET /data/2.5/weather?id=" + id_City +"&appid="+App_id+ "&units=metric" +" HTTP/1.1\r\n" +
"Host: " + WeatherServer + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n"+
"Connection: close\r\n\r\n";
//char json[]=
// "{\"coord\":{\"lon\":-102.3,\"lat\":21.88},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":16,\"pressure\":1024,\"humidity\":19,\"temp_min\":16,\"temp_max\":16},\"wind\":{\"speed\":2.6,\"deg\":210},\"clouds\":{\"all\":75},\"dt\":1452966060,\"sys\":{\"type\":1,\"id\":3966,\"message\":0.0032,\"country\":\"MX\",\"sunrise\":1452951019,\"sunset\":1452990487},\"id\":4019233,\"name\":\"Aguascalientes\",\"cod\":200}";
//StaticJsonBuffer<800> jsonBuffer;
//DynamicJsonBuffer jsonBuffer;
//JsonObject& root = jsonBuffer.parseObject(json);
Servo myservo; // create servo object to control a servo
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!
postToIFTTT();
// Then clear the serial buffer:
while (Serial.available())
Serial.read();
}
}
void postToIFTTT()
{
//char json[]=
//"{\"coord\":{\"lon\":-102.3,\"lat\":21.88},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":16,\"pressure\":1024,\"humidity\":19,\"temp_min\":16,\"temp_max\":16},\"wind\":{\"speed\":2.6,\"deg\":210},\"clouds\":{\"all\":75},\"dt\":1452966060,\"sys\":{\"type\":1,\"id\":3966,\"message\":0.0032,\"country\":\"MX\",\"sunrise\":1452951019,\"sunset\":1452990487},\"id\":4019233,\"name\":\"Aguascalientes\",\"cod\":200}";
char json[]="";
StaticJsonBuffer<2048> jsonBuffer;
//DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
// Create a client, and initiate a connection
ESP8266Client client;
if (client.connect(WeatherServer, httpPort) <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}
Serial.println(F("Connected."));
Serial.println(F("Posting to IFTT Event!"));
client.print(httpHeader);
//delay(500); // wait for server to respond
// read response
String section="header";
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
// we’ll parse the HTML body here
if (section=="header") { // headers..
Serial.print(".");
if (line=="\n") { // skips the empty space at the beginning
section="json";
}
}
else if (section=="json") { // print the good stuff
section="ignore";
String result = line.substring(1);
//Serial.print(result);
//delay(1000);
// Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
DynamicJsonBuffer jsonBuffer;
//StaticJsonBuffer<2048> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
//JsonObject& json_parsed = jsonBuffer.parseObject(json);
if (!root.success())
{
//Serial.println("parseObject() failed");
//return;
}
// available() will return the number of characters
// currently in the receive buffer.
// Read all the lines of the reply from server and print them to Serial
//while (client.available())
// Serial.write(client.read()); // read() gets the FIFO char
// connected() is a boolean return value - 1 if the
// connection is active, 0 if it's closed.
if (client.connected())
client.stop(); // stop() closes a TCP connection.
/*
//
// Step 3: Retrieve the values
//
/*
if (!root.success()) {
Serial.println(F("parseObject() failed"));
return;
}
*/
//float longi = root["coord"]["lon"];
//float lati = root["coord"]["lat"];
//uint8_t type = root["main"]["temp"];
//const char* sensor = root["weather"][0]["main"];
//const char* sensor2 = root["weather"][0]["description"];
//Serial.println();
//Serial.println(type);
// Serial.println(longi);
// Serial.println(lati);
//Serial.println(sensor);
//Serial.println(sensor2);
//myservo.write(type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment