Created
November 25, 2018 23:23
-
-
Save th3o6a1d/b68c87c7013b49b466644ea25d2f48f4 to your computer and use it in GitHub Desktop.
Get number of bikes available from citibike station by ID and blink ESP8266 default light.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ArduinoJson.h> | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* host = "gbfs.citibikenyc.com"; | |
const String url = "/gbfs/en/station_status.json"; | |
const uint16_t port = 80; | |
const String station_id = "422"; | |
int bikesAvailable = 0; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
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()); | |
} | |
void updateBikes() { | |
Serial.print("connecting to "); | |
Serial.print(host); | |
Serial.print(':'); | |
Serial.println(port); | |
WiFiClient client; | |
if (!client.connect(host, port)) { | |
Serial.println("connection failed"); | |
delay(5000); | |
return; | |
} | |
if (client.connect(host, port)) { | |
Serial.println("connected"); | |
// Make a HTTP request: | |
client.print("GET " + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Content-Type: application/json\r\n" + | |
"Connection: keep-alive\r\n\r\n"); | |
client.println(); | |
} | |
String preJSON = ""; | |
while (client.connected()) { | |
preJSON = client.readStringUntil('}'); | |
if (preJSON.indexOf("\"station_id\":\"" + station_id + "\"") > 0) { | |
preJSON.remove(0, 1); | |
preJSON = preJSON + "}"; | |
break; | |
} | |
} | |
const size_t bufferSize = JSON_OBJECT_SIZE(11) + 250; | |
DynamicJsonBuffer jsonBuffer(bufferSize); | |
JsonObject& root = jsonBuffer.parseObject(preJSON); | |
const char* station_id = root["station_id"]; | |
int num_bikes_available = root["num_bikes_available"]; | |
int num_ebikes_available = root["num_ebikes_available"]; | |
int num_bikes_disabled = root["num_bikes_disabled"]; | |
int num_docks_available = root["num_docks_available"]; | |
int num_docks_disabled = root["num_docks_disabled"]; | |
int is_installed = root["is_installed"]; | |
int is_renting = root["is_renting"]; | |
int is_returning = root["is_returning"]; | |
long last_reported = root["last_reported"]; | |
bool eightd_has_available_keys = root["eightd_has_available_keys"]; | |
bikesAvailable = num_bikes_available; | |
Serial.print("Station ID: "); | |
Serial.println(station_id); | |
Serial.print("Bikes Available: "); | |
Serial.println(num_bikes_available); | |
Serial.println("closing connection"); | |
client.stop(); | |
} | |
void loop() { | |
updateBikes(); | |
int counter = 0; | |
while (counter < bikesAvailable) { | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(1000); | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(1000); | |
counter += 1; | |
} | |
delay(30000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment