Skip to content

Instantly share code, notes, and snippets.

@sabas1080
Created January 19, 2016 20:44
Show Gist options
  • Save sabas1080/812d680019c1cfa34158 to your computer and use it in GitHub Desktop.
Save sabas1080/812d680019c1cfa34158 to your computer and use it in GitHub Desktop.
Example EggTimer Get Data Period Example
/************************************************************
EggTimer_Data_Get.ino
Example EggTimer Get Period Example
Andres Sabas @ The Inventor's House
Original Creation Date: Jan 17, 2016
This example demonstrates how to use the TCP client
functionality of the ESP8266 WiFiSecure library to GET
data to a EggTimer on
https://eggtimer.herokuapp.com
Development environment specifics:
IDE: Arduino 1.6.5
Hardware Platform:
ESP8266 HUZZAH Adafruit
or NodeMCU: 1.0
This code is beerware; if you see me (or any other The Inventor's House
member) at the local, and you've found our code helpful,
please buy us a round!
Distributed as-is; no warranty is given.
************************************************************/
//Include that library first:
#include <ESP8266WiFi.h>
// Include the ESP8266 WiFiSecure library:
#include <WiFiClientSecure.h>
//////////////////////////////
// WiFi Network Definitions //
//////////////////////////////
// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "YourWifiName";
const char myPSK[] = "YourWifiPassword";
/////////////////////
// EggTimer Constants //
/////////////////////
// Egg destination server:
const char* EggTimerServer = "eggtimer.herokuapp.com";
// Egg https por:
const int httpsPort = 443;
// Egg API key:
const String Api_Key = "YourAPIKey";
String httpHeader = (String("GET ") + "/api/v2/periods/?format=json HTTP/1.1\r\n"+
"Host: "+ EggTimerServer+"\r\n"+
"User-Agent: ESP8266\r\n"+
"Authorization: Token "+ Api_Key + "\r\n"+
"Content-Type: application/json\r\n\r\n");
void setup()
{
int status;
Serial.begin(9600);
Serial.println();
Serial.print(F("connecting to "));
Serial.println(mySSID);
WiFi.begin(mySSID, myPSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(F("WiFi connected"));
Serial.println(F("IP address is: "));
Serial.println(WiFi.localIP());
Serial.println(F("Press any key to get to EggTimer!"));
}
void loop()
{
// If a character has been received over serial:
if (Serial.available())
{
// !!! Make sure we haven't posted recently
// Post to EggTimerServer!
postToEggTimer();
// Then clear the serial buffer:
while (Serial.available())
Serial.read();
}
}
void postToEggTimer()
{
// Create a client, and initiate a connection
WiFiClientSecure client;
if (client.connect(EggTimerServer, httpsPort) <= 0)
{
Serial.println(F("Failed to connect to server."));
return;
}
Serial.println(F("Connected."));
Serial.println(F("Posting to EggTimer Event!"));
client.print(httpHeader);
while (client.available() < 1) {
Serial.print(".");
delay(100);
}
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
// 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.
Serial.println(F("Close!"));
}
}
@jessamynsmith
Copy link

great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment