收藏官网首页
查看: 9514|回复: 1

[经验分享] 【基于机智云开发平台+Gokit】+ DIY一个环境监测仪

1

主题

1

帖子

87

积分

注册会员

Rank: 2

积分
87
跳转到指定楼层
楼主
 楼主| 发表于 2021-11-7 20:27:20 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
免费使用STM32、APP自动代码生成工具

很幸运获得了一块创客版gokit开发板,有空了学习了怎么将采集的环境传感器数据接入机智云平台,能够在手机app上查看数据控制。
板子资料链接:
http://docs.gizwits.com/zh-cn/deviceDev/Gokit3/GoKit3%E7%A1%AC%E4%BB%B6%E6%89%8B%E5%86%8C.html

一、按键
功能扩展板上设计有三个可编程按键,用户可根据需要来使用。



按键KEY1、KEY2、KEY3依次连接到D6、D7、A1接口,按下为低电平。

二、DHT11温湿度传感器
扩展板上设计了一款有已校准数字信号输出的温湿度传感器:DHT11。原理图如下:






DHT11连接到了D3接口。

该传感器是一款含有已校准数字信号输出的温湿度复合传感器。它应用专用的数字模块采集技术和温湿度传感技术,确保产品具有极高的可靠性不卓越的长期稳定性。传感器包拪一个电阻式感湿元件和一个 NTC测温元件,并不一个高性能MCU连接。因此该产品具有品质卓越、抗干扰能力强、性价比极高等优点。每个 DHT11 传感器都在极为精确的湿度校验室中进行校准。校准系数以程序的形式储存在 OTP 内存中,传感器内部在检测信号的处理过程中要调用这些校准系数。单线制串行接口,只需要1个GPIO即可驱动,使系统集成变得简易快捷。超小的体积、极低的功耗,信号传输距离可达 20 米以上,产品为 4 针单排引脚封装。
相关参数:精度湿度+-5%RH, 温度+-2℃,量程湿度20-90%RH, 温度0~50℃。
DHT11的驱动使用arduino提供的库即可:

测试代码如下:
  1. // Example testing sketch for various DHT humidity/temperature sensors
  2. // Written by ladyada, public domain

  3. // REQUIRES the following Arduino libraries:
  4. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
  5. // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

  6. #include "DHT.h"

  7. #define DHTPIN 3     // Digital pin connected to the DHT sensor
  8. // Feather HUZZAH esp8266 note: use pins 3, 4, 5, 12, 13 or 14 --
  9. // Pin 15 can work but DHT must be disconnected during program upload.

  10. // Uncomment whatever type you're using!
  11. //#define DHTTYPE DHT11   // DHT 11
  12. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  13. //#define DHTTYPE DHT21   // DHT 21 (AM2301)

  14. // Connect pin 1 (on the left) of the sensor to +5V
  15. // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
  16. // to 3.3V instead of 5V!
  17. // Connect pin 2 of the sensor to whatever your DHTPIN is
  18. // Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
  19. // Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
  20. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

  21. // Initialize DHT sensor.
  22. // Note that older versions of this library took an optional third parameter to
  23. // tweak the timings for faster processors.  This parameter is no longer needed
  24. // as the current DHT reading algorithm adjusts itself to work on faster procs.
  25. DHT dht(DHTPIN, DHTTYPE);

  26. void setup() {
  27.   Serial.begin(9600);
  28.   Serial.println(F("DHTxx test!"));

  29.   dht.begin();
  30. }

  31. void loop() {
  32.   // Wait a few seconds between measurements.
  33.   delay(2000);

  34.   // Reading temperature or humidity takes about 250 milliseconds!
  35.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  36.   float h = dht.readHumidity();
  37.   // Read temperature as Celsius (the default)
  38.   float t = dht.readTemperature();
  39.   // Read temperature as Fahrenheit (isFahrenheit = true)
  40.   float f = dht.readTemperature(true);

  41.   // Check if any reads failed and exit early (to try again).
  42.   if (isnan(h) || isnan(t) || isnan(f)) {
  43.     Serial.println(F("Failed to read from DHT sensor!"));
  44.     return;
  45.   }

  46.   // Compute heat index in Fahrenheit (the default)
  47.   float hif = dht.computeHeatIndex(f, h);
  48.   // Compute heat index in Celsius (isFahreheit = false)
  49.   float hic = dht.computeHeatIndex(t, h, false);

  50.   Serial.print(F("Humidity: "));
  51.   Serial.print(h);
  52.   Serial.print(F("%  Temperature: "));
  53.   Serial.print(t);
  54.   Serial.print(F("°C "));
  55.   Serial.print(f);
  56.   Serial.print(F("°F  Heat index: "));
  57.   Serial.print(hic);
  58.   Serial.print(F("°C "));
  59.   Serial.print(hif);
  60.   Serial.println(F("°F"));
  61. }
复制代码
效果演示:



三、驱动RGB三色LED
扩展板上设计了一个RGB三色LED指示灯,通过P9813驱动,可以实现对LED的全色彩控制。原理图如下:





