From 931c5bb104aac219b9d7cd0b6bf8ac923da0fc49 Mon Sep 17 00:00:00 2001 From: Greg McGuire Date: Fri, 7 Jul 2023 17:42:12 -0400 Subject: [PATCH] Debugging WebSockets lib --- Firmware/RTK_Surveyor/McityOS.ino | 310 ++++++------------------- Firmware/RTK_Surveyor/RTK_Surveyor.ino | 12 +- Firmware/RTK_Surveyor/Tasks.ino | 28 ++- Firmware/RTK_Surveyor/settings.h | 4 +- 4 files changed, 96 insertions(+), 258 deletions(-) diff --git a/Firmware/RTK_Surveyor/McityOS.ino b/Firmware/RTK_Surveyor/McityOS.ino index df1cc36ec..e5d803368 100644 --- a/Firmware/RTK_Surveyor/McityOS.ino +++ b/Firmware/RTK_Surveyor/McityOS.ino @@ -6,266 +6,104 @@ */ -// void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) { -// switch(type) { -// case sIOtype_DISCONNECT: -// USE_SERIAL.printf(F"[IOc] Disconnected!\n"); -// break; -// case sIOtype_CONNECT: -// { -// USE_SERIAL.printf(F"[IOc] Connected to url: %s\n", payload); - -// // join default namespace (no auto join in Socket.IO V3) -// socketIO.send(sIOtype_CONNECT, "/octane"); - -// // Create and send auth event -// DynamicJsonDocument doc(1024); -// JsonArray array = doc.to(); - -// array.add("auth"); - -// // add payload (parameters) for the event -// JsonObject param1 = array.createNestedObject(); -// param1["x-api-key"] = settings.mcityOSAPIKey; - -// // JSON to String (serializion) -// String output; -// serializeJson(doc, output); - -// // Send event -// socketIO.sendEVENT(output); -// } -// break; -// case sIOtype_EVENT: -// { -// char * sptr = NULL; -// int id = strtol((char *)payload, &sptr, 10); -// USE_SERIAL.printf(F"[IOc] get event: %s id: %d\n", payload, id); -// if(id) { -// payload = (uint8_t *)sptr; -// } -// DynamicJsonDocument doc(1024); -// DeserializationError error = deserializeJson(doc, payload, length); -// if(error) { -// USE_SERIAL.print(F("deserializeJson() failed: ")); -// USE_SERIAL.println(error.c_str()); -// return; -// } - -// String eventName = doc[0]; -// USE_SERIAL.printf(F"[IOc] event name: %s\n", eventName.c_str()); - -// // Message Includes a ID for a ACK (callback) -// if(id) { -// // creat JSON message for Socket.IO (ack) -// DynamicJsonDocument docOut(1024); -// JsonArray array = docOut.to(); - -// // add payload (parameters) for the ack (callback function) -// JsonObject param1 = array.createNestedObject(); -// param1["now"] = millis(); - -// // JSON to String (serializion) -// String output; -// output += id; -// serializeJson(docOut, output); - -// // Send event -// socketIO.send(sIOtype_ACK, output); -// } -// } -// break; -// case sIOtype_ACK: -// USE_SERIAL.printf(F"[IOc] get ack: %u\n", length); -// break; -// case sIOtype_ERROR: -// USE_SERIAL.printf(F"[IOc] get error: %u\n", length); -// break; -// case sIOtype_BINARY_EVENT: -// USE_SERIAL.printf(F"[IOc] get binary: %u\n", length); -// break; -// case sIOtype_BINARY_ACK: -// USE_SERIAL.printf(F"[IOc] get binary ack: %u\n", length); -// break; -// } -// } - -// void socketIOEvent(WebsocketsEvent event, String data) { -// if(event == WebsocketsEvent::ConnectionOpened) { -// Serial.println("Connnection Opened"); -// } else if(event == WebsocketsEvent::ConnectionClosed) { -// Serial.println("Connnection Closed"); -// } else if(event == WebsocketsEvent::GotPing) { -// Serial.println("Got a Ping!"); -// } else if(event == WebsocketsEvent::GotPong) { -// Serial.println("Got a Pong!"); -// } - - -// switch(event) { -// case WebsocketsEvent::ConnectionClosed: -// Serial.printf(F"[IOc] Disconnected!\n"); -// break; -// case WebsocketsEvent::ConnectionOpened: -// { -// Serial.printf(F"[IOc] Connected to url: %s\n", payload); +void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) { + switch(type) { + case sIOtype_DISCONNECT: + USE_SERIAL.printf("[IOc] Disconnected!\n"); + break; + case sIOtype_CONNECT: + { + USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload); -// // join default namespace (no auto join in Socket.IO V3) -// socketIO.send(sIOtype_CONNECT, "/octane"); + // join default namespace (no auto join in Socket.IO V3) + socketIO.send(sIOtype_CONNECT, "/octane"); -// // Create and send auth event -// DynamicJsonDocument doc(1024); -// JsonArray array = doc.to(); + // Create and send auth event + DynamicJsonDocument doc(1024); + JsonArray array = doc.to(); -// array.add("auth"); + array.add("auth"); -// // add payload (parameters) for the event -// JsonObject param1 = array.createNestedObject(); -// param1["x-api-key"] = settings.mcityOSAPIKey; + // add payload (parameters) for the event + JsonObject param1 = array.createNestedObject(); + param1["x-api-key"] = settings.mcityOSAPIKey; -// // JSON to String (serializion) -// String output; -// serializeJson(doc, output); + // JSON to String (serializion) + String output; + serializeJson(doc, output); -// // Send event -// socketIO.sendEVENT(output); -// } -// break; -// case sIOtype_EVENT: -// { -// char * sptr = NULL; -// int id = strtol((char *)payload, &sptr, 10); -// USE_SERIAL.printf(F"[IOc] get event: %s id: %d\n", payload, id); -// if(id) { -// payload = (uint8_t *)sptr; -// } -// DynamicJsonDocument doc(1024); -// DeserializationError error = deserializeJson(doc, payload, length); -// if(error) { -// USE_SERIAL.print(F("deserializeJson() failed: ")); -// USE_SERIAL.println(error.c_str()); -// return; -// } - -// String eventName = doc[0]; -// USE_SERIAL.printf(F"[IOc] event name: %s\n", eventName.c_str()); - -// // Message Includes a ID for a ACK (callback) -// if(id) { -// // creat JSON message for Socket.IO (ack) -// DynamicJsonDocument docOut(1024); -// JsonArray array = docOut.to(); - -// // add payload (parameters) for the ack (callback function) -// JsonObject param1 = array.createNestedObject(); -// param1["now"] = millis(); - -// // JSON to String (serializion) -// String output; -// output += id; -// serializeJson(docOut, output); + // Send event + socketIO.sendEVENT(output); + } + break; + case sIOtype_EVENT: + { + char * sptr = NULL; + int id = strtol((char *)payload, &sptr, 10); + USE_SERIAL.printf("[IOc] get event: %s id: %d\n", payload, id); + if(id) { + payload = (uint8_t *)sptr; + } + DynamicJsonDocument doc(1024); + DeserializationError error = deserializeJson(doc, payload, length); + if(error) { + USE_SERIAL.print(F("deserializeJson() failed: ")); + USE_SERIAL.println(error.c_str()); + return; + } -// // Send event -// socketIO.send(sIOtype_ACK, output); -// } -// } -// break; -// case sIOtype_ACK: -// USE_SERIAL.printf(F"[IOc] get ack: %u\n", length); -// break; -// case sIOtype_ERROR: -// USE_SERIAL.printf(F"[IOc] get error: %u\n", length); -// break; -// case sIOtype_BINARY_EVENT: -// USE_SERIAL.printf(F"[IOc] get binary: %u\n", length); -// break; -// case sIOtype_BINARY_ACK: -// USE_SERIAL.printf(F"[IOc] get binary ack: %u\n", length); -// break; -// } -// } + String eventName = doc[0]; + USE_SERIAL.printf("[IOc] event name: %s\n", eventName.c_str()); -void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { + // Message Includes a ID for a ACK (callback) + if(id) { + // creat JSON message for Socket.IO (ack) + DynamicJsonDocument docOut(1024); + JsonArray array = docOut.to(); + // add payload (parameters) for the ack (callback function) + JsonObject param1 = array.createNestedObject(); + param1["now"] = millis(); - switch(type) { - case WStype_DISCONNECTED: - USE_SERIAL.printf("[WSc] Disconnected!\n"); - break; - case WStype_CONNECTED: - { - USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); + // JSON to String (serializion) + String output; + output += id; + serializeJson(docOut, output); - // send message to server when Connected - //webSocket.sendTXT("Connected"); + // Send event + socketIO.send(sIOtype_ACK, output); } + } break; - case WStype_TEXT: - USE_SERIAL.printf("[WSc] get text: %s\n", payload); - - // send message to server - // webSocket.sendTXT("message here"); + case sIOtype_ACK: + USE_SERIAL.printf("[IOc] get ack: %u\n", length); break; - case WStype_BIN: - USE_SERIAL.printf("[WSc] get binary length: %u\n", length); - //hexdump(payload, length); - - // send data to server - // webSocket.sendBIN(payload, length); + case sIOtype_ERROR: + USE_SERIAL.printf("[IOc] get error: %u\n", length); + break; + case sIOtype_BINARY_EVENT: + USE_SERIAL.printf("[IOc] get binary: %u\n", length); + break; + case sIOtype_BINARY_ACK: + USE_SERIAL.printf("[IOc] get binary ack: %u\n", length); break; - case WStype_ERROR: - case WStype_FRAGMENT_TEXT_START: - case WStype_FRAGMENT_BIN_START: - case WStype_FRAGMENT: - case WStype_FRAGMENT_FIN: - break; } } void setupMcityOS() { - if (settings.enableMcityOS != true) + if (settings.enableMcityOS != true) { + Serial.println("McityOS not enabled"); return; + } + + Serial.println("McityOS enabled, establishing websocket connection"); // server address, port and URL - //socketIO.beginSSL(settings.mcityOSServer, 443, "/socket.io/?EIO=4"); - //wsclient.connect(settings.mcityOSServer, 443, "/"); - wsclient.beginSSL(settings.mcityOSServer, 443, "/"); + socketIO.beginSSL(settings.mcityOSServer, 443, "/socket.io/?EIO=4"); // event handler - //socketIO.onEvent(socketIOEvent); - wsclient.onEvent(webSocketEvent); + socketIO.onEvent(socketIOEvent); } -//unsigned long messageTimestamp = 0; -//void loop() { -// socketIO.loop(); -// -//// uint64_t now = millis(); -//// -//// if(now - messageTimestamp > 2000) { -//// messageTimestamp = now; -//// -//// // create JSON message for Socket.IO (event) -//// DynamicJsonDocument doc(1024); -//// JsonArray array = doc.to(); -//// -//// // add event name -//// // Hint: socket.on('event_name', .... -//// array.add("event_name"); -//// -//// // add payload (parameters) for the event -//// JsonObject param1 = array.createNestedObject(); -//// param1["now"] = (uint32_t) now; -//// -//// // JSON to String (serializion) -//// String output; -//// serializeJson(doc, output); -//// -//// // Send event -//// socketIO.sendEVENT(output); -//// -//// // Print JSON for debugging -//// USE_SERIAL.println(output); -//// } -//} + diff --git a/Firmware/RTK_Surveyor/RTK_Surveyor.ino b/Firmware/RTK_Surveyor/RTK_Surveyor.ino index 12a4ea181..a3469db3b 100644 --- a/Firmware/RTK_Surveyor/RTK_Surveyor.ino +++ b/Firmware/RTK_Surveyor/RTK_Surveyor.ino @@ -40,7 +40,7 @@ */ const int FIRMWARE_VERSION_MAJOR = 1; -const int FIRMWARE_VERSION_MINOR = 4; +const int FIRMWARE_VERSION_MINOR = 5; //Define the RTK board identifier: // This is an int which is unique to this variant of the RTK Surveyor hardware which allows us @@ -146,7 +146,7 @@ uint32_t casterResponseWaitStartTime = 0; //Used to detect if caster service tim //Websocket connection to Mcity OS //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include -// + #include #include // @@ -155,7 +155,9 @@ uint32_t casterResponseWaitStartTime = 0; //Used to detect if caster service tim //SocketIOclient socketIO; //#include -WebSocketsClient wsclient; +//WebSocketsClient wsclient; +SocketIOclient socketIO; + //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= @@ -387,9 +389,9 @@ void loop() // if (settings.enableMcityOS == true && wsclient.available()) if (settings.enableMcityOS == true) - wsclient.loop(); +// wsclient.loop(); // wsclient.poll(); -// socketIO.loop(); + socketIO.loop(); //Menu system via ESP32 USB connection if (Serial.available()) menuMain(); //Present user menu diff --git a/Firmware/RTK_Surveyor/Tasks.ino b/Firmware/RTK_Surveyor/Tasks.ino index aaf15c889..67487aedc 100644 --- a/Firmware/RTK_Surveyor/Tasks.ino +++ b/Firmware/RTK_Surveyor/Tasks.ino @@ -38,16 +38,16 @@ void McityOSF9PSerialReadTask(void *e) dtostrf(nmea.getSpeed() / 1000., 0, 3, speed_str); dtostrf(nmea.getCourse() / 1000., 0, 3, heading_str); - // sprintf_P(json_psm, PSTR("[\"v2x_PSM\", {\"id\": 1, \"payload\": {" - // "\"messageSet\": \"J2735_201603\"," - // "\"id\": \"0010BEEF\"," - // "\"type\": \"pedestrian\"," - // "\"size\": \"small\"," - // "\"latitude\": %s," - // "\"longitude\": %s," - // "\"elevation\": %s," - // "\"speed\": %s," - // "\"heading\": %s}}]"), latitude_str, longitude_str, elevation_str, speed_str, heading_str); + sprintf_P(json_psm, PSTR("[\"v2x_PSM\", {\"id\": 1, \"payload\": {" + "\"messageSet\": \"J2735_201603\"," + "\"id\": \"0010BEEF\"," + "\"type\": \"pedestrian\"," + "\"size\": \"small\"," + "\"latitude\": %s," + "\"longitude\": %s," + "\"elevation\": %s," + "\"speed\": %s," + "\"heading\": %s}}]"), latitude_str, longitude_str, elevation_str, speed_str, heading_str); v2xMessageAvailable = true; } @@ -67,12 +67,10 @@ void McityOSSendV2XTask(void *e) v2xMessageAvailable = false; // Send event -// if (wsclient.available()) - if (wsclient.isConnected()) { - Serial.println("Sending test"); - wsclient.sendTXT("test"); + if (socketIO.isConnected()) { + //Serial.printf("Sending position: %s\n", json_psm); + socketIO.sendEVENT(json_psm, 0); } - // socketIO.sendEVENT("test"); } taskYIELD(); diff --git a/Firmware/RTK_Surveyor/settings.h b/Firmware/RTK_Surveyor/settings.h index 71e4cd150..267c42592 100644 --- a/Firmware/RTK_Surveyor/settings.h +++ b/Firmware/RTK_Surveyor/settings.h @@ -273,8 +273,8 @@ struct struct_settings { char mountPoint[50] = "MTF"; char mountPointUser[50] = "mtf"; char mountPointPW[50] = "Mcity"; - char wifiSSID[50] = "Mcity"; - char wifiPW[50] = "splb0splb0"; + char wifiSSID[50] = "Mcity-2.4"; + char wifiPW[50] = "automated2015"; bool enableMcityOS = true; char mcityOSServer[50] = "octane.mvillage.um.city"; char mcityOSAPIKey[20] = "reticulatingsplines";