|
我們在mBlock中,使用Makeblock的PM2.5模組可取得空氣中的細懸浮微粒指數,若需要再使用WIFI模組上傳資料到ThinkSpeak時,就會無法持續更新PM2.5的指數。因為WIFI模組增強版的擴展指令並非原廠開發,而忽略了一些細節導致這個狀況,但這個非原廠的擴展又是超好用,怎麼辦?接下來,我們就來教大家如何克服這個問題,GO!
下載本地安裝擴展:
ThinkSpeak擴展
ME IoT WiFi Module mCore.zip
(2.5 KB, 下載次數: 22)
PM2.5擴展
PM2.5_V1.6.zip
(3.89 KB, 下載次數: 18)
硬體接法:
* IOT WIFI 模組接 1 號埠
* 表情面版接 2 號埠
* PM2.5模組接 4 號埠
mBlock程式:
PM2.5範例01
自動產生的 Arduino C 程式碼:
- #include <Arduino.h>
- #include <Wire.h>
- #include <SoftwareSerial.h>
- #include <MePm25Sensor.h>
- #include <MeMCore.h>
- /* HKU e-Learning Lab: Steve Suen, Ken Law, Karl Cheung, Nicholas Szeto, Envose Au (Special thanks to Mr. Kwok Tsz Fung) */
- MeDCMotor motor_9(9);
- MeDCMotor motor_10(10);
- void move(int direction, int speed)
- {
- int leftSpeed = 0;
- int rightSpeed = 0;
- if(direction == 1){
- leftSpeed = speed;
- rightSpeed = speed;
- }else if(direction == 2){
- leftSpeed = -speed;
- rightSpeed = -speed;
- }else if(direction == 3){
- leftSpeed = -speed;
- rightSpeed = speed;
- }else if(direction == 4){
- leftSpeed = speed;
- rightSpeed = -speed;
- }
- motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
- motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
- }
-
- double angle_rad = PI/180.0;
- double angle_deg = 180.0/PI;
- double PM25;
- int receivePin[] = {12, 10, A3, A1};
- int transmitPin[] = {11, 9, A2, A0};
- SoftwareSerial wifiConnection(receivePin[1-1], transmitPin[1-1]);
- MePm25Sensor myMePm25Sensor(4);
- uint16_t getPMValue(int type)
- {
- switch(type)
- {
- case 1:
- return myMePm25Sensor.readPm1_0Concentration();
- break;
- case 2:
- return myMePm25Sensor.readPm2_5Concentration();
- break;
- case 3:
- return myMePm25Sensor.readPm10Concentration();
- break;
- }
- }
- MeLEDMatrix ledMtx_2(2);
- char thingSpeakHttpGet[256];
- void setup(){
- myMePm25Sensor.begin(9600);
- ledMtx_2.setColorIndex(1);
- ledMtx_2.setBrightness(6);
- wifiConnection.begin(9600);
- wifiConnection.println(F("AT+CWMODE=1"));
- delay(1000);
- wifiConnection.println(F("AT+CIPMUX=0"));
- wifiConnection.println("AT+CWJAP=""+(String)"你Wifi的SSID"+""+""+(String)"你Wifi的密碼"+""");
- delay(10000);
- }
- void loop(){
-
- _delay(1);
- PM25 = getPMValue(2);
- ledMtx_2.showNum(PM25,3);
- sprintf(thingSpeakHttpGet, "GET /update?api_key=%s&field1=%d&field2=%d&field3=%d&field4=%d&field5=%d&field6=%d&field7=%d&field8=%d", "你的API Key", (int)PM25, (int)6, (int)6, (int)0, (int)0, (int)0, (int)0, (int)0);
- wifiConnection.println(F("AT+CIPSTART="TCP","184.106.153.149",80"));
- delay(3000);
- wifiConnection.println("AT+CIPSEND="+(String)(strlen(thingSpeakHttpGet)+2));
- delay(3000);
- wifiConnection.println(thingSpeakHttpGet);
- delay(3000);
- wifiConnection.println(F("AT+CIPCLOSE"));
- _delay(15);
- _loop();
- }
- void _delay(float seconds){
- long endTime = millis() + seconds * 1000;
- while(millis() < endTime)_loop();
- }
- void _loop(){
- myMePm25Sensor.rxloop();
- }
複製代碼
修改說明圖解:
PM2.5修正說明
修改後的 Arduino C 程式碼:
- #include <Arduino.h>
- #include <Wire.h>
- #include <SoftwareSerial.h>
- #include <MePm25Sensor.h>
- #include <MeMCore.h>
- /* HKU e-Learning Lab: Steve Suen, Ken Law, Karl Cheung, Nicholas Szeto, Envose Au (Special thanks to Mr. Kwok Tsz Fung) */
- MeDCMotor motor_9(9);
- MeDCMotor motor_10(10);
- void move(int direction, int speed)
- {
- int leftSpeed = 0;
- int rightSpeed = 0;
- if(direction == 1){
- leftSpeed = speed;
- rightSpeed = speed;
- }else if(direction == 2){
- leftSpeed = -speed;
- rightSpeed = -speed;
- }else if(direction == 3){
- leftSpeed = -speed;
- rightSpeed = speed;
- }else if(direction == 4){
- leftSpeed = speed;
- rightSpeed = -speed;
- }
- motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
- motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
- }
-
- double angle_rad = PI/180.0;
- double angle_deg = 180.0/PI;
- double PM25;
- int receivePin[] = {12, 10, A3, A1};
- int transmitPin[] = {11, 9, A2, A0};
- SoftwareSerial wifiConnection(receivePin[1-1], transmitPin[1-1]);
- MePm25Sensor myMePm25Sensor(4);
- uint16_t getPMValue(int type)
- {
- switch(type)
- {
- case 1:
- return myMePm25Sensor.readPm1_0Concentration();
- break;
- case 2:
- return myMePm25Sensor.readPm2_5Concentration();
- break;
- case 3:
- return myMePm25Sensor.readPm10Concentration();
- break;
- }
- }
- MeLEDMatrix ledMtx_2(2);
- char thingSpeakHttpGet[256];
- void setup(){
- ledMtx_2.setColorIndex(1);
- ledMtx_2.setBrightness(6);
- wifiConnection.begin(9600);
- wifiConnection.println(F("AT+CWMODE=1"));
- delay(1000);
- wifiConnection.println(F("AT+CIPMUX=0"));
- wifiConnection.println("AT+CWJAP=""+(String)"你Wifi的SSID"+""+""+(String)"你Wifi的密碼"+""");
複製代碼
|
|