7/18/2017
二氧化碳監測與Raspberry PI
Raspberry PI 或稱樹莓派,是最近幾年蠻有名的微型單版機電腦。最新版本(RPi 3)仍然是約略名片大小,但擁有1G RAM 加上 64bit ARMv8使它能做的事情已經和一般的PC沒有太大的不同。
這對純軟體工程師來說,是一個很容易介入IOT硬體的開始。早期,需要嵌入式系統(Embedded System)或者對各類硬體輸出入有一定了解,才能做一些有趣的應用,而現在由於RPi的關係,讓這些應用的門檻降的非常低。
因為RPi已經可以運行各類Linux Distribution,FreeBSD 甚至Windows10。讓軟體工程師可以直接撰寫有趣的應用程式,降低考慮硬體的問題。
不過,「降低」並不代表完全不需要考慮硬體問題。實際上很多市面上的硬體零件,其規格和輸出入方式各有不同,還是得有基本了解或者「取得範例」,才能達到想要的效果。
以市面上容易買到的「攀藤CO2模組」為例,它的文件相當的「精簡」,也沒有任何範例程式,因此需要自己想像一下官方文件到底在說什麼。
想跳過以下冗長說明的,可以直接看這個python範例。
官方文件提及這個模組式需要用標準序列埠Serial Port連結。因此可以想像就是把Raspberry TX/RX 和這個硬體的 RX/TX對接,並且硬體需要5v的電,也要接到Raspberry的5V輸出以及接地,因此就是4, 6, 8, 10這四個pin腳。
接上之後,需要送出指令,這個模組才會運作。根據中文官方文件,你需要知道指令,以及把指令組合成7個bytes然後送出。
但是其實,這個裝置也只有一個指令,也就是0xe3,上面述的DATAH, DATAL其實是填0,而不是如同文件上「打X」。由於只有一個指令,所以校正數字當然就一定也只有一個可能。簡單的說,其實只要(而且也只能)送出以下資料到serial port即可:
[ 0x42, 0x4d, 0xe3, 0x00, 0x00, 1, 114]
#python 範例程式節錄 送出serial port
ser = serial.Serial("/dev/ttyAMA0", 9600)
init_cmd =[ 0x42, 0x4d, 0xe3, 0x00, 0x00, 1, 114]
for c in init_cmd:
ser.write(chr(c))
上述程式在RPi中會預設serial port已經不作為TTY (終端機)使用,因此才能直接對/dev/ttyAMA0執行輸出入。由於RPi預設serial port(8,10)式作為終端機使用,因此請記得disable它,並且重開機。
接下來,根據文件的內容,需要接收(讀取)一個12個bytes的資料,資料內容也都是固定,真正有意義的是第五和第六個byte,這兩個byte應該組合成一個數字。這個裝置最好的地方在於,它的數字已經是CO2的PPM值,這比某些其他公司的裝置還需要用各種方式換算方便許多。
兩個bytes組成一個數字的方式很多,例如可以把第一個位數利用 << 位移8,然後加上第二的位數。當然也可以把第一個位數*256加上第二個位數。
#python 範例程式部分節錄
while True:
count = ser.inWaiting()
if count >= 12:
recv = ser.read(count)
h8 = recv[4]
l8 = recv[5]
print(str(ord(h8)*256 + ord(l8))+" PPM,")
最終結果就是,會有個Raspberry PI可以監視並且告知附近環境的CO2含量。
在辦公室監視一整天的結果非常明顯,上班前的CO2含量比較低,人越多的時候CO2含量會快速增加。連續兩日,都是在每日下午5點到達最高峰,接下來逐漸下降至隔天開始上班為止。
7/12/2017
Serverless design for IoT - An example leverage AWS and GrovePi
AWS announced IOT service in about 2015 and gradually release other relative service (for example: IoT Button) for those who need to tackle with the problem on huge amount of increasing response of "The Things". And it is of course the area which cloud provider what to provide a optional solution.
To demonstrate the benefits of leveraging the serverless design and also utilize the power of AWS cloud. I build an example project combines Serverless design, AWS IoT, Respberry Pi, Grove Sensor system and GrovePI. It will provide in door air quality (office) for me if I want to know that before entering office. So that I can have an excuse to work from somewhere else? :)
In this example, a GrovePi mounts in Raspberry Pi (B+) to control Grove's Air Quality Sensor, HCHO sensor and dust sensor. As a software engineer, I assembled all these inside a paper box. See picture below.
RPi and GrovePi are inside the box. 3 sensors are out there. |
Reminder: to use AWS service, the most important things is to read official document. AWS has many different services and there are too many out-of-date articles in somebody's blog. It doesn't mean that authors were wrong, it is just out-of-date. Of course, it is the same in Raspberry Pi and all other 3rd party open source library, try to read official document (or official wiki/blog) to have overall view.
The full implementation and design concerns
(please check all the project detail in github)
(1) Grove's 3 sensors + GrovePi + Raspberry Pi
The hardware parts. Check GrovePi's official web site to know how to put them together.GrovePi might be the easiest way to program Grove's system from Raspberry Pi, if you have more then 2 device in a machine. However, if you have only one sensor, then just use RPi's GPIO.
(2) AWS IoT service
Although we didn't program anything in side the hardware, we still need to setup things in AWS IoT service. And of course, it will be better to read at least the tutorial.Screenshot of AWS IoT Tutorial |
In this example, we register a "Thing" named: InDoorSensor1 and the most important thing is to have default Shadow Object as below:
{ "desired": { "welcome": "aws-iot", "air quality": 43, "action": "wait" }, "reported": { "welcome": "aws-iot", "action": "wait", "air quality": 43 } }The device will keep sync the Shadow in AWS and if the desired state change to "do", it will (a) do a one time air sensor data collection and then (b) update air quality in Shadow object (c) change to "wait" state. In sort, the Shadow and Device will sync the state (wait or do) and the state's sync is the major function provide from AWS.
(3) AWS IoT Python SDK + GrovePi Python library
AWS provides a few SDK for device, in this project, we use Python to do AWS Cloud access (no matter notification or change Shadow state)In the Raspberry PI B+, you need to:
(a) install AWSIoTPythonSDK
# pip install AWSIoTPythonSDK (also see here)
(b) consider the protocol (MQTT vs Websocket). In some environment, the MQTT port might be block. AWS SDK provides MQTT via WebSocket which of course allow broker use port 443.
(c) certificates: please do read AWS IoT Certificate document if you didn't have experience before
If you use Raspberry PI version B+ 2 or 3, then it will be easy to install nodejs/npm and all other fancy stuff.
(4) IoT Shadow
The Shadow means an identify object of IoT device. This allows client to change the state of a object and then sync to IoT device. In certain scenario, it allows programmer no need to take care of network error handling or any off line case. However, you still need to fully understand what means exactly the "desired" and "reported" state.
It is possible to edit Shadow state from AWS admin console direct for testing purpose. (you won't want to do so if you have thousands IoT device).
(5) Lambda, API Gateway
Supposedly, an application will NOT access specific IoT device, it normally access a service and that service provides information or allow meaningful user actions.
In this case, a lambda service is simple a python program which can (1) retrieve current state and also current air quality value (2) update state to "do". And as always, the Lambda is behind an API Gateway and which means, potentially, all other application could use this API to access necessary (filtered) information.
see the activity hand draw:
Next Project
Hopefully, I can have more budget to purchase Raspberry PI 3 and also CO2 sensor and then also gather data to draw graphic in D3. Also, I am thinking to use LINE to send air quality information to my colleague or neighborhood.
Labels:
AWS,
big data,
consultant,
IoT,
serverless,
system design,
training talent
訂閱:
文章 (Atom)