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

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应用示波器”示例。请参考以下 学习如何克隆一个示例 。

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”的实例。请看下面截图中的步骤。查看 文档 了解更多的技术细节。

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

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

创建、上传和设置模板

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

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

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

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

部署脚本

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

 
 
 
 
  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也可以通过连接设备接收 推送通知 。比如当温度大于阈值时,就会出现通知。

网页标题:基于Python和JavaScript编写物联网温度计程序
文章源于:http://www.hantingmc.com/qtweb/news17/371517.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联