Created
July 29, 2020 22:40
-
-
Save alexastrum/88ef0c60d156f748a4ac04d26763a28d 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
#define WIFI_SSID "" | |
#define WIFI_PASSWORD "" | |
// Firebase | |
#define DEVICE_ID "TestDevice" | |
#define PROJECT_ID "" |
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
void displayText(String msg) { Serial.println(msg); } | |
void blinkLED(int duration) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(duration); | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(100); | |
} | |
void displayStatus(String msg) { | |
displayText(msg); | |
blinkLED(100); | |
} | |
void displayError(String msg) { | |
displayText("Error: " + msg); | |
blinkLED(300); | |
} | |
void displaySuspend(String msg) { | |
displayText("Execution suspended: " + msg); | |
digitalWrite(LED_BUILTIN, HIGH); // Turn on LED. | |
while (true) { | |
// Stop execution. | |
} | |
} | |
void setupDisplay() { | |
pinMode(LED_BUILTIN, OUTPUT); | |
Serial.begin(115200); | |
if (!Serial) { | |
// Allow enough time for our Serial Monitor to connect to the serial port. | |
delay(3000); | |
} | |
if (Serial) { | |
Serial.println("\nSerial port connected."); | |
} | |
} |
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 <ArduinoHttpClient.h> | |
bool firebaseDatabasePut(String path, DynamicJsonDocument jsonDoc, String idToken = "") { | |
String jsonStr; | |
serializeJson(jsonDoc, jsonStr); | |
displayStatus("Saving to RTDB: " + path + " = " + jsonStr); | |
String host = String(PROJECT_ID) + ".firebaseio.com"; | |
WiFiSSLClient wifiClient; | |
HttpClient httpClient = HttpClient(wifiClient, host, 443); | |
String url = "/" + path + ".json"; | |
if (idToken != "") { | |
url = url + "?auth=" + idToken; | |
} | |
httpClient.put(url, "application/json", jsonStr); | |
int statusCode = httpClient.responseStatusCode(); | |
String response = httpClient.responseBody(); | |
if (statusCode != 200) { | |
displayError(String(statusCode) + " " + response); | |
return false; | |
} | |
displayStatus("Saved to RTDB."); | |
return true; | |
} |
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 "config.h" | |
#include "display.h" | |
#include "wifi.h" | |
#include "json.h" | |
#include "firebase_database.h" | |
void setup() | |
{ | |
setupDisplay(); | |
setupWiFi(); | |
} | |
void loop() | |
{ | |
DynamicJsonDocument json(1024); | |
json["hello"] = "world"; | |
firebaseDatabasePut(DEVICE_ID, json); | |
displaySuspend("All done!"); | |
} |
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 <ArduinoJson.h> | |
/** | |
* Converts a String into a JsonDocument of correct size. | |
* In case of error, the returned `jsonDoc.isNull()` will be true. | |
*/ | |
DynamicJsonDocument toJsonDocument(String str, int size = 0) { | |
if (size == 0) { | |
size = JSON_OBJECT_SIZE(1) + str.length(); | |
} | |
DynamicJsonDocument jsonDoc(str.length() * 2); | |
DeserializationError error = deserializeJson(jsonDoc, str); | |
if (error) { | |
displayError("JSON " + String(error.c_str()) + ": " + str); | |
jsonDoc.clear(); | |
} | |
return jsonDoc; | |
} |
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 <WiFiNINA.h> | |
#include <utility/wifi_drv.h> | |
/** | |
* Re-initialize the WiFi driver. | |
* This is currently necessary to switch from BLE to WiFi. | |
*/ | |
void resetWiFi() { | |
wiFiDrv.wifiDriverDeinit(); | |
wiFiDrv.wifiDriverInit(); | |
} | |
void connectToWiFi() | |
{ | |
int status = WiFi.status(); | |
if (status == WL_CONNECTED) | |
{ | |
return; | |
} | |
displayStatus("Connecting to WiFi..."); | |
WiFi.setHostname(DEVICE_ID); | |
while(true) { | |
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
if (status == WL_CONNECTED) { | |
break; | |
} | |
displayError("Retrying in 5 seconds..."); | |
delay(5000); | |
resetWiFi(); | |
} | |
displayStatus("Connected to WiFi."); | |
} | |
void setupWiFi() | |
{ | |
int status = WiFi.status(); | |
if (status == WL_NO_SHIELD) | |
{ | |
displaySuspend("WiFi shield missing!"); | |
} | |
if (status == WL_NO_MODULE) | |
{ | |
displaySuspend("Communication with WiFi module failed!"); | |
} | |
if (WiFi.firmwareVersion() < WIFI_FIRMWARE_LATEST_VERSION) | |
{ | |
displayStatus("Please upgrade WiFi firmware!"); | |
} | |
connectToWiFi(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment