void ICACHE_FLASH_ATTR onesec(void *arg) { int i; struct time_entry *t; int recompute = 0; static int first_compute = 0; if (!first_compute) { if (time() > SANE_TIME) { first_compute = 1; compute_times(); } os_printf("waiting for sane_time %d %d\n",sntp_get_current_timestamp(),time()); return; } for (i = 1; i <=8; i++) { if (countdown[i-1]) { countdown[i-1]--; if (countdown[i-1] == 0) { set_relay(i,0,0); } } } time_t now = time(); for (i = time_count, t = times; i > 0; i--, t++) { //os_printf("zone=%d now=%d ontime=%d recompute=%d ctr=%d\n",t->zone,now, t->ontime, recompute, i); if (now >= t->ontime) { recompute++; if (t->zone == 100) { start_reset(); } else if (t->zone == 101) { set_all_relays_off(); } else { set_relay(t->zone,1,t->duration); } } } if (recompute) compute_times(); }
void ICACHE_FLASH_ATTR set_all_relays_off() { int i; for (i = 1; i<=8; i++) { set_relay(i,0,0); } }
int ICACHE_FLASH_ATTR cgiStatus(HttpdConnData *connData) { char buff[50]; char buff2[10]; int len=httpdFindArg(connData->post->buff, "chan", buff, sizeof(buff)); int len2=httpdFindArg(connData->post->buff, "val", buff2, sizeof(buff2)); if (len > 0 && len2 > 0) { os_printf("chan=%s val=%s\n",buff,buff2); int relay = atoi(buff); int onoff = atoi(buff2); set_relay(relay,onoff,300); } char *sp = buff; *sp++ = '['; int i; for (i = 1; i <= 8; i++) { *sp++ = '"'; *sp++ = 'o'; if (get_relay(i)) { *sp++ = 'n'; } else { *sp++ = 'f'; *sp++ = 'f'; } *sp++ = '"'; *sp++ = ','; } sp--; *sp++ = ']'; *sp++ = '\0'; httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; }
/* When the roving module receives a packet that starts with @, it passes the rest * of the command to this function. */ uint8_t parse_shemp_command(uint8_t * string, uint16_t length) { // TODO Make this entire function much more modular uint16_t cur_index = 0; if(string[cur_index] == 'W') { // Wait cur_index++; uint16_t wait_ms = read_number_from_string(&string[cur_index], 4); wait(wait_ms); // And then clear output buffer reset_output_buffer(); } else if(string[cur_index] == 'R') { // Relay cur_index++; if (string[cur_index] == 'N') { set_relay(ON); } else if (string[cur_index] == 'F') { set_relay(OFF); } cur_index++; } else if (string[cur_index] == 'H') { // Sync with header cur_index++; server_wants_header = TRUE; okay_to_transmit = FALSE; //transmit_header(); } else if(string[cur_index] == 'K') { // Okay to transmit cur_index++; server_wants_header = FALSE; okay_to_transmit = TRUE; } else if(string[cur_index] == 'S') { cur_index++; // Sensor configuration uint8_t sensor_loc = 0; uint8_t sensor_type = 0; uint8_t sensor_state = 0; time_ref sensor_period = 0; uint16_t sensor_size = 0; uint8_t sensor_index = 0; #define ENABLED 'E' #define DISABLED 'D' while(cur_index < length) { if(string[cur_index] == 'L') { cur_index++; // Location sensor_loc = string[cur_index]; cur_index++; } else if(string[cur_index] == 'T') { cur_index++; // Type sensor_type = string[cur_index]; cur_index++; } else if(string[cur_index] == 's') { cur_index++; // State if(string[cur_index] == 'E') { cur_index++; sensor_state = ENABLED; } else if (string[cur_index] == 'D') { cur_index++; sensor_state = DISABLED; } } else if(string[cur_index] == 'P') { cur_index++; sensor_period = new_time_from_string(&string[cur_index]); cur_index += STRING_TO_TIME_LENGTH; } else if(string[cur_index] == 'C') { cur_index++; sensor_size = read_number_from_string(&string[cur_index], 5); cur_index += 5; } } // Command is parsed, now to determine how to do it if (sensor_loc == 'I') { // Internal, internal voltage, current, or wattage if (sensor_type == 'W') { // Wattage can be enabled and disabled and period changed if (sensor_state == DISABLED) disable_internal_wattage_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_wattage_sensor(sensor_period, sensor_size); wait(1); //This ensures that all 3 sensors involved are in sync enable_internal_wattage_sensor(); } } } else if (sensor_type == 'V') { // Handle internal voltage sensor if (sensor_state == DISABLED) disable_internal_voltage_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_voltage_sensor(sensor_period, sensor_size); wait(1); enable_internal_voltage_sensor(); } } } else if (sensor_type == 'I') { // Handle internal current sensor if (sensor_state == DISABLED) disable_internal_current_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_internal_current_sensor(sensor_period, sensor_size); wait(1); enable_internal_current_sensor(); } } } else if (sensor_type == 'T') { // Handle internal temperature sensor // It can be enabled, disabled, period and size changed if (sensor_state == DISABLED) disable_internal_temperature_sensor(); else if (sensor_state == ENABLED) { // Else it should be enabled if (sensor_period && sensor_size) { // We have all the needed information create_temperature_sensor('I',sensor_period, sensor_size); wait(1); enable_internal_temperature_sensor(); } } } } else if (sensor_loc == 'A' || sensor_loc == 'B') { sensor_index = sensor_loc - 'A'; if(sensor_state == DISABLED) { // Should disable that sensor if it matches if(sensor_type == sensor_get_type(aux_sensor[sensor_index])) { sensor_disable_this(aux_sensor[sensor_index]); } } else if (sensor_state == ENABLED) { if(sensor_period && sensor_size) { // We have everything we need, but maybe we are already set up, do we have to change anything? if(sensor_type != sensor_get_type(aux_sensor[sensor_index]) || time_cmp(sensor_period, sensor_get_period(aux_sensor[sensor_index])) || sensor_size != sensor_get_size(aux_sensor[sensor_index])) { // It is a different type // Or a different period // Or a different size sensor_delete_this(aux_sensor[sensor_index]); aux_sensor[sensor_index] = 0; } sensor_ref s; if(sensor_type == 'T') { // External Temperature Sensor s = create_temperature_sensor(sensor_loc, sensor_period, sensor_size); } else if(sensor_type == 'A') { // External Audio Sensor s = create_audio_sensor(sensor_loc, sensor_period, sensor_size); } else if(sensor_type == 'L') { // External Light Sensor s = create_light_sensor(sensor_loc, sensor_period, sensor_size); } if(s) { aux_sensor[sensor_index] = s; if(aux_sensor_enabled[sensor_index]) sensor_enable_this(aux_sensor[sensor_index]); } } } } time_delete(&sensor_period); } return SUCCESS; }