基于Python和JavaScript编写物联网温度计程序

开发 前端
本文我们将介绍使用单片机微控制器连接Zerynth,开发一个简单但强大的物联网温度计。

Zerynth作为Android和iOS手机端应用程序,在物联网项目中,可以对图形界面进行快速原型设计。

借助Zerynth可以把任何手机作为智能对象加入控制器组成物联网系统。尤其是通过建立双向通信信道,可以管理和控制与它连接的手机设备。

本文我们将介绍使用单片机微控制器连接Zerynth,开发一个简单但强大的物联网温度计。

准备工作

首先你需要一块电路板,选择 Zerynth支持的32位微控制器设备 即可。我们选择的是 Flip&Click Mikroelektronika ,它拥有许多和Arduino平台产品一样的属性,其中就包括作为Arduino Due核心的32位AT91SAM3X8E微芯片。

接着选择带有温度(HTS221)和相对湿度传感器的 Temp&Hum Click 来测量温度。

然后采用 WiFi PLUS Click 将电路板连接到互联网, WiFi PLUS Click 具有MRF24WB0MA-2.4GHz特性,能兼容IEEE std 802.11微芯片模块,并且是车载TCP/IP栈和802.11连接管理器匹配的MCW1001的控制器。

***也是最重要的一点,你需要

  • Zerynth Studio,为物联网服务的强大的开发工具,能使用Python嵌入式编程。 点击下载 。
  • Zerynth APP 。

组装物联网温度计

Flip&Click是Arduino的衍生品,一方面它属于Arduino产品,但另一方面,你会发现它身上包含“单机电路板”才有的四个开放mikroBUS套接字的模块。从本质上讲,这些模块是组装Arduino原型的附加模块,但如果缩减去掉,Flip&Click也能勉强适用,只是需要在电路板上的A槽和B槽分别加入Temp&Hum和Wifi Plus clicks。

使用Python来编程物联网温度计

参考示例

一旦你 安装Zerynth Studio 并 创建Zerynth用户 ,就可以克隆“Zerynth应用示波器”示例。请参考以下 学习如何克隆一个示例 。

基于Python和JavaScript编写物联网温度计程序

