|
| 1 | +/* |
| 2 | + September 1st, 2020 |
| 3 | + SparkFun Electronics |
| 4 | + Nathan Seidle |
| 5 | +
|
| 6 | + This firmware runs the core of the SparkFun RTK Surveyor product. It runs on an ESP32 |
| 7 | + and communicates with the ZED-F9P. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "BluetoothSerial.h" |
| 11 | + |
| 12 | +BluetoothSerial SerialBT; |
| 13 | + |
| 14 | +//Hardware connections v10 |
| 15 | +const int statLED = 13; |
| 16 | +const int positionAccuracyLED_20mm = 13; //POSACC1 |
| 17 | +const int positionAccuracyLED_100mm = 15; //POSACC2 |
| 18 | +const int positionAccuracyLED_1000mm = 2; //POSACC3 |
| 19 | +const int baseStatusLED = 4; |
| 20 | +const int baseSwitch = 5; |
| 21 | +const int bluetoothStatusLED = 13; |
| 22 | + |
| 23 | +//Bluetooth status LED goes from off (LED off), no connection (blinking), to connected (solid) |
| 24 | +enum BluetoothState |
| 25 | +{ |
| 26 | + BT_OFF = 0, |
| 27 | + BT_ON_NOCONNECTION, |
| 28 | + BT_CONNECTED, |
| 29 | +}; |
| 30 | +volatile byte bluetoothState = BT_OFF; |
| 31 | + |
| 32 | +//Base status goes from Rover-Mode (LED off), surveying in (blinking), to survey is complete/trasmitting RTCM (solid) |
| 33 | +enum BaseState |
| 34 | +{ |
| 35 | + BASE_OFF = 0, |
| 36 | + BASE_SURVEYING_IN, |
| 37 | + BASE_TRANSMITTING, |
| 38 | +}; |
| 39 | +volatile byte baseState = BASE_OFF; |
| 40 | +uint32_t lastBluetoothLEDBlink = 0; |
| 41 | + |
| 42 | +uint32_t lastTime = 0; |
| 43 | + |
| 44 | +void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { |
| 45 | + if (event == ESP_SPP_SRV_OPEN_EVT) { |
| 46 | + Serial.println("Client Connected"); |
| 47 | + bluetoothState = BT_CONNECTED; |
| 48 | + digitalWrite(bluetoothStatusLED, HIGH); |
| 49 | + } |
| 50 | + |
| 51 | + if (event == ESP_SPP_CLOSE_EVT ) { |
| 52 | + Serial.println("Client disconnected"); |
| 53 | + bluetoothState = BT_ON_NOCONNECTION; |
| 54 | + digitalWrite(bluetoothStatusLED, LOW); |
| 55 | + lastBluetoothLEDBlink = millis(); |
| 56 | + } |
| 57 | + |
| 58 | + // if (event == ESP_SPP_WRITE_EVT ) { |
| 59 | + // Serial.println("W"); |
| 60 | + // if(Serial1.available()) SerialBT.write(Serial1.read()); |
| 61 | + // } |
| 62 | +} |
| 63 | + |
| 64 | +void setup() |
| 65 | +{ |
| 66 | + Serial.begin(115200); //UART0 for programming and debugging |
| 67 | + Serial1.begin(115200); //UART1 on pins 16/17 for SPP. The ZED-F9P will be configured to output NMEA over its UART1 at 115200bps. |
| 68 | + Serial.println("SparkFun RTK Surveyor v1.0"); |
| 69 | + |
| 70 | + pinMode(statLED, OUTPUT); |
| 71 | + pinMode(positionAccuracyLED_20mm, OUTPUT); |
| 72 | + pinMode(positionAccuracyLED_100mm, OUTPUT); |
| 73 | + pinMode(positionAccuracyLED_1000mm, OUTPUT); |
| 74 | + pinMode(baseStatusLED, OUTPUT); |
| 75 | + pinMode(bluetoothStatusLED, OUTPUT); |
| 76 | + pinMode(baseSwitch, INPUT_PULLUP); |
| 77 | + |
| 78 | + SerialBT.register_callback(callback); |
| 79 | + if (!SerialBT.begin("SparkFun RTK Surveyor")) |
| 80 | + { |
| 81 | + Serial.println("An error occurred initializing Bluetooth"); |
| 82 | + bluetoothState = BT_OFF; |
| 83 | + digitalWrite(bluetoothStatusLED, LOW); |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + Serial.println("Bluetooth initialized"); |
| 88 | + bluetoothState = BT_ON_NOCONNECTION; |
| 89 | + digitalWrite(bluetoothStatusLED, HIGH); |
| 90 | + lastBluetoothLEDBlink = millis(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void loop() |
| 95 | +{ |
| 96 | + //Update Bluetooth LED status |
| 97 | + if (bluetoothState == BT_ON_NOCONNECTION) |
| 98 | + { |
| 99 | + if (millis() - lastBluetoothLEDBlink > 500) |
| 100 | + { |
| 101 | + if (digitalRead(bluetoothStatusLED) == LOW) |
| 102 | + digitalWrite(bluetoothStatusLED, HIGH); |
| 103 | + else |
| 104 | + digitalWrite(bluetoothStatusLED, LOW); |
| 105 | + lastBluetoothLEDBlink = millis(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (bluetoothState == BT_CONNECTED) |
| 110 | + { |
| 111 | + yield(); |
| 112 | + SerialBT.write(Serial1.read()); |
| 113 | + yield(); |
| 114 | + |
| 115 | + if (digitalRead(bluetoothStatusLED) == LOW) |
| 116 | + digitalWrite(bluetoothStatusLED, HIGH); |
| 117 | + else |
| 118 | + digitalWrite(bluetoothStatusLED, LOW); |
| 119 | + } |
| 120 | + |
| 121 | + // } |
| 122 | + // else |
| 123 | + // { |
| 124 | + // Serial.println("No BT"); |
| 125 | + // for (int x = 0 ; x < 1000 ; x++) |
| 126 | + // { |
| 127 | + // delay(1); |
| 128 | + // yield(); |
| 129 | + // } |
| 130 | + // //delay(1000); |
| 131 | + // } |
| 132 | + delay(1); |
| 133 | + |
| 134 | +} |
0 commit comments