P9813 时钟和数据两个驱动管脚连接在 I2C 的总线上。

P9813 是一款全彩点光源 LED 驱劢芯片,采用 CMOS 工艺,提供三路恒流驱劢及256级灰度调制输出。采用双线传输方式(DATA不CLK),内建再生,可提升传输距离。用于驱动显示灯光发换、各式字符发换、彩色动漫图案。根据不同控制器和客户不同形式要求,进行脱机或联机运行。
该芯片的特点:
IC 输入电压 5V~6.5V;内置LDO,4.5V稳压输出;
外置单电阻反馈模式,三路驱劢,每路驱动电流 0-45mA;
带有输入信号校验功能;
内置环振(1.2MHz)支持不间断 FREE-RUN 调制输出,维持画面静止功能;
最大串行输入数据时钟频率15MHz;
具有信号锁相再生,1MHz 数据传输速度下级联点光源可达 1024 个;
LED 驱动端口耐压 17V。

这里采用网友开源的代码驱动:
https://github.com/pjpmarques/ChainableLED
测试代码如下:
  1. <blockquote>#include <ChainableLED.h>
复制代码
效果演示:





四、接入机智云平台
找到了帖子https://club.gizwits.com/forum.php?mod=viewthread&tid=6728&highlight=arduino
写的很详细,照着这个一步步做就成功接入机智云平台了。感谢网友的无私分享。

创建产品后依据模板导入数据点:

选择这个模板:

删除模板数据点里面不需要的,然后添加自己定义的:

生成arduino代码包:

下载这个代码包:

得到这样的文件:
examples文件夹,里面有2个文件夹networkConfig和simpleTry,进去后有networkConfig.ino、simpleTry.ino,这是arduino项目。
simpleTry.ino里面有机智云程序框架,networkConfig.ino里面有按键配置入网的方法。


将这些文件拷贝到这个目录(也可以将Gizwits整个目录拷贝到arduino的用户库文件目录):

这里修改下头文件引用为"",如果将Gizwits整个目录拷贝到arduino的用户库文件目录那么不用修改。

机智云生成的代码框架,可以看到都帮我们把接口写好了,自己填充一下就行了:


