收藏官网首页
查看: 8130|回复: 3

SOC方案实现硬件定时插座

4

主题

13

帖子

194

积分

注册会员

Rank: 2

积分
194
跳转到指定楼层
楼主
发表于 2018-7-26 20:09:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
注册成为机智云开发者,手机加虚拟设备快速开发
本帖最后由 az666 于 2018-7-26 20:38 编辑

嵌入式制作智能定时设备。

参考教程:http://club.gizwits.com/forum.ph ... =%E5%AE%9A%E6%97%B6

可实现自动检测定时开机或者关机
意思就是,如果原来是关闭的定好时间就会定时开机,反之亦然。
小时和分钟进行定时
经过我的测试小时以上也是可以稳定运行的。分钟定时也很准确

最简单的嵌入式开发即利用机智云的SOC方案,利用安信可的编译器进行bin固件的编写。最后在烧写进esp8266中,实现最简单的智能远程控制设备,这次的另一个重要亮点在于实现的硬件计时,也就是实现了硬件端的定时触发,这次的效果是定时启动设备。








依旧是最经典的Makefile修改为esp编译模式

  1. BOOT?=new
  2. APP?=1
  3. SPI_SPEED?=40
  4. SPI_MODE?=QIO
  5. SPI_SIZE_MAP?=6
复制代码
user_main.c的文件简单修改即可,加入要控制的按键初始化。
  1. LOCAL void ICACHE_FLASH_ATTR keyInit(void)
  2. {
  3.     singleKey[0] = keyInitOne(KEY_0_IO_NUM, KEY_0_IO_MUX, KEY_0_IO_FUNC,
  4.                                 key1LongPress, key1ShortPress);
  5.     singleKey[1] = keyInitOne(KEY_1_IO_NUM, KEY_1_IO_MUX, KEY_1_IO_FUNC,
  6.                                 key2LongPress, key2ShortPress);
  7.     keys.singleKey = singleKey;
  8.     keyParaInit(&keys);
  9.     PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); //GPIO12初始化
  10.     GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);//GPIO12 低电平输出
  11. }
