【教程分享】Arduino接入机智云,实现物联网开发
准备工作:
复制代码
复制代码
同时修改长短按键。
复制代码
复制代码
复制代码
复制代码
复制代码
- 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 阴极
- Ardunio 按键
- A4 K1
- A5 K2
教程开始
创建完所有数据点之后。点击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;