Created
March 27, 2017 21:57
-
-
Save c5e3/1badd42d47620adcdf8348fcd71fa8c6 to your computer and use it in GitHub Desktop.
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
/* | |
* install this plugin for rhythmbox: | |
* https://github.com/pcarranza/rhythmweb2 | |
*/ | |
#include <ArduinoJson.h> | |
#include <ESP8266WiFi.h> | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
const char* server = "192.168.1.11"; | |
const int port = 7000; | |
const char* resource = "/rest/status"; | |
const unsigned long BAUD_RATE = 115200; | |
const unsigned long HTTP_TIMEOUT = 10000; | |
const size_t MAX_CONTENT_SIZE = 1024; | |
#define WLAN_SSID "SSID" | |
#define WLAN_PASS "PASSWORD" | |
#define OLED_MOSI D3 | |
#define OLED_CLK D4 | |
#define OLED_DC D1 | |
#define OLED_CS D0 | |
#define OLED_RESET D2 | |
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); | |
WiFiClient client; | |
struct UserData { | |
char artist[32]; | |
char title[32]; | |
}; | |
void setup() { | |
initSerial(); | |
initWifi(); | |
initDisplay(); | |
} | |
void loop() { | |
if (connect(server, port)) { | |
if (sendRequest(server, resource) && skipResponseHeaders()) { | |
UserData userData; | |
if (readReponseContent(&userData)) { | |
printUserData(&userData); | |
} | |
} | |
} | |
disconnect(); | |
delay(3000); | |
} | |
void initSerial() { | |
Serial.begin(BAUD_RATE); | |
while (!Serial); | |
Serial.println("Serial ready"); | |
} | |
void initWifi() { | |
Serial.println(); Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WLAN_SSID); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); Serial.println(WiFi.localIP()); | |
delay(1000); | |
} | |
void initDisplay(){ | |
display.begin(SSD1306_SWITCHCAPVCC); | |
display.display(); | |
display.clearDisplay(); | |
display.display(); | |
} | |
bool connect(const char* hostName, int hostPort) { | |
Serial.print("Connect to "); | |
Serial.println(hostName); | |
bool ok = client.connect(hostName, hostPort); | |
Serial.println(ok ? "Connected" : "Connection Failed!"); | |
return ok; | |
} | |
bool sendRequest(const char* host, const char* resource) { | |
Serial.print("GET "); | |
Serial.println(resource); | |
client.print("GET "); | |
client.print(resource); | |
client.println(" HTTP/1.0"); | |
client.print("Host: "); | |
client.println(host); | |
client.println("Connection: close"); | |
client.println(); | |
return true; | |
} | |
bool skipResponseHeaders() { | |
char endOfHeaders[] = "\r\n\r\n"; | |
client.setTimeout(HTTP_TIMEOUT); | |
bool ok = client.find(endOfHeaders); | |
if (!ok) { | |
Serial.println("No response or invalid response!"); | |
} | |
return ok; | |
} | |
bool readReponseContent(struct UserData* userData) { | |
const size_t BUFFER_SIZE = | |
JSON_OBJECT_SIZE(6) | |
+ JSON_OBJECT_SIZE(13) | |
+ MAX_CONTENT_SIZE; | |
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE); | |
JsonObject& root = jsonBuffer.parseObject(client); | |
if (!root.success()) { | |
Serial.println("JSON parsing failed!"); | |
return false; | |
} | |
strcpy(userData->artist, root["playing_entry"]["artist"]); | |
strcpy(userData->title, root["playing_entry"]["title"]); | |
return true; | |
} | |
void printUserData(const struct UserData* userData) { | |
Serial.print("artist: #"); | |
Serial.print(userData->artist); | |
Serial.println("#"); | |
Serial.print("title: #"); | |
Serial.print(userData->title); | |
Serial.println("#"); | |
display.clearDisplay(); | |
display.setTextSize(1); | |
display.setTextColor(WHITE); | |
display.setCursor(0,0); | |
display.println(userData->artist); | |
//display.setTextSize(1); | |
display.println(""); | |
display.println(userData->title); | |
display.display(); | |
} | |
void disconnect() { | |
Serial.println("Disconnect"); | |
client.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment