firebase使用_使用Firebase进行物联网原型设计:如何事半功倍

firebase使用

by Bayrem Gharssellaoui

由Bayrem Gharssellaoui

使用Firebase进行物联网原型设计:如何事半功倍 (Internet of Things prototyping with Firebase: how to do more with less)

IoT is all about connecting devices, or “things” as you may call them, to the internet and then analyzing data collected from these devices in order to extract an added value. In this article you’ll discover how to benefit from Firebase when working on an IoT project and how it can help you develop IoT proto* much faster and easier than traditional methods. You will also develop your own IoT project and host it in Firebase.

物联网就是将设备或您可能称为“物”的设备连接到互联网,然后分析从这些设备收集的数据以提取附加值。 在本文中,您将发现在进行IoT项目时如何从Firebase中受益,以及它如何帮助您比传统方法更快,更轻松地开发IoT原型。 您还将开发自己的IoT项目并将其托管在Firebase中。

在Firebase实时数据库之前 (Before Firebase Realtime Database)

Most of my IoT projects require some way of communication between the different endpoints. These endpoints can be anything from devices and services to applications, and eventually data needs to be stored somewhere for further processing and analyzing.

我的大多数物联网项目都需要不同端点之间的某种通信方式。 这些端点可以是从设备,服务到应用程序的任何事物,终需要将数据存储在某个地方以进行进一步的处理和分析。

So let’s say you want to build an IoT system where a device will measure temperature and humidity values from sensors and send them to a database service to store them. Then you want to have a web application that will fetch these values and display them in a dashboard. Quite simple, right…?

假设您要构建一个物联网系统,其中设备将测量传感器的温度和湿度值,并将其发送到数据库服务以进行存储。 然后,您想要一个Web应用程序,该应用程序将获取这些值并将其显示在仪表板中。 很简单,对吧?

The easy and lightweight way to go about this is to setup an MQTT broker that will act as a hub and reroute all incoming messages published from the device to all subscribed clients like the web application in this case.

解决此问题的简单轻便的方法是设置一个MQTT代理,该代理将充当集线器,并将从设备发布的所有传入消息重新路由到所有订阅的客户端(例如Web应用程序)。

Now the question here is how can the web application display the data? Meaning will it be displaying messages coming from the broker directly or will it be fetching data from a database service?

现在,这里的问题是Web应用程序如何显示数据? 意味着它将显示直接来自代理的消息,还是从数据库服务中获取数据?

Let’s say that you want the app to do both things: display data coming from the broker in realtime and fetch data from the database. In this case you can think of 2 ways (actually there are many different ways) to achieve this:

比方说,你想要的应用程序做两件事:显示数据从代理来实时从数据库中获取数据。 在这种情况下,您可以想到两种方法(实际上有很多不同的方法)可以实现这一目的:

First solution:

个解决方案:

Using this architecture the device will first publish its data to the broker then it will send an HTTP request to the database web-service to save the data. For this solution the device needs to implement 2 clients: an MQTT and an HTTP client.

使用这种架构,设备将首先将其数据发布到代理,然后将HTTP请求发送到数据库Web服务以保存数据。 对于此解决方案,设备需要实现2个客户端:MQTT和HTTP客户端。

Second solution:

第二种解决方案:

The other way to go about this is that the device will send or publish its data to the broker, then the broker (as expected) will reroute this message to all connected subscribers like the web app. But this time there’s another subscriber connected which represents an API Engine that will accept this data and send it to the database web-service to be stored.

解决此问题的另一种方法是设备将数据发送或发布给代理,然后代理(如预期的那样)会将此消息重新路由到所有连接的订户(如Web应用程序)。 但是这一次连接了另一个订阅者,该订阅者代表一个API引擎,该引擎将接受此数据并将其发送到要存储的数据库Web服务。

