Skip to content

Instantly share code, notes, and snippets.

@dicamarques14
Last active January 26, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dicamarques14/41a45c1cef073683eea6 to your computer and use it in GitHub Desktop.
Save dicamarques14/41a45c1cef073683eea6 to your computer and use it in GitHub Desktop.
Blink When a Twitch channel goes live for ESP8266 with arduino IDE
<?php
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$dataArray = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $_GET["name"], false), true);
foreach($dataArray['streams'] as $mydata){
if($mydata['_id'] != null){
echo "<p>1</p>";
}else{
echo "<p>test</p>";
}
}
if($dataArray['streams'] == null or $dataArray['streams'] == "")
{
echo "<p>0</p>";
}
?>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
const char* ssid = "*******"; // insert your SSID
const char* password = "*****"; // insert your password
const char* host = "smth.smth.com"; //Server where the PHP is, you'll need to find a way to host the PHP, because the ESP doesnt support HTTPs
const int httpPort = 80; // Server Port, most servers use 80
const int pinLed = 2; // led pin for telling when updating
const int UpdateLed = 4; // led pin for when someone goes live
String ChannelList[] = { "Orb", "Lirik", "Aplfisher", "xSmaK", "Kylelandrypiano", "Milan3D222", "DgCompany54", "MentOfer"}; // Channel list
//-----------------------------
bool Status[50];
bool Blinking = false;
unsigned long previousMillis = 0; // will store last time status was updated
const long UpdateInterval = 300000; // interval at which to blink (milliseconds)
String url = "/twitch.php?name=";
int j = 0; //amount of channel in list, no need to change
int retry = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pinLed, OUTPUT);
pinMode(UpdateLed, OUTPUT);
digitalWrite(pinLed, 1);
digitalWrite(UpdateLed, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(pinLed, 0);
j = sizeof( ChannelList ) / sizeof( ChannelList[0] );
for(int i = 0; i < j ;i++){
Status[i] = TwitchChannelStatus(ChannelList[i]);
}
Serial.println("");
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= UpdateInterval) {
previousMillis = currentMillis;
updateStatus();
}
if(Blinking == true){
BlinkLed(UpdateLed);
Blinking = false;
}
delay(10);
}
void updateStatus(){
digitalWrite(pinLed, 1);
Serial.println("Update Status");
for(int i = 0; i < j ;i++){
bool NewStatus = TwitchChannelStatus(ChannelList[i]);
if (NewStatus != Status[i]){
if(NewStatus){
Serial.println("IS NOW LIVE");
Status[i] = NewStatus;
Blinking = true;
}
if(!NewStatus){
Serial.println("IS NOW OFFLINE");
Status[i] = NewStatus;
}
}
}
Serial.println("Finished Update\n");
digitalWrite(pinLed, 0);
}
bool TwitchChannelStatus(String username){
HTTPClient http;
char c;
String channel = url + username;
http.begin(host, httpPort, channel); //HTTP
int httpCode = http.GET();
if(httpCode) {
// HTTP header has been send and Server response header has been handled
//Serial.print("[HTTP] GET... code: ");
//Serial.println(httpCode);
// file found at server
if(httpCode == 200) {
String payload = http.getString();
Serial.println(payload);
c = payload[3];
//Serial.println(c);
}
} else {
Serial.println("[HTTP] GET... failed, no connection or no HTTP server");
}
Serial.print(username);
Serial.print(": ");
switch (c) {
case '0':
Serial.println("Offline");
return false;
break;
case '1':
Serial.println("LIVE");
return true;
break;
default:
if(retry <= 4){
Serial.println("UPS....");
retry++;
TwitchChannelStatus(username);
}
else{
retry = 0;
return false;
}
break;
}
}
void BlinkLed(int ledPin)
{
for(int k=0; k < 3; k++){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment