Merge pull request #9 from jonasniesner/jonasniesner-patch-4

Save config in modem + esp communication
This commit is contained in:
Jonas Niesner
2025-06-24 18:06:35 +02:00
committed by GitHub
6 changed files with 1613 additions and 383 deletions

View File

@@ -12,6 +12,7 @@ lib_deps =
vshymanskyy/TinyGSM@^0.12.0 vshymanskyy/TinyGSM@^0.12.0
koendv/RTT Stream@^1.4.1 koendv/RTT Stream@^1.4.1
arduino-libraries/ArduinoHttpClient@^0.6.1 arduino-libraries/ArduinoHttpClient@^0.6.1
bblanchon/ArduinoJson@^6.21.4
build_unflags = -DNRF52 -DUSE_LFXO build_unflags = -DNRF52 -DUSE_LFXO
build_flags = -DNRF52840_XXAA -DUSE_LFRC build_flags = -DNRF52840_XXAA -DUSE_LFRC
upload_protocol = jlink upload_protocol = jlink

View File

@@ -0,0 +1,45 @@
<?php
// Sensor Data Receiver - POST JSON Version
// This script receives sensor data via POST with JSON payload
header('Content-Type: application/json');
// Get JSON input
$json_input = file_get_contents('php://input');
$data = json_decode($json_input, true);
// Create response array
$response = array(
'status' => 'success',
'message' => 'Data received successfully',
'timestamp' => time(),
'received_data' => $data
);
// Check if JSON was parsed successfully
if ($data === null) {
$response['status'] = 'error';
$response['message'] = 'Invalid JSON data received';
$response['raw_input'] = $json_input;
http_response_code(400);
} else {
// Log the received data to a JSON file
$log_filename = 'sensor_data_' . date('Y-m-d') . '.json';
$log_entry = array(
'timestamp' => date('Y-m-d H:i:s'),
'data' => $data
);
// Append to daily log file
file_put_contents($log_filename, json_encode($log_entry, JSON_PRETTY_PRINT) . "\n", FILE_APPEND | LOCK_EX);
// Also log to a general log file
$general_log = 'sensor_data_all.json';
file_put_contents($general_log, json_encode($log_entry, JSON_PRETTY_PRINT) . "\n", FILE_APPEND | LOCK_EX);
$response['logged_to'] = $log_filename;
}
// Output response as JSON
echo json_encode($response, JSON_PRETTY_PRINT);
?>

File diff suppressed because it is too large Load Diff

View File