As you may have noticed in this solution, the HTTP client is decoupled from the device and implemented as a backend service. This way you make the device program much lighter. This is an important thing to keep in mind when developing on constrained IoT devices where resources like CPU and memory are limited.

如您在该解决方案中可能已经注意到的,HTTP客户端与设备分离,并实现为后端服务。 这样,您可以使设备程序更轻松。 在受限的物联网设备上进行开发时要牢记这一点,该设备的CPU和内存等资源有限。

Still, this solution required some additional work in developing the back-end service that will act as a persistence layer.

尽管如此,此解决方案仍需要在开发后端服务中做一些额外的工作,这些后端服务将充当持久层。

So is there much easier way of doing this?

那么有没有更简单的方法可以做到这一点?

救火基地 (Firebase to the rescue)

As you may have seen above, things can get quite complex quite easily. For someone like me who wants to get up and running quickly when working on a prototype, that can take some extra time. That’s why, in this part of the article, you’ll see how Firebase can make your life easier and save you a lot of time when developing an IoT prototype.

正如您可能已经在上面看到的,事情很容易变得很复杂。 对于像我这样想要在启动原型时快速启动并运行的人来说,这可能会花费一些额外的时间。 这就是为什么在本文的这一部分中,您将了解Firebase如何在开发IoT原型时使您的生活更轻松并节省大量时间。

Firebase offers many cloud services that ranges from authentication, storage, and cloud functions to hosting your web application. In this article you’ll use 2 services: Realtime Database and Hosting.

Firebase提供了许多云服务,范围从身份验证,存储和云功能到托管Web应用程序。 在本文中,您将使用两种服务:实时数据库和托管。

Let’s start with Firebase Realtime Database. The first thing that comes to mind when reading this service name is: okay so I know what a database is but what does it mean to be realtime here?

让我们从Firebase Realtime Database开始。 读取此服务名称时首先想到的是:好的,我知道什么是数据库,但是在这里实时是什么意思?

Well according to Wikipedia:

根据维基百科:

real-time database is a database system which uses real-time processing to handle workloads whose state is constantly changing. This differs from traditional databases containing persistent data, mostly unaffected by time

实时数据库是使用实时处理来处理状态不断变化的工作负载的数据库系统。 这与包含持久性数据的传统数据库不同,传统数据库通常不受时间影响

In the case of Firebase Realtime Database, clients will be connected to the database and will maintain an open bidirectional connection via websockets. Then if any client pushes data to the database it will be triggered and (in this case) inform all connected clients that it has been changed by sending them the newly saved data.

对于Firebase Realtime数据库,客户端将连接到数据库,并将通过websockets保持开放的双向连接。 然后,如果有任何客户端将数据推送到数据库,它将被触发并(在这种情况下)通过向它们发送新保存的数据来通知所有已连接的客户端它已被更改。

This way of working may remind you of the MQTT broker and how it reacts when it receives a message from a publisher and sends it to all subscribers. The difference this time is the addition of the data persistent part, which is the database. So as you can see here, you don’t need to route messages yourself using other protocols — Firebase Realtime Database will take care of that plus performing its normal database function. Amazing isn’t it?

这种工作方式可能会让您想起MQTT代理,以及它在收到来自发布者的消息并将其发送给所有订阅者时的React。 这次的区别是增加了数据持久性部分,即数据库。 因此,如您在这里看到的,您不需要使用其他协议自己路由消息-Firebase Realtime Database将解决此问题并执行其正常的数据库功能。 是不是很神奇?

Returning to the IoT system mentioned earlier, you can now connect the device to the Firebase Realtime Database and make it push data periodically to the database. On the other part of the system you have a web application which will be connected to the same service as the device and will receive new data whenever there is a change in the database.

回到前面提到的物联网系统,您现在可以将设备连接到Firebase实时数据库,并使其定期将数据推送到数据库。 在系统的另一部分,您有一个Web应用程序,该应用程序将连接到与该设备相同的服务,并且每当数据库发生更改时,它将接收新数据。

