Skip to content

Instantly share code, notes, and snippets.

@vulcan25
Last active September 18, 2019 18:18
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 vulcan25/ee9879da03435cf507ae51f1733e7baf to your computer and use it in GitHub Desktop.
Save vulcan25/ee9879da03435cf507ae51f1733e7baf to your computer and use it in GitHub Desktop.
Attempt to read Flask event-stream on ESP8266
#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;
}
}
String line;
bool in_message=false;
while(client.available()){
char ch = client.read();
if (ch == '~'){
// We are in a message block
in_message=true;
}
if (in_message == true && ch !='*' && ch !='~'){
// if we're in the message and not a start/stop char:
line += ch;
}
if (ch =='*'){
// This is the end char, so print line to display
// and clear the contents of line, until next * is
// received.
display.setCursor(0,0);
display.clearDisplay();
display.print(line);
String line; // clear the line
display.display();
}
}
Serial.println();
Serial.println("closing connection");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment