|
| 1 | + |
| 2 | + |
| 3 | +//Base status goes from Rover-Mode (LED off), surveying in (blinking), to survey is complete/trasmitting RTCM (solid) |
| 4 | +typedef enum |
| 5 | +{ |
| 6 | + BASE_OFF = 0, |
| 7 | + BASE_SURVEYING_IN_NOTSTARTED, //User has indicated base, but current pos accuracy is too low |
| 8 | + BASE_SURVEYING_IN_SLOW, |
| 9 | + BASE_SURVEYING_IN_FAST, |
| 10 | + BASE_TRANSMITTING, |
| 11 | +} BaseState; |
| 12 | + |
| 13 | + |
| 14 | +void menuMain() |
| 15 | +{ |
| 16 | + while (1) |
| 17 | + { |
| 18 | + Serial.println(); |
| 19 | + Serial.println("Menu:"); |
| 20 | + Serial.println("1) Connect to RTK2GO"); |
| 21 | + |
| 22 | + byte incoming = getByteChoice(30); //Timeout after x seconds |
| 23 | + |
| 24 | + if (incoming == '1') |
| 25 | + { |
| 26 | + Serial.println("Xmit to RTK2Go. Press key to stop"); |
| 27 | + delay(10); //Wait for any serial to arrive |
| 28 | + while (Serial.available()) Serial.read(); //Flush |
| 29 | + |
| 30 | + WiFiClient client; |
| 31 | + |
| 32 | + while (Serial.available() == 0) |
| 33 | + { |
| 34 | + //Spin until we have a frame ready |
| 35 | + //Serial.print("Getting frame"); |
| 36 | + while (toCastFrameStale == true) |
| 37 | + { |
| 38 | + //Serial.print("."); |
| 39 | + delay(100); |
| 40 | + } |
| 41 | + |
| 42 | + //Connect if we are not already |
| 43 | + if (client.connected() == false) |
| 44 | + { |
| 45 | + Serial.printf("Opening socket to %s\n", casterHost); |
| 46 | + |
| 47 | + if (client.connect(casterHost, casterPort) == true) //Attempt connection |
| 48 | + { |
| 49 | + Serial.printf("Connected to %s:%d\n", casterHost, casterPort); |
| 50 | + |
| 51 | + const int SERVER_BUFFER_SIZE = 512; |
| 52 | + char serverBuffer[SERVER_BUFFER_SIZE]; |
| 53 | + |
| 54 | + snprintf(serverBuffer, SERVER_BUFFER_SIZE, "SOURCE %s /%s\r\nSource-Agent: NTRIP %s/v%d.%d\r\n\r\n", |
| 55 | + mntpnt_pw, mntpnt, ntrip_server_name, FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR); |
| 56 | + |
| 57 | + Serial.printf("Sending credentials:\n%s\n", serverBuffer); |
| 58 | + client.write(serverBuffer, strlen(serverBuffer)); |
| 59 | + |
| 60 | + //Wait for response |
| 61 | + unsigned long timeout = millis(); |
| 62 | + while (client.available() == 0) |
| 63 | + { |
| 64 | + if (millis() - timeout > 5000) |
| 65 | + { |
| 66 | + Serial.println(">>> Client Timeout !"); |
| 67 | + client.stop(); |
| 68 | + return; |
| 69 | + } |
| 70 | + delay(10); |
| 71 | + } |
| 72 | + |
| 73 | + //Check reply |
| 74 | + bool connectionSuccess = false; |
| 75 | + char response[512]; |
| 76 | + int responseSpot = 0; |
| 77 | + while (client.available()) |
| 78 | + { |
| 79 | + response[responseSpot++] = client.read(); |
| 80 | + if (strstr(response, "200") > 0) //Look for 'ICY 200 OK' |
| 81 | + connectionSuccess = true; |
| 82 | + if (responseSpot == 512 - 1) break; |
| 83 | + } |
| 84 | + response[responseSpot] = '\0'; |
| 85 | + //Serial.printf("RTK2Go response: %s", response); |
| 86 | + |
| 87 | + if (connectionSuccess == false) |
| 88 | + { |
| 89 | + Serial.printf("Failed to connect to RTK2Go: %s", response); |
| 90 | + } |
| 91 | + } //End attempt to connect |
| 92 | + else |
| 93 | + { |
| 94 | + Serial.println("Connection to host failed"); |
| 95 | + } |
| 96 | + } //End connected == false |
| 97 | + |
| 98 | + if (client.connected() == true) |
| 99 | + { |
| 100 | + //We are already connected so now push RTCM frame |
| 101 | + client.write(toCastBuffer, castSpot); |
| 102 | + serverBytesSent += castSpot; |
| 103 | + toCastFrameStale = true; |
| 104 | + Serial.printf("Sent: %d / Total sent: %d\n", castSpot, serverBytesSent); |
| 105 | + } |
| 106 | + |
| 107 | + //Close socket if we don't have new data for 10s |
| 108 | + if (millis() - lastReceivedCastFrame_ms > maxTimeBeforeHangup_ms) |
| 109 | + { |
| 110 | + Serial.println("RTCM timeout. Disconnecting..."); |
| 111 | + client.stop(); |
| 112 | + } |
| 113 | + |
| 114 | + delay(10); |
| 115 | + } |
| 116 | + |
| 117 | + Serial.println("User pressed a key"); |
| 118 | + Serial.println("Disconnecting..."); |
| 119 | + client.stop(); |
| 120 | + } |
| 121 | + else if (incoming == '2') |
| 122 | + { |
| 123 | + Serial.println("Hi"); |
| 124 | + } |
| 125 | + else if (incoming == 0xFF) |
| 126 | + break; |
| 127 | + else |
| 128 | + { |
| 129 | + Serial.printf("Unknown: 0x%02X\n", incoming); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + while (Serial.available()) Serial.read(); //Empty buffer of any newline chars |
| 134 | +} |
| 135 | + |
| 136 | +int baseState = BASE_TRANSMITTING; |
| 137 | + |
| 138 | +//If the ZED has any new NMEA data, pass it out over Bluetooth |
| 139 | +//Task for reading data from the GNSS receiver. |
| 140 | +void F9PSerialReadTask(void *e) |
| 141 | +{ |
| 142 | + while (true) |
| 143 | + { |
| 144 | + //If we are in base transmitting mode and user wants to serve to client, check if 100ms has passed without new serial. |
| 145 | + //This indicates end of RTCM frame. This presumes NMEA and RAWX sentences are turned off, and 1Hz |
| 146 | + if (baseState == BASE_TRANSMITTING) //&& settings.serveToCaster == true) |
| 147 | + { |
| 148 | + if (toCastFrameComplete == false) |
| 149 | + { |
| 150 | + if (millis() - lastReceivedRTCM_ms > 800) |
| 151 | + { |
| 152 | + toCastFrameComplete = true; |
| 153 | + toCastFrameStale = false; //ntripServer() will now post buffer next socket connection |
| 154 | + lastReceivedCastFrame_ms = millis(); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (GPS.available()) |
| 160 | + { |
| 161 | + auto s = GPS.readBytes(rBuffer, SERIAL_SIZE_RX); |
| 162 | + |
| 163 | + //If we are in base transmitting mode and user wants to serve to client, then fill toCastBuffer |
| 164 | + if (baseState == BASE_TRANSMITTING) //&& settings.serveToCaster == true) |
| 165 | + { |
| 166 | + if (toCastFrameComplete == true) //Start of a new frame so flush old data |
| 167 | + { |
| 168 | + castSpot = 0; |
| 169 | + toCastFrameComplete = false; |
| 170 | + } |
| 171 | + |
| 172 | + lastReceivedRTCM_ms = millis(); |
| 173 | + |
| 174 | + //Append latest incoming UART bytes (should be RTCM) to outgoing cast buffer |
| 175 | + if (castSpot + s >= TOCAST_BUFFER_SIZE) |
| 176 | + { |
| 177 | + Serial.println("Overrun"); |
| 178 | + for (int x = 0 ; x < TOCAST_BUFFER_SIZE ; x++) |
| 179 | + Serial.write(toCastBuffer[x]); |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + for (int x = 0 ; x < s ; x++) |
| 184 | + { |
| 185 | + toCastBuffer[castSpot] = rBuffer[x]; |
| 186 | + castSpot++; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + // else if (SerialBT.connected()) |
| 191 | + // { |
| 192 | + // SerialBT.write(rBuffer, s); |
| 193 | + // } |
| 194 | + } |
| 195 | + taskYIELD(); |
| 196 | + } |
| 197 | +} |
0 commit comments