But what about hosting the web application?

但是托管 Web应用程序呢?

Firebase offers a hosting service that you can use to host your app rather than managing your own web server and dealing with deployment and networking configurations. The good thing is that it is free (yet limited) and pretty easy to use.

Firebase提供了一项托管服务,您可以使用该托管服务托管应用程序,而不必管理自己的Web服务器以及处理部署和网络配置。 好消息是它是免费的(但还很有限),并且非常易于使用。

Now for the part you all have been waiting for. In this demo you’ll use the example discussed throughout this article and bring it to life.

现在,您一直在等待的一部分。 在本演示中,您将使用本文中讨论的示例并将其变为现实。

If you remember, the IoT system is made of 2 endpoints: the first one is the IoT device which is responsible for sending temperature and humidity data to the Firebase Realtime Database. This in turn will communicate with the second endpoint — the web application — that will read the data and display it on a nice dashboard.

请记住,物联网系统由两个端点组成 :个是物联网设备 ,负责将温度和湿度数据发送到Firebase实时数据库。 反过来,这将与第二个端点( Web应用程序)进行通信,该端点将读取数据并将其显示在漂亮的仪表板上。

I’ll break this project into 3 steps so it becomes easier to follow.

我将这个项目分为3个步骤,以使其更容易理解。

1.设置Firebase实时数据库 (1. Setting up Firebase Realtime Database)

There is nothing special about this step. You just need to go to your firebase console and create a new project. After your project is ready, go to the database section and make sure to create a Realtime Database not a cloud Firestore Database. Select start in test mode and proceed as you’ll be using this database only for testing and prototyping and not for production solutions (so you can ignore the red warning). Now the database should be ready for use.

此步骤没有什么特别的。 您只需要转到Firebase控制台并创建一个新项目。 项目准备就绪后,请转到“数据库”部分,并确保创建Realtime数据库而不是Cloud Firestore数据库。 选择“以测试模式启动”,然后继续进行操作,因为您将使用此数据库仅用于测试和原型设计,而不用于生产解决方案(因此您可以忽略红色警告)。 现在,数据库应该可以使用了。

2.开发物联网设备应用程序 (2. Developing the IoT device application)

When talking about embedded systems development, you often hear terms like low level programming, assembly, registers, memory management and so on. These terms and concepts are related to the hardware specifics you’re working with and can change from one to another.

在谈论嵌入式系统开发时,您经常会听到诸如底层编程,汇编,寄存器,内存管理等术语。 这些术语和概念与您正在使用的硬件详细信息有关,并且可以彼此不同。

That’s why, when prototyping ideas, you don’t have time to dig deep into these specifics and study them in details because you’ll be dealing at the same time with other high level languages and thus other ways of thinking about code. So instead you should have a clear overall idea about the architecture and characteristics of the device and how to use them.

这就是为什么在对概念进行原型制作时,您没有时间去深入研究这些细节并进行详细研究,因为您将同时与其他语言以及代码的其他思考方式打交道。 因此,您应该对设备的体系结构和特性以及如何使用它们有一个清晰的总体思路。

Luckily for you there is a platform that was made specially for prototyping and abstracting all the low level functionalities of hardware. Yes I’m talking about the famous Arduino.

幸运的是,您有一个专门用于原型设计和抽象硬件的所有低级功能的平台。 是的,我在谈论的Arduino 。

Arduino - HomeOpen-source electronic prototyping platform enabling users to create interactive electronic objects.www.arduino.cc

Arduino-家庭 开源电子原型平台,使用户能够创建交互式电子对象。 www.arduino.cc

Note here that when I’m talking about Arduino, I’m referring to the Arduino Framework with its IDE, compilers, and libraries — not the board.

请注意,当我谈论Arduino时,是指Arduino框架及其IDE,编译器和库,而不是开发板。

