ESP8266 & Current Plays
the above picture is of the esp8266 running the code below in post #2
Since its my first forum post i gotta start this off by saying that i $%^* love plex. has completly changed the entertainment experience for me and some close friends over the last few years. so grateful damn grateful to the devs that i purchased plexpass just to support them (more on this later) & wanted to give back in any way that i can!
I use plexpy quite often to monitor the plex server stats. its conveniant to check it on the phone via a browser but i just felt like it was such a hassle to constantly have to pull out my fone all the time, when really the only thing im wanting to know is see how current plays there are at any given moment.
Ended up creating this and wanted to post the code and steps required incase anyone else wanted to do the same, build on it, or whatever else you wish. Most of the code is just a hacked up version of BasicHttpClient.ino Once I realized I was going to share this, i wanted to make sure all the code used was free to share under the appropriate licenses. (and it is! whoo!!)
After some googling of different IoT devices (crazy huge selection by the way), i came across an system on a chip (SoC) called the esp8266. It’s basically a small module, has its own dedicated memory, processor, wifi (2.4Ghz b/g/n), and even some analog and digital inputs/outputs for reading/sending data! You can buy just the esp8266 module for cheap, like $4. Thing is, to program the device it’d be convenient to use USB & the esp8266 is very specific about the voltage level going into it (you don’t want to fry the circuitry with too much voltage - though they are super cheap!) So the esp8266 variant im am using has built in usb-to-serial, as well as an SH1106 OLED screen module. I don’t want to say the specific brand or model that i got, but your options to acquire one are vast… from china to the states, there are tons of vendors. Just make sure to check your vendors review for good plenty of good feedback, and check for documentation online before making your purchase.
prerequisites
- ESP8266 w/ built in OLED ( $12)
- plex server w/ plexpy
- Arduino IDE Software
- USB Power Supply Source
- USB Cable
Install the Arduino IDE. This is what we’ll be using to communicate with the board. You may want to do some web searches on ‘esp8266 arduino’ as you will need to install some libraries that are listed in the header area of the following code. To install these: within the arduino program; in the menu bar goto Sketch, => Include Library
You can install the library .zip’s you find on github this way by selecting ‘Add .Zip Library’, or goto ‘Manage Libraries…’ and take advantage of the search box in the top right =)
code:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include <ESP8266HTTPClient.h>
#include "SH1106.h"
ESP8266WiFiMulti WiFiMulti;
SH1106 display(0x3c, 5, 4); // address, SDA, SCL inputs D1, D2
void setup() {
display.init();
//display.invertDisplay();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 0, "Initializing ...");
display.display();
for(uint8_t t = 5; t > 0; t--) {
delay(1000);
}
WiFiMulti.addAP("WIFINAME", "WIFIPASSWORD");
delay(500);
display.clear();
}
void loop() {
display.clear();
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
display.drawString(0, 20, "connected.");
HTTPClient http;
http.begin("PLEXPYIP", PLEXPYPORT, "/api/v2?apikey=PASTEYOURAPIKEYHERE&cmd=get_activity"); //HTTP
// PLEXPYIP - ip address or dns hostname of the server plexpy is running on
// PLEXPYPORT - port number plexpy is running on. uncommented.
// the next entry is the GET request, the example used is if plex is in the root directory
// example:
// http.begin("259.32.23.12", 60061, "/api/v2?apikey=01234567890123456789&cmd=get_activity");
int httpCode = http.GET();
if(httpCode) {
if(httpCode == 200) {
String payload = http.getString();
display.setFont(ArialMT_Plain_10);
display.clear();
display.drawStringMaxWidth(0, 0, 124, String(payload));
display.display();
}
} else {
}
}
delay(5000);
}
notes
copy paste that into the arduino sketch, select the correct COM port (com1, com2, etc.) and upload the code. make note to change the PLEXPYIP & PLEXPYPORT settings to match that of your setup, and to update the WIFINAME & WIFIPASSWORD to match that of your wifi settings.
This this is totally portable, as long as your within range of your home wifi it will display the first few rows of the plexpy api json results. due to sheer luck and awesomness, the stream_count field just happens to be right smack on the middle line of the oled display, with the variable immediately afterwards being the number of current plays.
i plan to parse the json when i have time, and have the oled just display 'current plays: X".
My first choice here was to use the official plex api. Instead of responding in the JSON format like plexpy uses, the official plex api responds back in gloriously formatted XML with tons of metadata goodies. next time i have time i plan on implementing the plex api right away as it’s very well documented.






