Connecting Kidbright board to Firebase

Anya A.
5 min readMar 27, 2020

--

This tutorial will demonstrate how to fetch temperature value from a built-in sensor and send it to a firebase database. We are using the Arduino IDE as an integrated development environment.

What you need:

  1. a Kidbright board (see more: https://medium.com/@anyapava/introduction-to-kidbright-board-24c596ed3b37)
  2. Arduino IDE (can be downloaded from https://www.arduino.cc/en/main/software)

Setting up a Firebase database

Firebase is Google’s cloud storage platform that provides a real-time database that lets you store and sync data in realtime. Data is stored as JSON. To configure database:

  1. Go to the firebase console and click +Add project

2. Once created, you can view the URL of the database by clicking on “Realtime Database” from the left panel. Copy the URL without [https://] for example: led-control-xxxxx.firebaseio.com, Write this value down. This value will later be used later to #define FIREBASE_HOST “led-control-xxxxx.firebaseio.com”

3. Select “Project Setting” from the left panel. Navigate to Service accounts tab > Database secrets. Copy the database secret value, write this value down. This value will later be used to #define FIREBASE_AUTH “DATABASE_SECRET_VALUE”

4. We can now proceed to the next section of the tutorial

Sending temperature values to a firebase database

  1. Open Arduino IDE
  2. Make sure that ESP32 board manager is installed on Arduino IDE if not please follow this tutorial to install additional board manager before proceeding to the next step > https://medium.com/@anyapava/tutorial-programming-kidbright-using-arduino-ide-450ac8994ecb
  3. Select Kidbright input serial port and select board as Node32s from Tools tab on the top bar menu
  4. Additional libraries are required. These libraries include Adafruit_GFX_Library, Adafruit_LED_Backpack, ArduinoJson-v5.13.3, IOXhop_FirebaseESP32, NTPClient. All libraries can be downloaded from https://my.pcloud.com/publink/show?code=XZgzxMkZEviMIiUgK2BQBRdJNLiHiR78MnIX. These libraries can be installed by navigating to the top bar menu Sketch → Include Library →Add .ZIP libraries to include the libraries one by one. The other way is to extract all libraries in a folder name libraries which is usually situated inside the Arduino folder.
  5. Once the libraries are successfully installed. Modify the following code to add database URL and database secret obtained from the previous sections as well as WIFI network credentials and upload it to your Kidbright board.
//This code wite for Kidbright that use ESP32 
//Please make sure these libraries are in folder of arduino. #\Documents\Arduino\libraries
#include <WiFi.h> //ESP32 libraries
#include <WiFiUdp.h>
#include <NTPClient.h> //NTP libraries#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h" //LED matrix 16*8 libraries
#include <IOXhop_FirebaseESP32.h> //Firebase libraries
#define LM73_ADDR 0x4D
// Replace with your Firebase URL and API Key
#define FIREBASE_HOST "" //please paste in this form not include https://
#define FIREBASE_AUTH ""
// Replace with your network credentials
const char* ssid = "";
const char* password = "";
int analog_value = 0;
double temp=0;
float readTemperature() { //Temperature sensor on kidbright board
Wire1.beginTransmission(LM73_ADDR);
Wire1.write(0x00); // Temperature Data Register
Wire1.endTransmission();

uint8_t count = Wire1.requestFrom(LM73_ADDR, 2);
float temp = 0.0;
if (count == 2) {
byte buff[2];
buff[0] = Wire1.read();
buff[1] = Wire1.read();
temp += (int)(buff[0]<<1);
if (buff[1]&0b10000000) temp += 1.0;
if (buff[1]&0b01000000) temp += 0.5;
if (buff[1]&0b00100000) temp += 0.25;
if (buff[0]&0b10000000) temp *= -1.0;
}
return temp;
}
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
Adafruit_8x16minimatrix matrix = Adafruit_8x16minimatrix();void setup() {
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

matrix.begin(0x70); // pass in the address
matrix.setTextSize(1);
matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
matrix.setTextColor(LED_ON);
matrix.setRotation(1);

// Initialize Serial Monitor
Serial.begin(115200);
Wire1.begin(4, 5);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server

Serial.println("");
Serial.println("WiFi connected."); //If wifi connected print connected in LED matrix 16*8
for (int x=17; x>=-90; x--) {
matrix.clear();
matrix.setCursor(x,0);
matrix.print("WiFi Connected!");
matrix.writeDisplay();
delay(60);
}
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(25200); //GMT +7 = 25200
}
void loop() {


while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
Serial.println(formattedDate);
// Extract date
int splitGap = formattedDate.indexOf(" ");
dayStamp = formattedDate.substring(0, splitGap);
Serial.print("Date: ");
Serial.println(dayStamp);

//Extract time
timeStamp = formattedDate.substring(splitGap+1, formattedDate.length());
Serial.print("Time: ");
Serial.println(timeStamp);
//Extract temperature
temp = readTemperature();
Serial.print("Temp: ");
Serial.println(temp);
//Extract LDR on kidbright board
analog_value = analogRead(36);
if(analog_value > 1000){
analog_value = 1000;
}
int LDR = (1000 - analog_value)*100/1000;

//Form data packet before send to firebase
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["temperature"] = temp;
root["light"] = LDR;
root["time"] = formattedDate;
Firebase.push("Sensor2", root); //In this case child name in firebase is Sensor2

// handle error
if (Firebase.failed()) {
Serial.print("pushing to firebase failed:");
Serial.println(Firebase.error());
return;
}
Serial.print("pushed to firebase!");

for (int x=17; x>=-90; x--) { //Print day in LED matrix 16*8
matrix.clear();
matrix.setCursor(x,0);
matrix.print("Date:");
matrix.println(dayStamp);
matrix.writeDisplay();
delay(60);
}

for (int x=17; x>=-77; x--) { //Print time in LED matrix 16*8
matrix.clear();
matrix.setCursor(x,0);
matrix.print("Time:");
matrix.println(timeStamp);
matrix.writeDisplay();
delay(60);
}

for (int x=17; x>=-50; x--) { //Print temperature in LED matrix 16*8
matrix.clear();
matrix.setCursor(x,0);
matrix.print("*C ");
matrix.println(temp);
matrix.writeDisplay();
delay(60);
}
for (int x=17; x>=-50; x--) { //Print LDR in LED matrix 16*8
matrix.clear();
matrix.setCursor(x,0);
matrix.print("Light:");
matrix.println(LDR);
matrix.writeDisplay();
delay(60);
}
}

6. The value fetched from the built-in temperature sensor of kidbright board should be sent to the firebase database in a real-time manner. This value can be viewed by selecting “Realtime Database” from the left panel.

--

--

No responses yet