So you will be using Arduino to program the board, but what board you ask? Well, you need a board that is able to interface with sensors and measure physical values like temperature. It should also be able to connect to the internet in order to send this data to the database. Finally, it should be able to be programmed using the Arduino framework.

因此,您将使用Arduino对电路板进行编程,但是您要问什么板子? 好吧,您需要一块能够与传感器连接并测量诸如温度之类的物理值的电路板。 它还应该能够连接到Internet,以便将这些数据发送到数据库。 后,它应该能够使用Arduino框架进行编程。

There are a variety of boards on the market that can achieve these tasks. Some of them are Arduino boards and others are Arduino-compatible boards.

市场上有许多可以完成这些任务的电路板。 其中一些是Arduino板,另一些是Arduino兼容板。

In this demo you will use the famous NodeMCU, an Arduino compatible board. It’s based on the ESP8266 SoC, a chipset produced by the Shanghai-based Chinese manufacturer, Espressif Systems. This board is attractive for developers, as single units can be bought for as little as $3.

在本演示中,您将使用的NodeMCU,这是Arduino兼容板。 它基于ESP8266 SoC,该芯片组由总部位于上海的中国制造商Espressif Systems生产。 该板对开发人员具有吸引力,因为单个单元的价格仅为3美元。

我们的装备 (Our Gear)

Although the Arduino platform offers an IDE for programming and uploading code to boards, it’s not very friendly to developers as it does not provide any intelliSense features or debugging functionalities. That’s why, for most my IoT projects, I use an environment called PlatformIO.

尽管Arduino平台提供了一个IDE来进行编程并将代码上传到板上,但由于它不提供任何intellelliSense功能或调试功能,因此对开发人员不是很友好。 因此,对于我的大多数物联网项目,我都使用称为PlatformIO的环境

PlatformIO: An open source ecosystem for IoT developmentCross-platform build system and library manager. Cross-platform IDE and unified debugger. Remote unit testing and…platformio.org

PlatformIO:用于物联网开发的开源生态系统 跨平台构建系统和库管理器。 跨平台IDE和统一调试器。 远程单元测试和… platformio.org

It’s an open source ecosystem for IoT development, and guess what? It supports the Arduino Framework. So you can use it to write Arduino code, compile it, and upload it to the board. Finally the cool thing about PlatformIO is that it comes as an extension that you can use inside Atom or VScode so you can use it alongside the other functionalities of your IDE (Atom or VScode). I highly recommend watching these 2 YouTube video tutorials to setup and get familiar with the environment.

这是一个用于物联网开发的开源生态系统,您猜怎么着? 它支持Arduino框架。 因此,您可以使用它来编写Arduino代码,对其进行编译,然后将其上传到开发板上。 后,关于PlatformIO的很棒的事情是,它是一个扩展 ,可以在AtomVScode中使用,因此可以将其与IDE的其他功能(Atom或VScode)一起使用。 我强烈建议您观看这2个YouTube视频教程,以设置并熟悉环境。

Enough talk — let’s get started:

足够多的谈话-让我们开始吧:

为NodeMCU开发固件 (Developing Firmware for NodeMCU)

kaizoku-619/firebase_nodemcuArduino Firebase Nodemcu client developed sending DHT11 data to Firebase Realtime Database …github.com

kaizoku-619 / firebase_nodemcu开发了Arduino Firebase Nodemcu客户端,将DHT11数据发送到Firebase Realtime Database… github.com

Before diving into developing the firmware, let’s talk about the electronics setup.

在深入开发固件之前,让我们谈谈电子设备的设置。

If you remember from the example earlier, the IoT device will measure temperature and humidity values from a sensor and send them to the cloud. In this case that means the NodeMCU will read temperature and humidity values from DHT11 sensor module and send them to Firebase. The DHT11 module will be used here because it’s cheap and doesn’t require any additional electronic components to function.

