ESP-TV

Thought up a new use case for one of the ESP modules I have lying around: Triggering stuff when the TV is powered up.

I've got some speakers connected to the TV, and they are plugged into a WiWo S20 (http://www.banggood.com/WiWo-S20-Wi-Fi-Smart-Remote-Control-Timing-Socket-USEU-Plug-p-953743.html). Smart switches on the WiFi, with a crappiy app. But some nice guy wrote a Perl script which you can use to control these switches: https://github.com/franc-carter/bauhn-wifi

So I've setup a Raspberry PI as a webserver, with a cgi-bin. If you navigate to /speakerson some magic happens and voila! There is sounds!

But now, when I turn on the TV, I don't automatically get sound. I still need to open up a browser, navigate to my 'Mediacenter Control Page' and click a button which triggers the /speakerson cgi script.

Thanks to a collegue of me (Thank you Bart!) I got the idea to improve this. When the TV is turned on, the USB ports are powered. If we then attach something which can do HTTP get requests when it's powered on, the sound goes on!

And what better tool for this job than one of my cheap ESP modules with the following code loaded onto it:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

IPAddress ip(192,168,1,210); // Static IP we want to give the ESP module  
IPAddress gateway(192,168,1,1);  
IPAddress subnet(255,255,255,0);

IPAddress control(192,168,1,106); // IP of the raspberry pi webserver

WiFiClient client;

const char* ssid     = "MyAwesomeSSID";  
const char* password = "NSA.Cant.Hack.This";

void setup(void) {  
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);

  // Wait for successful connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  if (client.connect(control, 80)) {
    // Make a HTTP request:
    client.println("GET /cgi-bin/speakerson.cgi");
    client.println("Host: control.lan");
    client.println("Connection: close");
    client.println();
  } else {
    // Well f***... What should I do now...
  }

  // Done, have a nice day
}

void loop(void) {  
  //nothing to do
}

And now in the other direction:

When the TV is powered off, the ESP goes offline. With a simple script in the crontab which pings the ESP every minute we can trigger the cgi script which turns the speakers off from the raspberry pi like this:

#!/bin/bash

if ! ping -c 1 192.168.1.210 ; then  
  curl http://192.168.1.106/speakersoff
fi