From b95de116ea0d6374501a81b36f4766cdd7c4536f Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Mon, 7 Oct 2024 14:58:40 +0900 Subject: [PATCH] update --- .../data/config.json | 2 +- .../src/main.cpp | 45 +++---- .../data/config.json | 3 +- .../src/main.cpp | 120 ++++-------------- 4 files changed, 46 insertions(+), 124 deletions(-) diff --git a/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/data/config.json b/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/data/config.json index 337826f2..8b4564fc 100644 --- a/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/data/config.json +++ b/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/data/config.json @@ -1,4 +1,4 @@ { - "device_name": "SDP Servo", + "device_name": "Elev. Internal panel", "uwb_id": -1 } \ No newline at end of file diff --git a/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/src/main.cpp b/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/src/main.cpp index bbdd34e8..16bf5e77 100644 --- a/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/src/main.cpp +++ b/sketchbooks/sdp_m5stickcplus_elevator_internal_panel/src/main.cpp @@ -41,16 +41,11 @@ String device_name; uint8_t mac_address[6] = {0}; // Interface -std::string packet_description_control = "Servo control"; +std::string packet_description_control = "Target floor"; std::string serialization_format_control = "i"; SDPInterfaceDescription interface_description_control = std::make_tuple(packet_description_control, serialization_format_control); -// Light Status -std::string packet_description_status = "Servo status (cur, min, max)"; -std::string serialization_format_status = "iii"; -std::vector body_status; - // UWB int uwb_id = -1; std::string packet_description_uwb = "UWB Station"; @@ -60,6 +55,8 @@ std::vector body_uwb; // int current_target = 0; int current_angle = 0; +bool press = false; +unsigned long last_pressed_sec = 0; // Other std::vector data; @@ -92,23 +89,13 @@ void init_servo() { } void callback_for_servo_control(const uint8_t* mac_address, const std::vector& body) { - Serial.printf("Length of body: %d\n", body.size()); - current_target = std::get(body[0]); - Serial.printf("Target: %d\n", current_target); + int32_t target_floor = std::get(body[0]); + if (target_floor == 2) { + press = true; + last_pressed_sec = millis() / 1000; + } } -// void setup() { -// M5.begin(true, true, true); -// init_servo(); -// } - -// void loop() { -// for (int i = SERVO_MIN_ANGLE; i <= SERVO_MAX_ANGLE; i = i + 10) { -// move_servo(i); -// delay(1000); -// } -// } - void setup() { // put your setup code here, to run once: M5.begin(true, true, true); @@ -159,6 +146,14 @@ void setup() { void loop() { delay(100); + if (press) { + current_target = 180; + if (millis() / 1000 - last_pressed_sec > 5) { + press = false; + current_target = 0; + } + } + // current angle update if (current_target < SERVO_MIN_ANGLE) { current_target = SERVO_MIN_ANGLE; @@ -181,14 +176,6 @@ void loop() { move_servo(current_angle); Serial.printf("Current target: %d, angle: %d\n", current_target, current_angle); // Send SDP Data - body_status.clear(); - body_status.push_back(SDPData((int32_t)current_angle)); - body_status.push_back(SDPData((int32_t)SERVO_MIN_ANGLE)); - body_status.push_back(SDPData((int32_t)SERVO_MAX_ANGLE)); - if (not send_sdp_data_packet(packet_description_status, body_status)) { - Serial.printf("Failed to send SDP data packet\n"); - Serial.printf("packet description is %s\n", packet_description_status.c_str()); - } if (uwb_id >= 0) { if (not send_sdp_data_packet(packet_description_uwb, body_uwb)) { Serial.printf("Failed to send SDP data packet\n"); diff --git a/sketchbooks/sdp_switchbot_elevator_button/data/config.json b/sketchbooks/sdp_switchbot_elevator_button/data/config.json index 41e4db2a..a01d870b 100644 --- a/sketchbooks/sdp_switchbot_elevator_button/data/config.json +++ b/sketchbooks/sdp_switchbot_elevator_button/data/config.json @@ -4,7 +4,8 @@ "wifi_password": "", "switchbot_token": "", "switchbot_secret": "", - "switchbot_device_id": "", + "switchbot_upward_device_id": "", + "switchbot_downward_device_id": "", "uwb_id": -1, "comment": "this is configuration for sdp_switchbot_light" } \ No newline at end of file diff --git a/sketchbooks/sdp_switchbot_elevator_button/src/main.cpp b/sketchbooks/sdp_switchbot_elevator_button/src/main.cpp index b068fda9..b4bbff15 100644 --- a/sketchbooks/sdp_switchbot_elevator_button/src/main.cpp +++ b/sketchbooks/sdp_switchbot_elevator_button/src/main.cpp @@ -27,8 +27,8 @@ String device_name; uint8_t mac_address[6] = {0}; // Interface -std::string packet_description_control = "Light control"; -std::string serialization_format_control = "?"; +std::string packet_description_control = "Elevator call"; +std::string serialization_format_control = "s"; SDPInterfaceDescription interface_description_control = std::make_tuple(packet_description_control, serialization_format_control); @@ -46,9 +46,10 @@ std::vector body_uwb; // Switchbot Client Configuration String wifi_ssid = ""; String wifi_password = ""; -String switchbot_device_id = ""; String switchbot_token = ""; String switchbot_secret = ""; +String switchbot_upward_device_id = ""; +String switchbot_downward_device_id = ""; // Port configuration M5StackSerialPortInfo port_info_m5atoms3 = M5StackSerialPortInfoList[PORT_A]; @@ -57,24 +58,6 @@ M5StackSerialPortInfo port_info_uwb = M5StackSerialPortInfoList[PORT_C]; // Other std::vector data; StaticJsonDocument<1024> result_json; -int loop_counter = 0; - -String get_bot_status_and_update_buf() { - Serial.printf("Get switchbot status\n"); - String result = send_serial_command( - String("") + "{\"command\":\"get_device_status\"," + "\"device_id\":\"" + switchbot_device_id + "\"}\n", 5000); - DeserializationError error = deserializeJson(result_json, result); - if (error or (result_json.containsKey("success") and not result_json["success"].as())) { - Serial.printf("deserializeJson() failed or get_device_status failed: %s, result: %s\n", error.c_str(), - result.c_str()); - return ""; - } else { - String power = result_json["result"]["body"]["power"]; - body_status.clear(); - body_status.push_back(SDPData(power == "on" ? true : false)); - return power; - } -} bool load_config_from_FS(fs::FS& fs, String filename = "/config.json") { StaticJsonDocument<1024> doc; @@ -82,9 +65,14 @@ bool load_config_from_FS(fs::FS& fs, String filename = "/config.json") { return false; } - if (not doc.containsKey("device_name") or not doc.containsKey("wifi_ssid") or not doc.containsKey("wifi_password") or - not doc.containsKey("switchbot_token") or not doc.containsKey("switchbot_secret") or - not doc.containsKey("switchbot_device_id") or not doc.containsKey("uwb_id")) { + if (not doc.containsKey("device_name") or + not doc.containsKey("wifi_ssid") or + not doc.containsKey("wifi_password") or + not doc.containsKey("switchbot_token") or + not doc.containsKey("switchbot_secret") or + not doc.containsKey("switchbot_upward_device_id") or + not doc.containsKey("switchbot_downward_device_id") or + not doc.containsKey("uwb_id")) { return false; } @@ -93,44 +81,31 @@ bool load_config_from_FS(fs::FS& fs, String filename = "/config.json") { wifi_password = doc["wifi_password"].as(); switchbot_token = doc["switchbot_token"].as(); switchbot_secret = doc["switchbot_secret"].as(); - switchbot_device_id = doc["switchbot_device_id"].as(); + switchbot_upward_device_id = doc["switchbot_upward_device_id"].as(); + switchbot_downward_device_id = doc["switchbot_downward_device_id"].as(); uwb_id = doc["uwb_id"].as(); return true; } -void callback_for_switch_control(const uint8_t* mac_address, const std::vector& body) { - unsigned int timeout = 10; +void callback_for_elevator_panel_control(const uint8_t* mac_address, const std::vector& body) { Serial.printf("Length of body: %d\n", body.size()); - bool control = std::get(body[0]); - Serial.printf("Light Control Command: %s\n", control ? "ON" : "OFF"); - if (control) { - Serial.printf("Turn On the light.\n"); + std::string target = std::get(body[0]); + if (target == "up") { + Serial.printf("Press upward button\n"); String ret = send_serial_command(String("") + "{\"command\":\"send_device_command\"," + "\"device_id\":\"" + - switchbot_device_id + "\"," + "\"sb_command_type\":\"command\"," + - "\"sb_command\":\"turnOn\"}\n", + switchbot_upward_device_id + "\"," + "\"sb_command_type\":\"command\"," + + "\"sb_command\":\"press\"}\n", 10000); Serial.printf("Response: %s\n", ret.c_str()); - } else { - Serial.printf("Turn Off the light.\n"); - Serial2.printf( - "{\"command\":\"send_device_command\",\"device_id\":\"%s\",\"sb_command_type\":\"command\",\"sb_command\":" - "\"turnOff\"}\n", - switchbot_device_id.c_str()); + } else if (target == "down") { + Serial.printf("Press downward button\n"); String ret = send_serial_command(String("") + "{\"command\":\"send_device_command\"," + "\"device_id\":\"" + - switchbot_device_id + "\"," + "\"sb_command_type\":\"command\"," + - "\"sb_command\":\"turnOff\"}\n", + switchbot_downward_device_id + "\"," + "\"sb_command_type\":\"command\"," + + "\"sb_command\":\"press\"}\n", 10000); Serial.printf("Response: %s\n", ret.c_str()); - } - Serial.printf("Light Control Command Done\n"); - int deadline = millis() / 1000 + timeout; - while (millis() / 1000 < deadline) { - Serial.printf("Fetching result."); - String power = get_bot_status_and_update_buf(); - if (power == (control ? "on" : "off")) { - break; - } - delay(1000); + } else { + Serial.printf("Unknown command\n"); } } @@ -164,7 +139,7 @@ void setup() { delay(1000); } } - register_sdp_interface_callback(interface_description_control, callback_for_switch_control); + register_sdp_interface_callback(interface_description_control, callback_for_elevator_panel_control); Serial.println("SDP Initialized!"); // Show device info @@ -216,41 +191,6 @@ void setup() { void loop() { delay(5000); - // Run dummy callback if Serial available - if (Serial.available()) { - uint8_t buf_dummy[240]; - data.clear(); - String str = Serial.readStringUntil('\n'); - Serial.printf("Input: %s\n", str.c_str()); - if (str.indexOf("turnOn") != -1) { - data.push_back(SDPData(true)); - } else if (str.indexOf("turnOff") != -1) { - data.push_back(SDPData(false)); - } else { - Serial.printf("Unknown command. Buffer clearing...\n"); - auto timeout = millis() + 5000; - while (millis() < timeout) { - delay(100); - if (Serial2.available()) { - Serial.println(String("response: ") + Serial2.readString()); - } - } - return; - } - std::string serialization_format = get_serialization_format(data); - bool result = generate_data_frame(buf_dummy, packet_description_control.c_str(), data); - if (not result) { - Serial.printf("Failed to generate data frame\n"); - return; - } else { - Serial.println("Generate data frame"); - } - Serial.printf("Dummy callback calling\n"); - _OnDataRecv(NULL, buf_dummy, sizeof(buf_dummy)); - Serial.printf("Dummy callback called\n"); - return; - } - // Send SDP Data if (not send_sdp_data_packet(packet_description_status, body_status)) { Serial.printf("Failed to send SDP data packet\n"); @@ -262,10 +202,4 @@ void loop() { Serial.printf("packet description is %s\n", packet_description_uwb.c_str()); } } - - // Get switchbot status - if (loop_counter % 50 == 0) { - get_bot_status_and_update_buf(); - } - loop_counter++; }