我需要加入温湿度采集、按键控制、RGB LED控制、烟雾传感器数据读取,其中烟雾传感器使用模拟输入管脚A2读取。
最终代码如下:
  1. /**
  2. ****************************************
  3. * @file         Gizwits + ArduinoUnoR3 Library
  4. * @brief        Datapoints handle , Gizwits Process
  5. * @author       Gizwits
  6. * @date         2017-08-01
  7. * @version      V03030000
  8. * @copyright    Gizwits
  9. *
  10. * @note         机智云.只为智能硬件而生
  11. *               Gizwits Smart Cloud  for Smart Products
  12. *               链接|增值ֵ|开放|中立|安全|自有|自由|生态
  13. *               www.gizwits.com
  14. *
  15. ****************************************/
  16. #include "Gizwits.h"
  17. #include <Wire.h>

  18. #include <ChainableLED.h>
  19. #define NUM_LEDS  1

  20. ChainableLED leds(A5, A4, NUM_LEDS);

  21. #include "DHT.h"

  22. #define DHTPIN 3
  23. #define DHTTYPE DHT11   
  24. DHT dht(DHTPIN, DHTTYPE);

  25. Gizwits myGizwits;

  26. #define   KEY1              6
  27. #define   KEY2              7
  28. #define   KEY1_SHORT_PRESS  1
  29. #define   KEY1_LONG_PRESS   2
  30. #define   KEY2_SHORT_PRESS  4
  31. #define   KEY2_LONG_PRESS   8
  32. #define   NO_KEY            0
  33. #define   KEY_LONG_TIMER    3
  34. unsigned long Last_KeyTime = 0;

  35. unsigned long gokit_time_s(void)
  36. {
  37.   return millis() / 1000;
  38. }

  39. char gokit_key1down(void)
  40. {
  41.   unsigned long keep_time = 0;
  42.   if (digitalRead(KEY1) == LOW)
  43.   {
  44.     delay(100);
  45.     if (digitalRead(KEY1) == LOW)
  46.     {
  47.       keep_time = gokit_time_s();
  48.       while (digitalRead(KEY1) == LOW)
  49.       {
  50.         if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
  51.         {
  52.           Last_KeyTime = gokit_time_s();
  53.           return KEY1_LONG_PRESS;
  54.         }
  55.       } //until open the key

  56.       if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
  57.       {
  58.         return KEY1_SHORT_PRESS;
  59.       }
  60.       return 0;
  61.     }
  62.     return 0;
  63.   }
  64.   return 0;
  65. }

  66. char gokit_key2down(void)
  67. {
  68.   unsigned long keep_time = 0;
  69.   if (digitalRead(KEY2) == LOW)
  70.   {
  71.     delay(100);
  72.     if (digitalRead(KEY2) == LOW)
  73.     {
  74.       keep_time = gokit_time_s();
  75.       while (digitalRead(KEY2) == LOW) //until open the key
  76.       {

  77.         if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
  78.         {
  79.           Last_KeyTime = gokit_time_s();
  80.           return KEY2_LONG_PRESS;
  81.         }
  82.       }

  83.       if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
  84.       {
  85.         return KEY2_SHORT_PRESS;
  86.       }
  87.       return 0;
  88.     }
  89.     return 0;
  90.   }
  91.   return 0;
  92. }

  93. char gokit_keydown(void)
  94. {
  95.   char ret = 0;
  96.   ret |= gokit_key2down();
  97.   ret |= gokit_key1down();
  98.   return ret;

  99. }

  100. void KEY_Handle(void)
  101. {
  102.   /*  Press for over than 3 second is Long Press  */
  103.   switch (gokit_keydown())
  104.   {
  105.     case KEY1_SHORT_PRESS:
  106.       myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
  107.       break;
  108.     case KEY1_LONG_PRESS:
  109.       myGizwits.setBindMode(WIFI_RESET_MODE);
  110.       break;
  111.     case KEY2_SHORT_PRESS:
  112.       myGizwits.setBindMode(WIFI_SOFTAP_MODE);
  113.       //Soft AP mode
  114.       break;
  115.     case KEY2_LONG_PRESS:
  116.     leds.setColorRGB(0,0,255,0);
  117.       myGizwits.setBindMode(WIFI_AIRLINK_MODE);
  118.       //AirLink mode
  119.       break;
  120.     default:
  121.       break;
  122.   }
  123. }

  124. void setup() {
  125.   leds.init();
  126.   
  127.   pinMode(KEY1, INPUT_PULLUP);
  128.   pinMode(KEY2, INPUT_PULLUP);

  129.   dht.begin();

  130.   myGizwits.begin();

  131. }

  132. void wifiStatusHandle()
  133. {
  134.   if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
  135.   {
  136.   }
  137.   
  138.   if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
  139.   {
  140.   }
  141.   
  142.   if(myGizwits.wifiHasBeenSet(WIFI_STATION))
  143.   {
  144.   }
  145.   
  146.   if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
  147.   {
  148.     leds.setColorRGB(0,255,0,0);//连接路由器亮红色
  149.   }
  150.   
  151.   if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER))
  152.   {
  153.     leds.setColorRGB(0,0,0,0);//断开路由器灯灭
  154.   }
  155.   
  156.   if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M))
  157.   {
  158.     leds.setColorRGB(0,0,0,255);//连接云平台亮蓝色
  159.   }
  160.   
  161.   if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M))
  162.   {
  163.     leds.setColorRGB(0,0,0,0);//断开云平台灯灭
  164.   }
  165. }

  166. unsigned long previousMillis = 0;

  167. void loop() {  
  168.   float h,t;
  169.   unsigned long currentMillis = millis();
  170.   if (currentMillis - previousMillis >= 2000) {
  171.     previousMillis = currentMillis;
  172.   h = dht.readHumidity();
  173.   t = dht.readTemperature();
  174.   if (isnan(h) || isnan(t)) {
  175.     return;
  176.   }
  177.   }

  178.   long varW_Temperature = t;//Add Sensor Data Collection
  179.   myGizwits.write(VALUE_Temperature, varW_Temperature);
  180.   unsigned long varW_Humidity = h;//Add Sensor Data Collection
  181.   myGizwits.write(VALUE_Humidity, varW_Humidity);
  182.   unsigned long varW_SMOKE = analogRead(A20);//Add Sensor Data Collection
  183.   myGizwits.write(VALUE_SMOKE, varW_SMOKE);

  184.   bool varR_FAN_OnOff = 0;
  185.   if(myGizwits.hasBeenSet(EVENT_FAN_OnOff))
  186.   {
  187.     myGizwits.read(EVENT_FAN_OnOff,&varR_FAN_OnOff);//Address for storing data

  188.   }
  189.   
  190.   KEY_Handle();//key handle , network configure
  191.   wifiStatusHandle();//WIFI Status Handle
  192.   myGizwits.process();
  193. }
复制代码
下载到arduino,然后长按KEY2绿灯亮,进入配网模式,一下子就成功了:app主页可以看到设备

数据展示:


视频演示:
https://www.ixigua.com/7027801591021306376

手机端录屏:
https://www.ixigua.com/7027801897272623135

原理图:
gokit3功能板原理图V3.0.PDF (789.4 KB, 下载次数: 1)
GoKit3(S)-ESP8266转接板原理图.PDF (201.56 KB, 下载次数: 2)
代码:
simpleTry.rar (27.25 KB, 下载次数: 0)

总结:机智云平台开发需要联网控制的智能硬件产品十分方便,开发十分迅速,官网文档中心、论坛资料丰富,即使没有接触过机智云的人也能快速上手。

134

主题

404

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11780
沙发
发表于 2021-11-8 10:34:27 | 只看该作者
谢谢分享呀,好机友
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表