Refactor code
This commit is contained in:
@@ -2,13 +2,16 @@
|
|||||||
|
|
||||||
Adafruit_BMP085 bmp180;
|
Adafruit_BMP085 bmp180;
|
||||||
|
|
||||||
void setup_180() {
|
void setup_180()
|
||||||
if (!bmp180.begin()) {
|
{
|
||||||
|
if (!bmp180.begin())
|
||||||
|
{
|
||||||
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
|
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_180(JsonObject &root) {
|
void loop_180(JsonObject &root)
|
||||||
|
{
|
||||||
JsonObject bmp388 = root.createNestedObject("bmp388");
|
JsonObject bmp388 = root.createNestedObject("bmp388");
|
||||||
/*
|
/*
|
||||||
Serial.print("Temperature = ");
|
Serial.print("Temperature = ");
|
||||||
|
|||||||
49
src/bno055.h
49
src/bno055.h
@@ -8,19 +8,29 @@ void displaySensorDetails(void)
|
|||||||
sensor_t sensor;
|
sensor_t sensor;
|
||||||
bno.getSensor(&sensor);
|
bno.getSensor(&sensor);
|
||||||
Serial.println("------------------------------------");
|
Serial.println("------------------------------------");
|
||||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
Serial.print("Sensor: ");
|
||||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
Serial.println(sensor.name);
|
||||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
Serial.print("Driver Ver: ");
|
||||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx");
|
Serial.println(sensor.version);
|
||||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx");
|
Serial.print("Unique ID: ");
|
||||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx");
|
Serial.println(sensor.sensor_id);
|
||||||
|
Serial.print("Max Value: ");
|
||||||
|
Serial.print(sensor.max_value);
|
||||||
|
Serial.println(" xxx");
|
||||||
|
Serial.print("Min Value: ");
|
||||||
|
Serial.print(sensor.min_value);
|
||||||
|
Serial.println(" xxx");
|
||||||
|
Serial.print("Resolution: ");
|
||||||
|
Serial.print(sensor.resolution);
|
||||||
|
Serial.println(" xxx");
|
||||||
Serial.println("------------------------------------");
|
Serial.println("------------------------------------");
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_bno(void)
|
void setup_bno(void)
|
||||||
{
|
{
|
||||||
Serial.println("Orientation Sensor Test"); Serial.println("");
|
Serial.println("Orientation Sensor Test");
|
||||||
|
Serial.println("");
|
||||||
|
|
||||||
/* Initialise the sensor */
|
/* Initialise the sensor */
|
||||||
if (!bno.begin())
|
if (!bno.begin())
|
||||||
@@ -37,9 +47,11 @@ void setup_bno(void)
|
|||||||
displaySensorDetails();
|
displaySensorDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printEvent(sensors_event_t* event, JsonObject obj) {
|
void printEvent(sensors_event_t *event, JsonObject obj)
|
||||||
|
{
|
||||||
double x = -1000000, y = -1000000, z = -1000000; //dumb values, easy to spot problem
|
double x = -1000000, y = -1000000, z = -1000000; //dumb values, easy to spot problem
|
||||||
if (event->type == SENSOR_TYPE_ACCELEROMETER) {
|
if (event->type == SENSOR_TYPE_ACCELEROMETER)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Accl:");
|
Serial.print("Accl:");
|
||||||
#endif
|
#endif
|
||||||
@@ -47,7 +59,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->acceleration.y;
|
y = event->acceleration.y;
|
||||||
z = event->acceleration.z;
|
z = event->acceleration.z;
|
||||||
}
|
}
|
||||||
else if (event->type == SENSOR_TYPE_ORIENTATION) {
|
else if (event->type == SENSOR_TYPE_ORIENTATION)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Orient:");
|
Serial.print("Orient:");
|
||||||
#endif
|
#endif
|
||||||
@@ -55,7 +68,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->orientation.y;
|
y = event->orientation.y;
|
||||||
z = event->orientation.z;
|
z = event->orientation.z;
|
||||||
}
|
}
|
||||||
else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) {
|
else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Mag:");
|
Serial.print("Mag:");
|
||||||
#endif
|
#endif
|
||||||
@@ -63,7 +77,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->magnetic.y;
|
y = event->magnetic.y;
|
||||||
z = event->magnetic.z;
|
z = event->magnetic.z;
|
||||||
}
|
}
|
||||||
else if (event->type == SENSOR_TYPE_GYROSCOPE) {
|
else if (event->type == SENSOR_TYPE_GYROSCOPE)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Gyro:");
|
Serial.print("Gyro:");
|
||||||
#endif
|
#endif
|
||||||
@@ -71,7 +86,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->gyro.y;
|
y = event->gyro.y;
|
||||||
z = event->gyro.z;
|
z = event->gyro.z;
|
||||||
}
|
}
|
||||||
else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) {
|
else if (event->type == SENSOR_TYPE_ROTATION_VECTOR)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Rot:");
|
Serial.print("Rot:");
|
||||||
#endif
|
#endif
|
||||||
@@ -79,7 +95,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->gyro.y;
|
y = event->gyro.y;
|
||||||
z = event->gyro.z;
|
z = event->gyro.z;
|
||||||
}
|
}
|
||||||
else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) {
|
else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Linear:");
|
Serial.print("Linear:");
|
||||||
#endif
|
#endif
|
||||||
@@ -87,7 +104,8 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
y = event->acceleration.y;
|
y = event->acceleration.y;
|
||||||
z = event->acceleration.z;
|
z = event->acceleration.z;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Unk:");
|
Serial.print("Unk:");
|
||||||
#endif
|
#endif
|
||||||
@@ -105,7 +123,6 @@ void printEvent(sensors_event_t* event, JsonObject obj) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop_bno(JsonObject &root)
|
void loop_bno(JsonObject &root)
|
||||||
{
|
{
|
||||||
JsonObject bno055 = root.createNestedObject("bno055");
|
JsonObject bno055 = root.createNestedObject("bno055");
|
||||||
|
|||||||
8
src/constants.h
Normal file
8
src/constants.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#define SEALEVELPRESSURE_HPA (1013.25)
|
||||||
|
#define NETWORK_NAME "Rocket"
|
||||||
|
#define NEWORK_PASSWORD ""
|
||||||
|
#define UDP_ADDRESS "192.168.1.255"
|
||||||
|
#define ALTITUDE_THRESHOLD 50.0
|
||||||
|
#define PRESSURE_THRESHOLD 3.0
|
||||||
|
#define START_ACCELERATION_THRESHOLD 5.0
|
||||||
|
#define TIME_TO_DEPLOY 8 * 1000
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static NMEAGPS gps;
|
static NMEAGPS gps;
|
||||||
gps_fix fix;
|
gps_fix fix;
|
||||||
|
|
||||||
@@ -18,7 +17,8 @@ void setup_gps()
|
|||||||
|
|
||||||
void loop_gps(JsonObject &root)
|
void loop_gps(JsonObject &root)
|
||||||
{
|
{
|
||||||
if (gps.available( gpsPort)) {
|
if (gps.available(gpsPort))
|
||||||
|
{
|
||||||
fix = gps.read();
|
fix = gps.read();
|
||||||
}
|
}
|
||||||
JsonObject gps = root.createNestedObject("gps");
|
JsonObject gps = root.createNestedObject("gps");
|
||||||
|
|||||||
16
src/lora.h
16
src/lora.h
@@ -28,7 +28,8 @@ const int8_t TXpower = 20; //LoRa transmit power in dBm
|
|||||||
|
|
||||||
SX127XLT LT;
|
SX127XLT LT;
|
||||||
|
|
||||||
void setup_lora() {
|
void setup_lora()
|
||||||
|
{
|
||||||
SPI.begin(SCK, MISO, MOSI, NSS);
|
SPI.begin(SCK, MISO, MOSI, NSS);
|
||||||
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE))
|
||||||
{
|
{
|
||||||
@@ -78,16 +79,19 @@ void sendLocation(int32_t Lat, int32_t Lon)
|
|||||||
LT.transmitSXBuffer(0, len, 10000, TXpower, NO_WAIT);
|
LT.transmitSXBuffer(0, len, 10000, TXpower, NO_WAIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loop_lora_pre()
|
||||||
void loop_lora_pre() {
|
{
|
||||||
if (gps.available( gpsPort) && digitalRead(DIO0)) {
|
if (gps.available(gpsPort) && digitalRead(DIO0))
|
||||||
|
{
|
||||||
fix = gps.read();
|
fix = gps.read();
|
||||||
sendLocation(fix.latitudeL(), fix.longitudeL());
|
sendLocation(fix.latitudeL(), fix.longitudeL());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_lora() {
|
void loop_lora()
|
||||||
if (digitalRead(DIO0)) {
|
{
|
||||||
|
if (digitalRead(DIO0))
|
||||||
|
{
|
||||||
sendLocation(fix.latitudeL(), fix.longitudeL());
|
sendLocation(fix.latitudeL(), fix.longitudeL());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/main.cpp
38
src/main.cpp
@@ -1,4 +1,4 @@
|
|||||||
#define SEALEVELPRESSURE_HPA (1013.25)
|
#include <constants.h>
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
@@ -15,14 +15,16 @@
|
|||||||
|
|
||||||
bool deployed = 0;
|
bool deployed = 0;
|
||||||
|
|
||||||
void deploy() {
|
void deploy()
|
||||||
|
{
|
||||||
digitalWrite(PWM, HIGH);
|
digitalWrite(PWM, HIGH);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
digitalWrite(PWM, LOW);
|
digitalWrite(PWM, LOW);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TaskDeploycode( void * pvParameters ){
|
void TaskDeploycode(void *pvParameters)
|
||||||
|
{
|
||||||
deploy();
|
deploy();
|
||||||
deployed = 1;
|
deployed = 1;
|
||||||
}
|
}
|
||||||
@@ -36,7 +38,8 @@ bool save = false;
|
|||||||
float previous_height;
|
float previous_height;
|
||||||
bool altitude = false;
|
bool altitude = false;
|
||||||
|
|
||||||
void setup(){
|
void setup()
|
||||||
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Wire.setClock(800000);
|
Wire.setClock(800000);
|
||||||
setup_sdcard();
|
setup_sdcard();
|
||||||
@@ -58,12 +61,15 @@ void setup(){
|
|||||||
pinMode(PWM, OUTPUT);
|
pinMode(PWM, OUTPUT);
|
||||||
|
|
||||||
uint8_t buffer[50] = "test.txt";
|
uint8_t buffer[50] = "test.txt";
|
||||||
while(1) {
|
while (1)
|
||||||
if (connected) {
|
{
|
||||||
|
if (connected)
|
||||||
|
{
|
||||||
bmp.performReading();
|
bmp.performReading();
|
||||||
original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA);
|
original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA);
|
||||||
udp.parsePacket();
|
udp.parsePacket();
|
||||||
if(udp.read(buffer, 50) > 0){
|
if (udp.read(buffer, 50) > 0)
|
||||||
|
{
|
||||||
Serial.print("Server to client: ");
|
Serial.print("Server to client: ");
|
||||||
Serial.println((char *)buffer);
|
Serial.println((char *)buffer);
|
||||||
start = millis();
|
start = millis();
|
||||||
@@ -78,7 +84,8 @@ void setup(){
|
|||||||
Serial.println(original_height);
|
Serial.println(original_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop()
|
||||||
|
{
|
||||||
DynamicJsonDocument doc(1024);
|
DynamicJsonDocument doc(1024);
|
||||||
JsonObject obj = doc.to<JsonObject>();
|
JsonObject obj = doc.to<JsonObject>();
|
||||||
obj["seconds"] = millis() / 1000;
|
obj["seconds"] = millis() / 1000;
|
||||||
@@ -95,18 +102,21 @@ void loop(){
|
|||||||
file.print('\n');
|
file.print('\n');
|
||||||
file.flush();
|
file.flush();
|
||||||
//only send data when connected
|
//only send data when connected
|
||||||
if(connected){
|
if (connected)
|
||||||
|
{
|
||||||
//Send a packet
|
//Send a packet
|
||||||
udp.beginPacket(udpAddress, udpPort);
|
udp.beginPacket(udpAddress, udpPort);
|
||||||
serializeJson(doc, udp);
|
serializeJson(doc, udp);
|
||||||
udp.println();
|
udp.println();
|
||||||
udp.endPacket();
|
udp.endPacket();
|
||||||
}
|
}
|
||||||
if (!save & !deployed & ((obj["bmp388"]["altitude"].as<float>() - original_height) > 50.0)) {
|
if (!save & !deployed & ((obj["bmp388"]["altitude"].as<float>() - original_height) > ALTITUDE_THRESHOLD))
|
||||||
|
{
|
||||||
Serial.println("Deploy is save");
|
Serial.println("Deploy is save");
|
||||||
save = true;
|
save = true;
|
||||||
}
|
}
|
||||||
if (save & !deployed & (((obj["bmp388"]["altitude"].as<float>() - previous_height) * 5) < 3.0)) {
|
if (save & !deployed & (((obj["bmp388"]["altitude"].as<float>() - previous_height) * 5) < PRESSURE_THRESHOLD))
|
||||||
|
{
|
||||||
Serial.println("Start deploy altitude");
|
Serial.println("Start deploy altitude");
|
||||||
altitude = true;
|
altitude = true;
|
||||||
deployed = true;
|
deployed = true;
|
||||||
@@ -115,12 +125,14 @@ void loop(){
|
|||||||
if (!flight & (max(max(abs(obj["bno055"]["linear_accel_data"]["x"].as<float>()),
|
if (!flight & (max(max(abs(obj["bno055"]["linear_accel_data"]["x"].as<float>()),
|
||||||
abs(obj["bno055"]["linear_accel_data"]["y"].as<float>())),
|
abs(obj["bno055"]["linear_accel_data"]["y"].as<float>())),
|
||||||
max(abs(obj["bno055"]["linear_accel_data"]["y"].as<float>()),
|
max(abs(obj["bno055"]["linear_accel_data"]["y"].as<float>()),
|
||||||
abs(obj["bno055"]["linear_accel_data"]["z"].as<float>()))) > 5.0)) {
|
abs(obj["bno055"]["linear_accel_data"]["z"].as<float>()))) > START_ACCELERATION_THRESHOLD))
|
||||||
|
{
|
||||||
Serial.println("Timer trigger");
|
Serial.println("Timer trigger");
|
||||||
start = millis();
|
start = millis();
|
||||||
flight = true;
|
flight = true;
|
||||||
}
|
}
|
||||||
if(flight & ((millis()-start) > 8*1000) & !deployed) { // Вот таймер
|
if (flight & ((millis() - start) > TIME_TO_DEPLOY) & !deployed)
|
||||||
|
{ // Вот таймер
|
||||||
Serial.println("Start deploy timeout");
|
Serial.println("Start deploy timeout");
|
||||||
deployed = 1;
|
deployed = 1;
|
||||||
digitalWrite(PWM, HIGH);
|
digitalWrite(PWM, HIGH);
|
||||||
|
|||||||
144
src/sdcard.h
144
src/sdcard.h
@@ -1,19 +1,22 @@
|
|||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#include "SD_MMC.h"
|
#include "SD_MMC.h"
|
||||||
|
|
||||||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.printf("Listing directory: %s\n", dirname);
|
Serial.printf("Listing directory: %s\n", dirname);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
File root = fs.open(dirname);
|
File root = fs.open(dirname);
|
||||||
if(!root){
|
if (!root)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Failed to open directory");
|
Serial.println("Failed to open directory");
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!root.isDirectory()){
|
if (!root.isDirectory())
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Not a directory");
|
Serial.println("Not a directory");
|
||||||
#endif
|
#endif
|
||||||
@@ -21,16 +24,21 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
|||||||
}
|
}
|
||||||
|
|
||||||
File file = root.openNextFile();
|
File file = root.openNextFile();
|
||||||
while(file){
|
while (file)
|
||||||
if(file.isDirectory()){
|
{
|
||||||
|
if (file.isDirectory())
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print(" DIR : ");
|
Serial.print(" DIR : ");
|
||||||
#endif
|
#endif
|
||||||
Serial.println(file.name());
|
Serial.println(file.name());
|
||||||
if(levels){
|
if (levels)
|
||||||
|
{
|
||||||
listDir(fs, file.name(), levels - 1);
|
listDir(fs, file.name(), levels - 1);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print(" FILE: ");
|
Serial.print(" FILE: ");
|
||||||
#endif
|
#endif
|
||||||
@@ -44,42 +52,52 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void createDir(fs::FS &fs, const char * path){
|
void createDir(fs::FS &fs, const char *path)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.printf("Creating Dir: %s\n", path);
|
Serial.printf("Creating Dir: %s\n", path);
|
||||||
#endif
|
#endif
|
||||||
if(fs.mkdir(path)){
|
if (fs.mkdir(path))
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Dir created");
|
Serial.println("Dir created");
|
||||||
#endif
|
#endif
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("mkdir failed");
|
Serial.println("mkdir failed");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeDir(fs::FS &fs, const char * path){
|
void removeDir(fs::FS &fs, const char *path)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.printf("Removing Dir: %s\n", path);
|
Serial.printf("Removing Dir: %s\n", path);
|
||||||
#endif
|
#endif
|
||||||
if(fs.rmdir(path)){
|
if (fs.rmdir(path))
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Dir removed");
|
Serial.println("Dir removed");
|
||||||
#endif
|
#endif
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("rmdir failed");
|
Serial.println("rmdir failed");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFile(fs::FS &fs, const char * path){
|
void readFile(fs::FS &fs, const char *path)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.printf("Reading file: %s\n", path);
|
Serial.printf("Reading file: %s\n", path);
|
||||||
#endif
|
#endif
|
||||||
File file = fs.open(path);
|
File file = fs.open(path);
|
||||||
if(!file){
|
if (!file)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Failed to open file for reading");
|
Serial.println("Failed to open file for reading");
|
||||||
#endif
|
#endif
|
||||||
@@ -88,79 +106,102 @@ void readFile(fs::FS &fs, const char * path){
|
|||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.print("Read from file: ");
|
Serial.print("Read from file: ");
|
||||||
#endif
|
#endif
|
||||||
while(file.available()){
|
while (file.available())
|
||||||
|
{
|
||||||
Serial.write(file.read());
|
Serial.write(file.read());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(fs::FS &fs, const char * path, const char * message){
|
void writeFile(fs::FS &fs, const char *path, const char *message)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.printf("Writing file: %s\n", path);
|
Serial.printf("Writing file: %s\n", path);
|
||||||
#endif
|
#endif
|
||||||
File file = fs.open(path, FILE_WRITE);
|
File file = fs.open(path, FILE_WRITE);
|
||||||
if(!file){
|
if (!file)
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Failed to open file for writing");
|
Serial.println("Failed to open file for writing");
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(file.print(message)){
|
if (file.print(message))
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("File written");
|
Serial.println("File written");
|
||||||
#endif
|
#endif
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
#ifdef debug
|
#ifdef debug
|
||||||
Serial.println("Write failed");
|
Serial.println("Write failed");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void appendFile(fs::FS &fs, const char * path, const char * message){
|
void appendFile(fs::FS &fs, const char *path, const char *message)
|
||||||
|
{
|
||||||
Serial.printf("Appending to file: %s\n", path);
|
Serial.printf("Appending to file: %s\n", path);
|
||||||
|
|
||||||
File file = fs.open(path, FILE_APPEND);
|
File file = fs.open(path, FILE_APPEND);
|
||||||
if(!file){
|
if (!file)
|
||||||
|
{
|
||||||
Serial.println("Failed to open file for appending");
|
Serial.println("Failed to open file for appending");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(file.print(message)){
|
if (file.print(message))
|
||||||
|
{
|
||||||
Serial.println("Message appended");
|
Serial.println("Message appended");
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("Append failed");
|
Serial.println("Append failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void renameFile(fs::FS &fs, const char * path1, const char * path2){
|
void renameFile(fs::FS &fs, const char *path1, const char *path2)
|
||||||
|
{
|
||||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||||
if (fs.rename(path1, path2)) {
|
if (fs.rename(path1, path2))
|
||||||
|
{
|
||||||
Serial.println("File renamed");
|
Serial.println("File renamed");
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("Rename failed");
|
Serial.println("Rename failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void deleteFile(fs::FS &fs, const char * path){
|
void deleteFile(fs::FS &fs, const char *path)
|
||||||
|
{
|
||||||
Serial.printf("Deleting file: %s\n", path);
|
Serial.printf("Deleting file: %s\n", path);
|
||||||
if(fs.remove(path)){
|
if (fs.remove(path))
|
||||||
|
{
|
||||||
Serial.println("File deleted");
|
Serial.println("File deleted");
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("Delete failed");
|
Serial.println("Delete failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void testFileIO(fs::FS &fs, const char * path){
|
void testFileIO(fs::FS &fs, const char *path)
|
||||||
|
{
|
||||||
File file = fs.open(path);
|
File file = fs.open(path);
|
||||||
static uint8_t buf[512];
|
static uint8_t buf[512];
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
uint32_t start = millis();
|
uint32_t start = millis();
|
||||||
uint32_t end = start;
|
uint32_t end = start;
|
||||||
if(file){
|
if (file)
|
||||||
|
{
|
||||||
len = file.size();
|
len = file.size();
|
||||||
size_t flen = len;
|
size_t flen = len;
|
||||||
start = millis();
|
start = millis();
|
||||||
while(len){
|
while (len)
|
||||||
|
{
|
||||||
size_t toRead = len;
|
size_t toRead = len;
|
||||||
if(toRead > 512){
|
if (toRead > 512)
|
||||||
|
{
|
||||||
toRead = 512;
|
toRead = 512;
|
||||||
}
|
}
|
||||||
file.read(buf, toRead);
|
file.read(buf, toRead);
|
||||||
@@ -169,20 +210,23 @@ void testFileIO(fs::FS &fs, const char * path){
|
|||||||
end = millis() - start;
|
end = millis() - start;
|
||||||
Serial.printf("%u bytes read for %u ms\n", flen, end);
|
Serial.printf("%u bytes read for %u ms\n", flen, end);
|
||||||
file.close();
|
file.close();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("Failed to open file for reading");
|
Serial.println("Failed to open file for reading");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
file = fs.open(path, FILE_WRITE);
|
file = fs.open(path, FILE_WRITE);
|
||||||
if(!file){
|
if (!file)
|
||||||
|
{
|
||||||
Serial.println("Failed to open file for writing");
|
Serial.println("Failed to open file for writing");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
start = millis();
|
start = millis();
|
||||||
for(i=0; i<2048; i++){
|
for (i = 0; i < 2048; i++)
|
||||||
|
{
|
||||||
file.write(buf, 512);
|
file.write(buf, 512);
|
||||||
}
|
}
|
||||||
end = millis() - start;
|
end = millis() - start;
|
||||||
@@ -190,26 +234,36 @@ void testFileIO(fs::FS &fs, const char * path){
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_sdcard() {
|
void setup_sdcard()
|
||||||
if(!SD_MMC.begin("/sdcard", true, false)){
|
{
|
||||||
|
if (!SD_MMC.begin("/sdcard", true, false))
|
||||||
|
{
|
||||||
Serial.println("Card Mount Failed");
|
Serial.println("Card Mount Failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t cardType = SD_MMC.cardType();
|
uint8_t cardType = SD_MMC.cardType();
|
||||||
|
|
||||||
if(cardType == CARD_NONE){
|
if (cardType == CARD_NONE)
|
||||||
|
{
|
||||||
Serial.println("No SD_MMC card attached");
|
Serial.println("No SD_MMC card attached");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print("SD_MMC Card Type: ");
|
Serial.print("SD_MMC Card Type: ");
|
||||||
if(cardType == CARD_MMC){
|
if (cardType == CARD_MMC)
|
||||||
|
{
|
||||||
Serial.println("MMC");
|
Serial.println("MMC");
|
||||||
} else if(cardType == CARD_SD){
|
}
|
||||||
|
else if (cardType == CARD_SD)
|
||||||
|
{
|
||||||
Serial.println("SDSC");
|
Serial.println("SDSC");
|
||||||
} else if(cardType == CARD_SDHC){
|
}
|
||||||
|
else if (cardType == CARD_SDHC)
|
||||||
|
{
|
||||||
Serial.println("SDHC");
|
Serial.println("SDHC");
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Serial.println("UNKNOWN");
|
Serial.println("UNKNOWN");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/sht31.h
10
src/sht31.h
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
Adafruit_SHT31 sht31 = Adafruit_SHT31();
|
Adafruit_SHT31 sht31 = Adafruit_SHT31();
|
||||||
|
|
||||||
void setup_sht() {
|
void setup_sht()
|
||||||
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
|
{
|
||||||
|
if (!sht31.begin(0x44))
|
||||||
|
{ // Set to 0x45 for alternate i2c addr
|
||||||
Serial.println("Couldn't find SHT31");
|
Serial.println("Couldn't find SHT31");
|
||||||
}
|
}
|
||||||
sht31.heater(1);
|
sht31.heater(1);
|
||||||
@@ -14,7 +16,8 @@ void setup_sht() {
|
|||||||
Serial.println("DISABLED");
|
Serial.println("DISABLED");
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_sht(JsonObject &root) {
|
void loop_sht(JsonObject &root)
|
||||||
|
{
|
||||||
JsonObject sht = root.createNestedObject("sht31");
|
JsonObject sht = root.createNestedObject("sht31");
|
||||||
float t = sht31.readTemperature();
|
float t = sht31.readTemperature();
|
||||||
float h = sht31.readHumidity();
|
float h = sht31.readHumidity();
|
||||||
@@ -36,7 +39,6 @@ void loop_sht(JsonObject &root) {
|
|||||||
|
|
||||||
// Toggle heater enabled state every 30 seconds
|
// Toggle heater enabled state every 30 seconds
|
||||||
// An ~3.0 degC temperature increase can be noted when heater is enabled
|
// An ~3.0 degC temperature increase can be noted when heater is enabled
|
||||||
/*
|
|
||||||
if (++loopCnt == 30) {
|
if (++loopCnt == 30) {
|
||||||
enableHeater = !enableHeater;
|
enableHeater = !enableHeater;
|
||||||
sht31.heater(enableHeater);
|
sht31.heater(enableHeater);
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
#include <WiFiUdp.h>
|
#include <WiFiUdp.h>
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
|
|
||||||
const char * networkName = "Overlord";
|
const char *networkName = NETWORK_NAME;
|
||||||
const char * networkPswd = "justmonika";
|
const char *networkPswd = NEWORK_PASSWORD;
|
||||||
|
|
||||||
const char * udpAddress = "192.168.2.255";
|
const char *udpAddress = UDP_ADDRESS;
|
||||||
const int udpPort = 3333;
|
const int udpPort = 3333;
|
||||||
|
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
@@ -13,8 +13,10 @@ boolean connected = false;
|
|||||||
WiFiUDP udp;
|
WiFiUDP udp;
|
||||||
|
|
||||||
//wifi event handler
|
//wifi event handler
|
||||||
void WiFiEvent(WiFiEvent_t event){
|
void WiFiEvent(WiFiEvent_t event)
|
||||||
switch(event) {
|
{
|
||||||
|
switch (event)
|
||||||
|
{
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
//When connected set
|
//When connected set
|
||||||
Serial.print("WiFi connected! IP address: ");
|
Serial.print("WiFi connected! IP address: ");
|
||||||
@@ -28,11 +30,13 @@ void WiFiEvent(WiFiEvent_t event){
|
|||||||
Serial.println("WiFi lost connection");
|
Serial.println("WiFi lost connection");
|
||||||
connected = false;
|
connected = false;
|
||||||
break;
|
break;
|
||||||
default: break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectToWiFi(const char * ssid, const char * pwd){
|
void connectToWiFi(const char *ssid, const char *pwd)
|
||||||
|
{
|
||||||
Serial.println("Connecting to WiFi network: " + String(ssid));
|
Serial.println("Connecting to WiFi network: " + String(ssid));
|
||||||
|
|
||||||
// delete old config
|
// delete old config
|
||||||
|
|||||||
Reference in New Issue
Block a user