Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgiszewski/a4b566e1ef750c17c5d5b2a7b5b30735 to your computer and use it in GitHub Desktop.
Save kgiszewski/a4b566e1ef750c17c5d5b2a7b5b30735 to your computer and use it in GitHub Desktop.
// the setup function runs once when you press reset or power the board
#include <SPI.h>
#include <Ethernet.h>
//define a constant that maps to pin 6
#define BLUE_PIN 6
//needs a mac address, just needs to be unique to your LAN
//you can make this number up
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//required for the fact server
const char* deviceId = "<the device Id from my API>";
//the fact server hostname
char host[] = "api.gizmo42.com";
//the url on the fact server to visit
const char* url = "/v1/facts/blogLastVisited";
//DHCP didn't work for me, so i manually set my private IP info
IPAddress ip(192, 168, 1, 203);
IPAddress gateway(192, 168, 1, 1);
IPAddress dns2(8, 8, 8, 8);
IPAddress subnet(255, 255, 255, 0);
//some vars to save some state
String lastReportedTime;
String lastVisitedOn;
//the wait in seconds before we poll the server again
int secondsToWait = 60;
//runs once
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(BLUE_PIN, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//config the ethernet
Ethernet.begin(mac, ip, dns2, gateway, subnet);
//dump the IP the debugger
Serial.println(Ethernet.localIP());
//for some reason the Ethernet needs some time to init
delay(5000);
}
//this will loop infinitely
void loop() {
Serial.println("Begin...");
//a ref to our http clienty
EthernetClient client;
//let it setup
delay(1000);
Serial.print("Connecting to ");
Serial.println(host);
//if we don't connect, let's restart the loop
if (!client.connect(host, 80) == 1) {
Serial.println("Connection failed!");
delay(10000);
return;
}
//seems we've connected to the server
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"x-device-id: " + deviceId + "\r\n" +
"Accept: " + "application/json\r\n" +
"Connection: close\r\n\r\n"
);
delay(10);
//parsing the response
while (client.connected())
{
if (client.available()) {
String line = client.readStringUntil('"');
line = client.readStringUntil('"');
line.trim();
Serial.println(line);
lastVisitedOn = line;
Serial.println("Last visited raw: " + line);
client.stop();
}
}
lastVisitedOn.trim();
lastVisitedOn = lastVisitedOn.substring(0, 27);
Serial.println("Disconnecting.");
Serial.println("LastReportedTime: " + lastReportedTime);
Serial.println("API report time: " + lastVisitedOn);
//if the fact server has a different value from our last stored value, react
if (lastReportedTime != lastVisitedOn)
{
lastReportedTime = lastVisitedOn;
Serial.println("We had a visitor!");
//blink the light 10 times
for (int i = 0; i < 10; i++)
{
digitalWrite(BLUE_PIN, HIGH);
delay(100);
digitalWrite(BLUE_PIN, LOW);
delay(100);
}
}
else
{
Serial.println("Nothing to see here!");
}
Serial.println("Waiting... ");
int currentSecond = 0;
//let's wait
while (currentSecond < secondsToWait)
{
currentSecond++;
delay(1000);
Serial.print(".");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment