/ plexpy

PlexPy & ESP8266

(side note: this is no longer working after PlexPy upgraded to Tautulli)

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 =)

arduino code

/**
* ESP8266 & PLEXPY API
* PLAY COUNT VIEWER
*/

#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);

void setup() {
    display.init();
    display.flipScreenVertically();
    display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
    display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 20, "Initializing\n device");
   display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2 - 10, "device");
   display.display();

   for(uint8_t t = 5; t > 0; t--) {

   display.clear();
   delay(1000);
}

WiFiMulti.addAP("WIFINAME", "WIFIPASSWORD");
delay(500);
}

void loop() {
    if((WiFiMulti.run() == WL_CONNECTED)) {        
    HTTPClient http;
    http.begin("PHPSERVER", 80, "/plex8266.php?val=all"); //HTTP
    int httpCode = http.GET();
    if(httpCode) {
        if(httpCode == 200) {

           String total = http.getString();
           display.clear();
           display.setTextAlignment(TEXT_ALIGN_LEFT);    
           display.setFont(ArialMT_Plain_16);
           display.drawString(0, 0, "plexpy stats");
           display.setFont(ArialMT_Plain_10);
           display.drawString(0, 18,  String(total));
           display.display();
        }
    } else {

    }
    }

delay(5000);
}

plex8266.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$URL = "http://PLEXPYIP:PLEXPYPORT/api/v2?apikey=PASTEYOURAPIKEYHERE&cmd=get_activity";
$data = json_decode(file_get_contents($URL),true);
$stream_count = $data['response']['data']['stream_count'];
$transcdoe_stream_count =0;
if($stream_count > 0){
    $sessions = $data['response']['data']['sessions'];
    for ($c=0;$c<count($sessions);$c++)
            if($sessions[$c]['transcode_key']!="")
                    $transcdoe_stream_count ++;
}
if (isset($_GET['val']))
    $val =$_GET['val'];
else
    $val = "all";
if($val=="all")
    echo "Direct Play: ".($stream_count-$transcdoe_stream_count)."\n Transcodes: ".$transcdoe_stream_count."\n Total ".$stream_count;
elseif($val=="direct")
    echo ($stream_count-$transcdoe_stream_count);
elseif($val=="trans")
    echo $transcdoe_stream_count;
elseif($val=="total")
    echo $stream_count;

//echo "<br /> <br />";
//print_r($data);
?>

make sure to replace again replace PLEXPYIP, PLEXPYPORT & PASTEYOURAPIKEYHERE with your relevant info.

PHP Input PHP Output
esp8266.php?=all returns formatted response of current transcodes, direct plays & total plays
esp8266?val=trans returns only a number of current transcodes
esp8266?val=direct returns only a number of current direct plays
esp8266?val=total returns only a number of total combines direct play and transcodes

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.

pics

plexpy_front

backside of the esp8266. notice the OLED type (SH1106) is printed on the module, as well as the pin numbers for accessing accessing the display.

plexpy_back