-
-
Save vulcan25/ee9879da03435cf507ae51f1733e7baf to your computer and use it in GitHub Desktop.
Attempt to read Flask event-stream on ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <String.h> | |
const char* ssid = "guest"; | |
const char* password = "superSecret"; | |
const char* host = "192.168.2.150"; | |
const char* streamId = "...................."; | |
const char* privateKey = "...................."; | |
// | |
// LCD | |
// | |
#include <Wire.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_GFX.h> | |
// OLED display TWI address | |
#define OLED_ADDR 0x3C | |
Adafruit_SSD1306 display(-1); | |
#if (SSD1306_LCDHEIGHT != 64) | |
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); | |
#endif | |
// | |
// =========== | |
// START SETUP | |
// =========== | |
// | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
// 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()); | |
// | |
// LCD SETUP | |
// | |
// initialize and clear display | |
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); | |
display.clearDisplay(); | |
display.display(); | |
// display a pixel in each corner of the screen | |
// display.drawPixel(0, 0, WHITE); | |
// display.drawPixel(127, 0, WHITE); | |
// display.drawPixel(0, 63, WHITE); | |
// display.drawPixel(127, 63, WHITE); | |
// display a line of text | |
display.setTextSize(2); | |
display.setTextColor(WHITE); | |
display.setCursor(0,0); | |
display.print("Display inited!"); | |
// update display with all of the above graphics | |
display.display(); | |
} | |
// | |
// ========== | |
// START LOOP | |
// ========== | |
// | |
int value = 0; | |
void loop() { | |
delay(1000); | |
++value; | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
// client.setTimeout(1000); | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/input/"; | |
url += streamId; | |
url += "?private_key="; | |
url += privateKey; | |
url += "&value="; | |
url += value; | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: keep-alive\r\n\r\n"); | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
// | |
// Change display to not connected. | |
// | |
display.setCursor(0,0); | |
display.print('X'); | |
display.display(); | |
return; | |
} | |
} | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
// String line = client.readStringUntil('\n'); | |
String line = client.readString(); | |
Serial.print(line); | |
// delay(500); | |
// | |
// PRINT TO DISPLAY | |
// | |
if (line.startsWith("~")) | |
{ | |
display.setCursor(0,0); | |
display.clearDisplay(); | |
display.print(line); | |
display.display(); | |
// delay(10); | |
// client.stop(); | |
} | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment