圓創力科技

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

查看: 8154|回復: 0

mBot PM2.5 ThinkSpeak上傳範例

[複製鏈接]
magiccar 發表於 2018-12-1 15:45 | 顯示全部樓層 |閱讀模式
我們在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

PM2.5範例01


自動產生的 Arduino C 程式碼:
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SoftwareSerial.h>

  4. #include <MePm25Sensor.h>
  5. #include <MeMCore.h>
  6. /* HKU e-Learning Lab: Steve Suen, Ken Law, Karl Cheung, Nicholas Szeto, Envose Au (Special thanks to Mr. Kwok Tsz Fung) */
  7. MeDCMotor motor_9(9);
  8. MeDCMotor motor_10(10);               

  9. void move(int direction, int speed)
  10. {
  11.       int leftSpeed = 0;
  12.       int rightSpeed = 0;
  13.       if(direction == 1){
  14.                 leftSpeed = speed;
  15.                 rightSpeed = speed;
  16.       }else if(direction == 2){
  17.                 leftSpeed = -speed;
  18.                 rightSpeed = -speed;
  19.       }else if(direction == 3){
  20.                 leftSpeed = -speed;
  21.                 rightSpeed = speed;
  22.       }else if(direction == 4){
  23.                 leftSpeed = speed;
  24.                 rightSpeed = -speed;
  25.       }
  26.       motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
  27.       motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
  28. }
  29.                                 
  30. double angle_rad = PI/180.0;
  31. double angle_deg = 180.0/PI;
  32. double PM25;
  33. int receivePin[] = {12, 10, A3, A1};
  34. int transmitPin[] = {11, 9, A2, A0};
  35. SoftwareSerial wifiConnection(receivePin[1-1], transmitPin[1-1]);
  36. MePm25Sensor myMePm25Sensor(4);
  37. uint16_t getPMValue(int type)
  38. {
  39.             switch(type)
  40.             {
  41.                         case 1:
  42.                         return myMePm25Sensor.readPm1_0Concentration();
  43.                                 break;
  44.                         case 2:
  45.                                 return myMePm25Sensor.readPm2_5Concentration();
  46.                                  break;
  47.                         case 3:
  48.                         return myMePm25Sensor.readPm10Concentration();
  49.                                 break;
  50.             }
  51. }
  52. MeLEDMatrix ledMtx_2(2);
  53. char thingSpeakHttpGet[256];

  54. void setup(){
  55.     myMePm25Sensor.begin(9600);
  56.     ledMtx_2.setColorIndex(1);
  57.     ledMtx_2.setBrightness(6);
  58.     wifiConnection.begin(9600);
  59.     wifiConnection.println(F("AT+CWMODE=1"));
  60.     delay(1000);
  61.     wifiConnection.println(F("AT+CIPMUX=0"));
  62.     wifiConnection.println("AT+CWJAP=""+(String)"你Wifi的SSID"+""+""+(String)"你Wifi的密碼"+""");
  63.     delay(10000);
  64. }

  65. void loop(){
  66.    
  67.     _delay(1);
  68.     PM25 = getPMValue(2);
  69.     ledMtx_2.showNum(PM25,3);
  70.     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);
  71.     wifiConnection.println(F("AT+CIPSTART="TCP","184.106.153.149",80"));
  72.     delay(3000);
  73.     wifiConnection.println("AT+CIPSEND="+(String)(strlen(thingSpeakHttpGet)+2));
  74.     delay(3000);
  75.     wifiConnection.println(thingSpeakHttpGet);
  76.     delay(3000);
  77.     wifiConnection.println(F("AT+CIPCLOSE"));
  78.     _delay(15);
  79.     _loop();
  80. }

  81. void _delay(float seconds){
  82.     long endTime = millis() + seconds * 1000;
  83.     while(millis() < endTime)_loop();
  84. }

  85. void _loop(){
  86.     myMePm25Sensor.rxloop();
  87. }
複製代碼

修改說明圖解:
 

PM2.5修正說明

PM2.5修正說明



修改後的 Arduino C 程式碼:
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <SoftwareSerial.h>

  4. #include <MePm25Sensor.h>
  5. #include <MeMCore.h>
  6. /* HKU e-Learning Lab: Steve Suen, Ken Law, Karl Cheung, Nicholas Szeto, Envose Au (Special thanks to Mr. Kwok Tsz Fung) */
  7. MeDCMotor motor_9(9);
  8. MeDCMotor motor_10(10);               

  9. void move(int direction, int speed)
  10. {
  11.       int leftSpeed = 0;
  12.       int rightSpeed = 0;
  13.       if(direction == 1){
  14.                 leftSpeed = speed;
  15.                 rightSpeed = speed;
  16.       }else if(direction == 2){
  17.                 leftSpeed = -speed;
  18.                 rightSpeed = -speed;
  19.       }else if(direction == 3){
  20.                 leftSpeed = -speed;
  21.                 rightSpeed = speed;
  22.       }else if(direction == 4){
  23.                 leftSpeed = speed;
  24.                 rightSpeed = -speed;
  25.       }
  26.       motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
  27.       motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
  28. }
  29.                                 
  30. double angle_rad = PI/180.0;
  31. double angle_deg = 180.0/PI;
  32. double PM25;
  33. int receivePin[] = {12, 10, A3, A1};
  34. int transmitPin[] = {11, 9, A2, A0};
  35. SoftwareSerial wifiConnection(receivePin[1-1], transmitPin[1-1]);
  36. MePm25Sensor myMePm25Sensor(4);
  37. uint16_t getPMValue(int type)
  38. {
  39.             switch(type)
  40.             {
  41.                         case 1:
  42.                         return myMePm25Sensor.readPm1_0Concentration();
  43.                                 break;
  44.                         case 2:
  45.                                 return myMePm25Sensor.readPm2_5Concentration();
  46.                                  break;
  47.                         case 3:
  48.                         return myMePm25Sensor.readPm10Concentration();
  49.                                 break;
  50.             }
  51. }
  52. MeLEDMatrix ledMtx_2(2);
  53. char thingSpeakHttpGet[256];

  54. void setup(){
  55.     ledMtx_2.setColorIndex(1);
  56.     ledMtx_2.setBrightness(6);
  57.     wifiConnection.begin(9600);
  58.     wifiConnection.println(F("AT+CWMODE=1"));
  59.     delay(1000);
  60.     wifiConnection.println(F("AT+CIPMUX=0"));
  61.     wifiConnection.println("AT+CWJAP=""+(String)"你Wifi的SSID"+""+""+(String)"你Wifi的密碼"+""");
複製代碼









您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

QQ|Archiver|手機版|小黑屋|圓創力科技有限公司 IOP Robotic Technology Co.,Ltd Tel: 07-3924582 Fax: 07-3924001

GMT+8, 2024-3-19 15:39 , Processed in 0.028146 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表