@@ -7,6 +7,7 @@
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#include <RTTStream.h> #include <RTTStream.h>
#include <ArduinoHttpClient.h> #include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include "ens210.h" #include "ens210.h"
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h" #include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
#include "sensors.h" #include "sensors.h"
@@ -28,24 +29,130 @@ bool espmodemrail = 0;
bool espon = 0; bool espon = 0;
bool modemon = 0; bool modemon = 0;
void measuretemp(); bool espSerialInitialized = false;
void measurepressure(); int currentFileHandle = -1;
void ButtonState(); bool fileOpen = false;
void measureBattery(); volatile int32_t wdcounter = 0;
void lightsense(); unsigned long lastTransmissionTime = 0;
void powerupesp(); bool modemInitialized = false;
// Power management functions
void wd_handler(); void wd_handler();
void gpioinit(); void gpioinit();
void measureacc();
void powerupmodem();
void softpwrup(); void softpwrup();
void powerdownesp();
void powerdownmodem(); // Sensor data management functions
void testmodem();
void getgps();
void httpreq();
void disconnectmodem();
void connectmodem();
void initmodem();
void sampleallsensors(); void sampleallsensors();
void sensorpwr(bool onoff); void sensorpwr(bool onoff);
void collectAllSensorData();
// New refactored sensor functions (implemented in main.cpp)
float readBatteryVoltage();
float readLightSensor(float gain = 0.25, int integrationTime = 100);
bool readTemperatureHumidity(float& temperature, float& humidity);
bool readPressure(float& pressure, float& sensorTemp);
bool readAccelerometers(float& acc1_x, float& acc1_y, float& acc1_z,
float& acc2_x, float& acc2_y, float& acc2_z);
bool readGPS(float& latitude, float& longitude, float& speed, float& altitude,
int& visibleSatellites, int& usedSatellites, float& accuracy);
bool readModemInfo();
bool readNetworkInfo();
void checkModemStorageSpace();
// HTTP Data Transmission System
struct ServerConfig {
const char* server;
const char* endpoint;
int port;
const char* apn;
const char* username;
const char* password;
};
bool initializeModem();
bool connectToNetwork();
bool disconnectFromNetwork();
bool sendSensorDataToServer(const SensorData& data, const ServerConfig& config);
bool sendSensorDataToServer(const SensorData& data);
void testModemAndNetwork();
void manageModemLifecycle();
// Data transmission scheduling
extern unsigned long lastTransmissionTime;
extern unsigned long transmissionInterval;
extern bool modemInitialized;
// GPS configuration
extern bool enableGPS;
extern unsigned long gpsTimeout;
// Abstracted File System Functions
bool modemFileOpen(const String& filename, int mode);
bool modemFileWrite(const String& data);
bool modemFileRead(String& data, int length = 1024);
bool modemFileSeek(int offset, int origin);
bool modemFilePosition(int& position);
bool modemFileClose();
bool modemFileDelete(const String& filename);
bool modemFileExists(const String& filename);
int modemFileSize(const String& filename);
String modemFileList();
bool checkModemReady();
// Configuration Storage
struct ModemConfig {
String device_name;
unsigned long transmission_interval;
bool enable_gps;
unsigned long gps_timeout;
String server_url;
String server_endpoint;
int server_port;
String apn;
String username;
String password;
bool enable_storage;
int max_stored_files;
ModemConfig() {
device_name = "Dev 1";
transmission_interval = 300000;
enable_gps = true;
gps_timeout = 15000;
server_url = "api.64eng.de";
server_endpoint = "/sensordata.php";
server_port = 80;
apn = "internet";
username = "";
password = "";
enable_storage = true;
max_stored_files = 10;
}
};
// Global configuration variable
extern ModemConfig deviceConfig;
void blinkLED(int times, int delayon, int delayoff, bool green);
bool saveConfigToModem(const ModemConfig& config);
bool loadConfigFromModem(ModemConfig& config);
void printConfig(const ModemConfig& config);
void LoadDeviceConfig();
void displaySensorDataSummary();
// ESP communication functions
bool initializeESPSerial();
void powerupESP();
void powerdownESP();
bool sendCommandToESP(const String& command);
bool readESPResponseDebug(String& response, unsigned long timeout = 5000);
void testESPCommunication();
// ESP response parsing functions
bool parseESPResponse(const String& rawResponse, String& filteredResponse, bool& hasOK);
bool sendESPCommandAndGetResponse(const String& command, String& response, unsigned long timeout);
// UART bit-banging functions
void uartSendByte(uint8_t byte);
bool testESPConnection();

View File

