溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-11-16 17:11:56 來(lái)源:億速云 閱讀:202 作者:柒染 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)基于Python和JavaScript怎樣編寫物聯(lián)網(wǎng)溫度計(jì)程序,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

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

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

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

準(zhǔn)備工作

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

接著選擇帶有溫度(HTS221)和相對(duì)濕度傳感器的 Temp&Hum Click 來(lái)測(cè)量溫度。

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

 Zerynth下載也是最重要的一點(diǎn),你需要

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

  • Zerynth APP 。

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

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

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

參考示例

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

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

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) 

這個(gè)示例中,Zerynth將從相連的電路板獲取的數(shù)據(jù)轉(zhuǎn)變成可視化的圖形示波器,這些模擬傳感器的數(shù)據(jù)通過(guò)“模擬”pin A4產(chǎn)生。

導(dǎo)入正確的wifi驅(qū)動(dòng)程序和傳感器庫(kù)

正如你在注釋中看到的,示例是基于 粒子光子板 和wifi驅(qū)動(dòng)的。想要使用WiFi Plus Click,必須修改以下幾行:

from broadcom.bcm43362 import bcm43362 as wifi_driver

修改為

from microchip.mcw1001a import mcw1001a as wifi_driver

同時(shí)

wifi_driver.auto_init()

修改為

wifi_driver.init(SERIAL2,D24) # slot B

為了使用Temp&Hum Click溫度傳感器,需要添加以下幾行代碼來(lái)導(dǎo)入庫(kù)并設(shè)置傳感器,這些可以在 幫助文檔 里面看到。

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

同時(shí)為了讀取到傳感器,有必要編寫下面一行。

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

設(shè)置SSID名稱和密碼

當(dāng)然,你還需要編輯想要連接的wifi網(wǎng)絡(luò)的SSID名稱和密碼:

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

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

現(xiàn)在我們要?jiǎng)?chuàng)建一個(gè)“連接裝置”以便關(guān)聯(lián)“zerynth”的實(shí)例。請(qǐng)看下面截圖中的步驟。查看 文檔 了解更多的技術(shù)細(xì)節(jié)。

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

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

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

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

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

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

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

單擊相應(yīng)的“Plus”圖標(biāo)來(lái)添加模板。

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

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

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

部署腳本

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

 # Zerynth App Oscilloscope    from wireless import wifi  from microchip.mcw1001a import mcw1001a as wifi_driver  import streams import adc  streams.serial()  # Import the Zerynth APP library  from zerynthapp import zerynthapp  # Import the HTS221 library  from stm.hts221 import hts221  temp_hum = hts221.HTS221(I2C0, D21) # slot A  sleep(1000)  print("STARTING...")  try:  # Device UID and TOKEN can be created in the ADM panel  zapp = zerynthapp.ZerynthApp("DEVICE UID", "DEVICE TOKEN",ip = "178.22.65.123", log=True)  # connect to the wifi network (Set your SSID and password below)  wifi_driver.init(SERIAL2,D24) # slot B  for i in range(0,5):  try:  wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD")  break  except Exception as e:  print("Can't link",e)  else:  print("Impossible to link!")  while True:  sleep(1000)  # Start the Zerynth app instance!  # Remember to create a template with the files under the "template" folder you just cloned  # upload it to the ADM and associate it with the connected device  zapp.run()  # Read the sensor and send values to the ADM  while True:  sleep(1000)  tmp, hum = temp_hum.get_temp_humidity() # Read tmp and hum  print("Temp is:", tmp, "Humidity is:", hum)  try:  zapp.event({"data":tmp})  except Exception as e:  print(e)  if tmp>30:  # send mobile notification  # (there is a limit of one notification per minute per device on the ADM sandbox)  try:  zapp.notify("ALARM!","High Temperature!")  except Exception as e:  print(e)  except Exception as e:  print(e)

切記“設(shè)備UID”、“設(shè)備令牌”、“名稱”和“密碼”必須符合自己的參數(shù)。

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

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

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

關(guān)于基于Python和JavaScript怎樣編寫物聯(lián)網(wǎng)溫度計(jì)程序就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI