diff --git a/.cproject b/.cproject index a3443bd..3fc5d46 100644 --- a/.cproject +++ b/.cproject @@ -51,6 +51,7 @@ + @@ -67,6 +68,7 @@ + @@ -87,6 +89,7 @@ + @@ -298,6 +301,7 @@ + @@ -318,6 +322,7 @@ + @@ -345,6 +350,7 @@ + diff --git a/include/api/api_status_content.h b/include/api/api_status_content.h index 8ec0ec2..be35751 100644 --- a/include/api/api_status_content.h +++ b/include/api/api_status_content.h @@ -14,6 +14,9 @@ #include "rte_main.h" #include "rte_wx.h" +#define ENTRIES_STRING(ENTRY) \ + ENTRY(main_config_data_basic->callsign, callsign) \ + #define ENTRIES_32INT_STATUS(ENTRY) \ ENTRY(master_time, master_time) \ ENTRY(rte_main_rx_total, rx_total) \ @@ -30,6 +33,7 @@ ENTRY(rte_main_average_battery_voltage, average_battery_voltage) \ ENTRY(rte_main_wakeup_count, wakeup_count) \ ENTRY(rte_main_going_sleep_count, going_sleep_count) \ + ENTRY(main_config_data_basic->ssid, ssid) \ #define ENTRIES_16INT_WEATHER(ENTRY) \ @@ -38,7 +42,7 @@ ENTRY(rte_wx_temperature_average_internal, temperature_internal)\ ENTRY(rte_wx_pressure_average, pressure) \ ENTRY(rte_wx_average_winddirection, wind_direction) \ - ENTRY(rte_wx_average_windspeed, wind_gust) \ + ENTRY(rte_wx_average_windspeed, wind_speed) \ ENTRY(rte_wx_max_windspeed, wind_gust) \ #endif /* API_STATUS_CONTENT_H_ */ diff --git a/include/api/api_xmacro_helpers.h b/include/api/api_xmacro_helpers.h index 8e2722e..f9df395 100644 --- a/include/api/api_xmacro_helpers.h +++ b/include/api/api_xmacro_helpers.h @@ -16,14 +16,18 @@ LN = sprintf(OUT + LN, "{\r\n"); \ -#define PRINT_32INT(integer, name) LN += sprintf(OUT + LN, "" #name ":%ld,", integer); -#define PRINT_16INT(integer, name) LN += sprintf(OUT + LN, "" #name ":%d,", integer); -#define PRINT_STRING(integer, name) LN += sprintf(OUT + LN, "" #name ":%s,", integer); +#define PRINT_32INT(integer, name) LN += sprintf(OUT + LN, "\"" #name "\":%ld,", integer); +#define PRINT_16INT(integer, name) LN += sprintf(OUT + LN, "\"" #name "\":%d,", integer); +#define PRINT_STRING(string, name) LN += sprintf(OUT + LN, "\"" #name "\":\"%s\",", string); #define END LN += sprintf(OUT + LN - 1, "}\r\n"); #define PRINT_ALL_STATUS \ ENTRIES_32INT_STATUS(PRINT_32INT); \ ENTRIES_16INT_STATUS(PRINT_16INT); \ + ENTRIES_STRING(PRINT_STRING); \ + +#define PRINT_ALL_MEASUREMENTS \ + ENTRIES_16INT_WEATHER(PRINT_16INT) \ #endif /* API_XMACRO_HELPERS_H_ */ diff --git a/include/aprsis.h b/include/aprsis.h index 4da552b..1778bf2 100644 --- a/include/aprsis.h +++ b/include/aprsis.h @@ -13,8 +13,10 @@ extern uint8_t aprsis_connected; -void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, char * callsign, uint8_t ssid, uint32_t passcode); +void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, char * callsign, uint8_t ssid, uint32_t passcode, char * default_server, uint16_t default_port); uint8_t aprsis_connect_and_login(char * address, uint8_t address_ln, uint16_t port); +uint8_t aprsis_connect_and_login_default(void); +void aprsis_disconnect(void); void aprsis_receive_callback(srl_context_t* srl_context); void aprsis_check_alive(void); diff --git a/include/etc/api_configuration.h b/include/etc/api_configuration.h new file mode 100644 index 0000000..ad9bfe6 --- /dev/null +++ b/include/etc/api_configuration.h @@ -0,0 +1,18 @@ +/* + * api_configuration.h + * + * Created on: Apr 30, 2022 + * Author: mateusz + */ + +#ifndef API_CONFIGURATION_H_ +#define API_CONFIGURATION_H_ + +// API cycle are fired once every minute and counts from zero +// up to 100 and then starts again. These defines controls how +// often in minutes API communication is triggered + +#define API_STATUS_CALL_CYCLE_INTERVAL 20 +#define API_MEASUREMENTS_CALL_CYCLE_INTERVAL 5 + +#endif /* API_CONFIGURATION_H_ */ diff --git a/src/api.c b/src/api.c index f49a98c..194e9b6 100644 --- a/src/api.c +++ b/src/api.c @@ -8,7 +8,8 @@ * 19 maja, Krszystof Binek, miedy 12 a 13 */ -#include +#include "api/api_status_content.h" +#include "etc/api_configuration.h" #include "http_client/http_client.h" @@ -17,14 +18,38 @@ /** * Buffers for generating JSON and URL */ -#define API_BUFFER_LN 256 +#define API_BUFFER_LN 384 char api_buffer[API_BUFFER_LN]; #define URL_BUFFER_LN 96 char api_url_buffer[URL_BUFFER_LN]; -int api_buffer_idx = 0; +/** + * Index to move around a buffer with request body + */ +uint32_t api_buffer_idx = 0; + +/** + * Value returned from http client + */ uint8_t api_retval = 0xFF; +/** + * Cycle counter to control the frequency of api calls + */ +int8_t api_cycle_counter = 0; + +/** + * This is used to retrigger specific api communication in case of any request + * appears on the same call to `api_pooler` function. For code simplicity only one + * API request is possible in the single 'api_pooler' cycle + */ +#define API_TRIGGER_STATUS (1 << 1) +#define API_TRIGGER_MEASUREMENTS (1 << 2) +int8_t api_retrigger_api_call = 0; + +/** + * Pointers to base url and station name (stored within flash) + */ const char * api_base_url; const char * api_station_name; @@ -34,6 +59,8 @@ typedef enum api_endpoint{ PARAMETEO_WX } api_endpoint_t; +#define LN api_buffer_idx +#define OUT api_buffer static void api_construct_url_status(api_endpoint_t endpoint) { @@ -42,10 +69,10 @@ static void api_construct_url_status(api_endpoint_t endpoint) { switch (endpoint) { case PARAMETEO_STATUS: - snprintf(api_url_buffer, API_BUFFER_LN - 1, "%s/parameteo/%s/status", api_base_url, api_station_name); + snprintf(api_url_buffer, URL_BUFFER_LN - 1, "%s/parameteo/%s/status", api_base_url, api_station_name); break; case PARAMETEO_WX: - snprintf(api_url_buffer, API_BUFFER_LN - 1, "%s/parameteo/%s/wx", api_base_url, api_station_name); + snprintf(api_url_buffer, URL_BUFFER_LN - 1, "%s/parameteo/%s/wx", api_base_url, api_station_name); break; } } @@ -54,6 +81,7 @@ static void api_construct_url_status(api_endpoint_t endpoint) { static void api_callback(uint16_t http_code, char * content, uint16_t content_lenght) { + } void api_init(const char * api_base, const char * station_name) { @@ -62,16 +90,44 @@ void api_init(const char * api_base, const char * station_name) { } void api_send_json_status(void) { - #define LN api_buffer_idx - #define OUT api_buffer BEGIN PRINT_ALL_STATUS END - if (api_buffer_idx < 256) { + if (api_buffer_idx < API_BUFFER_LN) { api_construct_url_status(PARAMETEO_STATUS); api_retval = http_client_async_post(api_url_buffer, strlen("http://pogoda.cc:8080/meteo_backend/parameteo/skrzyczne/status"), OUT, strlen(OUT), 0, api_callback); } } +void api_send_json_measuremenets(void) { + BEGIN + PRINT_ALL_MEASUREMENTS + END + + if (api_buffer_idx < API_BUFFER_LN) { + api_construct_url_status(PARAMETEO_STATUS); + + api_retval = http_client_async_post(api_url_buffer, strlen("http://pogoda.cc:8080/meteo_backend/parameteo/skrzyczne/status"), OUT, strlen(OUT), 0, api_callback); + } +} + +void api_pooler(void) { + + api_cycle_counter++; + + if (api_cycle_counter > 100) { + api_cycle_counter = 0; + } + + if ((api_cycle_counter % API_STATUS_CALL_CYCLE_INTERVAL) == 0) { + api_send_json_status(); + } + + if ((api_cycle_counter % API_MEASUREMENTS_CALL_CYCLE_INTERVAL) == 0) { + api_send_json_measuremenets(); + } + +} + diff --git a/src/aprsis.c b/src/aprsis.c index 9dce9f4..b59cdac 100644 --- a/src/aprsis.c +++ b/src/aprsis.c @@ -40,17 +40,35 @@ int32_t aprsis_passcode; */ char aprsis_login_string[64]; +/** + * Default APRS-IS address to be used by + */ +const char * aprsis_default_server_address; + +uint16_t aprsis_default_server_address_ln = 0; + +uint16_t aprsis_default_server_port; + +/** + * Set to one if connections is established AND user is logged + */ uint8_t aprsis_logged = 0; +/** + * Set to one if connection to server is established (but maybe not logged) + */ uint8_t aprsis_connected = 0; const char * aprsis_sucessfull_login = "# logresp\0"; +/** + * A timestamp when server has send anything + */ uint32_t aprsis_last_keepalive_ts = 0; #define APRSIS_TIMEOUT_MS 123000//123000 -void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, char * callsign, uint8_t ssid, uint32_t passcode) { +void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, char * callsign, uint8_t ssid, uint32_t passcode, char * default_server, uint16_t default_port) { aprsis_serial_port = context; aprsis_gsm_modem_state = gsm_modem_state; @@ -65,10 +83,16 @@ void aprsis_init(srl_context_t * context, gsm_sim800_state_t * gsm_modem_state, aprsis_logged = 0; + aprsis_default_server_port = default_port; + + aprsis_default_server_address = default_server; + + aprsis_default_server_address_ln = strlen(aprsis_default_server_address); + } uint8_t aprsis_connect_and_login(char * address, uint8_t address_ln, uint16_t port) { - + // this function has blocking io uint8_t out = 2; if (aprsis_serial_port == 0 || aprsis_gsm_modem_state == 0 || aprsis_logged == 1) { @@ -151,6 +175,19 @@ uint8_t aprsis_connect_and_login(char * address, uint8_t address_ln, uint16_t po } +uint8_t aprsis_connect_and_login_default(void) { + + return aprsis_connect_and_login(aprsis_default_server_address, aprsis_default_server_address_ln, aprsis_default_server_port); +} + +void aprsis_disconnect(void) { + gsm_sim800_tcpip_close(aprsis_serial_port, aprsis_gsm_modem_state); + + aprsis_logged = 0; + + aprsis_connected = 0; +} + void aprsis_receive_callback(srl_context_t* srl_context) { // if something was actually received diff --git a/src/main.c b/src/main.c index ef961e2..d881109 100644 --- a/src/main.c +++ b/src/main.c @@ -958,7 +958,8 @@ int main(int argc, char* argv[]){ http_client_init(&main_gsm_state, main_gsm_srl_ctx_ptr, 0); api_init("http://pogoda.cc:8080/meteo_backend\0", "skrzyczne\0"); - //aprsis_init(&main_gsm_srl_ctx, &main_gsm_state, "SP8EBC", 10, 23220); + + aprsis_init(&main_gsm_srl_ctx, &main_gsm_state, "SP8EBC", 10, 23220, TEST_IP, 14580); } #endif @@ -1230,6 +1231,9 @@ int main(int argc, char* argv[]){ main_wx_sensors_pool_timer = 65500; } + /** + * ONE MINUTE POOLING + */ if (main_one_minute_pool_timer < 10) { main_set_monitor(4); @@ -1258,6 +1262,9 @@ int main(int argc, char* argv[]){ main_one_minute_pool_timer = 60000; } + /** + * ONE SECOND POOLING + */ if (main_one_second_pool_timer < 10) { main_set_monitor(6); @@ -1302,6 +1309,9 @@ int main(int argc, char* argv[]){ main_one_second_pool_timer = 1000; } + /** + * TWO SECOND POOLING + */ if (main_two_second_pool_timer < 10) { wx_check_force_i2c_reset(); @@ -1315,6 +1325,9 @@ int main(int argc, char* argv[]){ main_two_second_pool_timer = 2000; } + /** + * TEN SECOND POOLING + */ if (main_ten_second_pool_timer < 10) { main_set_monitor(8); diff --git a/system/include/gsm/sim800c_poolers.h b/system/include/gsm/sim800c_poolers.h index 011adec..5a477cf 100644 --- a/system/include/gsm/sim800c_poolers.h +++ b/system/include/gsm/sim800c_poolers.h @@ -13,7 +13,6 @@ #include "config_data.h" void gsm_sim800_poolers_one_minute(srl_context_t * srl_context, gsm_sim800_state_t * state); -void gsm_sim800_poolers_five_minutes(srl_context_t * srl_context, gsm_sim800_state_t * state); void gsm_sim800_poolers_one_second(srl_context_t * srl_context, gsm_sim800_state_t * state, const config_data_gsm_t * config); #endif /* INCLUDE_GSM_SIM800C_POOLERS_H_ */ diff --git a/system/src/gsm/sim800c_poolers.c b/system/src/gsm/sim800c_poolers.c index 8205d97..75728a6 100644 --- a/system/src/gsm/sim800c_poolers.c +++ b/system/src/gsm/sim800c_poolers.c @@ -26,16 +26,6 @@ void gsm_sim800_poolers_one_minute(srl_context_t * srl_context, gsm_sim800_state } - -void gsm_sim800_poolers_five_minutes(srl_context_t * srl_context, gsm_sim800_state_t * state) { - sim800_poolers_five++; - - if (sim800_poolers_five == 5) { - sim800_poolers_five = 0; - } - -} - void gsm_sim800_poolers_one_second(srl_context_t * srl_context, gsm_sim800_state_t * state, const config_data_gsm_t * config) { if (*state == SIM800_ALIVE) {