【基于机智云开发平台+Gokit】+ DIY一个环境监测仪
很幸运获得了一块创客版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提供的库即可:
测试代码如下:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include "DHT.h"
#define DHTPIN 3 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22(AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("%Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°FHeat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}效果演示:
三、驱动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
测试代码如下:
<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读取。
最终代码如下:
/**
****************************************
* @file Gizwits + ArduinoUnoR3 Library
* @brief Datapoints handle , Gizwits Process
* @author Gizwits
* @date 2017-08-01
* @version V03030000
* @copyright Gizwits
*
* @note 机智云.只为智能硬件而生
* Gizwits Smart Cloudfor Smart Products
* 链接|增值ֵ|开放|中立|安全|自有|自由|生态
* www.gizwits.com
*
****************************************/
#include "Gizwits.h"
#include <Wire.h>
#include <ChainableLED.h>
#define NUM_LEDS1
ChainableLED leds(A5, A4, NUM_LEDS);
#include "DHT.h"
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
Gizwits myGizwits;
#define KEY1 6
#define KEY2 7
#define KEY1_SHORT_PRESS1
#define KEY1_LONG_PRESS 2
#define KEY2_SHORT_PRESS4
#define KEY2_LONG_PRESS 8
#define NO_KEY 0
#define KEY_LONG_TIMER 3
unsigned long Last_KeyTime = 0;
unsigned long gokit_time_s(void)
{
return millis() / 1000;
}
char gokit_key1down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY1) == LOW)
{
delay(100);
if (digitalRead(KEY1) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY1) == LOW)
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY1_LONG_PRESS;
}
} //until open the key
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY1_SHORT_PRESS;
}
return 0;
}
return 0;
}
return 0;
}
char gokit_key2down(void)
{
unsigned long keep_time = 0;
if (digitalRead(KEY2) == LOW)
{
delay(100);
if (digitalRead(KEY2) == LOW)
{
keep_time = gokit_time_s();
while (digitalRead(KEY2) == LOW) //until open the key
{
if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)
{
Last_KeyTime = gokit_time_s();
return KEY2_LONG_PRESS;
}
}
if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)
{
return KEY2_SHORT_PRESS;
}
return 0;
}
return 0;
}
return 0;
}
char gokit_keydown(void)
{
char ret = 0;
ret |= gokit_key2down();
ret |= gokit_key1down();
return ret;
}
void KEY_Handle(void)
{
/*Press for over than 3 second is Long Press*/
switch (gokit_keydown())
{
case KEY1_SHORT_PRESS:
myGizwits.setBindMode(WIFI_PRODUCTION_TEST);
break;
case KEY1_LONG_PRESS:
myGizwits.setBindMode(WIFI_RESET_MODE);
break;
case KEY2_SHORT_PRESS:
myGizwits.setBindMode(WIFI_SOFTAP_MODE);
//Soft AP mode
break;
case KEY2_LONG_PRESS:
leds.setColorRGB(0,0,255,0);
myGizwits.setBindMode(WIFI_AIRLINK_MODE);
//AirLink mode
break;
default:
break;
}
}
void setup() {
leds.init();
pinMode(KEY1, INPUT_PULLUP);
pinMode(KEY2, INPUT_PULLUP);
dht.begin();
myGizwits.begin();
}
void wifiStatusHandle()
{
if(myGizwits.wifiHasBeenSet(WIFI_SOFTAP))
{
}
if(myGizwits.wifiHasBeenSet(WIFI_AIRLINK))
{
}
if(myGizwits.wifiHasBeenSet(WIFI_STATION))
{
}
if(myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))
{
leds.setColorRGB(0,255,0,0);//连接路由器亮红色
}
if(myGizwits.wifiHasBeenSet(WIFI_DISCON_ROUTER))
{
leds.setColorRGB(0,0,0,0);//断开路由器灯灭
}
if(myGizwits.wifiHasBeenSet(WIFI_CON_M2M))
{
leds.setColorRGB(0,0,0,255);//连接云平台亮蓝色
}
if(myGizwits.wifiHasBeenSet(WIFI_DISCON_M2M))
{
leds.setColorRGB(0,0,0,0);//断开云平台灯灭
}
}
unsigned long previousMillis = 0;
void loop() {
float h,t;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 2000) {
previousMillis = currentMillis;
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
return;
}
}
long varW_Temperature = t;//Add Sensor Data Collection
myGizwits.write(VALUE_Temperature, varW_Temperature);
unsigned long varW_Humidity = h;//Add Sensor Data Collection
myGizwits.write(VALUE_Humidity, varW_Humidity);
unsigned long varW_SMOKE = analogRead(A20);//Add Sensor Data Collection
myGizwits.write(VALUE_SMOKE, varW_SMOKE);
bool varR_FAN_OnOff = 0;
if(myGizwits.hasBeenSet(EVENT_FAN_OnOff))
{
myGizwits.read(EVENT_FAN_OnOff,&varR_FAN_OnOff);//Address for storing data
}
KEY_Handle();//key handle , network configure
wifiStatusHandle();//WIFI Status Handle
myGizwits.process();
}下载到arduino,然后长按KEY2绿灯亮,进入配网模式,一下子就成功了:app主页可以看到设备
数据展示:
视频演示:
https://www.ixigua.com/7027801591021306376
手机端录屏:
https://www.ixigua.com/7027801897272623135
原理图:
代码:
总结:机智云平台开发需要联网控制的智能硬件产品十分方便,开发十分迅速,官网文档中心、论坛资料丰富,即使没有接触过机智云的人也能快速上手。
谢谢分享呀,好机友;P
页:
[1]