PROTOCOL.h #ifndef _PROTOCOL_H #define _PROTOCOL_H #include <stdio.h> #include <stdbool.h> #include <stm32f10x.h> #define SoftAp_Mode 0x01 #define AirLink_Mode 0x02 #define MAX_P0_LEN 128 //p0数据最大长度 #define MAX_PACKAGE_LEN (MAX_P0_LEN*2) //数据缓冲区最大长度 #define MAX_RINGBUFFER_LEN MAX_PACKAGE_LEN //环形缓冲区最大长度 #define Max_UartBuf MAX_PACKAGE_LEN //串口数据缓冲区最大长度 #define PROTOCOL_DEBUG //调试通信数据信息宏开关 #define DEBUG //系统运行调试信息 #define Send_MaxTime 300 #define Send_MaxNum 3 #define PRO_VER "00000004" #define P0_VER "00000004" #define HARD_VER "02030100" #define SOFT_VER "02030003" #define PRODUCT_KEY "8a2d232e861e4de1843b7b827debe57d"//宠物屋PRODUCT_KEY717c14293174447eb27c90e084e46344 __packed typedef struct { uint8_t Message_Buf[Max_UartBuf]; //串口接收缓冲区 uint8_t Message_Len; //接受到的数据长度 } UART_HandleTypeDef; /*命令码*/ typedef enum { Pro_W2D_GetDeviceInfo_Cmd = 0x01, Pro_D2W__GetDeviceInfo_Ack_Cmd = 0x02, Pro_W2D_P0_Cmd = 0x03, Pro_D2W_P0_Ack_Cmd = 0x04, Pro_D2W_P0_Cmd = 0x05, Pro_W2D_P0_Ack_Cmd = 0x06, Pro_W2D_Heartbeat_Cmd = 0x07, Pro_D2W_heartbeatAck_Cmd = 0x08, Pro_D2W_ControlWifi_Config_Cmd = 0x09, Pro_W2D_ControlWifi_Config_Ack_Cmd = 0x0A, Pro_D2W_ResetWifi_Cmd = 0x0B, Pro_W2D_ResetWifi_Ack_Cmd = 0x0C, Pro_W2D_ReportWifiStatus_Cmd = 0x0D, Pro_D2W_ReportWifiStatus_Ack_Cmd = 0x0E, Pro_W2D_ReportMCUReset_Cmd = 0x0F, Pro_D2W_ReportMCUReset_Ack_Cmd = 0x10, Pro_W2D_ErrorPackage_Cmd = 0x11, Pro_D2W_ErrorPackage_Ack_Cmd = 0x12, /*7.28*/ Pro_Get_Network_Time_Cmd =0x17,//获取网络时间 Pro_Get_Network_Time_Ack_Cmd =0x18,//回复获取网络时间 } Pro_CmdTypeDef; typedef enum { SetLED_OnOff = 0x01, SetLED_Color = 0x02, SetLED_R = 0x04, SetLED_G = 0x08, SetLED_B = 0x10, SetMotor = 0x20, } Attr_FlagsTypeDef; typedef enum { LED_OnOff = 0x00, LED_OnOn = 0x01, Time_OnOff = 0x00, Time_OnOn = 0x02, Week_OnOff = 0x00, Week_OnOn = 0x04, } LED_ColorTypeDef; /************************************ * 重发机制结构体 **************************************/ __packed typedef struct { uint32_t SendTime;//重发的系统时间 uint8_t SendNum;//重发次数 uint8_t Flag;//1,表示有需要等待的ACK;0,表示无需要等待的ACK uint16_t ResendBufLen; uint8_t Cmd_Buff[Max_UartBuf];//重发数据缓冲区 } Pro_Wait_AckTypeDef; /************************************ * 协议标准头 **************************************/ __packed typedef struct { uint8_t Head[2]; uint16_t Len; uint8_t Cmd; uint8_t SN; uint8_t Flags[2]; } Pro_HeadPartTypeDef; /************************************ * 4.1 WiFi模组请求设备信息 **************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint8_t Pro_ver[8]; uint8_t P0_ver[8]; uint8_t Hard_ver[8]; uint8_t Soft_ver[8]; uint8_t Product_Key[32]; uint16_t Binable_Time; uint8_t Sum; } Pro_M2W_ReturnInfoTypeDef; /************************************ * 协议通用数据帧(4.2、4.4、4.6、4.9、4.10) ************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint8_t Sum; } Pro_CommonCmdTypeDef; /************************************ * 4.3 设备MCU通知WiFi模组进入配置模式 **************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint8_t Config_Method; uint8_t Sum; } Pro_D2W_ConfigWifiTypeDef; /************************************ * WiFi模组工作状态 ************************************/ #define Wifi_SoftAPMode (uint8_t)(1<<0) //SoftAP #define Wifi_StationMode (uint8_t)(1<<1) //Station #define Wifi_ConfigMode (uint8_t)(1<<2) // #define Wifi_BindingMode (uint8_t)(1<<3) // #define Wifi_ConnRouter (uint8_t)(1<<4) // #define Wifi_ConnClouds (uint8_t)(1<<5) //Connect OK /************************************ * 4.5 WiFi模组向MCU通知WiFi模组工作状态的变化 ************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint16_t Wifi_Status; uint8_t Sum; } Pro_W2D_WifiStatusTypeDef; /************************************ * 4.13获取网络时间 ************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint16_t year; uint8_t month; uint8_t day; uint8_t hour; uint8_t minute; uint8_t second; uint32_t other; uint8_t Sum; } Pro_W2D_GetTimeTypeDef; /************************************ * 非法报文类型 ************************************/ typedef enum { Error_AckSum = 0x01, //校验错误 Error_Cmd = 0x02, //命令码错误 Error_Other = 0x03, //其他 } Error_PacketsTypeDef; /************************************ * 4.7 非法消息通知 ************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; Error_PacketsTypeDef Error_Packets; uint8_t Sum; } Pro_ErrorCmdTypeDef; /************************************ * P0 command 命令码 ************************************/ typedef enum { P0_W2D_Control_Devce_Action = 0x01, P0_W2D_ReadDevStatus_Action = 0x02, P0_D2W_ReadDevStatus_Action_ACK = 0x03, P0_D2W_ReportDevStatus_Action = 0X04, } P0_ActionTypeDef; /************************************ * P0报文标准头 **************************************/ __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; P0_ActionTypeDef Action; } Pro_HeadPartP0CmdTypeDef; short exchangeBytes(short value); uint8_t CheckSum( uint8_t *buf, int packLen ); void GizWits_init(uint8_t P0_Len); void GizWits_D2WResetCmd(void); void GizWits_D2WGetCmd(void);//获取网络时间 void GizWits_D2WConfigCmd(uint8_t WiFi_Mode); void GizWits_DevStatusUpgrade(uint8_t *P0_Buff, uint32_t Time, uint8_t flag); void GizWits_WiFiStatueHandle(uint16_t wifiStatue); u8 GizWits_MessageHandle(u8 * Message_Buf, u8 Length_buf); #endif /*_PROTOCOL_H*/ |
/***************************************************** * Function Name : GizWits_ControlDeviceHandle * Description : Analy P0 Package根据数据有效位标志,进行控制 4.10 WiFi模组控制MCU WiFi发: Header(2B)__LEN(2B)__CMD(1B)__sn(1B)__falg(2B)__ActionBit(1B)__attrFlags(1B)__attrValue(6B)__CheckSum(1B) 0xFFFF 0x000D 0x03 0x## 0x0000 0x01 标志位 控制值 0x## MCU 回通用消息帧: Header(2B)__LEN(2B)__CMD(1B)__sn(1B)__falg(2B)__CheckSum(1B) 0xFFFF 0x0005 0x04 0x## 0x0000 0x## Bit0~5ED_ONOFF, LED_Color, R,G,B, Motor_speed WirteTypeDef_t{ uint8_t Attr_Flags; //信息位 是否有效的标志 uint8_t LED_Cmd; uint8_t LED_R; uint8_t LED_G; uint8_t LED_B; MOTOR_T Motor; } 依靠接收的数据帧,存储到WirteTypeDef,再更改设备状态 *****************************************************/ void GizWits_ControlDeviceHandle() //WiFi模组控制MCU { //标志位 0位开关 共1个字节 if( (WirteTypeDef.Attr_Flags & (1<<0)) == (1<<0))//低位第0位ONOFF 是否设置标志位7位 { if((WirteTypeDef.LED_Cmd &(1<<0))== 0) { LED_RGB_Control(0,0,0); LED_OFF(LED1); ReadTypeDef.LED_Cmd &= ~(1<<0);//存储状态LED_OnOff a&=~(1<<i);//将a的bit i清0 printf("LED OFF \r\n"); } if((WirteTypeDef.LED_Cmd &(1<<0))== (1<<0)) { ReadTypeDef.LED_Cmd |= (1<<0);//LED_OnOn a|=1<<i;//将a的bit i置1 LED_ON(LED1); LED_RGB_Control(254,0,0);//默认打开时,是红色 printf("LED ON \r\n"); } } //标志位是否启用定时器 if( (WirteTypeDef.Attr_Flags & (1<<1)) == (1<<1))//低位第1位Time_OnOff { if((WirteTypeDef.LED_Cmd & (1<<1))== 0) { //LED_RGB_Control(0,0,0); LED_OFF(LED2); ReadTypeDef.LED_Cmd &= ~(1<<1);//存储状态LED_OnOff a&=~(1<<i);//将a的bit i清0 printf("Time_OnOff OFF \r\n"); } if((WirteTypeDef.LED_Cmd & (1<<1)) == (1<<1)) { ReadTypeDef.LED_Cmd |= (1<<1);//LED_OnOn a|=1<<i;//将a的bit i置1 LED_ON(LED2); //LED_RGB_Control(254,254,0);// printf("Time_OnOff ON \r\n"); } } //标志位是否启用倒计时 if( (WirteTypeDef.Attr_Flags & (1<<2)) == (1<<2)) { if((WirteTypeDef.LED_Cmd & (1<<2))== 0) { //LED_RGB_Control(0,0,0); LED_OFF(LED3); ReadTypeDef.LED_Cmd &= ~(1<<2);//存储状态LED_OnOff a&=~(1<<i);//将a的bit i清0 printf("Count_Down_OnOff OFF \r\n"); TIM_Cmd(TIM4,DISABLE); //失能TIMx外设 time_min = 0; time_sec = 0; min_flag = 0; } if((WirteTypeDef.LED_Cmd & (1<<2)) == (1<<2)) { ReadTypeDef.LED_Cmd |= (1<<2);//LED_OnOn a|=1<<i;//将a的bit i置1 LED_ON(LED3); //LED_RGB_Control(254,254,254);// printf("Count_Down_OnOff ON \r\n"); CountDown_Minute_time=exchangeBytes(ReadTypeDef.CountDown_Minute);//重新给当前设置的值给倒计时 if(CountDown_Minute_time!=0) { TIM_Cmd(TIM4, ENABLE); //使能TIMx外设 } } } //标志位是否重复星期 if( (WirteTypeDef.Attr_Flags & (1<<3)) == (1<<3)) { ReadTypeDef.Week_Repeat=WirteTypeDef.Week_Repeat; GizWits_DevStatusUpgrade((uint8_t *)&ReadTypeDef, 10*60*1000, 1); } //标志位定时开机 注:时间以分钟为单位1小时=60分钟=0x3c 存储的是16进制 if( (WirteTypeDef.Attr_Flags & (1<<4)) == (1<<4)) { printf("Time_On_Minute ON = %d \r\n",exchangeBytes(WirteTypeDef.Time_On_Minute)); if(exchangeBytes(WirteTypeDef.Time_On_Minute)<1441) { ReadTypeDef.Time_On_Minute=WirteTypeDef.Time_On_Minute; CountDown_Time_On_Minute=exchangeBytes(ReadTypeDef.Time_On_Minute);//存储的是高低位改变过的 GizWits_DevStatusUpgrade((uint8_t *)&ReadTypeDef, 10*60*1000, 1); } } //标志位定时关机 if( (WirteTypeDef.Attr_Flags & (1<<5)) == (1<<5)) { printf("Time_On_Minute OFF = %d \r\n",exchangeBytes(WirteTypeDef.Time_Off_Minute)); if(exchangeBytes(WirteTypeDef.Time_Off_Minute)<1441) { ReadTypeDef.Time_Off_Minute=WirteTypeDef.Time_Off_Minute; CountDown_Time_Off_Minute=exchangeBytes(ReadTypeDef.Time_Off_Minute);//存储的是高低位改变过的 GizWits_DevStatusUpgrade((uint8_t *)&ReadTypeDef, 10*60*1000, 1); } } //标志位倒计时 if( (WirteTypeDef.Attr_Flags & (1<<6)) == (1<<6)) { printf("CountDown_Minute T = %d \r\n",exchangeBytes(WirteTypeDef.CountDown_Minute)); if(exchangeBytes(WirteTypeDef.CountDown_Minute)<1441) { ReadTypeDef.CountDown_Minute=WirteTypeDef.CountDown_Minute; GizWits_DevStatusUpgrade((uint8_t *)&ReadTypeDef, 10*60*1000, 1); CountDown_Minute_time=exchangeBytes(ReadTypeDef.CountDown_Minute);//当前设置的值给倒计时 if(((ReadTypeDef.LED_Cmd & (1<<2)) == (1<<2))&&(CountDown_Minute_time!=0)) { time_min = 0; time_sec = 0; min_flag = 0; TIM_Cmd(TIM4, ENABLE); //使能TIMx外设 } } } } /***************************************************** * Function Name : GizWits_GatherSensorData(); * Description : Gather Sensor Data * Input : None * Output : None * Return : None * Attention : None *****************************************************/ void GizWits_GatherSensorData(void) { // ReadTypeDef.Infrared = IR_Handle(); // DHT11_Read_Data(&curTem, &curHum); // ReadTypeDef.Temperature = (curTem + lastTem) /2; // ReadTypeDef.Humidity = (curHum + lastHum)/2; // ReadTypeDef.Temperature = ReadTypeDef.Temperature + 13;//Temperature Data Correction//根据协议,矫正数据 // lastTem = curTem; // lastHum = curHum; } /***************************************************** * Function Name : KEY_Handle * Description : Key processing function * KEY1 短按 : None * KEY1 长按 : None * KEY2 短按 : SoftAP方式配置WiFi模组 * KEY2 长按 : AirLink方式配置WiFi模组 * 4.13获取网络时间 __packed typedef struct { Pro_HeadPartTypeDef Pro_HeadPart; uint16_t year; uint8_t month; uint8_t day; uint8_t hour; uint8_t minute; uint8_t second; uint32_t other; uint8_t Sum; } Pro_W2D_GetTimeTypeDef; *****************************************************/ void KEY_Handle(void) { uint8_t Key_return =0; Key_return = ReadKeyValue(); if(Key_return & KEY_UP) { if(Key_return & PRESS_KEY1)//短按K1 { #ifdef PROTOCOL_DEBUG printf("KEY1 PRESS\r\n"); #endif GizWits_D2WGetCmd(); } if(Key_return & PRESS_KEY2)//短按K2 { #ifdef PROTOCOL_DEBUG printf("KEY2 PRESS ,Soft AP mode\r\n"); #endif //Soft AP mode, RGB red LED_RGB_Control(255, 0, 0); GizWits_D2WConfigCmd(SoftAp_Mode); NetConfigureFlag = 1; } } if(Key_return & KEY_LONG) { if(Key_return & PRESS_KEY1)//长按K1 { #ifdef PROTOCOL_DEBUG printf("KEY1 PRESS LONG ,Wifi Reset\r\n"); #endif GizWits_D2WResetCmd();//解绑WIFI } if(Key_return & PRESS_KEY2)//长按K2 { //AirLink mode, RGB Green #ifdef PROTOCOL_DEBUG printf("KEY2 PRESS LONG ,AirLink mode\r\n"); #endif LED_RGB_Control(0, 128, 0); GizWits_D2WConfigCmd(AirLink_Mode);//进入AirLink_Mode模式 NetConfigureFlag = 1; } } } /***************************************************** * Function Name : GizWits_WiFiStatueHandle * Description : Callback function , Judge Wifi statue * Input : None * Output : None * Return : Bit , Attr_Flags * Attention : *****************************************************/ void GizWits_WiFiStatueHandle(uint16_t wifiStatue) { if(((wifiStatue & Wifi_ConnClouds) == Wifi_ConnClouds) && (NetConfigureFlag == 1)) { printf("W2M->Wifi_ConnClouds\r\n"); NetConfigureFlag = 0; LED_RGB_Control(0,0,0); } } /************* (C) COPYRIGHT 2011 STMicroelectronics ****END OF FILE***/ |
jipin 发表于 2016-7-29 17:38 没有源码 |
有没有源码 |
jipin 发表于 2016-8-2 20:52 可以重发下网盘地址吗?失效了 |
感谢分享 |
网盘失效 烦请楼主补一个 |
目前我也在搞这一块,加油 |
APP源码在手机上运行,进入到控制开关那页面闪退 |
有画好插座板子的原理图么 |
jipin 发表于 2016-7-29 17:45 楼主,照着这个我硬件基本搞定了,但是软件部分,实在是不会搞,能教我一下吗?有偿 |
智能插座的源码百度链接挂掉了,能再补一个嘛 |
谢谢分享 |
@jipin 您好!看你的帖子涨知识。很用心!帖子里给的百度网盘链接失效了, 可不可以把请您将新版“智能插座”源码和相关资料分享给我学习学习。邮箱: scsi_1@163.com。多谢了! |
赞一个,楼主分享的提适合新手学习的,采纳了 |
jipin 发表于 2016-8-2 20:52 楼主可以在发一次智能插座与微信宠物屋的百度云的链接吗 |
强大,顶起来 |
楼主,你好!能给你新版“智能插座”源码和相关资料分享一分吗?我的邮箱hydyguo@126.com |
楼主你好,可以问一下,这个电源外面的壳子有工模吗,哪里可以做这个电源壳子 |
WJK 发表于 2017-4-1 19:47 楼主,同上 |
楼主再分享一下源代码 |
学习了。 |
版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )