自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

開發(fā) 前端
本文我們將介紹使用單片機微控制器連接Zerynth,開發(fā)一個簡單但強大的物聯(lián)網(wǎng)溫度計。

Zerynth作為Android和iOS手機端應用程序,在物聯(lián)網(wǎng)項目中,可以對圖形界面進行快速原型設計。

借助Zerynth可以把任何手機作為智能對象加入控制器組成物聯(lián)網(wǎng)系統(tǒng)。尤其是通過建立雙向通信信道,可以管理和控制與它連接的手機設備。

本文我們將介紹使用單片機微控制器連接Zerynth,開發(fā)一個簡單但強大的物聯(lián)網(wǎng)溫度計。

準備工作

首先你需要一塊電路板,選擇 Zerynth支持的32位微控制器設備 即可。我們選擇的是 Flip&Click Mikroelektronika ,它擁有許多和Arduino平臺產品一樣的屬性,其中就包括作為Arduino Due核心的32位AT91SAM3X8E微芯片。

接著選擇帶有溫度(HTS221)和相對濕度傳感器的 Temp&Hum Click 來測量溫度。

然后采用 WiFi PLUS Click 將電路板連接到互聯(lián)網(wǎng), WiFi PLUS Click 具有MRF24WB0MA-2.4GHz特性,能兼容IEEE std 802.11微芯片模塊,并且是車載TCP/IP棧和802.11連接管理器匹配的MCW1001的控制器。

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

  • Zerynth Studio,為物聯(lián)網(wǎng)服務的強大的開發(fā)工具,能使用Python嵌入式編程。 點擊下載 。
  • Zerynth APP 。

組裝物聯(lián)網(wǎng)溫度計

Flip&Click是Arduino的衍生品,一方面它屬于Arduino產品,但另一方面,你會發(fā)現(xiàn)它身上包含“單機電路板”才有的四個開放mikroBUS套接字的模塊。從本質上講,這些模塊是組裝Arduino原型的附加模塊,但如果縮減去掉,F(xiàn)lip&Click也能勉強適用,只是需要在電路板上的A槽和B槽分別加入Temp&Hum和Wifi Plus clicks。

使用Python來編程物聯(lián)網(wǎng)溫度計

參考示例

一旦你 安裝Zerynth Studio 并 創(chuàng)建Zerynth用戶 ,就可以克隆“Zerynth應用示波器”示例。請參考以下 學習如何克隆一個示例 。

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

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將從相連的電路板獲取的數(shù)據(jù)轉變成可視化的圖形示波器,這些模擬傳感器的數(shù)據(jù)通過“模擬”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網(wǎng)絡的SSID名稱和密碼:

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

創(chuàng)建并設置一個連接設備

現(xiàn)在我們要創(chuàng)建一個“連接裝置”以便關聯(lián)“zerynth”的實例。請看下面截圖中的步驟。查看 文檔 了解更多的技術細節(jié)。

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

設備的證書(UID和TOKEN)可以從開發(fā)工具Zerynth Studio的ADM面板直接復制粘貼過來。

“IP”是Zerynth ADM的IP地址。當網(wǎng)絡驅動不支持主機名解析時填寫的這些參數(shù)可以派上用場。

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

創(chuàng)建、上傳和設置模板

Zerynth可以直接運行由HTML、CSS和JavaScript構成的漂亮的圖形用戶界面,根本不需要Android或iOS代碼!

此外,每個裝置的圖形界面托管于 Zerynth ADM sandbox ,并由一些列可在App上加載并顯示的HTML5、Javascript、Css和圖片文件組成。Zerynth添加模板后 ADM Javascript庫 允許應用程序與連接設備互相通信。

單擊相應的“Plus”圖標來添加模板。

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

然后從包含模板目錄上傳模板。注意,你可以修改模板定義文件“index.html”進行自定義。這里我們保留原樣。

基于Python和JavaScript編寫物聯(lián)網(wǎng)溫度計程序

部署腳本

經(jīng)過幾次修改后,代碼大概是這樣:

 

  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”、“設備令牌”、“名稱”和“密碼”必須符合自己的參數(shù)。

編寫完成即可 部署腳步到你的設備 。

如何在Zerynth應用上查看物聯(lián)網(wǎng)溫度計儀表板

在這個 極簡教程 里,你只需打開Zerynth應用,登錄并選擇指定的設備即可查看對應的物聯(lián)網(wǎng)溫度計指示板。***,Zerynth也可以通過連接設備接收 推送通知 。比如當溫度大于閾值時,就會出現(xiàn)通知。

責任編輯:未麗燕 來源: CSDN
相關推薦

2020-07-28 17:15:42

溫度計物聯(lián)網(wǎng)IOT

2017-07-18 15:26:20

微服務化DevOps容器化

2020-07-29 17:01:29

VSCode RTOSPython編程

2023-04-19 15:02:01

MQTT人工智能物聯(lián)網(wǎng)

2017-09-11 13:55:30

前端JavaScript物聯(lián)網(wǎng)

2015-05-08 13:09:12

JavaScriipt抽獎程序

2022-04-13 13:42:55

物聯(lián)網(wǎng)漏洞安全

2015-09-10 10:09:18

物聯(lián)網(wǎng)操作系統(tǒng)物聯(lián)網(wǎng)生態(tài)環(huán)境

2020-08-03 23:13:14

物聯(lián)網(wǎng)遠程辦公IOT

2022-07-26 10:16:24

物聯(lián)網(wǎng)工業(yè)物聯(lián)網(wǎng)

2023-05-18 11:00:34

物聯(lián)網(wǎng)智慧城市

2016-08-12 11:04:17

JavaScript物聯(lián)網(wǎng)應用

2019-12-03 12:11:02

智慧農業(yè)物聯(lián)網(wǎng)精準農業(yè)

2015-10-28 15:18:45

2021-09-08 05:52:57

工業(yè)物聯(lián)網(wǎng)物聯(lián)網(wǎng)IOT

2022-11-16 14:27:46

物聯(lián)網(wǎng)供應鏈管理

2020-08-29 18:17:35

物聯(lián)網(wǎng)接觸者追蹤IOT

2020-05-24 17:04:47

物聯(lián)網(wǎng)智慧城市IOT

2023-09-18 14:28:20

物聯(lián)網(wǎng)邊緣計算

2018-11-26 15:00:32

點贊
收藏

51CTO技術棧公眾號