Skip to content

Commit

Permalink
Merge commit
Browse files Browse the repository at this point in the history
'19db055ad74dc80f637f43bf08fee7929738abcf' into updates-july-2023_M5stampS3_1-6-2_boards
  • Loading branch information
robouden committed Nov 19, 2024
2 parents 0d250dc + 19db055 commit 85e2172
Show file tree
Hide file tree
Showing 69 changed files with 4,025 additions and 595 deletions.
7 changes: 0 additions & 7 deletions bGeigieCast.code-workspace

This file was deleted.

78 changes: 35 additions & 43 deletions bgeigiecast/api_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#define HOME_LOCATION_PRECISION_KM 0.4

// subtracting 1 seconds so data is sent more often than not.
#define SEND_FREQUENCY(last_send, sec, slack) (last_send == 0 || (millis() - last_send) > ((sec * 1000) - 1000))
#define SEND_FREQUENCY(last_send, sec, slack) (last_send == 0 || (millis() - last_send) > ((sec * 1000) - slack))

ApiConnector::ApiConnector(LocalStorage& config) :
Handler(),
_config(config),
_last_success_send(0),
_current_default_response(e_api_reporter_idle) {
_payload("") {
}

bool ApiConnector::time_to_send(unsigned offset) const {
Expand Down Expand Up @@ -45,7 +44,6 @@ bool ApiConnector::activate(bool retry) {
}

void ApiConnector::deactivate() {
_current_default_response = e_api_reporter_idle;
WiFiConnection::disconnect_wifi();
}

Expand All @@ -54,52 +52,52 @@ int8_t ApiConnector::handle_produced_work(const worker_map_t& workers) {

if(!geigie_connector || !geigie_connector->is_fresh()) {
// No fresh data
DEBUG_PRINTLN("no fresh data");
return _current_default_response;
return e_handler_idle;
}

if(!time_to_send()) {
DEBUG_PRINTLN("not time to send");
if (time_to_send(10000)) {
// almost time to send, start wifi if not connected yet
if (time_to_send(6000)) {
// almost time to send, start Wi-Fi if not connected yet
activate(true);
}
return _current_default_response;
return e_handler_idle;
}

const auto& reading = geigie_connector->get_data();

if(reading.valid_reading()) {
if (_config.get_use_home_location() && !reading
.near_coordinates(_config.get_home_latitude(), _config.get_home_longitude(), HOME_LOCATION_PRECISION_KM)) {
// Reading not near home location
DEBUG_PRINTLN("Reading not near configured home location");
return _current_default_response;
}
_current_default_response = send_reading(reading);
if(!reading.valid_reading()) {
return e_handler_idle;
}

if (_current_default_response == e_api_reporter_send_success) {
_last_success_send = millis();
}
if (_config.get_use_home_location() && !reading
.near_coordinates(_config.get_home_latitude(), _config.get_home_longitude(), HOME_LOCATION_PRECISION_KM)) {
// Reading not near home location
DEBUG_PRINTLN("Reading not near configured home location");
return e_handler_idle;
}
return _current_default_response;
}

ApiConnector::ApiHandlerStatus ApiConnector::send_reading(const Reading& reading) {
// All checks good, prep send data

if(!reading_to_json(reading, _payload)) {
DEBUG_PRINTLN("Unable to send, reading to payload conversion error");
return e_api_reporter_error_to_json;
}

if(!WiFi.isConnected() && !activate(true)) {
DEBUG_PRINTLN("Unable to send, lost connection");
return e_api_reporter_error_not_connected;
}

return start_task("api_send", 2048 * 4, 3, 0);
}


int8_t ApiConnector::handle_async() {

HTTPClient http;

char url[100];
sprintf(url,
"%s?api_key=%s&%s",
API_MEASUREMENTS_ENDPOINT,
_config.get_api_key(),
_config.get_use_dev() ? "test=true" : "");
sprintf(url, "%s?api_key=%s", API_MEASUREMENTS_ENDPOINT, _config.get_api_key());

//Specify destination for HTTP request
if(!http.begin(url)) {
Expand All @@ -108,34 +106,27 @@ ApiConnector::ApiHandlerStatus ApiConnector::send_reading(const Reading& reading
return e_api_reporter_error_remote_not_available;
}

char payload[200];

if(!reading_to_json(reading, payload)) {
return e_api_reporter_error_to_json;
}

char content_length[5];

sprintf(content_length, "%d", strlen(payload));

http.setUserAgent(HEADER_API_USER_AGENT);
http.addHeader("Host", API_HOST);
http.addHeader("Content-Type", HEADER_API_CONTENT_TYPE);
http.addHeader("User-Agent", HEADER_API_USER_AGENT);
http.addHeader("Content-Length", content_length);

int httpResponseCode = http.POST(payload);
int httpResponseCode = http.POST(_payload);

auto now = millis();

String response = http.getString();
DEBUG_PRINTF("POST complete, status %d\r\nrepsonse: \r\n\r\n%s\r\n\r\n", httpResponseCode, response.c_str());
DEBUG_PRINTF("POST complete, status %d\r\nresponse: \r\n\r\n%s\r\n\r\n", httpResponseCode, response.c_str());
http.end(); //Free resources

if (!_config.get_wifi_server() && _config.get_send_frequency() != e_api_send_frequency_5_sec) {
// Disconnect from wifi between readings (not needed when sending every 5 seconds)
// Disconnect from Wi-Fi between readings (not needed when sending every 5 seconds)
WiFiConnection::disconnect_wifi();
}

switch(httpResponseCode) {
case 200 ... 204:
_last_success_send = now;
return e_api_reporter_send_success;
case 400:
return e_api_reporter_error_server_rejected_post_400;
Expand Down Expand Up @@ -173,3 +164,4 @@ bool ApiConnector::reading_to_json(const Reading& reading, char* out) {
return true;
}


14 changes: 6 additions & 8 deletions bgeigiecast/api_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ApiConnector : public Handler {
* Collection of statuses for the api data handling, default is idle.
*/
enum ApiHandlerStatus {
e_api_reporter_idle,
e_api_reporter_send_success,
e_api_reporter_error_to_json,
e_api_reporter_error_not_connected,
Expand All @@ -45,7 +44,7 @@ class ApiConnector : public Handler {

/**
* Check if enough time has passed to send the latest reading to api
* @param offset: additional ms offset to check if its *almost* time to send (default 1 second)
* @param offset: additional ms offset (default 1 sec to overlap measurements better)
* @return true if time to send
*/
bool time_to_send(unsigned offset = 1000) const;
Expand All @@ -66,18 +65,17 @@ class ApiConnector : public Handler {

bool reading_to_json(const Reading& reading, char* out);

private:

/**
* Send a reading to the API
* @param reading: reading to send
* @return: true if the API call was successful
* @return: status
*/
ApiHandlerStatus send_reading(const Reading& reading);
int8_t handle_async() override;

private:

LocalStorage& _config;
char _payload[200];
uint32_t _last_success_send;
ApiHandlerStatus _current_default_response;
};

#endif //BGEIGIECAST_APICONNECTOR_H
2 changes: 2 additions & 0 deletions bgeigiecast/bgeigie_connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ int8_t BGeigieConnector::produce_data() {
data = _buffer.c_str();
DEBUG_PRINTLN(_buffer);
_buffer = "";
// Clear rest of input buffer (should be empty after new measurement anyway)
while (_serial_connection.available() > 0) _serial_connection.read();
return data.get_status() & k_reading_parsed ? Worker::e_worker_data_read : Worker::e_worker_error;
}
}
Expand Down
6 changes: 1 addition & 5 deletions bgeigiecast/configuration_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ void ConfigWebServer::add_urls() {
_config.get_wifi_ssid(),
_config.get_wifi_password(),
_config.get_api_key(),
_config.get_send_frequency(),
_config.get_use_dev()
_config.get_send_frequency()
));
});

Expand Down Expand Up @@ -170,9 +169,6 @@ void ConfigWebServer::handle_save() {
if(_server.hasArg(FORM_NAME_API_KEY)) {
_config.set_api_key(_server.arg(FORM_NAME_API_KEY).c_str(), false);
}
if(_server.hasArg(FORM_NAME_USE_DEV)) {
_config.set_use_dev(_server.arg(FORM_NAME_USE_DEV) == "1", false);
}
if(_server.hasArg(FORM_NAME_SEND_FREQ)){
_config.set_send_frequency(_server.arg(FORM_NAME_SEND_FREQ).toInt(), false);
}
Expand Down
2 changes: 1 addition & 1 deletion bgeigiecast/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#define BGEIGIECAST_DEBUGGER_H

#include "user_config.h"
#include <Arduino.h>

#ifdef UNIT_TEST
#undef ENABLE_DEBUG
#endif

#if ENABLE_DEBUG
#include <Arduino.h>

#ifndef DEBUG_STREAM
#define DEBUG_STREAM Serial
Expand Down
17 changes: 1 addition & 16 deletions bgeigiecast/http_pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ const char* HttpPages::get_config_connection_page(
const char* wifi_ssid,
const char* wifi_password,
const char* api_key,
uint8_t send_frequency,
bool use_dev
uint8_t send_frequency
) {
char ssid[20];
sprintf(ssid, ACCESS_POINT_SSID, device_id);
Expand Down Expand Up @@ -324,18 +323,6 @@ const char* HttpPages::get_config_connection_page(
"We suggest sending data every 5 minutes for stationary sensors"
"</span>"

// Use dev
"<label>Safecast server</label>"
"<label for='" FORM_NAME_USE_DEV "0' class='pure-radio'>"
"<input id='" FORM_NAME_USE_DEV "0' type='radio' name='" FORM_NAME_USE_DEV "' value='0' %s>Production"
"</label>"
"<label for='" FORM_NAME_USE_DEV "1' class='pure-radio'>"
"<input id='" FORM_NAME_USE_DEV "1' type='radio' name='" FORM_NAME_USE_DEV "' value='1' %s>Development"
"</label>"
"<span class='pure-form-message'>"
"Use development for testing purposes. Double check your API key when changing this option!"
"</span>"

"<br>"
"<button type='submit' class='pure-button pure-button-primary'>Save</button>"
"</fieldset>"
Expand All @@ -349,8 +336,6 @@ const char* HttpPages::get_config_connection_page(
send_frequency == ApiConnector::e_api_send_frequency_5_sec ? "checked" : "",
send_frequency == ApiConnector::e_api_send_frequency_1_min ? "checked" : "",
send_frequency == ApiConnector::e_api_send_frequency_5_min ? "checked" : "",
use_dev ? "" : "checked",
use_dev ? "checked" : "",
display_success ? success_message : ""
);
}
Expand Down
3 changes: 1 addition & 2 deletions bgeigiecast/http_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ class HttpPages {
const char* wifi_ssid,
const char* wifi_password,
const char* api_key,
uint8_t send_frequency,
bool use_dev
uint8_t send_frequency
);

/**
Expand Down
25 changes: 1 addition & 24 deletions bgeigiecast/local_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include "bgeigie_connector.h"
#include "api_connector.h"

#define D_SAVED_STATE Controller::k_savable_MobileMode
#define D_SEND_FREQUENCY ApiConnector::e_api_send_frequency_5_min

const char* memory_name = "data";

// Keys for config
Expand All @@ -18,7 +15,6 @@ const char* key_ap_password = "ap_password";
const char* key_wifi_ssid = "wifi_ssid";
const char* key_wifi_password = "wifi_password";
const char* key_api_key = "api_key";
const char* key_use_dev = "use_dev";
const char* key_send_frequency = "send_frequency";
const char* key_led_color_blind = "led_color_blind";
const char* key_wifi_server = "wifi_server";
Expand All @@ -37,7 +33,6 @@ LocalStorage::LocalStorage() :
_wifi_ssid(""),
_wifi_password(""),
_api_key(""),
_use_dev(D_USE_DEV_SERVER),
_send_frequency(D_SEND_FREQUENCY),
_led_color_blind(D_LED_COLOR_BLIND),
_led_color_intensity(D_LED_COLOR_INTENSITY),
Expand All @@ -57,7 +52,6 @@ void LocalStorage::reset_defaults() {
set_wifi_ssid(D_WIFI_SSID, true);
set_wifi_password(D_WIFI_PASSWORD, true);
set_api_key(D_APIKEY, true);
set_use_dev(D_USE_DEV_SERVER, true);
set_send_frequency(D_SEND_FREQUENCY, true);
set_led_color_blind(D_LED_COLOR_BLIND, true);
set_led_color_intensity(D_LED_COLOR_INTENSITY, true);
Expand Down Expand Up @@ -107,10 +101,6 @@ bool LocalStorage::get_wifi_server() const {
return _wifi_server;
}

bool LocalStorage::get_use_dev() const {
return _use_dev;
}

uint8_t LocalStorage::get_send_frequency() const {
return _send_frequency;
}
Expand Down Expand Up @@ -195,18 +185,6 @@ void LocalStorage::set_api_key(const char* api_key, bool force) {
}
}

void LocalStorage::set_use_dev(bool use_dev, bool force) {
if(force || (use_dev != _use_dev)) {
if(_memory.begin(memory_name)) {
_use_dev = use_dev;
_memory.putBool(key_use_dev, use_dev);
_memory.end();
} else {
DEBUG_PRINTLN("unable to save new value for use_dev");
}
}
}

void LocalStorage::set_send_frequency(uint8_t send_frequency, bool force) {
if(force || (send_frequency != _send_frequency)) {
if(_memory.begin(memory_name)) {
Expand Down Expand Up @@ -329,7 +307,7 @@ bool LocalStorage::clear() {
}

bool LocalStorage::activate(bool) {
_memory.begin(memory_name, true);
_memory.begin(memory_name, false);
_device_id = _memory.getUShort(key_device_id, D_DEVICE_ID);
if(_memory.getString(key_ap_password, _ap_password, CONFIG_VAL_MAX) == 0) {
strcpy(_ap_password, D_ACCESS_POINT_PASSWORD);
Expand All @@ -343,7 +321,6 @@ bool LocalStorage::activate(bool) {
if(_memory.getString(key_api_key, _api_key, CONFIG_VAL_MAX) == 0) {
strcpy(_api_key, D_APIKEY);
}
_use_dev = _memory.getBool(key_use_dev, D_USE_DEV_SERVER);
_send_frequency = _memory.getUChar(key_send_frequency, D_SEND_FREQUENCY);
_led_color_blind = _memory.getBool(key_led_color_blind, D_LED_COLOR_BLIND);
_led_color_intensity = _memory.getUChar(key_led_color_intensity, D_LED_COLOR_INTENSITY);
Expand Down
3 changes: 0 additions & 3 deletions bgeigiecast/local_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class LocalStorage : public Handler {
virtual const char* get_wifi_ssid() const final;
virtual const char* get_wifi_password() const final;
virtual const char* get_api_key() const final;
virtual bool get_use_dev() const final;
virtual uint8_t get_send_frequency() const final;
virtual bool is_led_color_blind() const final;
virtual uint8_t get_led_color_intensity() const final;
Expand All @@ -43,7 +42,6 @@ class LocalStorage : public Handler {
virtual void set_wifi_ssid(const char* wifi_ssid, bool force);
virtual void set_wifi_password(const char* wifi_password, bool force);
virtual void set_api_key(const char* api_key, bool force);
virtual void set_use_dev(bool use_dev, bool force);
virtual void set_send_frequency(uint8_t send_frequency, bool force);
virtual void set_led_color_blind(bool led_color_blind, bool force);
virtual void set_led_color_intensity(uint8_t led_color_intensity, bool force);
Expand Down Expand Up @@ -78,7 +76,6 @@ class LocalStorage : public Handler {

// API config (to connect to the API)
char _api_key[CONFIG_VAL_MAX];
bool _use_dev;
uint8_t _send_frequency;

// RGB LED config
Expand Down
Loading

0 comments on commit 85e2172

Please sign in to comment.