【教程分享】Arduino接入機智雲,實現物聯網開發html
準備工做:
- 1.雲端數據點建立。
- 2.Ardunio UNO R3一塊,某寶10來塊錢一塊。
- 3.ESP12F(32Mbit)一個(須要給模塊下載GAgent固件,固件下載,按照文件裏面地址對應下載,固件下載地址http://goms-1251025085.cosgz.myqcloud.com/GAgent_00ESP826_04020029-1524657141995.rar)
- 4.2.DHT11
- 5.RGB_LED(共陰)
- 6.微動開關
- 7. 5V繼電器一個
接線方法:
- Ardunio esp8266
- Txd Rxd
- Rxd Txd
- 3.3V VCC
- Gnd Gng
- Ardunio RGB_LED
- pin9 R
- pin10 G
- pin11 B
- GND COM
- Ardunio DHT11
- A0 data
- 5V VCC
- Gnd Gng
- Ardunio 繼電器
- pin2 in
- 5V VCC
- Gnd Gng
- Ardunio LED
- 5V 陽極
- pin3 陰極
建立完全部數據點以後。點擊MCU開發,按照下圖完成設置以後生成ardunio uno r3代碼。生成好代碼以後下載代碼
<ignore_js_op>
解壓代碼,導入到項目到開發環境之中(須要把文件解壓到軟件的庫目錄下,不然會編譯不過)
<ignore_js_op>
導入項目以後修改程序,對於程序介紹,此處我挑重點,其他代碼請自行下載附件查看。爲了方便查看增長的代碼,這個地方我就再也不增長庫,而是所有用驅動的方式來實現。
首先來看一下DHT11。咱們使用的是A0引腳,也就是第PC5引腳,驅動pin14代碼以下
- int temp;//溫度
- int humi;//溼度
- int tol;//校對碼
- int j;
- unsigned int loopCnt;
- int chr[40] = {0};//建立數字數組,用來存放40個bit
- unsigned long time;
- #define DHT11 14
- //溫溼度採集
- void read_dht11()
- {
- //delay(1000);//注意採集間隔應該大於1秒
- //設置19號接口模式爲:輸出
- //輸出低電平20ms(>18ms)
- //輸出高電平40μs
- pinMode(DHT11,OUTPUT);
- digitalWrite(DHT11,LOW);
- delay(20);
- digitalWrite(DHT11,HIGH);
- delayMicroseconds(40);
- digitalWrite(DHT11,LOW);
- //設置2號接口模式:輸入
- pinMode(DHT11,INPUT);
- //高電平響應信號
- loopCnt=10000;
- while(digitalRead(DHT11) != HIGH)
- {
- if(loopCnt-- == 0)
- {
- //若是長時間不返回高電平,跳出程序。
- break;
- }
- }
- //低電平響應信號
- loopCnt=30000;
- while(digitalRead(DHT11) != LOW)
- {
- if(loopCnt-- == 0)
- {
- //若是長時間不返回低電平,跳出程序。
- break;
- }
- }
- //開始讀取bit1-40的數值
- for(int i=0;i<40;i++)
- {
- while(digitalRead(DHT11) == LOW)
- {}
- //當出現高電平時,記下時間「time」
- time = micros();
- while(digitalRead(DHT11) == HIGH)
- {}
- //當出現低電平,記下時間,再減去剛纔儲存的time
- //得出的值若大於50μs,則爲‘1’,不然爲‘0’
- //並儲存到數組裏去
- if (micros() - time >50)
- {
- chr=1;
- }else{
- chr=0;
- }
- }
- //溼度,8位的bit,轉換爲數值
- humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
- //溫度,8位的bit,轉換爲數值
- temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
- //校對碼,8位的bit,轉換爲數值
- tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
- //輸出:溫度、溼度、校對碼
- //校對碼,我這裏沒用上
- //理論上,溼度+溫度=校對碼
- //若是數值不相等,說明讀取的數據有錯。
- bgn;//跳出程序,多是沒查DHT11可修改爲報警或者故障
- }
複製代碼
接下來修改按鍵,按鍵要把管腳修改過去,按鍵的管腳分別是A4 A5 對應18 19,修改事後:
- #define KEY1 18
- #define KEY2 19
- bool led = 0;//LED狀態,控制的時候進行取反
- bool gdq = 0;//繼電器狀態控制的時候進行取反
複製代碼
同時修改長短按鍵。
- void KEY_Handle(void)
- {
- /* Press for over than 3 second is Long Press */
- switch (gokit_keydown())
- {
- case KEY1_SHORT_PRESS:
- if(led)
- {
- led=0;
- mySerial.println(F("KEY1_SHORT_PRESS LED OFF"));
- }
- else
- {
- led=1;
- mySerial.println(F("KEY1_SHORT_PRESS LED ON"));
- }
- break;
- case KEY1_LONG_PRESS:
- mySerial.println(F("KEY1_LONG_PRESS Soft AP mode"));
- myGizwits.setBindMode(WIFI_SOFTAP_MODE);
- //Soft AP mode
- break;
- case KEY2_SHORT_PRESS:
- if(gdq)
- {
- gdq=0;
- mySerial.println(F("KEY2_SHORT_PRESS LED OFF"));
- }
- else
- {
- gdq=1;
- mySerial.println(F("KEY2_SHORT_PRESS LED ON"));
- }
- break;
- case KEY2_LONG_PRESS:
- mySerial.println(F("KEY2_LONG_PRESS ,AirLink mode"));
- myGizwits.setBindMode(WIFI_AIRLINK_MODE);
- //AirLink mode
- break;
- default:
- break;
- }
- }
複製代碼
到此按鍵就修改完了,接下來修改用戶處理函數,沒安裝notepad++的最好安裝一個,用記事本也行,我的習慣用notepad++。修改其餘代碼須要用到。利用
notepad++打開
gizwits_protocol.h文件,修改數據上報時間爲1秒或者2秒,此處我修改成2秒#define REPORT_TIME_MAX 2000 //2S,打開gizwits_product.c文件,裏面絕大部分程序思惟按照類上一篇帖子的思惟進行修改。
將如下內容補充完整。在gizwits_product.c裏面
- case EVENT_switch_led:
- currentDataPoint.valueswitch_led = dataPointPtr->valueswitch_led;
- attrFlags.flagswitch_led = 1;
- if(0x01 == currentDataPoint.valueswitch_led)
- {
- led=1;//打開大功率LED
- }
- else
- {
- led=0;//關閉大功率LED
- }
- break;
- case EVENT_switch_relay:
- currentDataPoint.valueswitch_relay = dataPointPtr->valueswitch_relay;
- attrFlags.flagswitch_relay = 1;
- if(0x01 == currentDataPoint.valueswitch_led)
- {
- gdq=1;//打開繼電器
- }
- else
- {
- gdq=0;//關閉繼電器
- }
- break;
- case EVENT_color_controls:
- currentDataPoint.valuecolor_controls = dataPointPtr->valuecolor_controls;
- attrFlags.flagcolor_controls = 1;
- mode_Cloud_data[0]=currentDataPoint.valuecolor_controls;
- break;
- case EVENT_LED_R:
- currentDataPoint.valueLED_R = dataPointPtr->valueLED_R;
- attrFlags.flagLED_R = 1;
- mode_Cloud_data[1]=currentDataPoint.valueLED_R;//紅色值
- if(mode_Cloud_data[1]!=1)mode_Cloud_data[0]=1;//自定義
- break;
- case EVENT_LED_G:
- currentDataPoint.valueLED_G = dataPointPtr->valueLED_G;
- attrFlags.flagLED_G = 1;
- mode_Cloud_data[2]=currentDataPoint.valueLED_G;//綠色值
- if(mode_Cloud_data[2]!=1)mode_Cloud_data[0]=1;//自定義
- break;
- case EVENT_LED_B:
- currentDataPoint.valueLED_B = dataPointPtr->valueLED_B;
- attrFlags.flagLED_B = 1;
- mode_Cloud_data[3]=currentDataPoint.valueLED_B;//藍色值
- if(mode_Cloud_data[0]!=1)mode_Cloud_data[0]=1;//自定義
- break;
複製代碼
在文件開頭全局一個int mode_Cloud_data[4]={0};//雲端數據緩存 0模式 1R 2G 3B
接下來回到Ardunio軟件,增長RGB驅動函數。
定義管腳
- int redPin = 9 ;
- int greenPin = 10;
- int bluePin = 11;
- 初始化
- pinMode(redPin ,OUTPUT);
- pinMode(greenPin ,OUTPUT);
- pinMode(bluePin ,OUTPUT);
- RGB控制函數
- //rgb控制
- void RGB_light_set_color(int red,int green,int blue)
- {
- analogWrite(redPin,red);
- analogWrite(greenPin,green);
- analogWrite(bluePin,blue);
- }
複製代碼
接下來咱們須要新增用戶處理的函數
- //用戶處理函數
- void setsystem()
- {
- if(led) digitalWrite(LED_SW,LOW);
- else digitalWrite(LED_SW,HIGH);
- if(gdq) digitalWrite(GDQ_SW,LOW);
- else digitalWrite(GDQ_SW,HIGH);
- switch(mode_Cloud_data[0])
- {
- case 0 :
- RGB_light_set_color(0,0,0);//關閉燈
- currentDataPoint.valuecolor_controls = 0;
- currentDataPoint.valueLED_R = 0;
- currentDataPoint.valueLED_G = 0;
- currentDataPoint.valueLED_B = 0;
- break;
- case 1 :
- RGB_light_set_color(mode_Cloud_data[1],mode_Cloud_data[2],mode_Cloud_data[3]);//自定義
- currentDataPoint.valuecolor_controls = 1;
- currentDataPoint.valueLED_R = mode_Cloud_data[1];
- currentDataPoint.valueLED_G = mode_Cloud_data[2];
- currentDataPoint.valueLED_B = mode_Cloud_data[3];
- break;
- case 2 :
- RGB_light_set_color(255,0,0);//紅色
- currentDataPoint.valuecolor_controls = 2;
- currentDataPoint.valueLED_R = 255;
- currentDataPoint.valueLED_G = 0;
- currentDataPoint.valueLED_B = 0;
- break;
- case 3 :
- RGB_light_set_color(0,255,0);//綠色
- currentDataPoint.valuecolor_controls = 3;
- currentDataPoint.valueLED_R = 0;
- currentDataPoint.valueLED_G = 255;
- currentDataPoint.valueLED_B = 0;
- break;
- case 4 :
- RGB_light_set_color(0,0,255);//藍色
- currentDataPoint.valuecolor_controls = 4;
- currentDataPoint.valueLED_R = 0;
- currentDataPoint.valueLED_G = 0;
- currentDataPoint.valueLED_B = 255;
- break;
- case 5 :
- RGB_light_set_color(255,255,0);//黃色
- currentDataPoint.valuecolor_controls = 5;
- currentDataPoint.valueLED_R = 255;
- currentDataPoint.valueLED_G = 255;
- currentDataPoint.valueLED_B = 0;
- break;
- case 6 :
- RGB_light_set_color(255,0,255);//紫色
- currentDataPoint.valuecolor_controls = 6;
- currentDataPoint.valueLED_R = 255;
- currentDataPoint.valueLED_G = 0;
- currentDataPoint.valueLED_B = 255;
- break;
- case 7 :
- RGB_light_set_color(255,52,179);//粉色(估計不是粉色)
- currentDataPoint.valuecolor_controls = 7;
- currentDataPoint.valueLED_R = 255;
- currentDataPoint.valueLED_G = 52;
- currentDataPoint.valueLED_B = 179;
- break;
- case 8 :
- RGB_light_set_color(255,255,255);//白色
- currentDataPoint.valuecolor_controls = 8;
- currentDataPoint.valueLED_R = 255;
- currentDataPoint.valueLED_G = 255;
- currentDataPoint.valueLED_B = 255;
- break;
- default:
- break;
- }
- }
複製代碼
將setsystem();函數放在loop裏面,同時吧DHT11
也放進去
- setsystem();
- read_dht11();
- currentDataPoint.valuetemperature = temp;
- currentDataPoint.valuehumidity = humi;
複製代碼