如果您还记得前面的示例,则IoT设备将测量传感器的温度和湿度值并将其发送到云中。 在这种情况下,这意味着NodeMCU将从DHT11传感器模块读取温度和湿度值,并将其发送到Firebase。 DHT11模块将在这里使用,因为它很便宜并且不需要任何其他电子组件即可工作。

As you can see from the wiring diagram above, DHT11 is connected to the board with 3 wires GND3.3V and Data signal in the middle. Connect the data pin to one of the Dx pins on the board and you’re done with wiring.

从上面的接线图中可以看出,DHT11连接到板上,中间有3线GND , 3.3V数据信号 。 将数据引脚连接到板上的Dx引脚之一,即可完成接线。

Now that the wiring is done you can start coding the firmware using PlatformIO.

接线完成后,您就可以开始使用PlatformIO对固件进行编码了。

But before you dig into writing code directly, let’s stop for a moment and think about what the program should do.

但是,在直接研究代码之前,让我们先停下来思考一下程序应该做什么。

So the first thing the device needs to do is to connect to the internet. To do that it needs to connect to the WiFi access point. Once online, it needs to open a connection with Firebase. After that the device will read temperature and humidity values from the sensor and finally publish the data.

因此,设备需要做的件事就是连接到互联网。 为此,它需要连接到WiFi接入点。 联机后,需要打开与Firebase的连接。 此后,设备将从传感器读取温度和湿度值,后发布数据 。

Here is an ordered list of tasks:

这是任务的有序列表:

(Note that the world task is used here to refer to a functionality of a bloc of code and not a task in realtime OS like FreeRTOS tasks.)

(请注意,这里的world 任务是指一组代码的功能,而不是像FreeRTOS任务这样的实时OS中的任务。)

1. Connect to WiFi2. Open connection with Firebase3. Read sensor values3. Publish values to Firebase

1.连接到WiFi 2.与Firebase打开连接 3.读取传感器值 3.将  发布到Firebase

Let’s start with the first task:

让我们从个任务开始:

  • Connect to WiFi:

    连接到WiFi:

This function begins with printing the SSID (AP name) and setting the NodeMCU into station mode rather than AP mode. Then it keeps loading until it connects to the AP. SSID and PASSWORD are 2 constants defined in another file as you’ll see later.

此功能从打印SSID (AP名称)并将NodeMCU设置为工作站模式而不是AP模式开始。 然后,它将一直加载直到连接到AP。 SSID和PASSWORD是在另一个文件中定义的2个常量,稍后您将看到。

  • Open connection with Firebase:

    与Firebase的开放连接:

This function is very straightforward: it takes 2 parameters FIREBASE_HOST and FIREBASE_AUTH. These are also 2 constants defined in another file.

此函数非常简单:它具有2个参数FIREBASE_HOSTFIREBASE_AUTH。 这些也是在另一个文件中定义的2个常量。

  • Read sensor values:

    读取传感器值:

You start by defining 2 constants for the DHT sensor type and pin. After that you create a DHT object by passing these 2 constants to the DHT constructor. Finally you use the object methods readHumidity() and readTemperature() to read humidity and temperature respectively.

首先,为DHT传感器类型和引脚定义2个常数。 之后,通过将这两个常量传递给DHT构造函数来创建DHT对象。 后,使用对象方法readHumidity()readTemperature()分别读取湿度和温度。

  • Publish values to Firebase:

    将值发布到Firebase:

Here, the pushInt() method of Firebase class is used to push an integer to Firebase and in case of an error, print it.

在这里,Firebase类的pushInt()方法用于将整数推送到Firebase,并在出现错误的情况下进行打印。

And now you’re done with the tasks! All that’s left is to put these tasks together in a clean arduino sketch.

现在您已经完成了任务! 剩下的就是将这些任务放到干净的arduino草图中。

Start by creating a new project in PlatformIO as shown here:

首先在PlatformIO中创建一个新项目,如下所示:

