Progress commit

This commit is contained in:
Jared Dunbar 2023-04-06 17:32:33 -04:00
parent 13a9539efe
commit eefd943e81
4 changed files with 56 additions and 73 deletions

View File

@ -6,17 +6,17 @@
#define TXD2 17 #define TXD2 17
void Syren50::init() { void Syren50::init() {
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); Serial1.begin(9600);
delay(1000); // Give about 1 second to boot up, per datasheet 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) { void Syren50::command(uint8_t command, uint8_t data) {
Serial2.write(0x80); // Address Serial1.write(0x80); // Address
Serial2.write(command); // Command Serial1.write(command); // Command
Serial2.write(data); Serial1.write(data);
Serial2.write((0x80 + command + data) & 0b01111111); // Checksum Serial1.write((0x80 + command + data) & 0b01111111); // Checksum
Serial2.flush(); Serial1.flush();
delay(5); delay(5);
} }

View File

@ -10,12 +10,12 @@
[platformio] [platformio]
default_envs = esp32dev default_envs = leonardo
name = WiFiTrainReceiver name = TrainReceiver
[env:esp32dev] [env:leonardo]
platform = espressif32 platform = atmelavr
framework = arduino framework = arduino
board = esp32dev board = leonardo
lib_deps = lib_deps =
ArduinoJson jgromes/RadioLib@^5.7.0

View File

@ -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

View File

@ -1,29 +1,32 @@
#include "Arduino.h" #include "Arduino.h"
#include "WiFi.h"
#include <HTTPClient.h>
#include "creds.h"
#include <string>
#include <ArduinoJson.h>
#include "Syren50.h" #include "Syren50.h"
#include "string.h"
#include <RadioLib.h>
using namespace std; using namespace std;
Syren50 motor; 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() { void radioInit() {
WiFi.mode(WIFI_STA); Serial.print(F("[RF69] Initializing ... "));
WiFi.begin(WIFI_SSID, WIFI_PASS); int state = radio.begin(918.5, 300.0, 60.0, 250.0, 10, 32);
Serial.print("Connecting to WiFi .."); if (state == RADIOLIB_ERR_NONE) {
while (WiFi.status() != WL_CONNECTED) { Serial.println(F("success!"));
Serial.print('.'); } else {
delay(1000); Serial.print(F("failed, code "));
Serial.println(state);
while (true);
} }
Serial.println(WiFi.localIP());
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Begin!"); Serial.println("Begin!");
initWiFi(); radioInit();
motor.init(); motor.init();
} }
@ -41,38 +44,34 @@ void inertia(float throttle, int8_t direction, float &inertial_throttle, int8_t
float motor_throttle; float motor_throttle;
int8_t motor_direction; int8_t motor_direction;
void parse_json(String json_string, float &throttle, int8_t &direction) { uint8_t failure_count = 0;
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, json_string); void parse_string(String str, float &throttle, int8_t &direction) {
// Test if parsing succeeds. //int8_t index_comma = str.indexOf(",");
if (error) { //String throttle_string = str.substring(0, index_comma);
Serial.print(F("deserializeJson() failed: ")); //String direction_string = str.substring(index_comma);
Serial.println(error.f_str()); //throttle = strtof(throttle_string);
return; //direction = (int8_t) strtod(direction_string);
}
// Parse inputs and return them
throttle = doc["throttle"];
direction = (int8_t) doc["direction"];
} }
void get_control_inputs(float &throttle, int8_t &direction) { void get_control_inputs(float &throttle, int8_t &direction) {
if(WiFi.status()== WL_CONNECTED){ String str;
HTTPClient http; int state = radio.receive(str);
String serverName = "http://10.0.2.148:2093/control"; if (state == RADIOLIB_ERR_NONE) {
http.begin(serverName.c_str()); Serial.print(F("[RF69] Data:\t\t"));
int httpResponseCode = http.GET(); Serial.println(str);
Serial.print(F("[RF69] RSSI:\t\t"));
if (httpResponseCode == 200) { Serial.print(radio.getRSSI());
String payload = http.getString(); Serial.println(F(" dBm"));
parse_json(payload, throttle, direction); parse_string(str, throttle, direction);
Serial.println("200"); } 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 { } else {
Serial.println("Error communicating with webserver. Got HTTP status code:"); Serial.print(F("failed, code "));
Serial.println(httpResponseCode); Serial.println(state);
}
http.end();
} else {
Serial.println("WiFi not connected!");
} }
} }