26 #include <stima_module.h>
29 #include <registers.h>
30 #include <registers.h>
31 #include <registers-master.h>
33 #include <ArduinoLog.h>
34 #include <StreamUtils.h>
36 #include <i2c_config.h>
37 #include <json_config.h>
38 #include <ntp_config.h>
39 #include <constantdata_config.h>
40 #include <gsm_config.h>
44 #include <lcd_config.h>
46 #include <hd44780ioClass/hd44780_I2Cexp.h>
49 #ifdef ARDUINO_ARCH_AVR
50 #include <avr/sleep.h>
51 #include <avr/power.h>
59 #include <sdcard_utility.h>
68 #include <rmap_utility.h>
70 #include <json_utility.h>
73 #include <SensorDriver.h>
74 #include <i2c_utility.h>
75 #include <arduinoJsonRPC.h>
77 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
78 #include <ethernet_config.h>
79 #include <Ethernet2.h>
84 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
85 #include <gsm_config.h>
86 #include <sim800Client.h>
88 #include <Sim800IPStack.h>
94 #include <Countdown.h>
95 #include <MQTTClient.h>
117 uint8_t module_main_version;
118 uint8_t module_configuration_version;
138 uint8_t sensors_count;
139 uint16_t report_seconds;
142 uint8_t constantdata_count;
144 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
152 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
189 SUPERVISOR_TEST_SDCARD,
194 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
207 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
217 GSM_START_CONNECTION,
222 GSM_WAIT_FOR_SWITCH_OFF,
268 DATA_SAVING_OPEN_SDCARD,
269 DATA_SAVING_OPEN_FILE,
270 DATA_SAVING_SENSORS_LOOP,
271 DATA_SAVING_DATA_LOOP,
272 DATA_SAVING_WRITE_FILE,
273 DATA_SAVING_CLOSE_FILE,
275 DATA_SAVING_WAIT_STATE
276 } data_saving_state_t;
308 MQTT_CLOSE_DATA_FILE,
342 uint8_t sensors_count = 0;
393 #if (ENABLE_SDCARD_LOGGING)
405 WriteLoggingStream loggingStream(logFile,Serial);
418 File write_data_file;
438 bool is_mqtt_rpc_delay;
451 bool mqtt_session_present;
455 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
477 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
489 IPStack ipstack(s800);
499 MQTT::Client<IPStack, Countdown, MQTT_PACKET_SIZE, 1> mqtt_client = MQTT::Client<IPStack, Countdown, MQTT_PACKET_SIZE, 1>(ipstack,
IP_STACK_TIMEOUT_MS);
516 uint8_t display_set = 1;
524 SensorDriver *sensors[SENSORS_MAX];
603 time_t last_lcd_begin;
624 bool is_sdcard_error;
709 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
716 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
721 gsm_state_t gsm_state;
741 sensors_reading_state_t sensors_reading_state;
748 data_saving_state_t data_saving_state;
756 mqtt_state_t mqtt_state;
768 time_t getSystemTime();
923 void setNextTimeForSensorReading(time_t *next_time, uint16_t time_s);
933 bool mqttConnect(
char *username,
char *password);
943 bool mqttPublish(
const char *topic,
const char *message,
bool is_retained =
false);
951 void mqttRxCallback(MQTT::MessageData &md);
982 int prepare(JsonObject ¶ms, JsonObject &result);
991 int getjson(JsonObject ¶ms, JsonObject &result);
1009 int reboot(JsonObject ¶ms, JsonObject &result);
1032 volatile bool is_event_sensors_reading;
1054 void sensors_reading_task (
bool do_prepare =
true,
bool do_get =
true,
char *driver = NULL,
char *type = NULL, uint8_t address = 0, uint8_t node = 0, uint8_t *sensor_index = 0, uint32_t *wait_time = NULL);
1084 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
1099 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
1112 void gsm_task(
void);
1121 bool is_event_data_saving;
1129 void data_saving_task(
void);
1143 bool is_event_mqtt_paused;
1151 void mqtt_task(
void);
1171 #ifndef ARDUINO_ARCH_AVR
1172 #include <IWatchdog.h>
1173 #include "STM32LowPower.h"
1174 #include <STM32RTC.h>
1178 STM32RTC& rtc = STM32RTC::getInstance();
1180 HardwareSerial Serial1(PB11, PB10);
1182 void wdt_enable(
int wdt_timer){
1183 IWatchdog.begin(wdt_timer*1000000);
1190 IWatchdog.begin(32000000);
1193 void power_adc_disable(){};
1194 void power_spi_disable(){};
1195 void power_timer0_disable(){};
1196 void power_timer1_disable(){};
1197 void power_timer2_disable(){};
1198 void power_adc_enable(){};
1199 void power_spi_enable(){};
1200 void power_timer0_enable(){};
1202 void power_timer1_enable(){};
1203 void power_timer2_enable(){};
1208 void set_sleep_mode(
int SLEEP_MODE_PWR_DOWN){
1217 void sleep_enable(){};
1221 void sleep_disable(){};
1224 #define SLEEP_MODE_PWR_DOWN 1
#define JSON_BUFFER_LENGTH
Length in bytes for JSON data buffer.
Definition: json_config.h:30
#define MQTT_SERVER_LENGTH
Length in bytes for mqtt server.
Definition: mqtt_config.h:66
#define MQTT_USERNAME_LENGTH
Length in bytes for mqtt username.
Definition: mqtt_config.h:72
#define MQTT_CLIENT_ID_LENGTH
Length in bytes for mqtt client id.
Definition: mqtt_config.h:54
#define MQTT_MAINT_TOPIC_LENGTH
Length in bytes for mqtt maint topic.
Definition: mqtt_config.h:36
#define MQTT_ROOT_TOPIC_LENGTH
Length in bytes for mqtt root topic.
Definition: mqtt_config.h:30
#define MQTT_SENSOR_TOPIC_LENGTH
Length in bytes for mqtt sensor topic.
Definition: mqtt_config.h:48
#define MQTT_PASSWORD_LENGTH
Length in bytes for mqtt password.
Definition: mqtt_config.h:78
volatile uint8_t i2c_error
Number of i2c error.
Definition: i2c-leaf.h:220
uint32_t awakened_event_occurred_time_ms
System time (in millisecond) when the system has awakened from power down.
Definition: i2c-leaf.h:238
state_t
Main loop finite state machine.
Definition: i2c-leaf.h:100
volatile uint8_t ready_tasks_count
Number of tasks ready to execute.
Definition: i2c-leaf.h:232
state_t state
Current main loop state.
Definition: i2c-leaf.h:283
#define MQTT_RPC_TOPIC_LENGTH
Length in bytes for mqtt rpc topic.
Definition: mqtt_config.h:42
#define MQTT_RPC_RESPONSE_LENGTH
Length in bytes for mqtt rpc response message.
Definition: mqtt_config.h:72
bool is_time_set
If true, the time was readed from rtc or ntp and was setted in system.
Definition: stima.h:554
void load_configuration(void)
Load configuration from EEPROM.
Definition: i2c-leaf.ino:335
void supervisor_task(void)
Supervisor task. Manage RTC and NTP sync and open/close gsm and ethernet connection.
Definition: stima.ino:1699
bool extractSensorsParams(JsonObject ¶ms, char *driver, char *type, uint8_t *address, uint8_t *node)
Extract sensor's parameter.
ethernet_state_t ethernet_state
Ethernet task state.
Definition: stima.h:714
volatile time_t system_time
System time since 01/01/1970 00:00:00.
Definition: stima.h:658
EthernetUDP eth_udp_client
Ethernet UDP client structure.
Definition: stima.h:460
configuration_t writable_configuration
Configuration for this module.
Definition: stima.h:366
void init_tasks(void)
Init tasks variable and state.
Definition: i2c-leaf.ino:225
bool do_reset_first_run
If true, the first reading of the sensors was performed.
Definition: stima.h:536
bool is_first_run
If true, the first reading of the sensors was performed.
Definition: stima.h:530
void init_system(void)
Init system.
Definition: i2c-leaf.ino:290
volatile bool is_event_rtc
Enable or disable the Real Time Clock task.
Definition: stima.h:1060
int prepandget(JsonObject ¶ms, JsonObject &result)
RPC prepare and get json.
supervisor_state_t
Supervisor task finite state machine.
Definition: stima.h:183
@ SUPERVISOR_INIT
init task variables
Definition: stima.h:184
@ SUPERVISOR_WAIT_STATE
non-blocking waiting time
Definition: stima.h:191
@ SUPERVISOR_TIME_LEVEL_TASK
enable time task for sync time with ntp server
Definition: stima.h:187
@ SUPERVISOR_WAIT_CONNECTION_LEVEL_TASK
enable hardware related tasks for doing connection
Definition: stima.h:186
@ SUPERVISOR_MANAGE_LEVEL_TASK
enable tasks for manage data (mqtt)
Definition: stima.h:188
@ SUPERVISOR_END
performs end operations and deactivate task
Definition: stima.h:190
@ SUPERVISOR_CONNECTION_LEVEL_TASK
enable hardware related tasks for doing connection
Definition: stima.h:185
time_state_t time_state
Time task state.
Definition: stima.h:735
bool is_time_for_sensors_reading_updated
If true, the next time has been calculated to read the sensors.
Definition: stima.h:560
supervisor_state_t supervisor_state
Supervisor task state.
Definition: stima.h:707
ethernet_state_t
Ethernet task finite state machine.
Definition: stima.h:199
@ ETHERNET_END
performs end operations and deactivate task
Definition: stima.h:203
@ ETHERNET_CONNECT
begin ethernet operations
Definition: stima.h:201
@ ETHERNET_OPEN_UDP_SOCKET
open udp socket
Definition: stima.h:202
@ ETHERNET_WAIT_STATE
non-blocking waiting time
Definition: stima.h:204
@ ETHERNET_INIT
init task variables
Definition: stima.h:200
bool is_event_ethernet
Enable or disable the Ethernet task.
Definition: stima.h:1089
void init_spi(void)
Init SPI library.
Definition: i2c-leaf.ino:262
sensors_reading_state_t
Sensors reading task finite state machine.
Definition: stima.h:234
@ SENSORS_READING_INIT
init task variables
Definition: stima.h:235
@ SENSORS_SETUP_CHECK
check errors and if required try a sensor setup
Definition: stima.h:236
@ SENSORS_READING_IS_GETTED
check if the sensor has been readed
Definition: stima.h:240
@ SENSORS_READING_NEXT
go to next sensor
Definition: stima.h:242
@ SENSORS_READING_PREPARE
prepare sensor
Definition: stima.h:237
@ SENSORS_READING_GET
read and get values from sensor
Definition: stima.h:239
@ SENSORS_READING_IS_PREPARED
check if the sensor has been prepared
Definition: stima.h:238
@ SENSORS_READING_END
performs end operations and deactivate task
Definition: stima.h:243
@ SENSORS_READING_READ
intermediate state (future implementation...)
Definition: stima.h:241
@ SENSORS_READING_WAIT_STATE
non-blocking waiting time
Definition: stima.h:244
void init_logging(void)
Init logging system.
Definition: i2c-leaf.ino:141
void init_timer1(void)
Init Timer1 module.
Definition: stima.ino:737
void time_task(void)
Time task. Get time from NTP and sync RTC with it.
Definition: stima.ino:2085
void rtc_task(void)
Real Time Clock task. Read RTC time and sync system time with it.
Definition: stima.ino:2060
bool is_client_udp_socket_open
If true, the client (ethernet or gsm) was opened the UDP socket.
Definition: stima.h:572
bool is_datetime_set
A valid date and time is setted and usable by station.
Definition: stima.h:348
rpc_state_t
RPC task finite state machine.
Definition: stima.h:328
@ RPC_EXECUTE
execute function loop
Definition: stima.h:330
@ RPC_INIT
init task variables
Definition: stima.h:329
@ RPC_END
performs end operations
Definition: stima.h:331
char json_sensors_data[SENSORS_MAX][JSON_BUFFER_LENGTH]
buffer containing the data read by sensors in json text format.
Definition: stima.h:646
void interrupt_task_1s(void)
1 seconds task.
Definition: stima.ino:1643
int reboot(JsonObject ¶ms, JsonObject &result)
RPC reboot.
time_state_t
Time task finite state machine.
Definition: stima.h:251
@ TIME_WAIT_ONLINE_RESPONSE
wait ntp response
Definition: stima.h:254
@ TIME_END
performs end operations and deactivate task
Definition: stima.h:257
@ TIME_SET_SYNC_RTC_PROVIDER
set rtc time
Definition: stima.h:256
@ TIME_INIT
init task variables
Definition: stima.h:252
@ TIME_WAIT_STATE
non-blocking waiting time
Definition: stima.h:258
@ TIME_SEND_ONLINE_REQUEST
send ntp request
Definition: stima.h:253
@ TIME_SET_SYNC_NTP_PROVIDER
set ntp time
Definition: stima.h:255
bool is_event_rpc
Indicate if RPC is active or not.
Definition: stima.h:1158
void init_wire(void)
Init wire (i2c) library and performs checks on the bus.
Definition: i2c-leaf.ino:251
int prepare(JsonObject ¶ms, JsonObject &result)
RPC prepare.
char stima_name[20]
Name of this module.
Definition: stima.h:695
bool have_to_reboot
Request for a reboot as soon as possible.
Definition: stima.h:354
volatile bool is_event_time
Enable or disable the Time task.
Definition: stima.h:1074
void save_configuration(bool)
Save configuration to EEPROM.
Definition: i2c-leaf.ino:312
void init_rtc(void)
Init RTC module.
Definition: i2c-leaf.ino:266
void sensors_reading_task(bool do_prepare=true, bool do_get=true, char *driver=NULL, char *type=NULL, uint8_t address=0, uint8_t node=0, uint8_t *sensor_index=0, uint32_t *wait_time=NULL)
Sensors reading Task. Read data from sensors.
Definition: stima.ino:2644
void set_default_configuration(void)
Set default configuration to global configuration variable.
Definition: stima.ino:995
void init_wdt(uint8_t wdt_timer)
Init watchdog.
Definition: i2c-leaf.ino:202
int configure(JsonObject ¶ms, JsonObject &result)
RPC configuration.
state_t
Main loop finite state machine.
Definition: stima.h:169
@ REBOOT
reboot the machine
Definition: stima.h:176
@ INIT
init tasks and sensors
Definition: stima.h:170
@ TASKS_EXECUTION
execute active tasks
Definition: stima.h:174
@ END
go to ENTER_POWER_DOWN or TASKS_EXECUTION
Definition: stima.h:175
rpc_state_t rpc_state
RPC task state.
Definition: stima.h:763
void start_timer(void)
Start Timer1 module.
Definition: stima.ino:741
void init_pins(void)
Init hardware pins.
Definition: i2c-leaf.ino:244
bool is_event_sensors_reading_rpc
Enable or disable the Sensors reading task from RPC.
Definition: stima.h:1038
void print_configuration(void)
Print current configuration.
Definition: i2c-leaf.ino:301
void ethernet_task(void)
Ethernet task. Manage Ethernet operation.
Definition: stima.ino:2294
time_t ptr_time_data
Readed data pointer stored on SD-Card for data send.
Definition: stima.h:689
bool do_ntp_sync
If true, you must update the time from ntp.
Definition: stima.h:590
EthernetClient eth_tcp_client
Ethernet TCP client structure.
Definition: stima.h:466
bool is_event_client_executed
If true, the client has executed its task.
Definition: stima.h:578
int getjson(JsonObject ¶ms, JsonObject &result)
RPC get json.
volatile time_t next_ptr_time_for_testing_sensors
Next scheduled time (in seconds since 01/01/1970 00:0:00) for sensors reading.
Definition: stima.h:670
char json_sensors_data_test[JSON_BUFFER_LENGTH]
buffer containing the data read by sensors in json text format for test only.
Definition: stima.h:652
bool is_first_test
If true, the first reading of the sensors was performed.
Definition: stima.h:542
volatile time_t next_ptr_time_for_sensors_reading
Next scheduled time (in seconds since 01/01/1970 00:0:00) for sensors reading.
Definition: stima.h:664
void init_buffers(void)
Init buffers.
Definition: i2c-leaf.ino:208
void rtc_interrupt_handler(void)
Real Time Clock interrupt handler.
uint8_t sensor_reading_failed_count
Counter for failed and skipped sensors.
Definition: stima.h:677
void init_sensors(void)
Create and setup sensors.
Definition: i2c-leaf.ino:353
void stop_timer(void)
Stop Timer1 module.
Definition: stima.ino:749
bool is_event_time_executed
If true, the time task has executed.
Definition: stima.h:584
bool is_event_supervisor
Enable or disable the Supervisor task.
Definition: stima.h:1018
void init_rpc(void)
Register RPC.
Definition: stima.ino:703
time_t last_ntp_sync
Last date and time when ntp sync was performed.
Definition: stima.h:596
JsonRPC streamRpc(false)
Remote Procedure Call object.
volatile tmElements_t sensor_reading_time
Date and time corresponding to the last reading of the sensors.
Definition: stima.h:683
configuration_t readable_configuration
Configuration for this module.
Definition: stima.h:360
bool is_client_connected
If true, the client (ethernet or gsm) was connected to socket (TCP or UDP).
Definition: stima.h:566
void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms)
Enter power down mode.
Definition: i2c-leaf.ino:173
EEPROM saved configuration.
Definition: i2c-leaf.h:55
Definition: sensor_config.h:18
sensors configuration.
Definition: typedef.h:112