#include <Arduino.h>
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
// Provide the token generation process info.
#include <addons/TokenHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "DobraKava"
#define WIFI_PASSWORD "0991119900"
/* 2. Define the API Key */
#define API_KEY "AIzaSyBZhysDKAvQAEYKKePbcrDNbxII80aZrd8"
/* 3. Define the project ID */
#define FIREBASE_PROJECT_ID "cupcontrol-e0bb9"
/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "123456"
const int dataPin = 14; /* Q7 */
const int clockPin = 26; /* CP */
const int latchPin = 27; /* PL */
bool btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn1shift=false;
const int numBits = 8; /* Set to 8 * number of shift registers */
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
bool taskCompleted = false;
int countall,count1grop,count2grop,count3grop = 0;
unsigned long dataMillis = 0;
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
WiFiMulti multi;
#endif
void setup()
{
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
multi.addAP(WIFI_SSID, WIFI_PASSWORD);
multi.run();
#else
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
#endif
Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
if (millis() - ms > 10000)
break;
#endif
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
// The WiFi credentials are required for Pico W
// due to it does not have reconnect feature.
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
config.wifi.clearAP();
config.wifi.addAP(WIFI_SSID, WIFI_PASSWORD);
#endif
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
#if defined(ESP8266)
// In ESP8266 required for BearSSL rx/tx buffer for large data handle, increase Rx size as needed.
fbdo.setBSSLBufferSize(2048 /* Rx buffer size in bytes from 512 - 16384 */, 2048 /* Tx buffer size in bytes from 512 - 16384 */);
#endif
// Limit the size of response payload to be collected in FirebaseData
fbdo.setResponseSize(2048);
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
// You can use TCP KeepAlive in FirebaseData object and tracking the server connection status, please read this for detail.
// https://github.com/mobizt/Firebase-ESP-Client#about-firebasedata-object
// fbdo.keepAlive(5, 5, 1);
}
void loop()
{
// Step 1: Sample
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
if (bit == HIGH) {
Serial.print("1");
if (i==1) {btn1=true;}
if (i==2) {btn2=true;}
if (i==3) {btn3=true;}
if (i==4) {btn4=true;}
if (i==5) {btn5=true;}
if (i==6) {btn6=true;}
if (i==7) {btn7=true;}
if (i==8) {btn8=true;}
} else {
Serial.print("0");
if (i==1) {btn1=false;}
if (i==2) {btn2=false;}
if (i==3) {btn3=false;}
if (i==4) {btn4=false;}
if (i==5) {btn5=false;}
if (i==6) {btn6=false;}
if (i==7) {btn7=false;}
if (i==8) {btn8=false;}
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
Serial.println();
delay(1000);
if (btn1==true && btn1shift==false) { count1grop++; btn1shift=true; } else {btn1shift=false;}
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready() && (millis() - dataMillis > 6000 || dataMillis == 0))
{
dataMillis = millis();
String documentPath = "devices/nfm4rrPuw342Ea0P2rSE";
String mask = "Newname";
// If the document path contains space e.g. "a b c/d e f"
// It should encode the space as %20 then the path will be "a%20b%20c/d%20e%20f"
Serial.print("Get a document... ");
if (Firebase.Firestore.getDocument(&fbdo, FIREBASE_PROJECT_ID, "", documentPath.c_str(), mask.c_str()))
Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
else
Serial.println(fbdo.errorReason());
if (!taskCompleted)
{
taskCompleted = false;
// For the usage of FirebaseJson, see examples/FirebaseJson/BasicUsage/Create_Edit_Parse/Create_Edit_Parse.ino
FirebaseJson content;
content.clear();
countall++;
content.set("fields/altname/stringValue", "Newname");
content.set("fields/counter/integerValue", String(count1grop+count2grop+count3grop).c_str());
content.set("fields/grop1counter/integerValue", String(count1grop).c_str());
content.set("fields/boilertemp/integerValue", "123");
content.set("fields/grop1counter/integerValue", "459076");
// content.set("fields/Japan/mapValue/fields/population/integerValue", "125570000");
// content.set("fields/Belgium/mapValue/fields/time_zone/integerValue", "1");
// content.set("fields/Belgium/mapValue/fields/population/integerValue", "11492641");
// content.set("fields/Singapore/mapValue/fields/time_zone/integerValue", "8");
// content.set("fields/Singapore/mapValue/fields/population/integerValue", "5703600");
// info is the collection id, countries is the document id in collection info.
String documentPath = "devices/nfm4rrPuw342Ea0P2rSE";
Serial.print("Create document... ");
// if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "" /* databaseId can be (default) or empty */, documentPath.c_str(), content.raw()))
// Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
// else
// Serial.println(fbdo.errorReason());
if (Firebase.Firestore.patchDocument(&fbdo, FIREBASE_PROJECT_ID, "" /* databaseId can be (default) or empty */, documentPath.c_str(), content.raw(), "counter,boilertemp,grop1counter" /* updateMask */))
Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
else
Serial.println(fbdo.errorReason());
}
}
}