LOCAL void ICACHE_FLASH_ATTR finger_params_ready(finger_packet *packet) { char response[WEBSERVER_MAX_RESPONSE_LEN]; char data_str[WEBSERVER_MAX_VALUE]; json_data( response, MOD_FINGER, OK_STR, json_sprintf( data_str, "\"Address\" : \"0x%08X\", " "\"DBSize\" : %d, " "\"DBStored\" : %d, " "\"FirstFree\" : %d, " "\"SecurityLevel\" : %d, " "\"Mode\" : \"%s\"", finger_address(), finger_db_size(), finger_db_stored(), finger_first_free_id(), finger_security_level(), finger_mode_str(finger_current_mode) ), NULL ); user_event_raise(FINGER_URL, response); }
LOCAL void ICACHE_FLASH_ATTR finger_find_done(finger_packet *packet) { if (packet->data[0] != FINGER_OK) { finger_default(packet); finger_start_read(); return; } uint16 id = packet->data[1]*256 + packet->data[2]; uint16 score = packet->data[3]*256 + packet->data[4]; char response[WEBSERVER_MAX_VALUE]; char data[WEBSERVER_MAX_VALUE]; json_status( response, MOD_FINGER, "Match found", json_sprintf( data, "\"Match\" : {\"ID\" : %d, \"Score\" : %d}", id, score ) ); #if FINGER_DEBUG debug("FINGER: Match: ID = %d, Score = %d\n", id, score); #endif user_event_raise(FINGER_URL, response); finger_start_read(); }
LOCAL void ICACHE_FLASH_ATTR finger_save_done(finger_packet *packet) { finger_current_mode = FINGER_READ; if (packet->data[0] != FINGER_OK) { finger_default(packet); finger_frech_params(); finger_start_read(); return; } char response[WEBSERVER_MAX_VALUE]; char data[WEBSERVER_MAX_VALUE]; json_status( response, MOD_FINGER, "New fingerprint stored", json_sprintf(data, "\"ID\" : %d", finger_first_free_id()) ); #if FINGER_DEBUG debug("FINGER: Stored: ID = %d\n", finger_first_free_id()); #endif user_event_raise(FINGER_URL, response); finger_frech_params(); finger_start_read(); }
LOCAL void ICACHE_FLASH_ATTR mod_tc_mk2_read(i2c_config *config, char *response, bool poll) { poll = true; tc_config_data *config_data = (tc_config_data *)config->data; char address_str[MAX_I2C_ADDRESS]; json_i2c_address(address_str, config->address); i2c_status status = tc_read(config); if (status == I2C_OK) { char poll_str[WEBSERVER_MAX_VALUE]; if (poll) { json_poll_str(poll_str, tc_refresh / 1000, tc_each, tc_threshold); } else { poll_str[0] = '\0'; } char data_str[WEBSERVER_MAX_VALUE]; json_data( response, MOD_TC_MK2, OK_STR, json_sprintf( data_str, "\"Temperature\" : %s %s", config_data->temperature_str, poll_str ), address_str ); } else { json_error(response, MOD_TC_MK2, i2c_status_str(status), address_str); } }
LOCAL void ICACHE_FLASH_ATTR dimmer_response(i2c_config *config, char *response, bool poll) { dimmer_config_data *config_data = (dimmer_config_data *)config->data; char address_str[MAX_I2C_ADDRESS]; json_i2c_address(address_str, config->address); char poll_str[WEBSERVER_MAX_VALUE]; if (poll) { json_poll_str(poll_str, dimmer_refresh / 1000, dimmer_each, dimmer_threshold); } else { poll_str[0] = '\0'; } char data_str[WEBSERVER_MAX_VALUE]; json_data( response, DIMMER_STR, OK_STR, json_sprintf( data_str, "\"Relay\" : %d, " "\"Brightness\" : %d," "\"Current\" : %d" "%s", config_data->relay, config_data->brightness, config_data->current, poll_str ), address_str ); }
/* Make response */ LOCAL void ICACHE_FLASH_ATTR mb_pcd8544_set_response(char *response, bool is_fault, uint8 req_type) { char data_str[WEBSERVER_MAX_RESPONSE_LEN]; char full_device_name[USER_CONFIG_USER_SIZE]; mb_make_full_device_name(full_device_name, MB_PCD8544_DEVICE, USER_CONFIG_USER_SIZE); MB_PCD8544_DEBUG("PCD8544:Resp.prep:%d;isFault:%d:\n", req_type, is_fault) // Sensor fault if (is_fault) { json_error(response, full_device_name, DEVICE_STATUS_FAULT, NULL); } // POST request - status & config only else if (req_type == MB_REQTYPE_POST) { json_status(response, full_device_name, DEVICE_STATUS_OK, json_sprintf( data_str, "\"Config\" : {" "\"Reset_pin\": %d," "\"Sce_pin\":%d," "\"Dc_pin\": %d," "\"Sdin_pin\": %d," "\"Sclk_pin\": %d" "}", mb_p_pcd8544_config->resetPin, mb_p_pcd8544_config->scePin, mb_p_pcd8544_config->dcPin, mb_p_pcd8544_config->sdinPin, mb_p_pcd8544_config->sclkPin ) ); // normal event measurement } else { json_data( response, full_device_name, DEVICE_STATUS_OK, json_sprintf(data_str, "\"pcd8544\": {" "}" ), NULL ); } }
LOCAL void ICACHE_FLASH_ATTR user_relay_state(char *response) { char data_str[WEBSERVER_MAX_VALUE]; json_data( response, ESP8266, OK_STR, json_sprintf( data_str, "\"Relay\" : %d", relay_state ), NULL ); }
char ICACHE_FLASH_ATTR *json_data(char *buffer, const char *device, const char *status, const char *data, const char *extra) { uint32 extra_len = extra == NULL ? 0 : os_strlen(extra); char data_buff[os_strlen(data) + extra_len + 20]; return json_status( buffer, device, status, json_sprintf( data_buff, "\"Data\" : {%s}%s%s", data, extra_len == 0 ? "" : ", ", extra_len == 0 ? "" : extra ) ); }
void ICACHE_FLASH_ATTR user_event_system_timer() { char status[WEBSERVER_MAX_RESPONSE_LEN]; char data[WEBSERVER_MAX_RESPONSE_LEN]; user_event_raise( NULL, json_status( status, ESP8266, "SysTimer", json_sprintf( data, "\"Timer\": %d", system_get_time() ) ) ); }
LOCAL void ICACHE_FLASH_ATTR user_switch1_state(char *response) { char data_str[WEBSERVER_MAX_VALUE]; json_data( response, SWITCH1_STR, OK_STR, json_sprintf( data_str, "\"Relay\" : %d, " "\"Switch\" : %d, " "\"Preference\" : %d ", switch1_hardware[0].state, switch1_hardware[1].state, switch1_hardware[1].preference ), NULL ); }
LOCAL void ICACHE_FLASH_ATTR button_set_response(char *state) { char response[WEBSERVER_MAX_VALUE]; debug("BUTTON: %s\n", state); char data[WEBSERVER_MAX_VALUE]; json_data( response, ESP8266, OK_STR, json_sprintf( data, "\"Button\" : \"%s\"", state ), NULL ); user_event_raise(BUTTON_URL, response); }
void ICACHE_FLASH_ATTR user_event_server_disconnected(const char *msg) { char data[WEBSERVER_MAX_VALUE]; char buff[WEBSERVER_MAX_VALUE]; user_websocket_event( USER_CONFIG_IOT_URL, json_data( data, ESP8266, DISCONNECTED, json_sprintf( buff, "\"Message\": \"%s\"", msg ), NULL ), NULL ); }
void ICACHE_FLASH_ATTR wifi_scan_get_result(char *response) { if (wifi_scan_in_progress) { webserver_set_status(0); return; } if (wifi_scan_ap_count == 0 || wifi_scan_result == NULL) { if (wifi_scan_start()) { webserver_set_status(0); return; } webserver_set_status(200); json_error(response, ESP8266, "Can not start scan", NULL); return; } webserver_set_status(200); char result[WEBSERVER_MAX_VALUE*wifi_scan_ap_count]; os_memset(result, '\0', sizeof(result)); uint8 i=0; for (i=0; i<wifi_scan_ap_count; i++) { os_sprintf( result + os_strlen(result), "%s{\"SSID\" : \"%s\", \"Strength\" : %d, \"Mode\" : \"%s\"}", i > 0 ? ", " : "", wifi_scan_result[i]->ssid, wifi_scan_result[i]->rssi, wifi_auth_mode_str(wifi_scan_result[i]->authmode) ); } char data_str[WEBSERVER_MAX_VALUE*wifi_scan_ap_count]; json_data( response, ESP8266, OK_STR, json_sprintf( data_str, "\"WiFi\" : [%s]", result ), NULL ); }
LOCAL void ICACHE_FLASH_ATTR user_switch1_state(char *response) { char data_str[WEBSERVER_MAX_VALUE]; uint8 i; for (i=0; i<SWITCH_COUNT; i++) { if (switch1_hardware[i].type == SWITCH1_SWITCH) { switch1_hardware[i].state = GPIO_INPUT_GET(GPIO_ID_PIN(switch1_hardware[i].gpio.gpio_id)); } } json_data( response, SWITCH1_STR, OK_STR, json_sprintf( data_str, "\"Relay\" : %d, " "\"Switch\" : %d ", switch1_hardware[0].state, switch1_hardware[1].state ), NULL ); }
LOCAL void ICACHE_FLASH_ATTR adc_read(char *response, bool poll) { char data[WEBSERVER_MAX_VALUE]; char poll_str[WEBSERVER_MAX_VALUE]; poll = true; state = system_adc_read(); if (poll) { json_poll_str(poll_str, adc_refresh / 1000, adc_each, adc_threshold); } else { poll_str[0] = '\0'; } json_data( response, ESP8266, OK_STR, json_sprintf( data, "\"ADC\" : {\"Value\" : %d %s}", state, poll_str ), NULL ); }
void ICACHE_FLASH_ATTR mod_led_8x8_rgb_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { if (mod_led_8x8_text == NULL) { mod_led_8x8_text = (char *)os_zalloc(MOD_LED_8x8_RGB_MAX_TEXT); } if (method == POST && data != NULL && data_len != 0) { if (led_8x8_rgb_busy()) { json_error(response, MOD_LED8x8RGB, BUSY_STR, NULL); return; } if (mod_led_8x8_rgb_parse(data, data_len)) { if (!led_8x8_rgb_set_dimensions(mod_led_8x8_cols, mod_led_8x8_rows)) { json_error(response, MOD_LED8x8RGB, "Dimensions can not be set", NULL); return; } mod_led_8x8_rgb_preferences_set(); } if ( !led_8x8_rgb_scroll( mod_led_8x8_r, mod_led_8x8_g, mod_led_8x8_b, mod_led_8x8_text, MOD_LED_8x8_RGB_MAX_SPEED - mod_led_8x8_speed + 1, mod_led_8x8_rgb_scroll_done ) ) { json_error(response, MOD_LED8x8RGB, BUSY_STR, NULL); return; } } char data_str[WEBSERVER_MAX_VALUE * 2]; char *escaped = json_escape_str(mod_led_8x8_text, MOD_LED_8x8_RGB_MAX_TEXT); json_data( response, MOD_LED8x8RGB, OK_STR, json_sprintf( data_str, "\"cols\" : %d, " "\"rows\" : %d, " "\"Speed\" : %d, " "\"R\" : %d, " "\"G\" : %d, " "\"B\" : %d, " "\"Text\" : \"%s\"", led_8x8_rgb_get_cols(), led_8x8_rgb_get_rows(), mod_led_8x8_speed, mod_led_8x8_r, mod_led_8x8_g, mod_led_8x8_b, escaped ), NULL ); os_free(escaped); }
void ICACHE_FLASH_ATTR mod_irda_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { i2c_status status = I2C_OK; i2c_config *config = i2c_init_handler(MOD_IRDA, MOD_IRDA_URL, irda_init, url, response); if (config == NULL) { return; } irda_config_data *config_data = (irda_config_data *)config->data; struct jsonparse_state parser; int type; if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Mode") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); if (jsonparse_strcmp_value(&parser, "SIRC") == 0) { config_data->mode = 1; } else { config_data->mode = 0; } } else if (jsonparse_strcmp_value(&parser, "Device") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); config_data->device = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "Command") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); config_data->command = jsonparse_get_value_as_int(&parser); } } } status = irda_set(config); } char address_str[MAX_I2C_ADDRESS]; json_i2c_address(address_str, config->address); if (status == I2C_OK) { char data_str[WEBSERVER_MAX_VALUE]; json_data( response, MOD_IRDA, OK_STR, json_sprintf( data_str, "\"Mode\" : \"%s\", " "\"Device\" : %d, " "\"Command\" : %d ", irda_mode_str(config_data->mode), config_data->device, config_data->command ), address_str ); } else { json_error(response, MOD_IRDA, i2c_status_str(status), address_str); } }
void ICACHE_FLASH_ATTR emtr_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { if (device_get_uart() != UART_EMTR) { json_error(response, MOD_EMTR, DEVICE_NOT_FOUND, NULL); return; } if (emtr_registers.calibration == NULL) { emtr_registers.calibration = (emtr_calibration_registers *)os_zalloc(sizeof(emtr_calibration_registers)); } if (emtr_registers.event == NULL) { emtr_registers.event = (emtr_event_registers *)os_zalloc(sizeof(emtr_event_registers)); } struct jsonparse_state parser; int type; bool set_counter = false; emtr_mode mode = emtr_current_mode; _uint64_ counter_active = emtr_counter_active(); _uint64_ counter_apparent = emtr_counter_apparent(); if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "Mode") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); if (jsonparse_strcmp_value(&parser, "Log") == 0) { emtr_current_mode = EMTR_LOG; } else if (jsonparse_strcmp_value(&parser, "Configure") == 0) { emtr_current_mode = EMTR_CONFIGURE; } else if (jsonparse_strcmp_value(&parser, "Calibration") == 0) { emtr_current_mode = EMTR_CALIBRATION; } } else if (jsonparse_strcmp_value(&parser, "ReadInterval") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_read_interval = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "CounterActive") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); counter_active = jsonparse_get_value_as_int(&parser); set_counter = true; } else if (jsonparse_strcmp_value(&parser, "CounterApparent") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); counter_apparent = jsonparse_get_value_as_int(&parser); set_counter = true; } if (mode == EMTR_CONFIGURE) { if (jsonparse_strcmp_value(&parser, "OverCurrentLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_current_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverPowerLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_power_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverFrequencyLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_frequency_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "UnderFrequencyLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->under_frequency_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverTemperatureLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_temperature_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "UnderTemperatureLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->under_temperature_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "VoltageSagLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->voltage_sag_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "VoltageSurgeLimit") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->voltage_surge_limit = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverCurrentHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_current_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverPowerHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_power_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverFrequencyHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_frequency_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "UnderFrequencyHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->under_frequency_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "OverTemperatureHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->over_temperature_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "UnderTemperatureHold") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->under_temperature_hold = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "EventEnable") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->event_enable = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "EventMaskCritical") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->event_mask_critical = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "EventMaskStandard") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->event_mask_standard = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "EventTest") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->event_test = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "EventClear") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); emtr_registers.event->event_clear = jsonparse_get_value_as_int(&parser); } } } } if (mode == EMTR_CONFIGURE) { emtr_set_event(emtr_registers.event, NULL); } if (set_counter) { emtr_set_counter(counter_active, counter_apparent, NULL); } } char data_str[WEBSERVER_MAX_RESPONSE_LEN]; if (emtr_current_mode == EMTR_CALIBRATION) { json_data( response, MOD_EMTR, OK_STR, json_sprintf( data_str, "\"Address\" : \"0x%04X\", " "\"Mode\" : \"%s\", " "\"CounterActive\" : %d, " "\"CounterApparent\" : %d, " "\"ReadInterval\" : %d, " "\"GainCurrentRMS\" : %d, " "\"GainVoltageRMS\" : %d, " "\"GainActivePower\" : %d, " "\"GainReactivePower\" : %d, " "\"OffsetCurrentRMS\" : %d, " "\"OffsetActivePower\" : %d, " "\"OffsetReactivePower\" : %d, " "\"DCOffsetCurrent\" : %d, " "\"PhaseCompensation\" : %d, " "\"ApparentPowerDivisor\" : %d, " "\"SystemConfiguration\" : \"0x%08X\", " "\"DIOConfiguration\" : \"0x%04X\", " "\"Range\" : \"0x%08X\", " "\"CalibrationCurrent\" : %d, " "\"CalibrationVoltage\" : %d, " "\"CalibrationActivePower\" : %d, " "\"CalibrationReactivePower\" : %d, " "\"AccumulationInterval\" : %d", emtr_address(), emtr_mode_str(emtr_current_mode), emtr_counter_active(), emtr_counter_apparent(), emtr_read_interval, emtr_registers.calibration->gain_current_rms, emtr_registers.calibration->gain_voltage_rms, emtr_registers.calibration->gain_active_power, emtr_registers.calibration->gain_reactive_power, emtr_registers.calibration->offset_current_rms, emtr_registers.calibration->offset_active_power, emtr_registers.calibration->offset_reactive_power, emtr_registers.calibration->dc_offset_current, emtr_registers.calibration->phase_compensation, emtr_registers.calibration->apparent_power_divisor, emtr_registers.calibration->system_configuration, emtr_registers.calibration->dio_configuration, emtr_registers.calibration->range, emtr_registers.calibration->calibration_current, emtr_registers.calibration->calibration_voltage, emtr_registers.calibration->calibration_active_power, emtr_registers.calibration->calibration_reactive_power, emtr_registers.calibration->accumulation_interval ), NULL ); setTimeout(emtr_calibration_read, NULL, 1500); } else if (emtr_current_mode == EMTR_CONFIGURE) { json_data( response, MOD_EMTR, OK_STR, json_sprintf( data_str, "\"Address\" : \"0x%04X\", " "\"Mode\" : \"%s\", " "\"CounterActive\" : %d, " "\"CounterApparent\" : %d, " "\"ReadInterval\" : %d, " "\"OverCurrentLimit\" : %d, " "\"OverPowerLimit\" : %d, " "\"OverFrequencyLimit\" : %d, " "\"UnderFrequencyLimit\" : %d, " "\"OverTemperatureLimit\" : %d, " "\"UnderTemperatureLimit\" : %d, " "\"VoltageSagLimit\" : %d, " "\"VoltageSurgeLimit\" : %d, " "\"OverCurrentHold\" : %d, " "\"OverPowerHold\" : %d, " "\"OverFrequencyHold\" : %d, " "\"UnderFrequencyHold\" : %d, " "\"OverTemperatureHold\" : %d, " "\"UnderTemperatureHold\" : %d, " "\"EventEnable\" : %d, " "\"EventMaskCritical\" : %d, " "\"EventMaskStandard\" : %d, " "\"EventTest\" : %d, " "\"EventClear\" : %d", emtr_address(), emtr_mode_str(emtr_current_mode), emtr_counter_active(), emtr_counter_apparent(), emtr_read_interval, emtr_registers.event->over_current_limit, emtr_registers.event->over_power_limit, emtr_registers.event->over_frequency_limit, emtr_registers.event->under_frequency_limit, emtr_registers.event->over_temperature_limit, emtr_registers.event->under_temperature_limit, emtr_registers.event->voltage_sag_limit, emtr_registers.event->voltage_surge_limit, emtr_registers.event->over_current_hold, emtr_registers.event->over_power_hold, emtr_registers.event->over_frequency_hold, emtr_registers.event->under_frequency_hold, emtr_registers.event->over_temperature_hold, emtr_registers.event->under_temperature_hold, emtr_registers.event->event_enable, emtr_registers.event->event_mask_critical, emtr_registers.event->event_mask_standard, emtr_registers.event->event_test, emtr_registers.event->event_clear ), NULL ); setTimeout(emtr_events_read, NULL, 1500); } else { json_data( response, MOD_EMTR, OK_STR, json_sprintf( data_str, "\"Address\" : \"0x%04X\", " "\"Mode\" : \"%s\", " "\"CounterActive\" : %d, " "\"CounterApparent\" : %d, " "\"ReadInterval\" : %d", emtr_address(), emtr_mode_str(emtr_current_mode), emtr_counter_active(), emtr_counter_apparent(), emtr_read_interval ), NULL ); } emtr_start_read(); }
LOCAL void ICACHE_FLASH_ATTR emtr_read_done(emtr_packet *packet) { LOCAL emtr_output_registers *registers = NULL; LOCAL uint32 time = 0; LOCAL uint32 interval = 0; LOCAL uint32 now = 0; now = system_get_time(); interval = (time != 0) ? (now - time) / 1000 : 0 ; time = now; if (registers == NULL) { registers = (emtr_output_registers *)os_zalloc(sizeof(emtr_output_registers)); } emtr_parse_output(packet, registers); char event_str[20]; os_memset(event_str, 0, sizeof(event_str)); char response[WEBSERVER_MAX_RESPONSE_LEN]; char data_str[WEBSERVER_MAX_RESPONSE_LEN]; json_data( response, MOD_EMTR, OK_STR, json_sprintf( data_str, "\"Address\" : \"0x%04X\", " "\"CounterActive\" : %d, " "\"CounterApparent\" : %d, " "\"Interval\" : %d, " "\"CurrentRMS\" : %d, " "\"VoltageRMS\" : %d, " "\"ActivePower\" : %d, " "\"ReactivePower\" : %d, " "\"ApparentPower\" : %d, " "\"PowerFactor\" : %d, " "\"LineFrequency\" : %d, " "\"ThermistorVoltage\" : %d, " "\"EventFlag\" : %d, " "\"SystemStatus\" : \"0x%04X\"", emtr_address(), emtr_counter_active(), emtr_counter_apparent(), interval, registers->current_rms, registers->voltage_rms, registers->active_power, registers->reactive_power, registers->apparent_power, registers->power_factor, registers->line_frequency, registers->thermistor_voltage, registers->event_flag, registers->system_status ), NULL ); emtr_counter_add( registers->active_power * interval / 1000, registers->apparent_power * interval / 1000 ); user_event_raise(EMTR_URL, response); emtr_start_read(); if (registers->event_flag != 0) { emtr_clear_event(registers->event_flag, NULL); } }
void ICACHE_FLASH_ATTR mod_rgb_handler( struct espconn *pConnection, request_method method, char *url, char *data, uint16 data_len, uint32 content_len, char *response, uint16 response_len ) { i2c_status status; i2c_config *config = i2c_init_handler(MOD_RGB, MOD_RGB_URL, rgb_init, url, response); if (config == NULL) { return; } rgb_config_data *config_data = (rgb_config_data *)config->data; struct jsonparse_state parser; int type; if (method == POST && data != NULL && data_len != 0) { jsonparse_setup(&parser, data, data_len); while ((type = jsonparse_next(&parser)) != 0) { if (type == JSON_TYPE_PAIR_NAME) { if (jsonparse_strcmp_value(&parser, "R") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); config_data->red = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "G") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); config_data->green = jsonparse_get_value_as_int(&parser); } else if (jsonparse_strcmp_value(&parser, "B") == 0) { jsonparse_next(&parser); jsonparse_next(&parser); config_data->blue = jsonparse_get_value_as_int(&parser); } } } } char address_str[MAX_I2C_ADDRESS]; json_i2c_address(address_str, config->address); status = rgb_set(config); if (status == I2C_OK) { char data_str[WEBSERVER_MAX_VALUE]; json_data( response, MOD_RGB, OK_STR, json_sprintf( data_str, "\"R\" : %d, \"G\" : %d, \"B\" : %d", config_data->red, config_data->green, config_data->blue ), address_str ); } else { json_error(response, MOD_RGB, i2c_status_str(status), address_str); } }