Next you need to install the libraries needed for the project. Note that in this case you will install the libraries locally, meaning they’re only available for this project. This is a best practice if you decided one day to use another version of the library in another project.

接下来,您需要安装项目所需的库 。 请注意,在这种情况下,您将在本地安装库这意味着它们仅可用于该项目。 如果您决定一天在另一个项目中使用该库的另一个版本,则这是佳实践。

Go to the PlatformIO home page → Libraries and search for firebase. FirebaseArduino will show up, so click on it. But don’t click install button, instead click on the  next to install and choose the project to install the library into. Then finally click install. Repeat this process for the DHT library.

转到PlatformIO主页→库,然后搜索firebase。 FirebaseArduino将显示,因此单击它。 但是不要单击安装按钮 ,而是单击安装旁边的并选择要将库安装到的项目。 然后后单击安装。 对DHT库重复此过程。

Copy this code in the main.cpp file:

将此代码复制到main.cpp文件中:

Remember the file containing the credentials for the WiFi and Firebase? Go to the include folder and create a new file Creds.h and copy this code to it. Don’t forget to change the code according to your credentials.

还记得包含WiFi和Firebase凭据的文件吗? 转到include文件夹并创建一个新文件Creds.h并将此代码复制到其中。 不要忘记根据您的凭据 更改代码。

And there you go! Upload the firmware to NodeMCU and your device should be able to send data to Firebase Realtime Database.

然后你去了! 将固件上传到NodeMCU ,您的设备应该能够将数据发送到Firebase Realtime Database 。

3.开发和托管Web应用程序 (3. Developing and hosting the web application)

kaizoku-619/firebase_iot_webFirebase web application client connected to Firebase Realtime database fetching sensor values …github.com

kaizoku-619 / firebase_iot_web 连接到Firebase Realtime数据库的Firebase Web应用程序客户端,获取传感器值… github.com

Now that the IoT device is ready and pushing data to the Realtime Database, you can move to the other endpoint in the system, the web application. It will receive data from Firebase and display it on a dashboard.

现在,IoT设备已准备就绪,并将数据推送到实时数据库,您可以移动到系统中的另一个端点,即Web应用程序。 它将从Firebase接收数据并将其显示在仪表板上。

You will start by developing the app locally and eventually deploying it.

您将首先在本地开发应用程序,然后终对其进行部署。

Start by setting up the project.

首先设置项目。

安装Firebase CLI: (Installing Firebase CLI:)

The Firebase CLI (command line interface) requires Node.js and npm (the Node Package Manager). Once they are installed you proceed to install the Firebase CLI using npm by running:

Firebase CLI (命令行界面)需要Node.jsnpm (节点程序包管理器)。 安装它们后,您可以通过运行以下命令使用npm安装Firebase CLI:

npm install -g firebase-tools

You can follow the video to complete the project setup.

您可以按照视频来完成项目设置。

Once project setup is done your directory should look something like this:

完成项目设置后,您的目录应如下所示:

├── database.rules.json├── firebase.json└── public   ├── 404.html   └── index.html

Start by building the dashboard UI. This will be the index.html file. Open the file and change it to look like this:

首先构建仪表板UI。 这将是index.html文件。 打开文件并将其更改为如下所示:

This is a simple HTML page designed using Bootstrap Material Design. It consists of 2 Card elements, one for displaying Humidity and the other for displaying Temperature.

这是一个使用Bootstrap Material Design设计的简单HTML页面。 它由2个卡元素组成,一个用于显示湿度,另一个用于显示温度。

As for the Firebase part here, first you import the firebase dependency in the script tag inside the head tag. Finally when the page finishes loading, it calls app.js.

至于此处的Firebase部分,首先需要在head标签内的script标签中导入firebase依赖项。 后,页面完成加载后,将调用app.js。

Now that the Dashboard UI is ready you can move on to app.js where you implement the firebase connection and your business logic. In the same directory create a new file called app.js and copy this code:

