Add response callback for OTA configuration and update handling; adjust hardware configurations
This commit is contained in:
+37
-23
@@ -16,7 +16,7 @@ HardwareRfid rfid(4, 5); // SS=D2 (GPIO4), RST=D1 (GPIO5)
|
||||
// Demo for HardwareSerial
|
||||
ProtoSerial serial;
|
||||
|
||||
hardware_LedConfig configs[4];
|
||||
hardware_LedConfig configs[2];
|
||||
unsigned long lastChange = 0;
|
||||
int currentConfig = 0;
|
||||
|
||||
@@ -27,34 +27,40 @@ void onRfidTag(const hardware_SensorToControlMessage& msg) {
|
||||
// Serial.println(msg.payload.rfid_reading.card_id, HEX);
|
||||
}
|
||||
|
||||
void onOtaResponse(bool success, const char* ip_address, const char* error_message) {
|
||||
hardware_SensorOTAEnableResponse response = {0};
|
||||
response.success = success;
|
||||
if (success) {
|
||||
strcpy(response.ip_address, ip_address);
|
||||
} else {
|
||||
strcpy(response.error_message, error_message);
|
||||
}
|
||||
|
||||
hardware_SensorToControlMessage responseMsg = {0};
|
||||
responseMsg.sensor_id = 0; // or some id
|
||||
responseMsg.which_payload = hardware_SensorToControlMessage_ota_response_tag;
|
||||
responseMsg.payload.ota_response = response;
|
||||
|
||||
led.set(configs[1]);
|
||||
|
||||
serial.sendMessage(responseMsg);
|
||||
}
|
||||
|
||||
void onSerialMessage(const IncomingMessage& msg) {
|
||||
if (msg.which_payload == hardware_ControlToSensorMessage_led_config_tag) {
|
||||
led.set(msg.payload.led_config);
|
||||
} else if (msg.which_payload == hardware_ControlToSensorMessage_ota_enable_tag) {
|
||||
hardware_SensorOTAEnableResponse response = {0};
|
||||
response.success = ota.configure(msg.payload.ota_enable);
|
||||
if (response.success) {
|
||||
IPAddress ip = WiFi.localIP();
|
||||
if (!msg.payload.ota_enable.as_station_mode) {
|
||||
ip = WiFi.softAPIP();
|
||||
}
|
||||
strcpy(response.ip_address, ip.toString().c_str());
|
||||
} else {
|
||||
strcpy(response.error_message, "Failed to configure OTA");
|
||||
}
|
||||
|
||||
hardware_SensorToControlMessage responseMsg = {0};
|
||||
responseMsg.sensor_id = msg.control_id; // or some id
|
||||
responseMsg.which_payload = hardware_SensorToControlMessage_ota_response_tag;
|
||||
responseMsg.payload.ota_response = response;
|
||||
|
||||
serial.sendMessage(responseMsg);
|
||||
ota.set(msg.payload.ota_enable);
|
||||
} else if (msg.which_payload == hardware_ControlToSensorMessage_restart_tag) {
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
ota.setResponseCallback(onOtaResponse);
|
||||
|
||||
led.begin();
|
||||
rfid.begin();
|
||||
rfid.setCallback(onRfidTag);
|
||||
@@ -63,10 +69,18 @@ void setup() {
|
||||
|
||||
// Static config
|
||||
configs[0] = {0};
|
||||
configs[0].brightness = 128;
|
||||
configs[0].duration_ms = 0;
|
||||
configs[0].which_animation_params = 3;
|
||||
configs[0].animation_params.static_params.color = 0x00FF00; // Green
|
||||
configs[0].brightness = 64;
|
||||
configs[0].which_animation_params = hardware_LedConfig_static_params_tag;
|
||||
configs[0].animation_params.static_params.color = 0x55FF00; // Soft green
|
||||
|
||||
// Pulse config
|
||||
configs[1] = {0};
|
||||
configs[1].brightness = 128;
|
||||
configs[1].duration_ms = 0;
|
||||
configs[1].which_animation_params = 4;
|
||||
configs[1].animation_params.pulse_params.color = 0xFF0000; // Red
|
||||
configs[1].animation_params.pulse_params.speed_ms = 500;
|
||||
|
||||
|
||||
|
||||
led.set(configs[0]);
|
||||
|
||||
Reference in New Issue
Block a user