MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
firebase_logger.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "firebase_logger.h"
16 
17 //Provide the token generation process info.
18 #include "addons/RTDBHelper.h"
19 #include "addons/TokenHelper.h"
20 
21 void FireLogger::print_error()
22 {
23  Serial.printf("Error:\ncode: %d\nmsg: ", fbdo_.errorCode());
24  Serial.printf("HTTP code: %d\n", fbdo_.httpCode());
25  Serial.printf(
26  "Firebase http connected: %s\n", fbdo_.httpConnected() ? "true" : "false");
27  Serial.println(fbdo_.errorReason());
28 }
29 
30 bool FireLogger::is_keep_alive() { return fbdo_.isKeepAlive(); }
31 
32 bool FireLogger::is_connected() { return fbdo_.httpConnected(); }
33 
34 FireLogger::FireLogger() : path_prefix_("/users/") {}
35 
37  const char * fire_url, const char * fire_token, const char * email,
38  const char * pass)
39 {
40  while (WiFi.status() != WL_CONNECTED) {
41  Serial.println("WiFi not connected ...");
42  delay(1000);
43  }
44  Serial.print("WiFi connected at IP: ");
45  Serial.println(WiFi.localIP());
46  Serial.println("Signing up to Firebase");
47 
48  config_.database_url = fire_url;
49  config_.api_key = fire_token;
50  auth_.user.email = email;
51  auth_.user.password = pass;
52 
53  fbdo_.setResponseSize(4096);
54 
55  /* Assign the callback function for the long running token generation task */
56  config_.token_status_callback =
57  tokenStatusCallback; // see addons/TokenHelper.h
58  // Assign the maximum retry of token generation
59  config_.max_token_generation_retry = 5;
60 
61  Firebase.begin(&config_, &auth_);
62  // Getting the user UID might take a few seconds
63  Serial.println("Getting User ID");
64  while ((auth_.token.uid) == "") {
65  Serial.print('.');
66  delay(1000);
67  }
68  Serial.println("Got User ID");
69  path_prefix_ += auth_.token.uid.c_str();
70  path_prefix_ += "/";
71 }
72 
73 MB_String FireLogger::full_path(const char * path)
74 {
75  MB_String temp = path_prefix_.c_str();
76  return temp += path;
77 }
78 
79 void FireLogger::set_int(const char * path, int64_t value)
80 {
81  if (!Firebase.RTDB.setInt(&fbdo_, full_path(path), value)) {
82  print_error();
83  }
84 }
85 
86 void FireLogger::set_float(const char * path, double value)
87 {
88  if (!Firebase.RTDB.setDouble(&fbdo_, full_path(path), value)) {
89  print_error();
90  }
91 }
92 
93 void FireLogger::set_string(const char * path, const char * value)
94 {
95  if (!Firebase.RTDB.setString(&fbdo_, full_path(path), value)) {
96  print_error();
97  }
98 }
99 
100 void FireLogger::set_bool(const char * path, bool value)
101 {
102  if (!Firebase.RTDB.setBool(&fbdo_, full_path(path), value)) {
103  print_error();
104  }
105 }
106 
107 void FireLogger::push_time(const char * path, time_t timestamp)
108 {
109  if (!Firebase.RTDB.pushInt(&fbdo_, full_path(path), timestamp)) {
110  print_error();
111  }
112 }
113 
114 bool FireLogger::is_ready() { return Firebase.ready(); }
115 
117 {
118  if (Firebase.isTokenExpired()) {
119  Firebase.refreshToken(&config_);
120  Serial.println("Refreshed token");
121  return true;
122  }
123  return false;
124 }
bool is_connected()
Get http server connection status.
void begin(const char *fire_url, const char *fire_token, const char *email, const char *pass)
initialise and authorise system to use Firebase RTDB (real time database) make sure wifi is initialis...
void set_float(const char *path, double value)
Set a float value.
bool is_ready()
Check if authenticated.
void set_int(const char *path, int64_t value)
Set an integer value.
void set_bool(const char *path, bool value)
Set a bool value.
bool is_keep_alive()
Get TCP keepalive status.
bool check_and_refresh_token()
refresh token if expired
void set_string(const char *path, const char *value)
Set a string value.
void push_time(const char *path, time_t timestamp)
Push a timestamp into a list to a relative path, useful to log times like when initialisation was com...