void ICACHE_FLASH_ATTR 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); char *tokens[10]; 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); int tokenCount = splitString((char *) topicBuf, '/', tokens); if (tokenCount > 0) { if (strcmp("Raw", tokens[0]) == 0) { if (tokenCount == 4 && strcmp(sysCfg.device_id, tokens[1]) == 0 && strcmp("set", tokens[2]) == 0) { if (strlen(dataBuf) < NAME_SIZE - 1) { if (strcmp("name", tokens[3]) == 0) { strcpy(sysCfg.deviceName, dataBuf); } else if (strcmp("location", tokens[3]) == 0) { strcpy(sysCfg.deviceLocation, dataBuf); } else if (strcmp("updates", tokens[3]) == 0) { sysCfg.updates = atoi(dataBuf); os_timer_disarm(&transmit_timer); os_timer_arm(&transmit_timer, sysCfg.updates * 1000, true); } publishDeviceInfo(client); CFG_Save(); } } else if (tokenCount == 5 && strcmp(sysCfg.device_id, tokens[1]) == 0 && strcmp("set", tokens[3]) == 0 && strcmp("filter", tokens[4]) == 0) { uint8 filterID = atoi(tokens[2]); if (0 <= filterID && filterID <= 3) { if (strlen(dataBuf) < (sizeof(sysCfg.filters[0]) - 2)) { strcpy(sysCfg.filters[filterID], dataBuf); CFG_Save(); } } } } else if (strcmp("App", tokens[0]) == 0) { if (tokenCount >= 2 && strcmp("date", tokens[1]) == 0) { os_timer_disarm(&date_timer); // Restart it os_timer_arm(&date_timer, 10 * 60 * 1000, false); //10 minutes } else if (tokenCount == 5) { if (passesFilter(&tokens[1])) { saveData(tokens[1], tokens[2], tokens[3], tokens[4], dataBuf); } } } } os_free(topicBuf); os_free(dataBuf); }
void CFG_Load(void) { filehandle_t* cfgFile ; cfgFile = FS_OpenFile(CONFIG_FILENAME, "wrt"); FS_UploadToRAM(cfgFile); if (!cfgFile) { Log_Printf("[CFG_Load] Unable to find player settings file, creating it.\n"); CFG_Save(); CFG_Load(); return; } //Check the magic number if(strncmp(cfgFile->filedata, cfgMagicNumber, strlen(cfgMagicNumber))) { Log_Printf("[CFG_Load] Magic number check failed.\n"); return; } //Reading visited act FS_CloseFile(cfgFile); }
static void ICACHE_FLASH_ATTR checkLazyWrite(void) { if (dirtyCount == 0) return; if (!checkSum()) { // data has changed saveSum(); CFG_Save(); TESTP("sysCfg updated\n"); } }
static void ICACHE_FLASH_ATTR tcp_receive_cb(void *arg, char *pData, unsigned short len) { HttpdConnData c; char bfr[100] = { 0 }; struct espconn *conn = (struct espconn*) arg; httpdParseHeader(pData, &c); TESTP("URL=%s\n", c.url); if (httpSetupMode && os_strncmp(c.url, "/setup", 5) == 0) { if (httpdFindArg(c.getArgs, "Action", bfr, sizeof(bfr)) >= 0) { TESTP("Action=%s\n", bfr); if (os_strncmp(bfr, "Update", 6) == 0) { if (httpdFindArg(c.getArgs, "MQTThost", bfr, sizeof(bfr)) >= 0) { TESTP("MQTThost=%s\n", bfr); if (7 < os_strlen(bfr) && os_strlen(bfr) < sizeof(sysCfg.mqtt_host)) { os_strcpy(sysCfg.mqtt_host, bfr); } } if (httpdFindArg(c.getArgs, "MQTTport", bfr, sizeof(bfr)) >= 0) { TESTP("MQTTport=%s\n", bfr); if (1 <= os_strlen(bfr) && os_strlen(bfr) <= 4) { sysCfg.mqtt_port = atoi(bfr); } } if (httpdFindArg(c.getArgs, "MQTTuser", bfr, sizeof(bfr)) >= 0) { TESTP("MQTTuser=%s\n", bfr); if (0 <= os_strlen(bfr) && os_strlen(bfr) < sizeof(sysCfg.mqtt_user)) { os_strcpy(sysCfg.mqtt_user, bfr); } } if (httpdFindArg(c.getArgs, "MQTTpass", bfr, sizeof(bfr)) >= 0) { TESTP("MQTTpass=%s\n", bfr); if (0 <= os_strlen(bfr) && os_strlen(bfr) < sizeof(sysCfg.mqtt_pass)) { os_strcpy(sysCfg.mqtt_pass, bfr); } } if (httpdFindArg(c.getArgs, "DevPrefix", bfr, sizeof(bfr)) >= 0) { TESTP("DevPrefix=%s\n", bfr); if (2 <= os_strlen(bfr) && os_strlen(bfr) < sizeof(sysCfg.deviceID_prefix)) { os_strcpy(sysCfg.deviceID_prefix, bfr); os_sprintf(sysCfg.device_id, "%s%lx", sysCfg.deviceID_prefix, system_get_chip_id()); } } if (httpdFindArg(c.getArgs, "reboot", bfr, sizeof(bfr)) >= 0) { TESTP("reboot=%s\n", bfr); if (strcmp(bfr, "yes") == 0) { reboot = true; } } CFG_Save(); } } replyOK(conn); } else { replyFail(conn); } }
void ICACHE_FLASH_ATTR CFG_checkLazyWrite(void){ if (dirtyCount == 0) return; if ((system_get_time() - lastSetDirty) > 2000000) { if (!checkSum()) { // data has changed saveSum(); CFG_Save(); TESTP("sysCfg updated\n") } lastSetDirty = system_get_time(); }
void ICACHE_FLASH_ATTR mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) { char strTopic[topic_len + 1]; os_memcpy(strTopic, topic, topic_len); strTopic[topic_len] = '\0'; char strData[lengh + 1]; os_memcpy(strData, data, lengh); strData[lengh] = '\0'; char relayNum=strTopic[topic_len-1]; char strSubsTopic[strlen((char *)sysCfg.mqtt_relay_subs_topic)]; os_strcpy(strSubsTopic,(char *)sysCfg.mqtt_relay_subs_topic); strSubsTopic[(strlen((char *)sysCfg.mqtt_relay_subs_topic)-1)]=relayNum; os_printf("MQTT strSubsTopic: %s, strTopic: %s \r\n", strSubsTopic, strTopic); if (os_strcmp(strSubsTopic,strTopic) == 0 ) { os_printf("Relay %d is now: %s \r\n", relayNum-'0', strData); if(relayNum=='1') { currGPIO12State=atoi(strData); ioGPIO(currGPIO12State,12); } if(relayNum=='2') { currGPIO13State=atoi(strData); ioGPIO(currGPIO13State,13); } if(relayNum=='3') { currGPIO15State=atoi(strData); ioGPIO(currGPIO15State,15); } if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO12State; sysCfg.relay_2_state=currGPIO13State; sysCfg.relay_3_state=currGPIO15State; CFG_Save(); } } os_printf("MQTT topic: %s, data: %s \r\n", strTopic, strData); }
void ICACHE_FLASH_ATTR mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) { char strTopic[topic_len + 1]; os_memcpy(strTopic, topic, topic_len); strTopic[topic_len] = '\0'; char strData[lengh + 1]; os_memcpy(strData, data, lengh); strData[lengh] = '\0'; char relayNum=strTopic[topic_len-1]; char strSubsTopic[strlen((char *)sysCfg.mqtt_relay_subs_topic)]; os_strcpy(strSubsTopic,(char *)sysCfg.mqtt_relay_subs_topic); strSubsTopic[(strlen((char *)sysCfg.mqtt_relay_subs_topic)-1)]=relayNum; char mqtt_temp_topic[strlen((char *)sysCfg.mqtt_temp_subs_topic)]; os_strcpy(mqtt_temp_topic,(char *)sysCfg.mqtt_temp_subs_topic); os_printf("MQTT strSubsTopic: %s, strTopic: %s \r\n", strSubsTopic, strTopic); if (os_strcmp(strSubsTopic,strTopic) == 0 ) { os_printf("Relay %d is now: %s \r\n", relayNum-'0', strData); if(relayNum=='1') { currGPIO5State=atoi(strData); ioGPIO(currGPIO5State,RELAY_GPIO); } if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO5State; CFG_Save(); } }else if (os_strcmp(mqtt_temp_topic,strTopic) == 0 ) { MQTTreading=atoi(strData)*100; os_printf("MQTT temp: %d \r\n", MQTTreading); } os_printf("MQTT topic: %s, data: %s \r\n", strTopic, strData); }
void ICACHE_FLASH_ATTR CFG_Load() { os_printf("\nload (%x/%x)...\n", sizeof(sysCfg), SPI_FLASH_SEC_SIZE); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, (uint32 *) &saveFlag, sizeof(SAVE_FLAG)); if (saveFlag.flag == 0) { spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, (uint32 *) &sysCfg, sizeof(SYSCFG)); } else { spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, (uint32 *) &sysCfg, sizeof(SYSCFG)); } if (sysCfg.cfg_holder != CFG_HOLDER) { os_memset(&sysCfg, 0x00, sizeof sysCfg); sysCfg.cfg_holder = CFG_HOLDER; os_sprintf(sysCfg.sta_ssid, "%s", STA_SSID); os_sprintf(sysCfg.sta_pwd, "%s", STA_PASS); sysCfg.sta_type = STA_TYPE; os_sprintf(sysCfg.deviceID_prefix, DEVICE_PREFIX); os_sprintf(sysCfg.device_id, "%s%lx", sysCfg.deviceID_prefix, system_get_chip_id()); os_sprintf(sysCfg.mqtt_host, "%s", MQTT_HOST); sysCfg.mqtt_port = MQTT_PORT; os_sprintf(sysCfg.mqtt_user, "%s", MQTT_USER); os_sprintf(sysCfg.mqtt_pass, "%s", MQTT_PASS); sysCfg.security = DEFAULT_SECURITY; /* default non ssl */ sysCfg.mqtt_keepalive = MQTT_KEEPALIVE; int idx; for (idx = 0; idx < MAP_SIZE; idx++) { sysCfg.mapping[idx] = idx; } for (idx = 0; idx < SETTINGS_SIZE; idx++) { sysCfg.settings[idx] = SET_MINIMUM; } sysCfg.updates = UPDATES; sysCfg.inputs = INPUTS; os_sprintf(sysCfg.deviceName, "Not Set"); os_sprintf(sysCfg.deviceLocation, "Unknown"); CFG_Save(); } }
void ICACHE_FLASH_ATTR CFG_Load() { os_printf("\nload (%x/%x)...\n", sizeof(sysCfg), SPI_FLASH_SEC_SIZE); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, (uint32 *)&saveFlag, sizeof(SAVE_FLAG)); if (saveFlag.flag == 0) { spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } else { spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } if(sysCfg.cfg_holder != CFG_HOLDER || !checkSum()){ TESTP("Reinitialising sgsCFG\n"); initSysCfg(); saveSum(); CFG_Save(); } }
//Cgi that turns the Relays on or off according to the 'relayX' param in the GET data int ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData) { int len; char buff[128]; int gotcmd=0; if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } len=httpdFindArg(connData->getArgs, "relay1", buff, sizeof(buff)); if (len>0) { currGPIO12State=atoi(buff); ioGPIO(currGPIO12State,12); gotcmd=1; //Manually switching relays means switching the thermostat off if(sysCfg.thermostat1state!=0) { sysCfg.thermostat1state=0; } } if(gotcmd==1) { if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO12State; CFG_Save(); } httpdRedirect(connData, "relay.tpl"); return HTTPD_CGI_DONE; } else { //with no parameters returns JSON with relay state httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/json"); httpdHeader(connData, "Access-Control-Allow-Origin", "*"); httpdEndHeaders(connData); len=os_sprintf(buff, "{\"relay1\": %d\n,\"relay1name\":\"%s\"}\n", currGPIO12State,(char *)sysCfg.relay1name ); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } }
void ICACHE_FLASH_ATTR CFG_Load() { INFO("\r\nload ...\r\n"); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, (uint32 *)&saveFlag, sizeof(SAVE_FLAG)); if (saveFlag.flag == 0) { spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } else { spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } if(sysCfg.cfg_holder != CFG_HOLDER){ os_memset(&sysCfg, 0x00, sizeof sysCfg); sysCfg.cfg_holder = CFG_HOLDER; os_sprintf(sysCfg.device_id, MQTT_CLIENT_ID, system_get_chip_id()); sysCfg.device_id[sizeof(sysCfg.device_id) - 1] = '\0'; os_strncpy(sysCfg.sta_ssid, STA_SSID, sizeof(sysCfg.sta_ssid) - 1); os_strncpy(sysCfg.sta_pwd, STA_PASS, sizeof(sysCfg.sta_pwd) - 1); sysCfg.sta_type = STA_TYPE; os_strncpy(sysCfg.mqtt_host, MQTT_HOST, sizeof(sysCfg.mqtt_host) - 1); sysCfg.mqtt_port = MQTT_PORT; os_strncpy(sysCfg.mqtt_user, MQTT_USER, sizeof(sysCfg.mqtt_user) - 1); os_strncpy(sysCfg.mqtt_pass, MQTT_PASS, sizeof(sysCfg.mqtt_pass) - 1); sysCfg.security = DEFAULT_SECURITY; /* default non ssl */ sysCfg.mqtt_keepalive = MQTT_KEEPALIVE; INFO(" default configuration\r\n"); CFG_Save(); } }
void ICACHE_FLASH_ATTR CFG_Load() { INFO("load from 0x%X ...",CFG_LOCATION * SPI_FLASH_SEC_SIZE); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, (uint32 *)&saveFlag, sizeof(SAVE_FLAG)); if (saveFlag.flag == 0) { spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } else { spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } if(sysCfg.cfg_holder != CFG_HOLDER){ os_memset(&sysCfg, 0x00, sizeof sysCfg); INFO(" SSID : %s, PASS : %s ",STA_SSID,STA_PASS); sysCfg.cfg_holder = CFG_HOLDER; os_sprintf(sysCfg.sta_ssid, "%s", STA_SSID); os_sprintf(sysCfg.sta_pwd, "%s", STA_PASS); sysCfg.sta_type = STA_TYPE; os_sprintf(sysCfg.device_id, MQTT_CLIENT_ID, system_get_chip_id()); os_sprintf(sysCfg.mqtt_host, "%s", MQTT_HOST); sysCfg.mqtt_port = MQTT_PORT; os_sprintf(sysCfg.mqtt_user, "%s", MQTT_USER); os_sprintf(sysCfg.mqtt_pass, "%s", MQTT_PASS); sysCfg.security = DEFAULT_SECURITY; /* default non ssl */ sysCfg.mqtt_keepalive = MQTT_KEEPALIVE; INFO("default configuration"); CFG_Save();// LMR commented out } }
// CGI to set settings int ICACHE_FLASH_ATTR saveCGI(HttpdConnData *connData) { int len; char buff[10]; //char bin[10]; if (connData->conn == NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } len = httpdFindArg(connData->getArgs, "power", buff, sizeof(buff)); if (len != 0) ac_set_power(buff); len = httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len != 0) ac_set_mode(buff); len = httpdFindArg(connData->getArgs, "temp", buff, sizeof(buff)); if (len != 0) ac_set_temp(buff); len = httpdFindArg(connData->getArgs, "fan", buff, sizeof(buff)); if (len != 0) ac_set_fan(buff); len = httpdFindArg(connData->getArgs, "swing", buff, sizeof(buff)); if (len != 0) ac_set_swing(buff); ir_send(); mqttPublishSettings(0); CFG_Save(); httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/html"); httpdEndHeaders(connData); os_strcpy(buff, "OK"); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; }
void ICACHE_FLASH_ATTR LightSW_RunSequence(uint32_t *args) { // cycle measurement using scope GPIO_OUTPUT_SET(2, TRUE); LightSW* lamp = (LightSW*) args; if (lamp->stateName == SEQUENCE_IDLE) return; // report current status or result lamp->operationStatus = lamp->stateName; // read current light intensity lamp->currentIntensity = LightSW_ReadIntensityCounts(); // ==================== // Toggle Switch // ==================== if (lamp->stateName == TOGGLE_ON) { lamp->_sequenceTimer = system_get_time(); INFO("\tIdle > "); GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); lamp->stateName = TOGGLE_ON__FINALIZE; INFO("Toggle On > "); } else if (lamp->stateName == TOGGLE_ON__FINALIZE) { if (system_get_time() > lamp->_stateTimer + TOGGLE_ON_DURATION) { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->stateName = SEQUENCE_IDLE; lamp->operationStatus = OPERATION_STATUS__SUCCESFULL; INFO("Idle\r\n"); } } // ==================== // Power On // ==================== else if (lamp->stateName == POWER_ON) { lamp->_sequenceTimer = system_get_time(); INFO("\tIdle > "); if (lamp->currentIntensity <= sysCfg.minIntensity/2) { GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); lamp->stateName = POWER_ON__TOGGLE_ON; INFO("Toggle On > "); } else { lamp->stateName = SEQUENCE_IDLE; lamp->operationStatus = OPERATION_STATUS__ALREADY_ON; INFO("Already On! > Idle\r\n"); } } else if (lamp->stateName == POWER_ON__TOGGLE_ON) { if (system_get_time() > lamp->_stateTimer + TOGGLE_ON_DURATION) { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->_stateTimer = system_get_time(); lamp->stateName = POWER_ON__IS_ON; INFO("Is On? > "); } } else if (lamp->stateName == POWER_ON__IS_ON) { if (system_get_time() > lamp->_stateTimer + POWER_ON_DURATION) { if (lamp->currentIntensity <= sysCfg.minIntensity/2) { lamp->operationStatus = OPERATION_STATUS__NOT_ON; INFO("Not On! > "); } else { lamp->operationStatus = OPERATION_STATUS__SUCCESFULL; } lamp->stateName = SEQUENCE_IDLE; INFO("Idle\r\n"); } } // ==================== // Power Off // ==================== else if (lamp->stateName == POWER_OFF) { lamp->_sequenceTimer = system_get_time(); INFO("\tIdle > "); if (lamp->currentIntensity > sysCfg.minIntensity/2) { GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); lamp->stateName = POWER_OFF__TOGGLE_ON; INFO("Toggle On > "); } else { lamp->stateName = SEQUENCE_IDLE; lamp->operationStatus = OPERATION_STATUS__ALREADY_OFF; INFO("Already Off! > Idle\r\n"); } } else if (lamp->stateName == POWER_OFF__TOGGLE_ON) { if (system_get_time() > lamp->_stateTimer + TOGGLE_ON_DURATION) { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->_stateTimer = system_get_time(); lamp-> stateName = POWER_OFF__IS_OFF; INFO("Is Off? > "); } } else if (lamp->stateName == POWER_OFF__IS_OFF) { if (system_get_time() > lamp->_stateTimer + POWER_OFF_DURATION) { if (lamp->currentIntensity > sysCfg.minIntensity/2) { lamp->operationStatus = OPERATION_STATUS__NOT_OFF; INFO(" Not Off! > "); } else { lamp->operationStatus = OPERATION_STATUS__SUCCESFULL; } lamp->stateName = SEQUENCE_IDLE; INFO("Idle\r\n"); } } // ==================== // Calibrate // ==================== else if (lamp->stateName == CALIBRATE) { lamp->_sequenceTimer = system_get_time(); lamp->_intensityChangeTimer = lamp->_sequenceTimer; lamp->_lastIntensity = lamp->currentIntensity; INFO("\tIdle > "); GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); lamp->stateName = CALIBRATE__ACCELERATE; INFO("Accelerate > "); } else if (lamp->stateName == CALIBRATE__ACCELERATE) { if (system_get_time() > lamp->_stateTimer + ACCELERATE_DURATION) { lamp->stateName = CALIBRATE__TO_LIMIT; INFO("To Limit > "); } } else if (lamp->stateName == CALIBRATE__TO_LIMIT) { // move to next step when time limit is reached or intensity does not change if (system_get_time() > lamp->_stateTimer + TRAVEL_MAX_DURATION || abs(lamp->_intensityChange) <= DB_INTENSITY_CHANGE) { INFO("\r\n"); INFO("\tGot to %d > ", lamp->currentIntensity); sysCfg.minIntensity = lamp->currentIntensity; GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->_stateTimer = system_get_time(); lamp->stateName = CALIBRATE__TOGGLE_OFF; INFO("Toggle Off > "); } } else if (lamp->stateName == CALIBRATE__TOGGLE_OFF) { if (system_get_time() > lamp->_stateTimer + TOGGLE_OFF_DURATION) { lamp->_sequenceTimer = system_get_time(); GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); lamp->stateName = CALIBRATE__ACCELERATE_OTHER; INFO("Accelerate > "); } } else if (lamp->stateName == CALIBRATE__ACCELERATE_OTHER) { if (system_get_time() > lamp->_stateTimer + ACCELERATE_DURATION) { lamp->stateName = CALIBRATE__TO_OTHER_LIMIT; INFO("To Other Limit > "); } } else if (lamp->stateName == CALIBRATE__TO_OTHER_LIMIT) { // finish when time limit is reached or intensity does not change if (system_get_time() > lamp->_stateTimer + TRAVEL_MAX_DURATION || abs(lamp->_intensityChange) <= DB_INTENSITY_CHANGE) { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); INFO("\r\n"); INFO("\tGot to %d > ", lamp->currentIntensity); sysCfg.maxIntensity = lamp->currentIntensity; lamp->stateName = SEQUENCE_IDLE; INFO("Idle\r\n"); // swap identified values if required if(sysCfg.maxIntensity < sysCfg.minIntensity) { int tmpVal = sysCfg.maxIntensity; sysCfg.maxIntensity = sysCfg.minIntensity; sysCfg.minIntensity = tmpVal; } CFG_Save(); INFO("Calibration %d / %d\r\n", sysCfg.minIntensity, sysCfg.maxIntensity); lamp->operationStatus = OPERATION_STATUS__SUCCESFULL; } } // ==================== // Set Intensity // ==================== else if (lamp->stateName == SET_INTENSITY) { lamp->_sequenceTimer = system_get_time(); lamp->_intensityChangeTimer = lamp->_sequenceTimer; lamp->_lastIntensity = lamp->currentIntensity; INFO("\tIdle > "); if (abs(lamp->_targetIntensity - lamp->currentIntensity) < (sysCfg.maxIntensity - sysCfg.minIntensity) / DB_INTENSITY_RATIO) { INFO("On Target! > "); lamp->stateName = SEQUENCE_IDLE; lamp->operationStatus = OPERATION_STATUS__ALREADY_ON_TARGET; INFO("Idle\r\n"); } else { GPIO_OUTPUT_SET(lamp->_switchPin, TRUE); lamp->_stateTimer = system_get_time(); if (lamp->currentIntensity > sysCfg.minIntensity/2) { lamp->stateName = SET_INTENSITY__ACCELERATE; } else { lamp->stateName = SET_INTENSITY__GET_ON; INFO("Get On > "); } INFO("Moving from %d to %d > ", lamp->currentIntensity, lamp->_targetIntensity); } } else if (lamp->stateName == SET_INTENSITY__GET_ON) { if (system_get_time() > lamp->_stateTimer + POWER_ON_DURATION - ACCELERATE_DURATION) { lamp->_stateTimer = system_get_time(); lamp->stateName = SET_INTENSITY__ACCELERATE; } } else if (lamp->stateName == SET_INTENSITY__ACCELERATE) { if (system_get_time() > lamp->_stateTimer + ACCELERATE_DURATION) { bool goingUp = (lamp->_intensityChange > 0) ? true : false; lamp->_targetAbove = (lamp->currentIntensity < lamp->_targetIntensity) ? true : false; INFO("\r\n"); INFO("\tMoved by %d to %d, ", lamp->_intensityChange, lamp->currentIntensity); if ((lamp->_targetAbove && goingUp) || (!lamp->_targetAbove && !goingUp)) { lamp->stateName = SET_INTENSITY__GET_TARGET; INFO("Get Target > "); } else { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->_stateTimer = system_get_time(); lamp->stateName = SET_INTENSITY__TOGGLE_OFF; INFO("Toggle Off > "); } } } else if (lamp->stateName == SET_INTENSITY__TOGGLE_OFF) { if (system_get_time() > lamp->_stateTimer + TOGGLE_OFF_DURATION) { lamp->_stateTimer = system_get_time(); lamp->stateName = SET_INTENSITY; INFO("Return > \r\n"); } } else if (lamp->stateName == SET_INTENSITY__GET_TARGET) { bool valueAbove = (lamp->currentIntensity > lamp->_targetIntensity) ? true : false; // finalise when target is reached or intensity does not change if ((lamp->_targetAbove && valueAbove) || (!lamp->_targetAbove && !valueAbove) || abs(lamp->_intensityChange) <= DB_INTENSITY_CHANGE) { lamp->stateName = SET_INTENSITY__FINALIZE; } } else if (lamp->stateName == SET_INTENSITY__FINALIZE) { GPIO_OUTPUT_SET(lamp->_switchPin, FALSE); lamp->stateName = SEQUENCE_IDLE; INFO("\r\n"); INFO("\tFinalized at %d > ", lamp->currentIntensity); if (abs(lamp->_targetIntensity - lamp->currentIntensity) >= (sysCfg.maxIntensity - sysCfg.minIntensity) / DB_INTENSITY_RATIO) { lamp->operationStatus = OPERATION_STATUS__OFF_TARGET; INFO("Off Target! > "); } else { lamp->operationStatus = OPERATION_STATUS__ON_TARGET; } INFO("Idle\r\n"); } // ======================================== // Common Tasks & Check for Timeout // ======================================== if (lamp->stateName != SEQUENCE_IDLE) { if (system_get_time() > lamp->_sequenceTimer + SEQUENCE_TIMEOUT) { lamp->stateName = SEQUENCE_IDLE; lamp->operationStatus = OPERATION_STATUS__TIMEOUT; INFO("Timeout! > Idle\r\n"); } // Periodically sense light intensity change as required by certain states if ((lamp->stateName & SET_INTENSITY) == SET_INTENSITY || (lamp->stateName & CALIBRATE) == CALIBRATE) { if (system_get_time() > lamp->_intensityChangeTimer + SENSE_CHANGE_DURATION) { lamp->_intensityChange = lamp->currentIntensity - lamp->_lastIntensity; lamp->_lastIntensity = lamp->currentIntensity; lamp->_intensityChangeTimer = system_get_time(); } } } // cycle measurement using scope GPIO_OUTPUT_SET(2, FALSE); }
int ifttt_cgi(char* buf) { save_status = html_save_empt; int buf_cnt = 0; #define IFTTT_QUERY_NUM 3 CGI_QUERY icq[IFTTT_QUERY_NUM] = { {"event", NULL, "ok"}, {"key", NULL, "ok"}, {"ifttt_port", "00000", "ok"} }; if (QUERY_STRING) { // SCAN QUERY_STRING if (cgi_query_scan (QUERY_STRING, icq, IFTTT_QUERY_NUM) == CONN_FAIL ) return 0; // Check query entries if ( strlen(icq[0].valstr) > 64 ) {icq[0].err=html_id_err; save_status = html_save_err;}; if ( strlen(icq[1].valstr) > 64 ) {icq[1].err=html_id_err; save_status = html_save_err;}; int32 ifttt_port = strtol(icq[2].valstr, NULL , 10); if ( (ifttt_port<1 && ifttt_port>65535) || ifttt_port == ntohs(sysCfg.ntp_local_port) || ifttt_port == ntohs(HTTP_PORT)) {icq[2].err=html_id_err; save_status = html_save_err;}; // if the query values are OK - save to flash if ( save_status != html_save_err ) { strcpy (sysCfg.ifttt_event, icq[0].valstr); strcpy (sysCfg.ifttt_key, icq[1].valstr); sysCfg.ifttt_local_port = htons(ifttt_port); CFG_Save(); save_status = html_save_ok; } } // if there was no query or the query was saved succefully // read values from saved config if (save_status != html_save_err) { icq[0].valstr = sysCfg.ifttt_event; icq[1].valstr = sysCfg.ifttt_key; sprintf (icq[2].valstr, "%u", ntohs( sysCfg.ifttt_local_port) ); } // Web page HEADER buf_cnt += web_page_header (buf+buf_cnt, ifttt_cgi); const char* ifttt_cgi_html = "<form action=\"ifttt.cgi\">" "<p>IFTTT Event Name:<br>" "<input type=\"text\" name=\"event\" value=\"%s\"></p>" "<p>IFTTT key:<br>" "<input type=\"text\" name=\"key\" value=\"%s\"></p>" "<p>IFTTT local port:<br>" "<input type=\"number\" name=\"ifttt_port\" min=\"1\" max=\"65535\" value=\"%s\" maxlength=\"5\" size=\"5\"></p>" "<input type=\"submit\">" "</form>%s<br>" "</body></html>"; buf_cnt += sprintf (buf+buf_cnt, ifttt_cgi_html, icq[0].valstr, icq[1].valstr, icq[2].valstr, save_status); //Web page FOOTER buf_cnt += web_page_footer (buf+buf_cnt, ntp_cgi); return buf_cnt; }
int time_cgi(char* buf) { #define TIME_QUERY_NUM 8 static CGI_QUERY tcq[TIME_QUERY_NUM] = { {"hrs_start", "00", }, {"min_start", "01", }, {"hrs_end", "02", }, {"min_end", "03", }, {"hrs_tzone", "04", }, {"min_tzone", "05", }, {"min_period", "06", }, {"sec_period", "07", } }; int tcq_val [TIME_QUERY_NUM]; int buf_cnt = 0; int ii; save_status = html_save_empt; if (QUERY_STRING) { if (cgi_query_scan (QUERY_STRING, tcq, TIME_QUERY_NUM) == CONN_FAIL ) return 0; for (ii=0; ii<TIME_QUERY_NUM; ii++) { tcq_val[ii] = strtol(tcq[ii].valstr, NULL , 10); } if ( tcq_val[0]>=0 && tcq_val[0]<24 && tcq_val[1]>=0 && tcq_val[1]<60 && tcq_val[2]>=0 && tcq_val[2]<24 && tcq_val[3]>=0 && tcq_val[3]<60 && tcq_val[4]>-10 && tcq_val[3]<13 && (tcq_val[5]==0 || tcq_val[5]==15 || tcq_val[5]==30 || tcq_val[5]==45) && tcq_val[6]>=0 && tcq_val[6]<60 && tcq_val[7]>=0 && tcq_val[7]<60 ) { sysCfg.active_start = tcq_val[0] * SECONDS_IN_HOUR + tcq_val[1] * 60; sysCfg.active_end = tcq_val[2] * SECONDS_IN_HOUR + tcq_val[3] * 60; sysCfg.timezone = tcq_val[4] * SECONDS_IN_HOUR; if (tcq_val[4] <0 ) tcq_val[5] = (-1) * tcq_val[5]; sysCfg.timezone += (tcq_val[5] * 60); sysCfg.period_checking = tcq_val[6] * 60 + tcq_val[7]; CFG_Save(); save_status = html_save_ok; } else return 0; } tcq_val[0] = sysCfg.active_start / 60; //make minutes tcq_val[1] = tcq_val[0] % 60; //get minutes without round hours tcq_val[0] = ( tcq_val[0] - tcq_val[1] ) / 60; //get (round) hours tcq_val[2] = sysCfg.active_end / 60; tcq_val[3] = tcq_val[2] % 60; tcq_val[2] = ( tcq_val[2] - tcq_val[3] ) / 60; tcq_val[4] = sysCfg.timezone / 60; //make minutes tcq_val[5] = tcq_val[4] % 60; //get minutes without round hours tcq_val[4] = ( tcq_val[4] - tcq_val[5] ) / 60; //get (round) hours tcq_val[5] = abs ( tcq_val[5] ); //make minutes positive tcq_val[7] = sysCfg.period_checking % 60; tcq_val[6] = ( sysCfg.period_checking - tcq_val[7] ) / 60; for (ii=0; ii<TIME_QUERY_NUM; ii++) { // tcq[ii].valstr = malloc (3); sprintf (tcq[ii].valstr, "%02d", tcq_val[ii]); } // Web page HEADER buf_cnt += web_page_header (buf+buf_cnt, time_cgi); const char* time_cgi_html = "<form action=\"time.cgi\">" "<p>Mail check period:<br>" "Start time (HH:MM)<br>" "<input type=\"number\" name=\"hrs_start\" min=\"00\" max=\"23\" value=\"%s\" maxlength=\"2\" size=\"2\">  :  " "<input type=\"number\" name=\"min_start\" min=\"00\" max=\"59\" value=\"%s\" maxlength=\"2\" size=\"2\"><br>" "End time (HH:MM)<br>" "<input type=\"number\" name=\"hrs_end\" min=\"00\" max=\"23\" value=\"%s\" maxlength=\"2\" size=\"2\">  :  " "<input type=\"number\" name=\"min_end\" min=\"00\" max=\"59\" value=\"%s\" maxlength=\"2\" size=\"2\"></p>" "<p>Time zone (HH:MM)<br>" "<input type=\"number\" name=\"hrs_tzone\" min=\"-9\" max=\"12\" value=\"%s\" maxlength=\"2\" size=\"2\">  :  " "<input type=\"number\" name=\"min_tzone\" min=\"00\" max=\"45\" value=\"%s\" step=\"15\" maxlength=\"2\" size=\"2\"></p>" "<p>Check every (MM:SS)<br>" "<input type=\"number\" name=\"min_period\" min=\"00\" max=\"59\" value=\"%s\" maxlength=\"2\" size=\"2\">  :  " "<input type=\"number\" name=\"sec_period\" min=\"00\" max=\"59\" value=\"%s\" maxlength=\"2\" size=\"2\"></p>" "<input type=\"submit\">" "</form>%s" "</body></html>"; buf_cnt += sprintf (buf+buf_cnt, time_cgi_html, tcq[0].valstr, tcq[1].valstr, tcq[2].valstr, tcq[3].valstr, tcq[4].valstr, tcq[5].valstr, tcq[6].valstr, tcq[7].valstr, save_status); //Web page FOOTER buf_cnt += web_page_footer (buf+buf_cnt, time_cgi); return buf_cnt; }
int wifi_cgi(char* buf) { #ifdef DEBUG_CGI printf("\nwificgi -> %s\n", PATH_INFO); #endif char* radio_dhcp; char* radio_static; save_status = html_save_empt; ip_addr_t tmp_ip, tmp_netmask, tmp_gw, tmp_dns1, tmp_dns2; uint8 tmp_dhcp; int buf_cnt = 0; #define WIFI_QUERY_NUM 8 CGI_QUERY wcq[WIFI_QUERY_NUM] = { {"ssid", NULL, "ok"}, {"pswd", NULL, "ok"}, {"dhcp", NULL, "ok"}, {"ip", "000.000.000.001", "ok"}, {"mask", "000.000.000.002", "ok"}, {"gate", "000.000.000.003", "ok"}, {"dns1", "000.000.000.004", "ok"}, {"dns2", "000.000.000.005", "ok"} }; if (QUERY_STRING) { // break query to strings if (cgi_query_scan(QUERY_STRING, wcq, WIFI_QUERY_NUM) == CONN_FAIL) return 0; // convert query strings and check values if ( strlen(wcq[0].valstr)>32 ) {wcq[0].err=html_id_err; save_status = html_save_err;} //SSID if ( strlen(wcq[1].valstr)>32 ) {wcq[1].err=html_id_err; save_status = html_save_err;}//Password if ((strcmp(wcq[2].valstr, "0")) == 0) { radio_dhcp = ""; radio_static = html_checked; tmp_dhcp = 0; } else { if ((strcmp(wcq[2].valstr, "1")) == 0) { radio_dhcp = html_checked; radio_static = ""; tmp_dhcp = 1; } else return 0; } if ( !(inet_aton (wcq[3].valstr, &tmp_ip)) ) {wcq[3].err=html_id_err; save_status = html_save_err;}; //IP if ( !(inet_aton (wcq[4].valstr, &tmp_netmask)) ) {wcq[4].err=html_id_err; save_status = html_save_err;}; //Net mask if ( !(inet_aton (wcq[5].valstr, &tmp_gw)) ) {wcq[5].err=html_id_err; save_status = html_save_err;}; //Gateway if ( !(inet_aton (wcq[6].valstr, &tmp_dns1)) ) {wcq[6].err=html_id_err; save_status = html_save_err;}; //DNS 1 if ( !(inet_aton (wcq[7].valstr, &tmp_dns2)) ) {wcq[7].err=html_id_err; save_status = html_save_err;}; //DNS 2 // if the query values are OK - save to flash if ( save_status != html_save_err ) { if ( wcq[0].valstr[0] != 0 ) // if ssid is not empty {strcpy(sysCfg.sta_ssid, wcq[0].valstr); strcpy(sysCfg.sta_pswd, wcq[1].valstr); } sysCfg.sta_dhcp = tmp_dhcp; sysCfg.sta_ip_info.ip = tmp_ip; sysCfg.sta_ip_info.netmask = tmp_netmask; sysCfg.sta_ip_info.gw = tmp_gw; sysCfg.sta_dns[0] = tmp_dns1; sysCfg.sta_dns[1] = tmp_dns2; CFG_Save(); save_status = html_save_ok; } } // if there was no query or the query was saved succefully // read values from saved config if (save_status != html_save_err) { int ii; // for (ii = 3; ii<WIFI_QUERY_NUM-1; ii++) // wcq[ii].valstr = malloc (16); //read from sysCfg wcq[0].valstr = ""; //SSID wcq[1].valstr = ""; //Password if (sysCfg.sta_dhcp == 0) { radio_dhcp = ""; radio_static = html_checked; } else { radio_dhcp = html_checked; radio_static = ""; } inet_ntoa_r(sysCfg.sta_ip_info.ip, wcq[3].valstr, 16); inet_ntoa_r(sysCfg.sta_ip_info.netmask, wcq[4].valstr, 16); inet_ntoa_r(sysCfg.sta_ip_info.gw, wcq[5].valstr, 16); inet_ntoa_r(sysCfg.sta_dns[0], wcq[6].valstr, 16); inet_ntoa_r(sysCfg.sta_dns[1], wcq[7].valstr, 16); } #ifdef DEBUG_CGI printf ("F**K!!! we are here"); #endif buf_cnt += web_page_header (buf+buf_cnt, wifi_cgi); const char* wifi_cgi_html = "<p><form action=\"wifi.cgi\">" "SSID:\"%s\"<br>" "<p>Change SSID<br>" "(type or choose from list)<br>" "<input list=\"ide\" name=\"ssid\" value=\"%s\" />" "<datalist id=\"ide\">"; buf_cnt += sprintf (buf+buf_cnt, wifi_cgi_html, sysCfg.sta_ssid, wcq[0].valstr); //scanned SSIDs list generation struct bss_info* bss_tmp; while (bss_link) { #ifdef DEBUG_CGI printf ("\n\n ssid p = %p", (void*)bss_link ); printf ("\n\n ssid len = %u", bss_link->ssid_len ); printf ("\n\n ssid = %s", bss_link->ssid ); #endif buf_cnt += sprintf(buf + buf_cnt, "<option value=\"%s\" />", bss_link->ssid); bss_tmp = bss_link; bss_link = bss_link->next.stqe_next; } wifi_cgi_html = "</datalist><a href=/wifi_scan.cgi>SCAN</a><br>" "Password:<br><input type=\"text\" name=\"pswd\" value=\"%s\" ID=\"%s\"></p>" "<p><input type=\"radio\" name=\"dhcp\" value=\"1\" %s> DHCP client<br>" "<input type=\"radio\" name=\"dhcp\" value=\"0\" %s> Static address</p>" "Static - IP:<br><input type=\"text\" name=\"ip\" value=\"%s\" ID=\"%s\" pattern=\"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"><br>" "Static - Netmask:<br><input type=\"text\" name=\"mask\" value=\"%s\" ID=\"%s\"><br>" "Static - Gate:<br><input type=\"text\" name=\"gate\" value=\"%s\" ID=\"%s\"><br>" "Static - DNS 1:<br><input type=\"text\" name=\"dns1\" value=\"%s\" ID=\"%s\"><br>" "Static - DNS 2:<br><input type=\"text\" name=\"dns2\" value=\"%s\" ID=\"%s\"><br>" "<br><input type=\"submit\">" "</form>%s" "</body></html>"; buf_cnt += sprintf (buf+buf_cnt, wifi_cgi_html, wcq[1].valstr, wcq[1].err, radio_dhcp, radio_static, wcq[3].valstr, wcq[3].err, wcq[4].valstr, wcq[4].err, wcq[5].valstr, wcq[5].err, wcq[6].valstr, wcq[6].err, wcq[7].valstr, wcq[7].err, save_status ); /* char* valstrs[15]; valstrs[0] = wcq[1].valstr; valstrs[1] = wcq[1].err, valstrs[2] = radio_dhcp; valstrs[3] = radio_static; valstrs[4] = wcq[3].valstr; valstrs[5] = wcq[3].err; valstrs[6] = wcq[4].valstr; valstrs[7] = wcq[4].err; valstrs[8] = wcq[5].valstr; valstrs[9] = wcq[5].err; valstrs[10] = wcq[6].valstr; valstrs[11] = wcq[6].err; valstrs[12] = wcq[7].valstr; valstrs[13] = wcq[7].err; valstrs[14] = save_status; buf_cnt += sssprintf (buf+buf_cnt, wifi_cgi_html, valstrs); */ /* wifi_cgi_html = "</datalist><a href=/wifi_scan.cgi>SCAN</a><br>" "Password:<br><input type=\"text\" name=\"pswd\" value=\"%s\" ID=\"%s\"><br><br>" "<input type=\"radio\" name=\"dhcp\" value=\"1\" %s> DHCP client<br>" "<input type=\"radio\" name=\"dhcp\" value=\"0\" %s> Static address<br>"; buf_cnt += sprintf(buf + buf_cnt, wifi_cgi_html, wcq[1].valstr, wcq[1].err, radio_dhcp, radio_static); wifi_cgi_html = "Static - IP:<br><input type=\"text\" name=\"ip\" value=\"%s\" ID=\"%s\" pattern=\"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\"><br>" "Static - Netmask:<br><input type=\"text\" name=\"mask\" value=\"%s\" ID=\"%s\"><br>" "Static - Gate:<br><input type=\"text\" name=\"gate\" value=\"%s\" ID=\"%s\"><br>"; buf_cnt += sprintf(buf + buf_cnt, wifi_cgi_html, wcq[3].valstr, wcq[3].err, wcq[4].valstr, wcq[4].err, wcq[5].valstr, wcq[5].err); wifi_cgi_html = "Static - DNS 1:<br><input type=\"text\" name=\"dns1\" value=\"%s\" ID=\"%s\"><br>" "Static - DNS 2:<br><input type=\"text\" name=\"dns2\" value=\"%s\" ID=\"%s\"><br>" "<br><input type=\"submit\">" "</form>%s" "</body></html>"; buf_cnt += sprintf(buf + buf_cnt, wifi_cgi_html,wcq[6].valstr, wcq[6].err, wcq[7].valstr, wcq[7].err, save_status ); */ buf_cnt += web_page_footer (buf+buf_cnt, wifi_cgi); return buf_cnt; }
uint8_t MODEM_Init(void) { uint8_t numTry,i; MODEM_Info("\r\nGSM->TURNING ON...\r\n"); POWER_PIN_SET_OUTPUT; RESET_PIN_SET_OUTPUT; RTS_PIN_SET_OUTPUT; RTS_PIN_CLR; RESET_PIN_SET; POWER_PIN_SET; // Turn on GSM DelayMs(5); // Delay 5ms POWER_PIN_CLR; // Turn off GSM DelayMs(200); // Delay 200ms POWER_PIN_SET; // Turn on GSM // ---------- Turn on GPRS module ---------- RESET_PIN_CLR; // Reset GSM DelayMs(200); // Delay 200ms RESET_PIN_SET; // Start GSM module (Release reset) MODEM_FlushBuffer(); COMM_Puts("AT\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM->NOT RESPONSE\r\n"); numTry = 10; while(1) { watchdogFeed[WTD_MAIN_LOOP] = 0; MODEM_Info("\r\nGSM->AT\r\n"); COMM_Puts("AT\r"); if(!MODEM_CheckResponse("OK", 3000)) break; numTry--; if(numTry == 0) { MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } } } MODEM_Info("GSM->AT:OK\n"); // clear all configs MODEM_Info("\r\nGSM->CONFIGS CLEAR\r\n"); COMM_Puts("ATZ\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM->FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } //LED network status indication enable MODEM_Info("\r\nGSM->ENABLE STATUS LED\r\n"); COMM_Puts("AT+UGPIOC=20,2\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM->FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } MODEM_Info("\r\nGSM->DTE DISABLE\r\n"); COMM_Puts("AT&K0\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM-> DTE DISABLE FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } MODEM_Info("\r\nGSM->POWEWER SAVE MODE ENABLE:CONTROL BY RTS PIN\r\n"); COMM_Puts("AT+UPSV=2\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM-> UPSV FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } // Set SMS format to text mode MODEM_Info("\r\nGSM->SMS SET TEXT MODE\r\n"); COMM_Puts("AT+CMGF=1\r"); if(MODEM_CheckResponse("OK", 3000)) { MODEM_Info("\r\nGSM-> AT+CMGF=1 FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xaa; //loi SIM } // Set SMS DISPLAY mode MODEM_Info("\r\nGSM->SET SMS DISPLAYMODE\r\n"); COMM_Puts("AT+CNMI=2,1,0,0,0\r"); if(MODEM_CheckResponse("OK", 3000)){ MODEM_Info("\r\nGSM-> AT+CNMI=2,1,0,0,0 FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } // give the modem 20s to ready for dialing COMM_Puts("AT+CREG?\r"); if(MODEM_CheckResponse("+CREG: 0,1", 3000)) { MODEM_Info("\r\nNOT RESPONSE\r\n"); numTry = 120; MODEM_Info("\r\nGSM->CGREG :**"); while(1) { watchdogFeed[WTD_MAIN_LOOP] = 0; MODEM_Info("**"); COMM_Puts("AT+CREG?\r"); if(!MODEM_CheckResponse("+CREG: 0,1", 3000)) break; numTry--; if(numTry == 0) { MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } } } MODEM_Info("\r\nGSM->CGREG:OK\r\n"); // read modem IMEI GetIMEI(modemId, sizeof modemId); MODEM_Info("GSM-> IMEI:%s\n", modemId); if(strlen((char *)modemId) == 0) return 0xff; if(strcmp((char *)sysCfg.imei, (char *)modemId)) { strcpy((char *)sysCfg.imei, (char *)modemId); strcpy((char *)sysCfg.id, (char *)modemId); CFG_Save(); } MODEM_Info("\r\nGSM->AT+CLIP=1->(SHOW COMING PHONE NUMBER)\r\n"); COMM_Puts("AT+CLIP=1\r"); if(MODEM_CheckResponse("OK", 3000)){ MODEM_Info("\r\nGSM-> AT+CLIP=1 FAILS\r\n"); MODEM_Info("\r\nGSM->RETURN\r\n"); return 0xff; } DelayMs(1000); MODEM_GetCSQ(); //SendSMS("0978779222","thienhaiblue"); InternetSetProfiles((uint8_t *)sysCfg.gprsApn,(uint8_t *)sysCfg.gprsUsr,(uint8_t *)sysCfg.gprsPwd); DelayMs(3000); i = 5; while(i) { if(MODEM_EnableGPRS()) { MODEM_DisableGPRS(); i--; } else { break; } } if(i == 0) return 0xff; //SendSMS("0978779222","thienhaiblue"); return 0; }
void ICACHE_FLASH_ATTR CFG_Load() { INFO("\r\nload ...\r\n"); spi_flash_read((CFG_LOCATION + 3) * SPI_FLASH_SEC_SIZE, (uint32 *)&saveFlag, sizeof(SAVE_FLAG)); if (saveFlag.flag == 0) { spi_flash_read((CFG_LOCATION + 0) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } else { spi_flash_read((CFG_LOCATION + 1) * SPI_FLASH_SEC_SIZE, (uint32 *)&sysCfg, sizeof(SYSCFG)); } if(sysCfg.cfg_holder != CFG_HOLDER){ os_memset(&sysCfg, 0x00, sizeof sysCfg); sysCfg.cfg_holder = CFG_HOLDER; os_sprintf(sysCfg.sta_ssid, "%s", WIFI_CLIENTSSID); os_sprintf(sysCfg.sta_pwd, "%s", WIFI_CLIENTPASSWORD); sysCfg.sta_type = STA_TYPE; os_sprintf(sysCfg.device_id, MQTT_CLIENT_ID, system_get_chip_id()); os_sprintf(sysCfg.mqtt_host, "%s", MQTT_HOST); sysCfg.mqtt_port = MQTT_PORT; os_sprintf(sysCfg.mqtt_user, "%s", MQTT_USER); os_sprintf(sysCfg.mqtt_pass, "%s", MQTT_PASS); os_sprintf(sysCfg.base, "%s", MQTT_BASE); // Aidans os_sprintf(sysCfg.web_user, "admin"); // Default web password os_sprintf(sysCfg.web_pass, "password"); // Default web password sysCfg.enable_webpage_control = 1; // Enable webpage control // END Aidans sysCfg.security = DEFAULT_SECURITY; /* default non ssl */ sysCfg.mqtt_keepalive = MQTT_KEEPALIVE; sysCfg.dawn=DEFAULT_DAWN; sysCfg.dusk=DEFAULT_DUSK; sysCfg.peak=DEFAULT_PEAK; sysCfg.off_peak=DEFAULT_OFF_PEAK; sysCfg.frost=DEFAULT_FROST; sysCfg.on_1=DEFAULT_ON_1; sysCfg.off_1=DEFAULT_OFF_1; sysCfg.on_2=DEFAULT_ON_2; sysCfg.off_2=DEFAULT_OFF_2; sysCfg.out_0_status=DEFAULT_0_STATUS; sysCfg.out_4_status=DEFAULT_4_STATUS; sysCfg.out_5_status=DEFAULT_5_STATUS; sysCfg.out_12_status=DEFAULT_12_STATUS; sysCfg.out_13_status=DEFAULT_13_STATUS; sysCfg.in_14_bounce=DEFAULT_14_IN; sysCfg.sensor=DEFAULT_SENSOR; sysCfg.five_enable=FIVE_ENABLE; sysCfg.override_0=DEFAULT_MANUAL; sysCfg.override_0_time=DEFAULT_MANUAL_TIME; sysCfg.invert_0=DEFAULT_INVERT; sysCfg.calibrate = CALIBRATE; // AIDANS os_sprintf(sysCfg.web_user, "admin"); // Default web password os_sprintf(sysCfg.web_pass, "password"); // Default web password sysCfg.enable_webpage_control = 1; // Enable webpage control // aidan end INFO(" default configuration\r\n"); CFG_Save(); } }