
通过wifi将数据发布到NucleoL476的Ubidots。 ![]() 在这个项目中使用的东西 硬件组件 STMicroelectronics STM32 Nucleo-64板 ×1 STMicroelectronics X-Nucleo-IDW01M1 × 1 软件应用程序和在线服务 Arm Mbed MBED 目的 本教程介绍如何使用STMicroelectronics ODE生态系统将数据发送到Ubidots站点: NucleoL476与STM32L476RG X-Nucleo-IDW01M1 wifi屏蔽 MBed OS在线编译器 背景 我正在寻找一个允许发布数据的物联网平台,并且能够在传感器值超过临界阈值的情况下定义警报。 今天在网上看到有大量的云服务,大多数都提供免费的计划(仅限于少数设备和发送的数据) 物联网提供商公共和开源: http://www.postscapes.com/internet-of-things-platforms/ 然后在与Thingspeak进行一些总结测试之后,我发现了Ubidots(http://ubidots.com/)。基于硬件Arduino,ESP8266等的大量等效项目。我们还可以为每个物联网云提供商找到每个流行平台的特定库(Arduino familly,Energia ......) 但我想介绍微控制器STM32的可能性。这开辟了无限的应用项目,计量传感器,家庭自动化等等。 第1步:Ubidots设置 首先,您需要创建一个Ubidots帐户 file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg Ubidots主页 file:///C:/Users/ADMINI~1/AppData/Local/Temp/msohtmlclip1/01/clip_image006.jpg 登录到您的账户 在设备选项卡上。我们将创建一个新设备(使用添加设备) 将“NucleoL476”设置为设备名称。然后clic打开设备设置。 ![]() 设备创建 现在在NucleoL476内部,我们将创建3个变量,其名称,级别,温度和电池如下。 ![]() 变量创建 每个变量都可以通过Wifi通过Nucleo板进行更新。 第2步:API凭证 我们需要为STM32L476RG编写代码,以便通过wifi进行通信并更新3个变量的值。 稍后将使用Mbed在线编译器完成此操作。 对于Wifi本地接入点,我们定义2个字符串。 /* Wifi AccesPoint Settings */ #defineAP_SSID "YOUR_WIFI_SSID" #defineAP_PASSWORD "YOUR_WIFI_PASSWORD" 为了能够连接硬件并使用TCP与Ubidots进行身份验证,我们需要定义其他2个字符串。 #defineUBIDOTS_TOKEN "YOUR_UBIDOTS_TOKEN" #defineUBIDOTS_DEVICE "YOUR_UBIDOTS_LABEL_DEVICE" 应从您的帐户API凭据中读取第一个字符串UBIDOTS_TOKEN(参见下文) ![]() 第二个字符串是我们在步骤1中创建的设备标签名称:“NucleoL476” 您只需要更改这些定义即可实现本教程。 第3步:Hadware和Mbed 现在我们可以将X-Nucleo-IDW01M1连接到带有Morpho连接器的NucleoL476RG板上。 ![]() NucleoL476RG带IDW01M1 wifi屏蔽 重要提示:某些X-Nucleo-IDW01M1板需要稍加修改才能执行。检查是否安装了R21电阻器。如果焊接了R21,则必须将其拆下。 现在转到Mbed OS(http://os.mbed.com/),登录并打开在线编译器。 ![]() Mbed在线编译器 创建一个新项目并使用您的硬件进行配置(此处为NucleoL476) ![]() 将Nucleo mini USB连接到您的计算机,并记下您的操作系统分配的Virtual Com端口。 以115200波特,8位和1个停止位Xon / Xoff禁用打开串行终端(如Teraterm或Putty) 第4步:Mbed代码 该代码可在下面作为附件提供。 您可以将其复制并粘贴到Mbed Compiler中。评论很好,不应成为问题。 您必须包含一些额外的库,例如: NetworkSocketAPI X_NUCLEO_IDW01M1v2也是如此 和mbed level 变量是一个布尔值,显示Nucleo Board上用户蓝色按钮的状态。 温度变量通过STM32更新内部温度传感器连接到ADC。 然后使用VBAT / 3内部ADC通道更新电池变量。 复位后,程序将转换VBAT的ADC内部通道和mcu的内部温度传感器。这是一个例子,您可以使用任何数据。转换后的值存储在vbat和temp变量中。 它还将获得Nucleo Board用户按钮的状态(蓝色按钮)。 并尝试使用您提供的SSID和WPA密钥连接到wifi网络。 然后它将与Ubidots api建立TCP套接字连接。 如果已建立连接,则应用程序将构建Rest API命令HTTP请求,并POST HTTP命令。 POST /api/v1.6/devices/{LABEL_DEVICE}/?token={TOKEN} HTTP/1.1 Host: things.ubidots.com Content-Type: application/json Content-Length: 76 {"temperature": %0.2f, "battery": %0.2f, "level\": %d} 通过使用终端控制台,您将看到该程序的每个步骤。以及HTTP请求的反馈响应(如果你得到200就可以了) 现在您应该在线查看您的数据。 ![]() 应用程序将进入一个闪烁的循环。如果要再次发送数据,则需要通过重置重新启动它。 这是一个简单的教程,它是完全正常的。 您可以将它用于您自己的项目,并且很容易实现真实环境传感器的其他数据发布。 代码 main.cpp中C / C ++ Mbed代码 /* Copyright(C) 2017 romain reicher This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Thisprogram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not,see <http://www.gnu.org/licenses/>. About --------------------------------------------------------------------- Sendsensors value to Ubidots. Thisexample sends 3 variables to Ubidots. -STM32L476 VBAT/3 internal channel in mV -STM32L476 Internal Temperature Sensor in C -The status of onboard User Button (blue) on NucleoL476RG UseNucleoL476RG with X-Nucleo-IDW01M1v2 wifi shield Importantnote: Some IDW01M1 wifi shield had resistor R21 mounted whichinterfere with STLink/SWD programmer. Pleaseunmount R21 to fix it. romain reicher Date : 20/09/2017 Revision : v0.1 */ #include "mbed.h" #include "SpwfInterface.h" #include "TCPSocket.h" /* Wifi Acces Point Settings */ #define AP_SSID "YOUR_WIFI_SSID" #define AP_PASSWORD "YOUR_WIFI_PASSWORD" #define UBIDOTS_SERVER "things.ubidots.com" #define UBIDOTS_PORT 80 #define UBIDOTS_TOKEN "YOUR_UBIDOTS_TOKEN" #define UBIDOTS_DEVICE "YOUR_UBIDOTS_LABEL_DEVICE" /* Communication ressources */ SpwfSAInterface spwf(D8, D2, false); Serial pc(USBTX, USBRX); /* Digital ressources */ DigitalOut myLed(LED1); DigitalIn myButton(USER_BUTTON); /* Analog ressources */ AnalogIn adc_vbat(ADC_VBAT); // VBAT / 3internal to ADC channel AnalogIn adc_temp(ADC_TEMP); // InternalTemp Sensor to ADC Channel /* Global variables */ float temp = adc_temp.read() * 100; // Converted in C float batt = adc_vbat.read() * 30000; // Converted inmV bool level = false; /** *@brief Main program *@param None *@retval None */ int main() { /* Configure Serial baud rate */ pc.baud(115200); /* Update level variable state depending USER_BUTTONstate */ if (myButton == 0) level = true; else level = false; TCPSocket socket(&spwf); char sendBuffer[256]; char message[64]; int err; /* ######################## WIFI CONNECTION ########################*/ pc.printf("IDW01M1 NetworkSocketAPI TCPClient Ubidots\r\n"); pc.printf("Connecting to AP\r\n"); //* Connect to wifi acces point */ if(spwf.connect(AP_SSID,AP_PASSWORD, NSAPI_SECURITY_WPA2)) { pc.printf("Now connected\r\n"); } else { pc.printf("Error connecting to AP.\r\n"); return -1; } /* #################### GET CONNECTION INFOS######################## */ /* Get and print network connection parameters ip and macadress */ const char *ip =spwf.get_ip_address(); const char *mac =spwf.get_mac_address(); pc.printf("IP address is: %s\r\n", ip ? ip : "NoIP"); pc.printf("MAC address is: %s\r\n", mac ? mac : "NoMAC"); /* ##################### UBIDOATS SEND DATA######################### */ printf("Sending HTTP Data to Ubidots...\r\n"); /* Open a socket , create a TCP connection to Ubidots */ err = socket.connect(UBIDOTS_SERVER, UBIDOTS_PORT); if (err!=0) { pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); return -1; } else pc.printf("\r\nconnected to host server\r\n"); /* Construct content of HTTP command */ sprintf(message, "{\"temperature\": %0.2f, \"battery\": %0.2f, \"level\": %d}", temp, batt, (int)level); printf("Content Length = %d\r\n", (int)strlen(message)); /* Construct HTTP command to send */ sprintf(sendBuffer, "POST/api/v1.6/devices/%s/?token=%s HTTP/1.1\r\nHost: things.ubidots.com\r\nContent-Type:application/json\r\nContent-Length: %d\r\n\r\n%s", UBIDOTS_DEVICE,UBIDOTS_TOKEN, (int)strlen(message),message); pc.printf("HTTP command %s\r\n", sendBuffer); wait(2.0); /* Send http request to Ubidots */ int scount = socket.send(sendBuffer, (int)strlen(sendBuffer)); printf("sent %d [%.*s]\r\n", scount, strstr(sendBuffer, "\r\n") - sendBuffer, sendBuffer); /* Receive a simple http response and print out theresponse line */ char respBuffer[64]; int rcount = socket.recv(respBuffer, sizeof respBuffer); printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer); /* Close the socket to return its memory and bring downthe network interface */ pc.printf("Close Socket\r\n"); socket.close(); /* Disconnect */ pc.printf("Disconnect Wifi\r\n"); spwf.disconnect(); wait(1.0); pc.printf("Done\r\n"); myLed = 0; while(1) { myLed= !myLed; wait(1.0); } } |