复制代码
下面是gizwits_product.c的代码。里面有很详细的代码。
  1. <div class="blockcode"><blockquote>#include <stdio.h>
  2. #include <string.h>
  3. #include "gizwits_product.h"
  4. #include "driver/hal_key.h"

  5. /** User area The current device state structure */
  6. dataPoint_t currentDataPoint;
  7. bool isTimer;
  8. long timer_timers;
  9. long time_mills;//定义总秒数
  10. static os_timer_t os_timer;
  11. /**
  12. * 定时任务函数
  13. */
  14. void Led_Task_Run(void){
  15. //开灯
  16. GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);
  17. //执行完毕,我们要把定时时间设置0 ,定时使能状态为false
  18. timer_timers=0;         //根据继电器的种类和要定时的任务而定。这是低电平触发继电器的定时开机功能。
  19. isTimer=false;
  20. }
  21. void Led_Task_Off(void){
  22. //开灯
  23. GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);
  24. //执行完毕,我们要把定时时间设置0 ,定时使能状态为false
  25. timer_timers=0;         //根据继电器的种类和要定时的任务而定。这是低电平触发继电器的定时关机功能。
  26. isTimer=false;
  27. }
  28. int8_t ICACHE_FLASH_ATTR gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len)
  29. {
  30.     uint8_t i = 0;
  31.     dataPoint_t * dataPointPtr = (dataPoint_t *)data;
  32.     moduleStatusInfo_t * wifiData = (moduleStatusInfo_t *)data;

  33.     if((NULL == info) || (NULL == data))
  34.     {
  35.         GIZWITS_LOG("!!! gizwitsEventProcess Error \n");
  36.         return -1;
  37.     }

  38.     for(i = 0; i < info->num; i++)
  39.     {
  40.         switch(info->event[i])
  41.         {
  42.         case EVENT_on_off :
  43.             currentDataPoint.valueon_off = dataPointPtr->valueon_off;
  44.             GIZWITS_LOG("Evt: EVENT_on_off %d \n", currentDataPoint.valueon_off);
  45.             if(0x01 == currentDataPoint.valueon_off)
  46.             {
  47.                     GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0); //开灯
  48.             }
  49.             else
  50.             {
  51.                     GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1); //关灯
  52.             }
  53.             break;
  54.         case EVENT_T_on_off :
  55.             currentDataPoint.valueT_on_off = dataPointPtr->valueT_on_off;
  56.             if(0x01 == currentDataPoint.valueT_on_off)
  57.             {
  58.                     isTimer=true;//开启定时器
  59.             }
  60.             else
  61.             {
  62.                      /** 关闭该定时器 */
  63.                                 os_timer_disarm( &os_timer );
  64.                      /** 定时器使能为false */
  65.                                 isTimer=false;
  66.             }
  67.             break;
  68.         case EVENT_time_h:
  69.             currentDataPoint.valuetime_h= dataPointPtr->valuetime_h;
  70.             GIZWITS_LOG("Evt:EVENT_time_h %d\n",currentDataPoint.valuetime_h);
  71.             //user handle
  72.             break;
  73.         case EVENT_time_m:
  74.             currentDataPoint.valuetime_m= dataPointPtr->valuetime_m;
  75.             GIZWITS_LOG("Evt:EVENT_time_m %d\n",currentDataPoint.valuetime_m);
  76.             if(isTimer){
  77.                     if (currentDataPoint.valueon_off){  //判断继电器状态,如果原来是关闭状态,就定时开机,如果原来是开启状态,就定时关闭。
  78.                              /** 关闭该定时器 */
  79.                             os_timer_disarm( &os_timer );
  80.                             // 配置该定时器回调函数,指定的执行方法是: Led_Task_Run (),下面会提供代码
  81.                             os_timer_setfn( &os_timer, (ETSTimerFunc *) ( Led_Task_Off ), NULL );
  82.                             time_mills = (currentDataPoint.valuetime_h *60 + currentDataPoint.valuetime_m)*60000;
  83.                             /** 开启该定时器 :下发的是秒数,这里的单位是毫秒,要乘1000* ,后面false表示仅仅执行一次**/
  84.                             //os_timer_arm( &os_timer, currentDataPoint.valuetime_m*1000, false );
  85.                             os_timer_arm( &os_timer, time_mills, false );
  86.                             /**赋值给timer_timers,方便会调用 */
  87.                             timer_timers=currentDataPoint.valuetime_m;
  88.                     }
  89.                     else
  90.                     {
  91.                             /** 关闭该定时器 */
  92.                             os_timer_disarm( &os_timer );
  93.                         // 配置该定时器回调函数,指定的执行方法是: Led_Task_Run (),下面会提供代码
  94.                             os_timer_setfn( &os_timer, (ETSTimerFunc *) ( Led_Task_Run ), NULL );
  95.                             time_mills = (currentDataPoint.valuetime_h *60 + currentDataPoint.valuetime_m)*60000;
  96.                             /** 开启该定时器 :下发的是秒数,这里的单位是毫秒,要乘1000* ,后面false表示仅仅执行一次**/
  97.                             //os_timer_arm( &os_timer, currentDataPoint.valuetime_m*1000, false );
  98.                         os_timer_arm( &os_timer, time_mills, false );
  99.                             /**赋值给timer_timers,方便会调用 */
  100.                             timer_timers=currentDataPoint.valuetime_m;
  101.                     }
  102.                 }
  103.             break;

  104.         case WIFI_SOFTAP:
  105.             break;
  106.         case WIFI_AIRLINK:
  107.             break;
  108.         case WIFI_STATION:
  109.             break;
  110.         case WIFI_CON_ROUTER:
  111.             GIZWITS_LOG("@@@@ connected router\n");

  112.             break;
  113.         case WIFI_DISCON_ROUTER:
  114.             GIZWITS_LOG("@@@@ disconnected router\n");

  115.             break;
  116.         case WIFI_CON_M2M:
  117.             GIZWITS_LOG("@@@@ connected m2m\n");
  118.                         setConnectM2MStatus(0x01);

  119.             break;
  120.         case WIFI_DISCON_M2M:
  121.             GIZWITS_LOG("@@@@ disconnected m2m\n");
  122.                         setConnectM2MStatus(0x00);

  123.             break;
  124.         case WIFI_RSSI:
  125.             GIZWITS_LOG("@@@@ RSSI %d\n", wifiData->rssi);
  126.             break;
  127.         case TRANSPARENT_DATA:
  128.             GIZWITS_LOG("TRANSPARENT_DATA \n");
  129.             //user handle , Fetch data from [data] , size is [len]
  130.             break;
  131.         case MODULE_INFO:
  132.             GIZWITS_LOG("MODULE INFO ...\n");
  133.             break;
  134.             
  135.         default:
  136.             break;
  137.         }
  138.     }
  139.     system_os_post(USER_TASK_PRIO_2, SIG_UPGRADE_DATA, 0);
  140.    
  141.     return 0;
  142. }


  143. /**
  144. * User data acquisition

  145. * 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

  146. * @param none
  147. * @return none
  148. */
  149. void ICACHE_FLASH_ATTR userHandle(void)
  150. {

  151.     currentDataPoint.valueback = time_mills ;

  152.         currentDataPoint.valueon_off = !GPIO_INPUT_GET(12) ;
  153.         //是否开启定时器的回调
  154.     currentDataPoint.valueT_on_off =isTimer;
  155.         if(isTimer){
  156.          currentDataPoint.valuetime_m =timer_timers ;
  157.          }
  158.         else
  159.         {
  160.                 /*数据清零*/
  161.           currentDataPoint.valuetime_m =0;
  162.           currentDataPoint.valuetime_h =0;
  163.           currentDataPoint.valueback = 0;
  164.          }
  165.     system_os_post(USER_TASK_PRIO_2, SIG_UPGRADE_DATA, 0);
  166. }


  167. /**
  168. * Data point initialization function

  169. * In the function to complete the initial user-related data
  170. * @param none
  171. * @return none
  172. * @note The developer can add a data point state initialization value within this function
  173. */
  174. void ICACHE_FLASH_ATTR userInit(void)
  175. {
  176.     gizMemset((uint8_t *)¤tDataPoint, 0, sizeof(dataPoint_t));

  177.         /** Warning !!! DataPoint Variables Init , Must Within The Data Range
  178.          * 警告!!!!必须在数据范围内的数据池变量init**/
  179. /*
  180.                    currentDataPoint.valueon_off = 0;
  181.                    currentDataPoint.valueT_on_off = 0;
  182.                    currentDataPoint.valuetime_h = 0;
  183.                    currentDataPoint.valuetime_m = 0;
  184.                    currentDataPoint.valueback = 0;
  185. */

  186. }
复制代码



整体文件下载地址。七牛云链接:http://pbhutn8vv.bkt.clouddn.com/soc_dingshi.zip


2

主题

16

帖子

430

积分

中级会员

Rank: 3Rank: 3

积分
430
沙发
发表于 2020-1-20 21:18:42 | 只看该作者
必须顶一个真的太好了

点评

利害了,膜拜。  详情 回复 发表于 2022-6-4 20:07

0

主题

7

帖子

130

积分

注册会员

Rank: 2

积分
130
板凳
发表于 2022-6-4 20:07:30 | 只看该作者
只能小菜鸟 发表于 2020-1-20 21:18
必须顶一个真的太好了

利害了,膜拜。

0

主题

5

帖子

34

积分

新手上路

Rank: 1

积分
34
地板
发表于 2022-7-11 19:43:28 | 只看该作者
倒计时不好用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

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

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