From eefd943e818c891cc39355646366cbf8800a271a Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Thu, 6 Apr 2023 17:32:33 -0400 Subject: [PATCH] Progress commit --- lib/SyRen50/src/Syren50.cpp | 14 +++--- platformio.ini | 12 ++--- src/creds.h.txt | 16 ------- src/main.cpp | 87 ++++++++++++++++++------------------- 4 files changed, 56 insertions(+), 73 deletions(-) delete mode 100644 src/creds.h.txt diff --git a/lib/SyRen50/src/Syren50.cpp b/lib/SyRen50/src/Syren50.cpp index d2cdd47..3bb2d21 100644 --- a/lib/SyRen50/src/Syren50.cpp +++ b/lib/SyRen50/src/Syren50.cpp @@ -6,17 +6,17 @@ #define TXD2 17 void Syren50::init() { - Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); + Serial1.begin(9600); delay(1000); // Give about 1 second to boot up, per datasheet - Serial2.write(0b10101010); + Serial1.write(0b10101010); } void Syren50::command(uint8_t command, uint8_t data) { - Serial2.write(0x80); // Address - Serial2.write(command); // Command - Serial2.write(data); - Serial2.write((0x80 + command + data) & 0b01111111); // Checksum - Serial2.flush(); + Serial1.write(0x80); // Address + Serial1.write(command); // Command + Serial1.write(data); + Serial1.write((0x80 + command + data) & 0b01111111); // Checksum + Serial1.flush(); delay(5); } diff --git a/platformio.ini b/platformio.ini index 9ace56b..b4cccf5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,12 +10,12 @@ [platformio] -default_envs = esp32dev -name = WiFiTrainReceiver +default_envs = leonardo +name = TrainReceiver -[env:esp32dev] -platform = espressif32 +[env:leonardo] +platform = atmelavr framework = arduino -board = esp32dev +board = leonardo lib_deps = - ArduinoJson \ No newline at end of file + jgromes/RadioLib@^5.7.0 diff --git a/src/creds.h.txt b/src/creds.h.txt deleted file mode 100644 index 279730e..0000000 --- a/src/creds.h.txt +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef CREDS_H -#define CREDS_H - -/* - -INSTRUCTIONS: - -Change the text inside of the " characters to contain your SSID and Password for your WiFi -After that, rename this file from ending in .txt to .h and compile. - -*/ - -#define WIFI_SSID "YOUR SSID HERE" -#define WIFI_PASS "YOUR PASSWORD HERE!" - -#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f182043..c52e59b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,29 +1,32 @@ #include "Arduino.h" -#include "WiFi.h" -#include -#include "creds.h" -#include -#include #include "Syren50.h" +#include "string.h" +#include using namespace std; Syren50 motor; +// RF69 has the following connections: +// CS pin: 10 +// DIO0 pin: 2 +// RESET pin: 3 +RF69 radio = new Module(10, 2, 3); -void initWiFi() { - WiFi.mode(WIFI_STA); - WiFi.begin(WIFI_SSID, WIFI_PASS); - Serial.print("Connecting to WiFi .."); - while (WiFi.status() != WL_CONNECTED) { - Serial.print('.'); - delay(1000); - } - Serial.println(WiFi.localIP()); +void radioInit() { + Serial.print(F("[RF69] Initializing ... ")); + int state = radio.begin(918.5, 300.0, 60.0, 250.0, 10, 32); + if (state == RADIOLIB_ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } } void setup() { Serial.begin(115200); Serial.println("Begin!"); - initWiFi(); + radioInit(); motor.init(); } @@ -41,38 +44,34 @@ void inertia(float throttle, int8_t direction, float &inertial_throttle, int8_t float motor_throttle; int8_t motor_direction; -void parse_json(String json_string, float &throttle, int8_t &direction) { - StaticJsonDocument<200> doc; - DeserializationError error = deserializeJson(doc, json_string); - // Test if parsing succeeds. - if (error) { - Serial.print(F("deserializeJson() failed: ")); - Serial.println(error.f_str()); - return; - } - // Parse inputs and return them - throttle = doc["throttle"]; - direction = (int8_t) doc["direction"]; +uint8_t failure_count = 0; + +void parse_string(String str, float &throttle, int8_t &direction) { + //int8_t index_comma = str.indexOf(","); + //String throttle_string = str.substring(0, index_comma); + //String direction_string = str.substring(index_comma); + //throttle = strtof(throttle_string); + //direction = (int8_t) strtod(direction_string); } void get_control_inputs(float &throttle, int8_t &direction) { - if(WiFi.status()== WL_CONNECTED){ - HTTPClient http; - String serverName = "http://10.0.2.148:2093/control"; - http.begin(serverName.c_str()); - int httpResponseCode = http.GET(); - - if (httpResponseCode == 200) { - String payload = http.getString(); - parse_json(payload, throttle, direction); - Serial.println("200"); - } else { - Serial.println("Error communicating with webserver. Got HTTP status code:"); - Serial.println(httpResponseCode); - } - http.end(); + String str; + int state = radio.receive(str); + if (state == RADIOLIB_ERR_NONE) { + Serial.print(F("[RF69] Data:\t\t")); + Serial.println(str); + Serial.print(F("[RF69] RSSI:\t\t")); + Serial.print(radio.getRSSI()); + Serial.println(F(" dBm")); + parse_string(str, throttle, direction); + } else if (state == RADIOLIB_ERR_RX_TIMEOUT) { + throttle = 0.0f; + direction = 0; + } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { + Serial.println(F("CRC error!")); } else { - Serial.println("WiFi not connected!"); + Serial.print(F("failed, code ")); + Serial.println(state); } } @@ -81,4 +80,4 @@ void loop() { inertia(throttle, direction, motor_throttle, motor_direction); motor.move(motor_direction, (uint8_t) (motor_throttle * 127.0)); delay(25); -} \ No newline at end of file +}