@@ -0,0 +1,39 @@
#include "sensors.h"
// Global sensor data instance
SensorData sensorData;
// Implementation of toJSON method
String SensorData::toJSON() const {
String json = "{";
json += "\"timestamp\":" + String(timestamp) + ",";
json += "\"sensors\":{";
json += "\"light\":{\"lux\":" + String(lux) + "},";
json += "\"battery\":{\"voltage\":" + String(battery_voltage, 2) + "},";
json += "\"environment\":{";
json += "\"temperature\":" + String(case_temperature, 1) + ",";
json += "\"humidity\":" + String(case_humidity, 1) + ",";
json += "\"pressure\":" + String(pressure, 2) + ",";
json += "\"pressure_sensor_temp\":" + String(pressure_sensor_temp, 2);
json += "},";
json += "\"accelerometers\":{";
json += "\"acc1\":{\"x\":" + String(acc1_x) + ",\"y\":" + String(acc1_y) + ",\"z\":" + String(acc1_z) + "},";
json += "\"acc2\":{\"x\":" + String(acc2_x) + ",\"y\":" + String(acc2_y) + ",\"z\":" + String(acc2_z) + "}";
json += "},";
json += "\"gps\":{";
json += "\"latitude\":" + String(gps_latitude, 8) + ",";
json += "\"longitude\":" + String(gps_longitude, 8) + ",";
json += "\"speed\":" + String(gps_speed) + ",";
json += "\"altitude\":" + String(gps_altitude) + ",";
json += "\"visible_satellites\":" + String(gps_visible_satellites) + ",";
json += "\"used_satellites\":" + String(gps_used_satellites) + ",";
json += "\"accuracy\":" + String(gps_accuracy);
json += "},";
json += "\"system\":{";
json += "\"wakeup_reason\":" + String(wakeup_reason) + ",";
json += "\"watchdog_counter\":" + String(watchdog_counter);
json += "}";
json += "}";
json += "}";
return json;
}

View File

@@ -1,16 +1,110 @@
float lux = -1; #include <Arduino.h>
float batvoltage = 0.0;
float casetemp = 0.0;
float casehum = 0.0;
float pressure = 0.0;
float pressuresensortemp = 0.0;
float acc1x = 0.0;
float acc1y = 0.0;
float acc1z = 0.0;
float acc2x = 0.0;
float acc2y = 0.0;
float acc2z = 0.0;
int32_t wakeupreason = 0; // Sensor data structure to hold all readings
struct SensorData {
// Timestamp
unsigned long timestamp;
// Light sensor
float lux;
// Battery
float battery_voltage;
// Temperature and humidity
float case_temperature;
float case_humidity;
// Pressure sensor
float pressure;
float pressure_sensor_temp;
// Accelerometers
float acc1_x;
float acc1_y;
float acc1_z;
float acc2_x;
float acc2_y;
float acc2_z;
// System info
int32_t wakeup_reason;
int32_t watchdog_counter;
// GPS data (when available)
float gps_latitude;
float gps_longitude;
float gps_speed;
float gps_altitude;
int gps_visible_satellites;
int gps_used_satellites;
float gps_accuracy;
// Modem information
String modem_id;
float modem_temperature;
String sim_card_id;
String network_name;
String network_id;
// Network connection information
String link_type; // LTE, 2G, 3G, etc.
int signal_quality; // Signal quality (0-31, where 31 is best)
int signal_strength; // Signal strength in dBm
int network_registration_status; // Network registration status
String network_band; // Network band (e.g., "LTE BAND 20")
int network_channel; // Network channel number
String network_operator_code; // Network operator code
// Constructor to initialize all values
SensorData() {
timestamp = 0;
lux = -1;
battery_voltage = 0.0;
case_temperature = 0.0;
case_humidity = 0.0;
pressure = 0.0;
pressure_sensor_temp = 0.0;
acc1_x = 0.0;
acc1_y = 0.0;
acc1_z = 0.0;
acc2_x = 0.0;
acc2_y = 0.0;
acc2_z = 0.0;
wakeup_reason = 0;
watchdog_counter = 0;
gps_latitude = 0.0;
gps_longitude = 0.0;
gps_speed = 0.0;
gps_altitude = 0.0;
gps_visible_satellites = 0;
gps_used_satellites = 0;
gps_accuracy = 0.0;
modem_id = "";
modem_temperature = 0.0;
sim_card_id = "";
network_name = "";
network_id = "";
link_type = "";
signal_quality = 0;
signal_strength = 0;
network_registration_status = 0;
network_band = "";
network_channel = 0;
network_operator_code = "";
}
// Method to check if data is fresh (within last 5 minutes)
bool isFresh() const {
return (millis() - timestamp) < 300000; // 5 minutes
}
// Method to get JSON representation for HTTP transmission
String toJSON() const;
};
volatile int32_t wdcounter = 0; // Global sensor data instance
extern SensorData sensorData;
// Data collection functions
void collectAllSensorData();