main.py

 

  1. ################################################################################  
  2. # IoT Thermometer  
  3. ################################################################################  
  4. from wireless import wifi
  5. # this example is based on Particle Photon  
  6. # change the following line to use a different wifi driver  
  7. from broadcom.bcm43362 import bcm43362 as wifi_driver  
  8. import streams  
  9. import adc  
  10. # Import the Zerynth APP library  
  11. from zerynthapp import zerynthapp  
  12. streams.serial()  
  13. sleep(1000)  
  14. print("STARTING..." 
  15. try:  
  16. # Device UID and TOKEN can be created in the ADM panel  
  17. zapp = zerynthapp.ZerynthApp("DEVICE UID""DEVICE TOKEN", log=True 
  18. connect to the wifi network (Set your SSID and password below)  
  19. wifi_driver.auto_init() 
  20.  for i in range(0,5):  
  21. try:  
  22. wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD" 
  23. break  
  24. except Exception as e:  
  25. print("Can't link",e)  
  26. else 
  27. print("Impossible to link!" 
  28. while True 
  29. sleep(1000)  
  30. # Start the Zerynth app instance!  
  31. # Remember to create a template with the files under the "template" folder you just cloned  
  32. # upload it to the ADM and associate it with the connected device  
  33. zapp.run()  
  34. Read ADC and send values to the ADM  
  35. while True 
  36. sleep(1000)  
  37. x = (adc.read(A4)*100)//4096  
  38. zapp.event({"data":x})  
  39. if x>95:  
  40. # send mobile notification  
  41. # (there is a limit of one notification per minute per device on the ADM sandbox)  
  42. zapp.notify("ALARM!","The value is greater than 95!" 
  43. except Exception as e:  
  44. print(e) 

这个示例中,Zerynth将从相连的电路板获取的数据转变成可视化的图形示波器,这些模拟传感器的数据通过“模拟”pin A4产生。

导入正确的wifi驱动程序和传感器库

正如你在注释中看到的,示例是基于 粒子光子板 和wifi驱动的。想要使用WiFi Plus Click,必须修改以下几行:

  1. from broadcom.bcm43362 import bcm43362 as wifi_driver 

修改为

  1. from microchip.mcw1001a import mcw1001a as wifi_driver 

同时

  1. wifi_driver.auto_init() 

修改为

  1. wifi_driver.init(SERIAL2,D24) # slot B 

为了使用Temp&Hum Click温度传感器,需要添加以下几行代码来导入库并设置传感器,这些可以在 帮助文档 里面看到。

 

  1. # Import the HTS221 library  
  2. from stm.hts221 import hts221  
  3. temp_hum = hts221.HTS221(I2C0, D21) # sl 

同时为了读取到传感器,有必要编写下面一行。

  1. tmp, hum = temp_hum.get_temp_humidity() # Read tmp and hum 

设置SSID名称和密码

当然,你还需要编辑想要连接的wifi网络的SSID名称和密码:

  1. wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD"

创建并设置一个连接设备

现在我们要创建一个“连接装置”以便关联“zerynth”的实例。请看下面截图中的步骤。查看 文档 了解更多的技术细节。

基于Python和JavaScript编写物联网温度计程序

设备的证书(UID和TOKEN)可以从开发工具Zerynth Studio的ADM面板直接复制粘贴过来。

“IP”是Zerynth ADM的IP地址。当网络驱动不支持主机名解析时填写的这些参数可以派上用场。

基于Python和JavaScript编写物联网温度计程序

创建、上传和设置模板

Zerynth可以直接运行由HTML、CSS和JavaScript构成的漂亮的图形用户界面,根本不需要Android或iOS代码!

此外,每个装置的图形界面托管于 Zerynth ADM sandbox ,并由一些列可在App上加载并显示的HTML5、Javascript、Css和图片文件组成。Zerynth添加模板后 ADM Javascript库 允许应用程序与连接设备互相通信。

单击相应的“Plus”图标来添加模板。

基于Python和JavaScript编写物联网温度计程序

然后从包含模板目录上传模板。注意,你可以修改模板定义文件“index.html”进行自定义。这里我们保留原样。

基于Python和JavaScript编写物联网温度计程序

部署脚本

经过几次修改后,代码大概是这样:

 

  1. ################################################################################  
  2. # Zerynth App Oscilloscope  
  3. ################################################################################  
  4. from wireless import wifi  
  5. from microchip.mcw1001a import mcw1001a as wifi_driver  
  6. import streams 
  7. import adc  
  8. streams.serial()  
  9. # Import the Zerynth APP library  
  10. from zerynthapp import zerynthapp  
  11. # Import the HTS221 library  
  12. from stm.hts221 import hts221  
  13. temp_hum = hts221.HTS221(I2C0, D21) # slot A  
  14. sleep(1000)  
  15. print("STARTING..." 
  16. try:  
  17. # Device UID and TOKEN can be created in the ADM panel  
  18. zapp = zerynthapp.ZerynthApp("DEVICE UID""DEVICE TOKEN",ip = "178.22.65.123", log=True 
  19. connect to the wifi network (Set your SSID and password below)  
  20. wifi_driver.init(SERIAL2,D24) # slot B  
  21. for i in range(0,5):  
  22. try:  
  23. wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD" 
  24. break  
  25. except Exception as e:  
  26. print("Can't link",e)  
  27. else 
  28. print("Impossible to link!" 
  29. while True 
  30. sleep(1000)  
  31. # Start the Zerynth app instance!  
  32. # Remember to create a template with the files under the "template" folder you just cloned  
  33. # upload it to the ADM and associate it with the connected device  
  34. zapp.run()  
  35. Read the sensor and send values to the ADM  
  36. while True 
  37. sleep(1000)  
  38. tmp, hum = temp_hum.get_temp_humidity() # Read tmp and hum  
  39. print("Temp is:", tmp, "Humidity is:", hum)  
  40. try:  
  41. zapp.event({"data":tmp})  
  42. except Exception as e:  
  43. print(e)  
  44. if tmp>30:  
  45. # send mobile notification  
  46. # (there is a limit of one notification per minute per device on the ADM sandbox)  
  47. try: 
  48.  zapp.notify("ALARM!","High Temperature!" 
  49. except Exception as e: 
  50.  print(e)  
  51. except Exception as e:  
  52. print(e) 

切记“设备UID”、“设备令牌”、“名称”和“密码”必须符合自己的参数。

编写完成即可 部署脚步到你的设备 。

如何在Zerynth应用上查看物联网温度计仪表板

在这个 极简教程 里,你只需打开Zerynth应用,登录并选择指定的设备即可查看对应的物联网温度计指示板。***,Zerynth也可以通过连接设备接收 推送通知 。比如当温度大于阈值时,就会出现通知。

责任编辑:未丽燕 来源: CSDN
相关推荐

2020-07-28 17:15:42

温度计物联网IOT

2017-07-18 15:26:20

微服务化DevOps容器化

2020-07-29 17:01:29

VSCode RTOSPython编程

2017-09-11 13:55:30

前端JavaScript物联网

2023-04-19 15:02:01

MQTT人工智能物联网

2015-05-08 13:09:12

JavaScriipt抽奖程序

2023-05-18 11:00:34

物联网智慧城市

2015-09-10 10:09:18

物联网操作系统物联网生态环境

2022-04-13 13:42:55

物联网漏洞安全

2019-12-03 12:11:02

智慧农业物联网精准农业

2021-09-08 05:52:57

工业物联网物联网IOT

2020-08-03 23:13:14

物联网远程办公IOT

2022-07-26 10:16:24

物联网工业物联网

2023-04-18 16:12:14

2020-05-19 15:09:52

5G物联网移动宽带

2016-08-12 11:04:17

JavaScript物联网应用

2022-11-17 10:23:13

VS CodeCodiumPython

2022-11-16 14:27:46

物联网供应链管理

2015-10-28 15:18:45

2022-08-05 12:59:50

物联网IoT
点赞
收藏

51CTO技术栈公众号