-
-
Save ed7coyne/678398bb0d9fa1f826d581650606267a to your computer and use it in GitHub Desktop.
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 <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#define USE_SERIAL Serial | |
// Firebase host. | |
#define FBASE "" | |
ESP8266WiFiMulti WiFiMulti; | |
static const char kFirebaseFingerprint[] = | |
"7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"; | |
void setup() { | |
USE_SERIAL.begin(9600); | |
USE_SERIAL.printf("Mem at start %d", ESP.getFreeHeap()); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
for(uint8_t t = 0; t > 0; t--) { | |
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); | |
USE_SERIAL.flush(); | |
delay(1000); | |
} | |
WiFiMulti.addAP("GoogleGuest", ""); | |
} | |
void post() { | |
USE_SERIAL.println("POSTING ..."); | |
USE_SERIAL.printf("Mem before posting %d", ESP.getFreeHeap()); | |
HTTPClient http; | |
http.begin(FBASE, 443, "/post.json", true, kFirebaseFingerprint); | |
int httpCode = http.sendRequest("POST","1"); | |
USE_SERIAL.printf("POST ... code: %d\n", httpCode); | |
if (httpCode < 0) { | |
USE_SERIAL.printf("POST failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
void loop() { | |
// wait for WiFi connection | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
HTTPClient http; | |
USE_SERIAL.print("[HTTP] begin...\n"); | |
// configure server and url | |
http.begin(FBASE, 443, "/", true, kFirebaseFingerprint); | |
// needed for fbase streaming | |
http.addHeader("Accept", "text/event-stream"); | |
const char* headers_to_collect[] = {"Location"}; | |
http.collectHeaders(headers_to_collect, 1); | |
USE_SERIAL.print("[HTTP] GET...\n"); | |
USE_SERIAL.printf("Mem before stream %d\n", ESP.getFreeHeap()); | |
int httpCode = http.sendRequest("GET",""); | |
USE_SERIAL.printf("Mem after stream %d\n", ESP.getFreeHeap()); | |
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); | |
while(httpCode == 307) { | |
USE_SERIAL.print("Redirect:"); | |
USE_SERIAL.println(http.header("Location")); | |
http.begin(http.header("Location"), kFirebaseFingerprint); | |
httpCode = http.GET(); | |
} | |
int itr = 1; | |
if(httpCode > 0) { | |
// HTTP header has been send and Server response header has been handled | |
//USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); | |
// file found at server | |
if(httpCode == HTTP_CODE_OK) { | |
// get lenght of document (is -1 when Server sends no Content-Length header) | |
int len = http.getSize(); | |
// create buffer for read | |
uint8_t buff[128] = { 0 }; | |
// get tcp stream | |
WiFiClient * stream = http.getStreamPtr(); | |
// read all data from server | |
while(http.connected() && (len > 0 || len == -1)) { | |
// get available data size | |
size_t size = stream->available(); | |
if(size) { | |
// read up to 128 byte | |
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); | |
// write it to Serial | |
//USE_SERIAL.write(buff, c); | |
USE_SERIAL.println(c); | |
if(len > 0) { | |
len -= c; | |
} | |
} | |
delay(250); | |
if (itr++ % 30 == 0) { | |
post(); | |
} | |
} | |
USE_SERIAL.println(); | |
USE_SERIAL.print("[HTTP] connection closed or file end.\n"); | |
} | |
} else { | |
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment