1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| #include "ets_sys.h" #include "driver/uart.h" #include "osapi.h" #include "mqtt.h" #include "wifi.h" #include "pwm.h" #include "config.h" #include "debug.h" #include "gpio.h" #include "user_interface.h" #include "mem.h"
#define INTERVAL 500 #define INIT_DELAY 10000 #define HEART_BEAT 60 #define BEEP_DURATION 120
#define PWM_PERIOD 500 #define PWM_DUTY 100 #define PWM_IDLE 0
LOCAL uint64_t id = 0; LOCAL bool beeping = false; LOCAL bool beepTimes = 0; LOCAL bool remoteControl = false;
MQTT_Client mqttClient; os_timer_t sensor_timer; os_timer_t beep_timer;
uint32 io_info[][3] = {{PERIPHS_IO_MUX_GPIO2_U,FUNC_GPIO2,2}};
uint32 pwm_duty_init[1] = {PWM_IDLE};
void beep(bool isBeep) { beeping = isBeep; if(isBeep) { if(beepTimes < BEEP_DURATION) { os_printf("PWM -> Beep \r\n"); pwm_set_duty(PWM_DUTY, 0); pwm_start(); } else { os_printf("PWM -> Stop Beep \r\n"); pwm_set_duty(PWM_IDLE, 0); pwm_start(); } } else { os_printf("PWM 0 -> No Beep \r\n"); pwm_set_duty(PWM_IDLE, 0); pwm_start(); } }
void handleSensor(void) { id++; os_timer_disarm(&sensor_timer);
if(id % HEART_BEAT == 0) { MQTT_Publish(&mqttClient, "/device/heartbeat", "DOOR_SENSOR", 11, 1, 0); }
uint8 sensor_res = GPIO_INPUT_GET(GPIO_ID_PIN(0));
if(sensor_res != 0) { beepTimes++; if(beeping == false && remoteControl == false) { os_printf("Door Opened!...\r\n"); beep(true); MQTT_Publish(&mqttClient, "/device/data", "DOOR_SENSOR::door_open", 11, 1, 0); } } else { if(remoteControl) { beepTimes++; if(beepTimes >= BEEP_DURATION) { remoteControl = false; } } else { beepTimes = 0; }
if(beeping == true && remoteControl == false) { os_printf("Door Closed!...\r\n"); beep(false); MQTT_Publish(&mqttClient, "/device/data", "DOOR_SENSOR::door_close", 11, 1, 0); } } os_timer_setfn(&sensor_timer, (os_timer_func_t *)handleSensor, NULL);
os_timer_arm(&sensor_timer, INTERVAL, 1); }
void initSensor() {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); os_printf("Sensor READY\r\n");
os_timer_disarm(&sensor_timer); os_timer_setfn(&sensor_timer, (os_timer_func_t *)handleSensor, NULL); os_timer_arm(&sensor_timer, INIT_DELAY, 1); }
void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ MQTT_Connect(&mqttClient); } else { MQTT_Disconnect(&mqttClient); } } void mqttConnectedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Connected\r\n");
MQTT_Publish(client, "/device/online", "DOOR_SENSOR", 11, 2, 0); MQTT_Subscribe(client, "/device/on", 1); MQTT_Subscribe(client, "/device/off", 1); }
void mqttDisconnectedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Disconnected\r\n"); }
void mqttPublishedCb(uint32_t *args) { MQTT_Client* client = (MQTT_Client*)args; os_printf("MQTT: Published\r\n"); }
void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len) { char *topicBuf = (char*)os_zalloc(topic_len+1), *dataBuf = (char*)os_zalloc(data_len+1);
MQTT_Client* client = (MQTT_Client*)args;
os_memcpy(topicBuf, topic, topic_len); topicBuf[topic_len] = 0;
os_memcpy(dataBuf, data, data_len); dataBuf[data_len] = 0;
os_printf("Receive topic: %s, data: %s \r\n", topicBuf, dataBuf);
if(os_strncmp(topicBuf, "/device/on", 10) == 0) { os_printf("Remote control -> on. \r\n"); remoteControl = true; beep(true); } if(os_strncmp(topicBuf, "/device/off", 11) == 0) { os_printf("Remote control -> off. \r\n"); remoteControl = true; beep(false); }
os_free(topicBuf); os_free(dataBuf); }
uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void) { enum flash_size_map size_map = system_get_flash_size_map(); uint32 rf_cal_sec = 0; switch (size_map) { case FLASH_SIZE_4M_MAP_256_256: rf_cal_sec = 128 - 5; break; case FLASH_SIZE_8M_MAP_512_512: rf_cal_sec = 256 - 5; break; case FLASH_SIZE_16M_MAP_512_512: case FLASH_SIZE_16M_MAP_1024_1024: rf_cal_sec = 512 - 5; break; case FLASH_SIZE_32M_MAP_512_512: case FLASH_SIZE_32M_MAP_1024_1024: rf_cal_sec = 1024 - 5; break; default: rf_cal_sec = 0; break; } return rf_cal_sec; }
void user_init(void) { uart_init(BIT_RATE_115200, BIT_RATE_115200); os_delay_us(1000000);
CFG_Load();
MQTT_InitConnection(&mqttClient, sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.security); MQTT_InitClient(&mqttClient, sysCfg.device_id, sysCfg.mqtt_user, sysCfg.mqtt_pass, sysCfg.mqtt_keepalive, 1);
MQTT_InitLWT(&mqttClient, "/lwt", "offline", 0, 0); MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnData(&mqttClient, mqttDataCb);
WIFI_Connect(sysCfg.sta_ssid, sysCfg.sta_pwd, wifiConnectCb);
os_printf("\r\nSystem started ...\r\n");
pwm_init(PWM_PERIOD, pwm_duty_init , 1 ,io_info); set_pwm_debug_en(0); pwm_start(); os_printf("PWM START\r\n");
initSensor(); }
|