登录机智云开发者中心后选择Soc 方案
需要下载AiThinkerIDE_V1.5.2 开发环境。
1、修改代码SoC_ESP8266_32M_source\app\driver 添加传感器驱动。
1)、水淹传感器-使用三极管加电阻
#include "driver/water.h"
#include "osapi.h"
void WaterInit(void){
/* Migrate your driver code */
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12);//选择IO功能
WATER_IO_IN;//端口输入
}
uint8_t WaterRead(void){
return WATER_IN;
}
2)、人体红外传感器 HC_SR505
#include "driver/HC_SR505.h"
#include "osapi.h"
void HC_SR505Init(void){
/* Migrate your driver code */
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);//选择IO功能
HC_SR505_IO_IN;//端口输入
//PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO4_U);
}
uint8_t HC_SR505Read(void){
return HC_SR505_IN;
}
3)、温湿度传感器DHT22
#include "driver/hal_temp_hum.h"
#include "osapi.h"
th_typedef_t temphum_typedef;
static void ICACHE_FLASH_ATTR tempHumDelay(unsigned int us)
{
/* Define your delay function */
os_delay_us(us);
}
//Reset DHT11
static void ICACHE_FLASH_ATTR dht11Rst(void)
{
DHT11_IO_OUT; //SET OUTPUT
DHT11_OUT_LOW; //GPIOA.0=0
tempHumDelay(18*1000); //Pull down Least 18ms
DHT11_OUT_HIGH; //GPIOA.0=1
tempHumDelay(40);
DHT11_IO_IN; //SET INPUT
}
static uint8_t ICACHE_FLASH_ATTR dht11Check(void)
{
uint8_t retry=0;
if(DHT11_IN == 0){os_printf("@@@dh11Init-dht11Check 43Line : %d\r\n", 0);
while ((!DHT11_IN)&&retry<=85) //DHT11 Pull down 40~80us
{
retry++;
tempHumDelay(1);
}
if(retry>85){
os_printf("@@@dh11Init-dht11Check 52Line : %d\r\n", retry);
return 1;
}
else
retry=0;
while (DHT11_IN&&retry<=85) //DHT11 Pull up 40~80us
{
retry++;
tempHumDelay(1);
}
if(retry>85){
os_printf("@@@dh11Init-dht11Check 65Line : %d\r\n", retry);
return 1; //chack error
}
else
return 0;
}os_printf("@@@dh11Init-dht11Check 69Line : %d\r\n", 1);
return 1;
}
static uint8_t ICACHE_FLASH_ATTR dht11ReadBit(void)
{
u8 dat;
uint8_t retry=0;
while((!DHT11_IN) && retry<100) //等待变为高电平
{
retry++;
tempHumDelay(1);
}
retry = 0;
dat = 0;
tempHumDelay(30);
if(DHT11_IN == 1){
dat = 1;
}
while(DHT11_IN&&retry<100) //wait become Low level
{
retry++;
tempHumDelay(1);
}
return dat;
}
static uint8_t ICACHE_FLASH_ATTR hdt11ReadByte(void)
{
uint8_t i;
uint8_t dat=0;
for (i=0; i<8; i++)
{
//dat<<=1;
//dat |= dht11ReadBit();
u8 retry = 0;
while(DHT11_IN == 0 && retry < 100)//等待低电平结束
{
retry++;
tempHumDelay(1);
}
tempHumDelay(40);
if(DHT11_IN == 1)//
{
retry = 0;
/*等待数据1的高电平结束*/
while(DHT11_IN == 1 && retry < 100){
retry++;
tempHumDelay(1);
}
dat |= (uint8_t)(0x01 << (7-i));//把第7-i位置1
}else{
dat &= (uint8_t)~(0x01 << (7-i));//把第7-i位置0
}
}
return dat;
}
uint8_t ICACHE_FLASH_ATTR dht11ReadData(u8 * temperature, u8 * humidity)
{
uint8_t i;
uint8_t buf[5];
dht11Rst();
if(0 == dht11Check())
{
for(i=0; i<5; i++)
{
buf[i] = hdt11ReadByte();
}
DHT11_IO_OUT;
DHT11_OUT_HIGH;
u8 sum = buf[0]+buf[1]+buf[2]+buf[3];
if(sum==buf[4])
{
*humidity=(buf[0]* 256 + buf[1])/10;
*temperature=(buf[2] * 256 + buf[3]) / 10;
os_printf("@@@dh11Init-ok buf[0-2]: %d-%d-%d-%d-%d\r\n", buf[0], buf[1], buf[2], buf[3],buf[4]);
}else{
os_printf("@@@dh11Init error buf[0-4]: %d-%d-%d-%d-%d\r\n", buf[0], buf[1], buf[2], buf[3],buf[4]);
}
}
else
{
os_printf("@@@dh11Init-checkerror 133Line : %d\r\n", 1);
return 1;
}
return 0;
}
uint8_t ICACHE_FLASH_ATTR dh11Init(void)
{
/* Migrate your driver code */
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
os_memset((uint8_t *)&temphum_typedef, 0, sizeof(th_typedef_t));
os_printf("dh11Init \r\n");
dht11Rst();
return dht11Check();
}
void ICACHE_FLASH_ATTR dh11SensorTest(void)
{
/* Test LOG model */
uint8_t curTem = 0;
uint8_t curHum = 0;
dht11ReadData(&curTem, &curHum);
os_printf("Temperature : %d , Humidity : %d", curTem, curHum);
}
2、修改SoC_ESP8266_32M_source\app\Gizwits\gizwits_product.c
此函数用来定时采集传感器数据。
/**
* User data acquisition
* Here users need to achieve in addition to data points other than the collection of data collection, can be self-defined acquisition frequency and design data filtering algorithm
* @param none
* @return none
*/
void ICACHE_FLASH_ATTR userHandle(void)
{
/*
currentDataPoint.valuesmoke = ;//Add Sensor Data Collection
currentDataPoint.valuewater = ;//Add Sensor Data Collection
currentDataPoint.valueelect = ;//Add Sensor Data Collection
currentDataPoint.valuehuman = ;//Add Sensor Data Collection
currentDataPoint.valuetemper = ;//Add Sensor Data Collection
currentDataPoint.valuehumidity = ;//Add Sensor Data Collection
*/
dhttime++;
if(DHT_TIMEOUT < dhttime)
{
dhttime = 0;
ret = dht11ReadData(&temperature, &humidity);
if(0 == ret){
currentDataPoint.valuetemper = temperature;
currentDataPoint.valuehumidity = humidity;
//currentDataPoint.valuesmoke = !currentDataPoint.valuesmoke;
}else{
GIZWITS_LOG("@@@ dh11Read error! \n");
}
GIZWITS_LOG("@@@ dh22ReadData ret= %d \n", ret);
GIZWITS_LOG("@@@@ dht22 valuetemper= %d\n", currentDataPoint.valuetemper);
GIZWITS_LOG("@@@@ dht22 valuehumidity= %d\n", currentDataPoint.valuehumidity);
currentSmokeValue = system_adc_read();
currentDataPoint.valuesmoke = currentSmokeValue > 400;
GIZWITS_LOG("@@@@ mq2 = %d\n", currentSmokeValue);
currentDataPoint.valuehuman = HC_SR505Read();
GIZWITS_LOG("@@@@ human = %d\n", currentDataPoint.valuehuman);
currentDataPoint.valuewater = WaterRead();
GIZWITS_LOG("@@@@ water = %d\n", currentDataPoint.valuewater);
}
system_os_post(USER_TASK_PRIO_2, SIG_UPGRADE_DATA, 0);
}
3、修改SoC_ESP8266_32M_source\app\user\user_main.c
此函数初始化设备驱动、串口等参数。
/**
* @brief program entry function
* In the function to complete the user-related initialization
* @param none
* @return none
*/
void ICACHE_FLASH_ATTR user_init(void)
{
uint32_t system_free_size = 0;
wifi_station_set_auto_connect(1);
wifi_set_sleep_type(NONE_SLEEP_T);//set none sleep mode
espconn_tcp_set_max_con(10);
uart_init_3(9600,115200);
UART_SetPrintPort(1);
GIZWITS_LOG( "---------------SDK version:%s--------------\n", system_get_sdk_version());
GIZWITS_LOG( "system_get_free_heap_size=%d\n",system_get_free_heap_size());
struct rst_info *rtc_info = system_get_rst_info();
GIZWITS_LOG( "reset reason: %x\n", rtc_info->reason);
if (rtc_info->reason == REASON_WDT_RST ||
rtc_info->reason == REASON_EXCEPTION_RST ||
rtc_info->reason == REASON_SOFT_WDT_RST)
{
if (rtc_info->reason == REASON_EXCEPTION_RST)
{
GIZWITS_LOG("Fatal exception (%d):\n", rtc_info->exccause);
}
GIZWITS_LOG( "epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n",
rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc);
}
if (system_upgrade_userbin_check() == UPGRADE_FW_BIN1)
{
GIZWITS_LOG( "---UPGRADE_FW_BIN1---\n");
}
else if (system_upgrade_userbin_check() == UPGRADE_FW_BIN2)
{
GIZWITS_LOG( "---UPGRADE_FW_BIN2---\n");
}
keyInit();
gizwitsInit();
HC_SR505Init();
uint8_t ret = dh11Init();
WaterInit();
GIZWITS_LOG("--- system_free_size = %d --- dht11Init= %d \n", system_get_free_heap_size(), ret);
//uart0_sendStr("\r\nHello world, JJX\n\r");
}