/ Twitch_On_Air_Sign / WINC1500_OnAirSign / WINC1500_OnAirSign.ino
WINC1500_OnAirSign.ino
  1  // SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <SPI.h>
  6  #include <WiFi101.h>
  7  #include <Adafruit_NeoPixel.h>
  8  
  9  #define PIN 12
 10  Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRBW + NEO_KHZ800);
 11  
 12  
 13  // Define the WINC1500 board connections below.
 14  // If you're following the Adafruit WINC1500 board
 15  // guide you don't need to modify these:
 16  #define WINC_CS   8
 17  #define WINC_IRQ  7
 18  #define WINC_RST  4
 19  #define WINC_EN   2     // or, tie EN to VCC and comment this out
 20  
 21  #define LED 13
 22  
 23  char ssid[] = "ssid"; //  your network SSID (name)
 24  char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
 25  int keyIndex = 0;            // your network key Index number (needed only for WEP)
 26  
 27  int status = WL_IDLE_STATUS;
 28  #define HOST "api.twitch.tv"
 29  #define PATH "/kraken/streams/adafruit"
 30  #define REFRESH 20  // seconds between refresh
 31  
 32  WiFiSSLClient client;
 33  
 34  // Fill the dots one after the other with a color
 35  void colorWipe(uint32_t c, uint8_t wait) {
 36    for(uint16_t i=0; i<strip.numPixels(); i++) {
 37      strip.setPixelColor(i, c);
 38      strip.show();
 39      delay(wait);
 40    }
 41  }
 42  
 43  void setup() {
 44  #ifdef WINC_EN
 45    pinMode(WINC_EN, OUTPUT);
 46    digitalWrite(WINC_EN, HIGH);
 47  #endif
 48    WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST);
 49  
 50    pinMode(LED, OUTPUT);
 51    
 52    //Initialize serial and wait for port to open:
 53    Serial.begin(9600);
 54  
 55    strip.begin();
 56  
 57    // check for the presence of the shield:
 58    if (WiFi.status() == WL_NO_SHIELD) {
 59      Serial.println("WiFi shield not present");
 60      // don't continue:
 61      while (true);
 62    }
 63  }
 64  
 65  void loop() {
 66    // attempt to connect to Wifi network:
 67    if (WiFi.status() != WL_CONNECTED) {
 68      while (WiFi.status() != WL_CONNECTED) {
 69        Serial.print("Attempting to connect to SSID: ");
 70        Serial.println(ssid);
 71        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
 72        status = WiFi.begin(ssid, pass);
 73    
 74        // wait 10 seconds for connection:
 75        uint8_t timeout = 10;
 76        while (timeout && (WiFi.status() != WL_CONNECTED)) {
 77          timeout--;
 78          delay(1000);
 79        }
 80      }
 81    
 82      Serial.println("Connected to wifi");
 83      printWifiStatus();
 84    }
 85    
 86    Serial.println("\nStarting connection to twitch...");
 87    // if you get a connection, report back via serial:
 88    if (client.connect(HOST, 443)) {
 89      Serial.println("connected to twitch api server");
 90      // Make a HTTP request:
 91      client.println("GET " PATH " HTTP/1.1");
 92      client.println("Host: " HOST);
 93      client.println("Connection: close");
 94      client.println();
 95    }
 96  
 97    boolean isStreaming = false;
 98    while (client.connected()) {
 99      if (client.find("\"stream\":{\"_id\":")) {
100        isStreaming = true;
101      }
102    }
103  
104    Serial.print("Streaming status: "); Serial.println(isStreaming);
105    digitalWrite(LED, isStreaming);
106    if (isStreaming) {
107      colorWipe(strip.Color(255, 0, 0), 50); // Red
108    } else {
109      colorWipe(strip.Color(10, 10, 10), 50); // Off
110    }
111    Serial.println("disconnecting from server.");
112    client.stop();
113  
114    delay(REFRESH*1000);
115  }
116  
117  
118  void printWifiStatus() {
119    // print the SSID of the network you're attached to:
120    Serial.print("SSID: ");
121    Serial.println(WiFi.SSID());
122  
123    // print your WiFi shield's IP address:
124    IPAddress ip = WiFi.localIP();
125    Serial.print("IP Address: ");
126    Serial.println(ip);
127  
128    // print the received signal strength:
129    long rssi = WiFi.RSSI();
130    Serial.print("signal strength (RSSI):");
131    Serial.print(rssi);
132    Serial.println(" dBm");
133  }