现在,仪表板用户界面已准备就绪,您可以继续执行app.js ,在其中实现Firebase连接和业务逻辑。 在同一目录中,创建一个名为app.js的新文件,然后复制以下代码:

The script starts by creating a configuration object. The best way is to copy it directly from the firebase project in the firebase console. To do that, navigate to your firebase console and go to the firebase project settings. Then scroll down and click on the <;/> icon as shown here:

该脚本首先创建一个配置对象。 好的方法是直接从firebase控制台中的firebase项目复制它。 为此,请导航至Firebase控制台,然后转到Firebase项目设置。 然后向下滚动并单击< ; />图标,如下所示:

I guess the rest of the code is self explanatory with the comments.

我想代码的其余部分是带有注释的自我解释。

Now your app should be ready and you can test it locally with the following command:

现在您的应用程序应该已经准备就绪,您可以使用以下命令在本地对其进行测试:

firebase serve

Note that on startup the app will fetch the last temperature and humidity values from the database. Great! The app is working locally, but up until now it’s working only inside your localhost and can’t be accessed from the outside world. This means that it’s time to host it on the web using Firebase Hosting.

请注意,在启动时,应用程序将从数据库中获取新的温度和湿度值。 大! 该应用程序在本地运行,但是直到现在,它仅在本地主机中运行,无法从外部访问。 这意味着是时候使用Firebase Hosting在网络上托管它了

But before you deploy it there is just one last thing you need to do. Go to the database.rules.json file and change the read and write rules to “true”. This method is not advised for production, as it is not secure, but it’s fine for demonstration purposes here.

但是在部署它之前,您只需要做后一件事。 转到database.rules.json文件,并将读取和写入规则更改为“ true ”。 不建议将此方法用于生产,因为它不安全,但此处适合演示用途。

With that done you are ready for deployment:

完成后,您就可以部署了:

firebase deploy

There you go! If you’ve made it this far, by now you should have a live web application deployed and up and running.

你去! 如果到目前为止您已经做到了,那么现在您应该已经部署了一个实时的Web应用程序,并且可以运行它。

全部放在一起 (Putting it all together)

It’s time to put everything together and test the system. Plug in the NodeMCU to your PC and upload the sketch if you haven’t already:

现在是时候将所有内容放在一起并测试系统了。 如果尚未将NodeMCU插入PC,然后上传草图:

Open up the web app and the firebase database and watch it change in realtime with the values sent by the device.

打开Web应用程序和firebase数据库,并使用设备发送的值实时查看其变化。

Now open up the serial monitor and watch the data pushed from the device to the web app. Note that you can open the console in the browser to watch the received values. Here I use Gtk Serial port terminal with a baud-rate of 115200 bps, but you can use the integrated PlatformIO Serial Monitor or any other tool of your choice.

现在,打开串行监视器 ,观察从设备推送到Web应用程序的数据。 请注意,您可以在浏览器中打开控制台以查看接收到的值。 在这里,我使用波特率为115200 bps的Gtk串行端口终端 ,但是您可以使用集成的PlatformIO串行监视器或您选择的任何其他工具。

故障排除 (Troubleshooting)

I faced a problem while developing the application on the ESP8266 (and it took me sometime to figure out). Even when everything was configured correctly (WiFi Connection, Firebase Host, Secret Key), the ESP8266 couldn’t connect to Firebase. This was caused by the wrong Fingerprint inside FirebaseHttpClient.h file in the Firebase library. You need to replace it with your own fingerprint. If you installed the library locally using PlatformIO then you can find the file in this path:

ESP8266上开发应用程序时遇到了一个问题(花了一些时间才弄清楚)。 即使正确配置了所有内容(WiFi连接,Firebase主机,密钥),ESP8266 也无法连接到Firebase。 这是由FirebaseHttpClient.h*文件中的错误指纹在火力地堡库造成的。 您需要用自己的指纹替换它。 如果使用PlatformIO在本地安装了库,则可以在以下路径中找到文件:

your_project_folder/.piolibdeps/FirebaseArduino_ID1259/src/

To generate a fingerprint, go to this website and copy your Firebase host link without the https part and click on Fingerprint Site (mine is: medium-iot-project.firebaseio.com):

生成指纹,请转到此网站并复制不带https的Firebase主机链接,然后单击“指纹站点”(我的站点是:medium-iot-project.firebaseio.com):

GRC | SSL TLS HTTPS Web Server Certificate FingerprintsGRC's HTTPS Web Server Certificate Fingerprint Servicewww.grc.com

GRC | SSL TLS HTTPS Web服务器证书指纹 GRC的HTTPS Web服务器证书指纹服务 www.grc.com

This will generate a fingerprint for your site, so go ahead and copy it in place of the old kFirebaseFingerprint[] value inside the FirebaseHttpClient.h file. That should fix the problem.

这将为您的站点生成一个指纹,因此请继续复制它,以代替FirebaseHttpClient.h*文件中的旧kFirebaseFingerprint []值。 那应该解决问题。

结论 (Conclusion)

Congratulations if you’ve made it this far!

恭喜,您已经做到了!

We’ve only scratched the surface here. The Internet of Things is all about experimenting and exploring, so I encourage you to not stop here and keep learning by making mistakes and finding bugs in your code. But above all don’t forget to have fun along your learning journey.

我们只在这里刮了一下表面。 物联网是所有关于试验和探索的事情,因此,我鼓励您不要在这里停留并通过在代码中发现错误和发现错误来继续学习。 但重要的是,不要忘记在学习过程中获得乐趣。

参考文献 (References)

Firebase Realtime Database | Firebase Realtime Database | FirebaseStore and sync data with our NoSQL cloud database. Data is synced across all clients in realtime, and remains available…firebase.google.comPlatformIO: An open source ecosystem for IoT developmentCross-platform build system and library manager. Cross-platform IDE and unified debugger. Remote unit testing and…platformio.orgArduino - HomeOpen-source electronic prototyping platform enabling users to create interactive electronic objects.www.arduino.ccBootstrap Material DesignThe most popular HTML, CSS, and JS Material Design library in the world.fezvrasta.github.ioesp8266/ArduinoESP8266 core for Arduino. Contribute to esp8266/Arduino development by creating an account on GitHub.github.comFirebaseExtended/firebase-arduinoArduino samples for Firebase. Contribute to FirebaseExtended/firebase-arduino development by creating an account on…github.comGRC | SSL TLS HTTPS Web Server Certificate FingerprintsGRC's HTTPS Web Server Certificate Fingerprint Servicewww.grc.com

Firebase实时数据库| Firebase实时数据库| Firebase 使用我们的NoSQL云数据库存储和同步数据。 数据在所有客户端之间实时同步,并保持可用状态 。firebase.google.com PlatformIO:IoT开发的开源生态系统 跨平台构建系统和库管理器。 跨平台IDE和统一调试器。 远程单元测试和… platformio.org Arduino-主页 开源电子原型平台,使用户能够创建交互式电子对象。 www.arduino.cc 引导材料设计 世界上受欢迎HTML,CSS和JS材料设计库。 面向 Arduino的 fezvrasta.github.io esp8266 / Arduino ESP8266内核。 通过在GitHub上创建一个帐户为esp8266 / Arduino开发做贡献。 github.com FirebaseExtended / firebase -arduino Firebase的Arduino示例。 通过对... github.com 创建帐户有助于FirebaseExtended /火力点,Arduino的发展 GRC | SSL TLS HTTPS Web服务器证书指纹 GRC的HTTPS Web服务器证书指纹服务 www.grc.com

翻译自: https://www.freecodecamp.org/news/iot-prototyping-with-firebase-doing-more-with-less-2f5c746dac8b/

firebase使用

相关文章