Progress commit
This commit is contained in:
parent
13a9539efe
commit
eefd943e81
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
jgromes/RadioLib@^5.7.0
|
||||
|
|
|
|||
|
|
@ -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
|
||||
85
src/main.cpp
85
src/main.cpp
|
|
@ -1,29 +1,32 @@
|
|||
#include "Arduino.h"
|
||||
#include "WiFi.h"
|
||||
#include <HTTPClient.h>
|
||||
#include "creds.h"
|
||||
#include <string>
|
||||
#include <ArduinoJson.h>
|
||||
#include "Syren50.h"
|
||||
#include "string.h"
|
||||
#include <RadioLib.h>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue