加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

【教程分享】Arduino uno R3接入机智云,快速实现智能设备开发

发布时间:2020-12-14 04:22:05 所属栏目:大数据 来源:网络整理
导读:【教程分享】Arduino接入机智云,实现物联网开发 ? ? 准备工作: 1.云端数据点创建。 2.Ardunio UNO R3一块,某宝10来块钱一块。 3.ESP12F(32Mbit)一个(需要给模块下载GAgent固件,固件下载,按照文件里面地址对应下载,固件下载地址 http://goms-1251025

【教程分享】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代码如下
  1. int temp;//温度
  2. int humi;//湿度
  3. int tol;//校对码
  4. int j;
  5. unsigned int loopCnt;
  6. int chr[40] = {0};//创建数字数组,用来存放40个bit
  7. unsigned long time;
  8. #define DHT11 14
  9. //温湿度采集
  10. void read_dht11()
  11. {
  12. ??//delay(1000);//注意采集间隔应该大于1秒
  13. ??//设置19号接口模式为:输出
  14. ??//输出低电平20ms(>18ms)
  15. ??//输出高电平40μs
  16. ??pinMode(DHT11,OUTPUT);
  17. ??digitalWrite(DHT11,LOW);
  18. ??delay(20);
  19. ??digitalWrite(DHT11,HIGH);
  20. ??delayMicroseconds(40);
  21. ??digitalWrite(DHT11,LOW);
  22. ??//设置2号接口模式:输入
  23. ??pinMode(DHT11,INPUT);
  24. ??//高电平响应信号
  25. ??loopCnt=10000;
  26. ??while(digitalRead(DHT11) != HIGH)
  27. ??{
  28. ? ? if(loopCnt-- == 0)
  29. ? ? {
  30. ? ?? ?? ? //如果长时间不返回高电平,跳出程序。
  31. ? ?? ?break;
  32. ? ? }
  33. ??}
  34. ??//低电平响应信号
  35. ??loopCnt=30000;
  36. ??while(digitalRead(DHT11) != LOW)
  37. ??{
  38. ? ? if(loopCnt-- == 0)
  39. ? ? {
  40. ? ? //如果长时间不返回低电平,跳出程序。
  41. ? ?? ?break;
  42. ? ? }
  43. ??}
  44. ??//开始读取bit1-40的数值??
  45. ? ? for(int i=0;i<40;i++)
  46. ??{
  47. ? ? while(digitalRead(DHT11) == LOW)
  48. ? ? {}
  49. ? ?? ?? ? //当出现高电平时,记下时间“time”
  50. ? ? time = micros();
  51. ? ? while(digitalRead(DHT11) == HIGH)
  52. ? ? {}
  53. //当出现低电平,记下时间,再减去刚才储存的time
  54. //得出的值若大于50μs,则为‘1’,否则为‘0’
  55. //并储存到数组里去
  56. ? ? if (micros() - time >50)
  57. ? ? {
  58. ? ?? ?chr=1;
  59. ? ? }else{
  60. ? ?? ?chr=0;
  61. ? ? }
  62. ??}
  63. ? ? //湿度,8位的bit,转换为数值
  64. ? ? humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
  65. ? ? //温度,8位的bit,转换为数值
  66. ? ? temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
  67. ? ? //校对码,8位的bit,转换为数值
  68. ? ? tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
  69. ? ? //输出:温度、湿度、校对码
  70. ? ? //校对码,我这里没用上
  71. ? ? //理论上,湿度+温度=校对码
  72. ? ? //如果数值不相等,说明读取的数据有错。??
  73. ? ?bgn;//跳出程序,可能是没查DHT11可修改成报警或者故障
  74. }
复制代码
?
接下来修改按键,按键要把管脚修改过去,按键的管脚分别是A4 A5 对应18??19,修改过后:
  1. #define? ?KEY1? ?? ?? ?? ???18
  2. #define? ?KEY2? ?? ?? ?? ???19
  3. bool led = 0;//LED状态,控制的时候进行取反
  4. bool gdq = 0;//继电器状态控制的时候进行取反
复制代码
同时修改长短按键。
  1. void KEY_Handle(void)
  2. {
  3. ??/*??Press for over than 3 second is Long Press??*/
  4. ??switch (gokit_keydown())
  5. ??{
  6. ? ?? ?case KEY1_SHORT_PRESS:
  7. ? ?? ?if(led)
  8. ? ?? ?{
  9. ? ?? ???led=0;
  10. ? ?? ???mySerial.println(F("KEY1_SHORT_PRESS LED OFF"));
  11. ? ?? ?}
  12. ? ?? ???else
  13. ? ?? ???{
  14. ? ?? ?? ? led=1;
  15. ? ?? ?? ? mySerial.println(F("KEY1_SHORT_PRESS LED ON"));
  16. ? ?? ???}
  17. ? ?? ?break;
  18. ? ? case KEY1_LONG_PRESS:
  19. ? ?? ?mySerial.println(F("KEY1_LONG_PRESS Soft AP mode"));
  20. ? ?? ?myGizwits.setBindMode(WIFI_SOFTAP_MODE);
  21. ? ?? ?//Soft AP mode
  22. ? ?? ?break;
  23. ? ? case KEY2_SHORT_PRESS:
  24. ? ?? ?if(gdq)
  25. ? ?? ?{
  26. ? ?? ???gdq=0;
  27. ? ?? ???mySerial.println(F("KEY2_SHORT_PRESS LED OFF"));
  28. ? ?? ?}
  29. ? ?? ???else
  30. ? ?? ???{
  31. ? ?? ?? ? gdq=1;
  32. ? ?? ?? ? mySerial.println(F("KEY2_SHORT_PRESS LED ON"));
  33. ? ?? ???}
  34. ? ?? ?break;
  35. ? ? case KEY2_LONG_PRESS:
  36. ? ?? ?mySerial.println(F("KEY2_LONG_PRESS,AirLink mode"));
  37. ? ?? ?myGizwits.setBindMode(WIFI_AIRLINK_MODE);
  38. ? ?? ?//AirLink mode
  39. ? ?? ?break;
  40. ? ? default:
  41. ? ?? ?break;
  42. ??}
  43. }
复制代码
到此按键就修改完了,接下来修改用户处理函数,没安装notepad++的最好安装一个,用记事本也行,个人习惯用notepad++。修改其他代码需要用到。利用
notepad++打开
gizwits_protocol.h文件,修改数据上报时间为1秒或者2秒,此处我修改为2秒#define REPORT_TIME_MAX 2000 //2S,打开gizwits_product.c文件,里面绝大部分程序思维按照类上一篇帖子的思维进行修改。
将以下内容补充完整。在gizwits_product.c里面

  1. case EVENT_switch_led:
  2. ? ?? ???currentDataPoint.valueswitch_led = dataPointPtr->valueswitch_led;
  3. ? ?? ???attrFlags.flagswitch_led = 1;
  4. ? ?? ?? ?? ?? ? if(0x01 == currentDataPoint.valueswitch_led)
  5. ? ?? ?? ?? ?? ? {
  6. ? ?? ?? ?? ?? ?? ?? ?? ?led=1;//打开大功率LED
  7. ? ?? ?? ?? ?? ? }
  8. ? ?? ?? ?? ?? ? else
  9. ? ?? ?? ?? ?? ? {
  10. ? ?? ?? ?? ?? ?? ?? ?? ?led=0;//关闭大功率LED
  11. ? ?? ?? ?? ?? ? }
  12. ? ?? ???break;
  13. ? ?? ?case EVENT_switch_relay:
  14. ? ?? ???currentDataPoint.valueswitch_relay = dataPointPtr->valueswitch_relay;
  15. ? ?? ???attrFlags.flagswitch_relay = 1;
  16. ? ?? ?? ?? ?? ? if(0x01 == currentDataPoint.valueswitch_led)
  17. ? ?? ?? ?? ?? ? {
  18. ? ?? ?? ?? ?? ?? ?? ?? ?gdq=1;//打开继电器
  19. ? ?? ?? ?? ?? ? }
  20. ? ?? ?? ?? ?? ? else
  21. ? ?? ?? ?? ?? ? {
  22. ? ?? ?? ?? ?? ?? ?? ?? ?gdq=0;//关闭继电器
  23. ? ?? ?? ?? ?? ? }? ?? ?? ?? ?? ?
  24. ? ?? ???break;
  25. ? ?? ?case EVENT_color_controls:
  26. ? ?? ???currentDataPoint.valuecolor_controls = dataPointPtr->valuecolor_controls;
  27. ? ?? ???attrFlags.flagcolor_controls = 1;
  28. ? ?? ?? ?? ?? ? mode_Cloud_data[0]=currentDataPoint.valuecolor_controls;
  29. ? ?? ???break;
  30. ? ?? ?case EVENT_LED_R:
  31. ? ?? ???currentDataPoint.valueLED_R = dataPointPtr->valueLED_R;
  32. ? ?? ???attrFlags.flagLED_R = 1;
  33. ? ?? ?? ?? ?? ? mode_Cloud_data[1]=currentDataPoint.valueLED_R;//红色值
  34. ? ?? ?? ?? ?? ? if(mode_Cloud_data[1]!=1)mode_Cloud_data[0]=1;//自定义
  35. ? ?? ???break;
  36. ? ?? ?case EVENT_LED_G:
  37. ? ?? ???currentDataPoint.valueLED_G = dataPointPtr->valueLED_G;
  38. ? ?? ???attrFlags.flagLED_G = 1;
  39. ? ?? ?? ?? ?? ? mode_Cloud_data[2]=currentDataPoint.valueLED_G;//绿色值
  40. ? ?? ?? ?? ?? ? if(mode_Cloud_data[2]!=1)mode_Cloud_data[0]=1;//自定义
  41. ? ?? ???break;
  42. ? ?? ?case EVENT_LED_B:
  43. ? ?? ???currentDataPoint.valueLED_B = dataPointPtr->valueLED_B;
  44. ? ?? ???attrFlags.flagLED_B = 1;
  45. ? ?? ?? ?? ?? ? mode_Cloud_data[3]=currentDataPoint.valueLED_B;//蓝色值
  46. ? ?? ?? ?? ?? ? if(mode_Cloud_data[0]!=1)mode_Cloud_data[0]=1;//自定义
  47. ? ?? ???break;
复制代码
在文件开头全局一个int mode_Cloud_data[4]={0};//云端数据缓存??0模式 1R 2G 3B


接下来回到Ardunio软件,增加RGB驱动函数。
定义管脚
  1. int redPin = 9 ;
  2. int greenPin = 10;
  3. int bluePin = 11;
  4. 初始化
  5. ??pinMode(redPin,OUTPUT);
  6. ??pinMode(greenPin,OUTPUT);
  7. ??pinMode(bluePin,OUTPUT);
  8. RGB控制函数
  9. //rgb控制
  10. void RGB_light_set_color(int red,int green,int blue)
  11. {
  12. ? ? analogWrite(redPin,red);
  13. ? ? analogWrite(greenPin,green);
  14. ? ? analogWrite(bluePin,blue);
  15. }
复制代码
接下来我们需要新增用户处理的函数
  1. //用户处理函数
  2. void setsystem()
  3. {
  4. ??if(led) digitalWrite(LED_SW,LOW);
  5. ? ? else digitalWrite(LED_SW,HIGH);
  6. ??if(gdq) digitalWrite(GDQ_SW,LOW);
  7. ? ? else digitalWrite(GDQ_SW,HIGH);
  8. ??switch(mode_Cloud_data[0])
  9. ??{
  10. ? ? case 0 :
  11. ? ?? ?RGB_light_set_color(0,0);//关闭灯
  12. ? ?? ?currentDataPoint.valuecolor_controls = 0;
  13. ? ?? ?currentDataPoint.valueLED_R = 0;
  14. ? ?? ?currentDataPoint.valueLED_G = 0;
  15. ? ?? ?currentDataPoint.valueLED_B = 0;
  16. ? ?? ?break;
  17. ? ? case 1 :
  18. ? ?? ?RGB_light_set_color(mode_Cloud_data[1],mode_Cloud_data[2],mode_Cloud_data[3]);//自定义
  19. ? ?? ?currentDataPoint.valuecolor_controls = 1;
  20. ? ?? ?currentDataPoint.valueLED_R = mode_Cloud_data[1];
  21. ? ?? ?currentDataPoint.valueLED_G = mode_Cloud_data[2];
  22. ? ?? ?currentDataPoint.valueLED_B = mode_Cloud_data[3];
  23. ? ?? ?break;
  24. ? ? case 2 :
  25. ? ?? ?RGB_light_set_color(255,0);//红色
  26. ? ?? ?currentDataPoint.valuecolor_controls = 2;
  27. ? ?? ?currentDataPoint.valueLED_R = 255;
  28. ? ?? ?currentDataPoint.valueLED_G = 0;
  29. ? ?? ?currentDataPoint.valueLED_B = 0;
  30. ? ?? ?break;
  31. ? ? case 3 :
  32. ? ?? ?RGB_light_set_color(0,255,0);//绿色
  33. ? ?? ?currentDataPoint.valuecolor_controls = 3;
  34. ? ?? ?currentDataPoint.valueLED_R = 0;
  35. ? ?? ?currentDataPoint.valueLED_G = 255;
  36. ? ?? ?currentDataPoint.valueLED_B = 0;
  37. ? ?? ?break;
  38. ? ? case 4 :
  39. ? ?? ?RGB_light_set_color(0,255);//蓝色
  40. ? ?? ?currentDataPoint.valuecolor_controls = 4;
  41. ? ?? ?currentDataPoint.valueLED_R = 0;
  42. ? ?? ?currentDataPoint.valueLED_G = 0;
  43. ? ?? ?currentDataPoint.valueLED_B = 255;
  44. ? ?? ?break;
  45. ? ? case 5 :
  46. ? ?? ?RGB_light_set_color(255,0);//黄色
  47. ? ?? ?currentDataPoint.valuecolor_controls = 5;
  48. ? ?? ?currentDataPoint.valueLED_R = 255;
  49. ? ?? ?currentDataPoint.valueLED_G = 255;
  50. ? ?? ?currentDataPoint.valueLED_B = 0;
  51. ? ?? ?break;
  52. ? ? case 6 :
  53. ? ?? ?RGB_light_set_color(255,255);//紫色
  54. ? ?? ?currentDataPoint.valuecolor_controls = 6;
  55. ? ?? ?currentDataPoint.valueLED_R = 255;
  56. ? ?? ?currentDataPoint.valueLED_G = 0;
  57. ? ?? ?currentDataPoint.valueLED_B = 255;
  58. ? ?? ?break;
  59. ? ? case 7 :
  60. ? ?? ?RGB_light_set_color(255,52,179);//粉色(估计不是粉色)
  61. ? ?? ?currentDataPoint.valuecolor_controls = 7;
  62. ? ?? ?currentDataPoint.valueLED_R = 255;
  63. ? ?? ?currentDataPoint.valueLED_G = 52;
  64. ? ?? ?currentDataPoint.valueLED_B = 179;
  65. ? ?? ?break;
  66. ? ? case 8 :
  67. ? ?? ?RGB_light_set_color(255,255);//白色
  68. ? ?? ?currentDataPoint.valuecolor_controls = 8;
  69. ? ?? ?currentDataPoint.valueLED_R = 255;
  70. ? ?? ?currentDataPoint.valueLED_G = 255;
  71. ? ?? ?currentDataPoint.valueLED_B = 255;
  72. ? ?? ?break;
  73. ? ? default:
  74. ? ?? ?break;
  75. ??}??
  76. }
复制代码
将setsystem();函数放在loop里面,同时吧DHT11
也放进去
  1. ??setsystem();
  2. ??read_dht11();
  3. ??currentDataPoint.valuetemperature = temp;
  4. ??currentDataPoint.valuehumidity = humi;
复制代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读