moving code to main repo

This commit is contained in:
ladyada 2018-06-13 18:09:08 -04:00
parent f57fd55dc8
commit 199e1f4a30
3 changed files with 242 additions and 0 deletions

View file

@ -0,0 +1,107 @@
/*
* Simple HTTP get webclient test
*/
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
const char* ssid = "ssid";
const char* password = "password";
#define HOST "api.twitch.tv"
#define PATH "/kraken/streams/adafruit"
#define REFRESH 30 // seconds between refresh
#define LED 13
#define PIN 12
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void setup() {
Serial.begin(115200);
delay(100);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
strip.begin();
}
int value = 0;
void loop() {
++value;
Serial.print("connecting to ");
Serial.println(HOST);
// Use WiFiClient class to create TCP connections
WiFiClientSecure client;
if (!client.connect(HOST, 443)) {
Serial.println("connection failed");
colorWipe(strip.Color(0, 0, 0), 50); // Off
return;
}
// We now create a URI for the request
Serial.print("Requesting URL: ");
Serial.println(PATH);
// This will send the request to the server
client.print(String("GET ") + PATH + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Connection: close\r\n\r\n");
int32_t timeout = millis() + 1000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
colorWipe(strip.Color(0, 0, 0), 50); // Off
return;
}
}
boolean isStreaming = false;
while (client.connected()) {
if (client.find("\"stream\":{\"_id\":")) {
isStreaming = true;
}
}
Serial.print("Streaming status: "); Serial.println(isStreaming);
digitalWrite(LED, isStreaming);
if (isStreaming) {
colorWipe(strip.Color(255, 0, 0), 50); // Red
} else {
colorWipe(strip.Color(0, 0, 0), 50); // Off
}
Serial.println("disconnecting from server.");
client.stop();
delay(REFRESH*1000);
}

View file

@ -0,0 +1 @@
Check out the tutorial at https://learn.adafruit.com/automatic-twitch-on-air-sign/

View file

@ -0,0 +1,134 @@
#include <SPI.h>
#include <Adafruit_WINC1500.h>
#include <Adafruit_NeoPixel.h>
#define PIN 12
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRBW + NEO_KHZ800);
// Define the WINC1500 board connections below.
// If you're following the Adafruit WINC1500 board
// guide you don't need to modify these:
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC and comment this out
#define LED 13
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
// Or just use hardware SPI (SCK/MOSI/MISO) and defaults, SS -> #10, INT -> #7, RST -> #5, EN -> 3-5V
//Adafruit_WINC1500 WiFi;
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
#define HOST "api.twitch.tv"
#define PATH "/kraken/streams/adafruit"
#define REFRESH 20 // seconds between refresh
Adafruit_WINC1500SSLClient client;
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void setup() {
#ifdef WINC_EN
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
#endif
pinMode(LED, OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(9600);
strip.begin();
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
}
void loop() {
// attempt to connect to Wifi network:
if (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
Serial.println("Connected to wifi");
printWifiStatus();
}
Serial.println("\nStarting connection to twitch...");
// if you get a connection, report back via serial:
if (client.connect(HOST, 443)) {
Serial.println("connected to twitch api server");
// Make a HTTP request:
client.println("GET " PATH " HTTP/1.1");
client.println("Host: " HOST);
client.println("Connection: close");
client.println();
}
boolean isStreaming = false;
while (client.connected()) {
if (client.find("\"stream\":{\"_id\":")) {
isStreaming = true;
}
}
Serial.print("Streaming status: "); Serial.println(isStreaming);
digitalWrite(LED, isStreaming);
if (isStreaming) {
colorWipe(strip.Color(255, 0, 0), 50); // Red
} else {
colorWipe(strip.Color(10, 10, 10), 50); // Off
}
Serial.println("disconnecting from server.");
client.stop();
delay(REFRESH*1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}