STIMA  3
stima.h
Go to the documentation of this file.
1 
3 /*********************************************************************
4 Copyright (C) 2017 Marco Baldinetti <m.baldinetti@digiteco.it>
5 authors:
6 Paolo Patruno <p.patruno@iperbole.bologna.it>
7 Marco Baldinetti <m.baldinetti@digiteco.it>
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **********************************************************************/
22 
23 #ifndef _STIMA_H
24 #define _STIMA_H
25 
26 #include <stima_module.h>
27 #include "stima-config.h"
28 #include <typedef.h>
29 #include <registers.h>
30 #include <registers.h>
31 #include <registers-master.h>
32 #include <debug.h>
33 #include <ArduinoLog.h>
34 #include <StreamUtils.h>
35 
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>
41 
42 #if (USE_LCD)
43 #include <Wire.h>
44 #include <lcd_config.h>
45 #include <hd44780.h>
46 #include <hd44780ioClass/hd44780_I2Cexp.h> // i2c LCD i/o class header
47 #endif
48 
49 #ifdef ARDUINO_ARCH_AVR
50 #include <avr/sleep.h>
51 #include <avr/power.h>
52 #include <avr/wdt.h>
53 #endif
54 
55 #include <SPI.h>
56 #include <Wire.h>
57 
58 #if (USE_SDCARD)
59 #include <sdcard_utility.h>
60 #endif
61 
62 #include <pcf8563.h>
63 
64 #if (USE_NTP)
65 #include <ntp.h>
66 #endif
67 
68 #include <rmap_utility.h>
69 #if (USE_JSON)
70 #include <json_utility.h>
71 #endif
72 #include <TimeLib.h>
73 #include <SensorDriver.h>
74 #include <i2c_utility.h>
75 #include <arduinoJsonRPC.h>
76 
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>
80 #if (USE_MQTT)
81 #include <IPStack.h>
82 #endif
83 
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>
87 #if (USE_MQTT)
88 #include <Sim800IPStack.h>
89 #endif
90 
91 #endif
92 
93 #if (USE_MQTT)
94 #include <Countdown.h>
95 #include <MQTTClient.h>
96 
97 #endif
98 
99 /*********************************************************************
100 * TYPEDEF
101 *********************************************************************/
102 
107 typedef struct {
108  char btable[CONSTANTDATA_BTABLE_LENGTH];
109  char value[CONSTANTDATA_VALUE_LENGTH];
111 
116 typedef struct {
117  uint8_t module_main_version;
118  uint8_t module_configuration_version;
119  uint8_t module_type;
120 
121  #if (USE_MQTT)
122  uint16_t mqtt_port;
123  char mqtt_server[MQTT_SERVER_LENGTH];
124  char mqtt_root_topic[MQTT_ROOT_TOPIC_LENGTH];
125  char mqtt_maint_topic[MQTT_MAINT_TOPIC_LENGTH];
126  char mqtt_rpc_topic[MQTT_RPC_TOPIC_LENGTH];
127  char mqtt_username[MQTT_USERNAME_LENGTH];
128  char mqtt_password[MQTT_PASSWORD_LENGTH];
129  char stationslug[STATIONSLUG_LENGTH];
130  char boardslug[BOARDSLUG_LENGTH];
131  #endif
132 
133  #if (USE_NTP)
134  char ntp_server[NTP_SERVER_LENGTH];
135  #endif
136 
137  sensor_t sensors[SENSORS_MAX];
138  uint8_t sensors_count;
139  uint16_t report_seconds;
140 
141  constantdata_t constantdata[USE_CONSTANTDATA_COUNT];
142  uint8_t constantdata_count;
143 
144  #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
145  bool is_dhcp_enable;
146  uint8_t ethernet_mac[ETHERNET_MAC_LENGTH];
147  uint8_t ip[ETHERNET_IP_LENGTH];
148  uint8_t netmask[ETHERNET_IP_LENGTH];
149  uint8_t gateway[ETHERNET_IP_LENGTH];
150  uint8_t primary_dns[ETHERNET_IP_LENGTH];
151 
152  #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
153  char gsm_apn[GSM_APN_LENGTH];
154  char gsm_username[GSM_USERNAME_LENGTH];
155  char gsm_password[GSM_PASSWORD_LENGTH];
156 
157  #endif
159 
160 
161 /*********************************************************************
162 * TYPEDEF for Finite State Machine
163 *********************************************************************/
164 
169 typedef enum {
171  #if (USE_POWER_DOWN)
172  ENTER_POWER_DOWN,
173  #endif
175  END,
176  REBOOT
178 
183 typedef enum {
189  SUPERVISOR_TEST_SDCARD,
193 
194 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
199 typedef enum {
206 
207 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
212 typedef enum {
213  GSM_INIT,
214  GSM_SWITCH_ON,
215  GSM_AUTOBAUD,
216  GSM_SETUP,
217  GSM_START_CONNECTION,
218  GSM_CHECK_OPERATION,
219  GSM_OPEN_UDP_SOCKET,
220  GSM_SUSPEND,
221  GSM_STOP_CONNECTION,
222  GSM_WAIT_FOR_SWITCH_OFF,
223  GSM_SWITCH_OFF,
224  GSM_END,
225  GSM_WAIT_STATE
226 } gsm_state_t;
227 
228 #endif
229 
234 typedef enum {
246 
251 typedef enum {
260 
261 #if (USE_SDCARD)
266 typedef enum {
267  DATA_SAVING_INIT,
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,
274  DATA_SAVING_END,
275  DATA_SAVING_WAIT_STATE
276 } data_saving_state_t;
277 #endif
278 
279 #if (USE_MQTT)
284 typedef enum {
285  MQTT_INIT,
286 
287  MQTT_OPEN_SDCARD,
288  MQTT_OPEN_PTR_FILE,
289  MQTT_PTR_READ,
290  MQTT_PTR_FIND,
291  MQTT_PTR_FOUND,
292  MQTT_PTR_END,
293 
294  MQTT_OPEN,
295  MQTT_CHECK,
296  MQTT_CONNECT,
297  MQTT_ON_CONNECT,
298  MQTT_SUBSCRIBE,
299  MQTT_CONSTANTDATA,
300 
301  MQTT_OPEN_DATA_FILE,
302 
303  MQTT_SENSORS_LOOP,
304  MQTT_DATA_LOOP,
305  MQTT_SD_LOOP,
306  MQTT_PUBLISH,
307 
308  MQTT_CLOSE_DATA_FILE,
309 
310  MQTT_DISCONNECT,
311  MQTT_RPC_DELAY,
312  MQTT_ON_DISCONNECT,
313 
314  MQTT_PTR_UPDATE,
315  MQTT_CLOSE_PTR_FILE,
316  // MQTT_CLOSE_SDCARD, //!< close sdcard
317 
318  MQTT_END,
319  MQTT_WAIT_STATE,
320  MQTT_WAIT_STATE_RPC
321 } mqtt_state_t;
322 #endif
323 
328 typedef enum {
331  RPC_END
333 
334 /*********************************************************************
335 * GLOBAL VARIABLE
336 *********************************************************************/
337 
342 uint8_t sensors_count = 0;
343 
349 
355 
361 
367 
372 volatile uint8_t ready_tasks_count;
373 
379 
384 JsonRPC streamRpc(false);
385 
386 #if (USE_SDCARD)
391 SdFat SD;
392 
393 #if (ENABLE_SDCARD_LOGGING)
398 File logFile;
399 
400 
405 WriteLoggingStream loggingStream(logFile,Serial);
406 #endif
407 
412 File read_data_file;
413 
418 File write_data_file;
419 
424 File test_file;
425 #endif
426 
427 #if (USE_MQTT)
432 File mqtt_ptr_file;
433 
438 bool is_mqtt_rpc_delay;
439 
444 char rpcpayload[MQTT_RPC_RESPONSE_LENGTH];
445 
446 
451 bool mqtt_session_present;
452 
453 #endif
454 
455 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
460 EthernetUDP eth_udp_client;
461 
466 EthernetClient eth_tcp_client;
467 
468 #if (USE_MQTT)
474 IPStack ipstack(eth_tcp_client);
475 #endif
476 
477 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
482 sim800Client s800;
483 
484 #if (USE_MQTT)
489 IPStack ipstack(s800);
490 #endif
491 
492 #endif
493 
494 #if (USE_MQTT)
499 MQTT::Client<IPStack, Countdown, MQTT_PACKET_SIZE, 1> mqtt_client = MQTT::Client<IPStack, Countdown, MQTT_PACKET_SIZE, 1>(ipstack, IP_STACK_TIMEOUT_MS);
500 
501 #endif
502 
503 #if (USE_LCD)
509 hd44780_I2Cexp lcd;
510 
511 
516 uint8_t display_set = 1;
517 
518 #endif
519 
524 SensorDriver *sensors[SENSORS_MAX];
525 
531 
537 
543 
548 bool is_test;
549 
555 
561 
567 
573 
579 
585 
591 
597 
598 #if (USE_LCD)
603 time_t last_lcd_begin;
604 
609 bool lcd_error;
610 
611 #endif
612 
613 #if (USE_SDCARD)
618 bool is_sdcard_open;
619 
624 bool is_sdcard_error;
625 #endif
626 
627 #if (USE_MQTT)
628 
633 char client_id[MQTT_CLIENT_ID_LENGTH];
634 
640 #endif
641 
647 
653 
658 volatile time_t system_time;
659 
665 
671 
672 
678 
683 volatile tmElements_t sensor_reading_time;
684 
690 
695 char stima_name[20];
696 
701 state_t state;
702 
708 
709 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
715 
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;
722 
723 #endif
724 
729 uint8_t i2c_error;
730 
736 
741 sensors_reading_state_t sensors_reading_state;
742 
743 #if (USE_SDCARD)
748 data_saving_state_t data_saving_state;
749 #endif
750 
751 #if (USE_MQTT)
756 mqtt_state_t mqtt_state;
757 #endif
758 
764 /*********************************************************************
765 * FUNCTIONS
766 *********************************************************************/
767 void realreboot();
768 time_t getSystemTime();
769 
775 void init_logging(void);
776 
784 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
785 
792 void init_wdt(uint8_t wdt_timer);
793 
799 void init_system(void);
800 
806 void init_rpc(void);
807 
813 void init_buffers(void);
814 
820 void init_tasks(void);
821 
827 void init_pins(void);
828 
834 void init_wire(void);
835 
841 void init_spi(void);
842 
848 void init_rtc(void);
849 
850 #if (USE_TIMER_1)
856 void init_timer1(void);
857 
863 void start_timer(void);
864 
870 void stop_timer(void);
871 #endif
872 
878 void interrupt_task_1s(void);
879 
885 void init_sensors(void);
886 
892 void print_configuration(void);
893 
899 void load_configuration(void);
900 
907 void save_configuration(bool);
908 
914 void set_default_configuration(void);
915 
923 void setNextTimeForSensorReading(time_t *next_time, uint16_t time_s);
924 
925 #if (USE_MQTT)
933 bool mqttConnect(char *username, char *password);
934 
943 bool mqttPublish(const char *topic, const char *message, bool is_retained = false);
944 
951 void mqttRxCallback(MQTT::MessageData &md);
952 #endif
953 
964 bool extractSensorsParams(JsonObject &params, char *driver, char *type, uint8_t *address, uint8_t *node);
965 
973 int configure(JsonObject &params, JsonObject &result);
974 
982 int prepare(JsonObject &params, JsonObject &result);
983 
991 int getjson(JsonObject &params, JsonObject &result);
992 
1000 int prepandget(JsonObject &params, JsonObject &result);
1001 
1009 int reboot(JsonObject &params, JsonObject &result);
1010 
1011 /*********************************************************************
1012 * TASKS
1013 *********************************************************************/
1019 
1026 void supervisor_task(void);
1027 
1032 volatile bool is_event_sensors_reading;
1033 
1039 
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);
1055 
1060 volatile bool is_event_rtc;
1061 
1068 void rtc_task(void);
1069 
1074 volatile bool is_event_time;
1075 
1082 void time_task(void);
1083 
1084 #if (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_ETH || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_ETH)
1090 
1097 void ethernet_task(void);
1098 
1099 #elif (MODULE_TYPE == STIMA_MODULE_TYPE_SAMPLE_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_REPORT_GSM || MODULE_TYPE == STIMA_MODULE_TYPE_PASSIVE_GSM)
1104 bool is_event_gsm;
1105 
1112 void gsm_task(void);
1113 
1114 #endif
1115 
1116 #if (USE_SDCARD)
1121 bool is_event_data_saving;
1122 
1129 void data_saving_task(void);
1130 #endif
1131 
1132 #if (USE_MQTT)
1137 bool is_event_mqtt;
1138 
1143 bool is_event_mqtt_paused;
1144 
1151 void mqtt_task(void);
1152 #endif
1153 
1159 
1160 /*********************************************************************
1161 * INTERRUPT HANDLER
1162 *********************************************************************/
1169 
1170 
1171 #ifndef ARDUINO_ARCH_AVR
1172 #include <IWatchdog.h>
1173 #include "STM32LowPower.h"
1174 #include <STM32RTC.h>
1175 #include <rtc.h>
1176 
1177 /* Get the rtc object */
1178 STM32RTC& rtc = STM32RTC::getInstance();
1179 
1180 HardwareSerial Serial1(PB11, PB10);
1181 
1182 void wdt_enable(int wdt_timer){
1183  IWatchdog.begin(wdt_timer*1000000);
1184 };
1185 void wdt_reset(){
1186  IWatchdog.reload();
1187 };
1188 void wdt_disable(){
1189  //WARNING: Once started the IWDG timer can not be stopped.
1190  IWatchdog.begin(32000000); // set to maximum value
1191 };
1192 
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(){};
1201 
1202 void power_timer1_enable(){};
1203 void power_timer2_enable(){};
1204 
1205 
1206 // To be done
1207 // manage sleep mode: RTC and timer 1 ....
1208 void set_sleep_mode(int SLEEP_MODE_PWR_DOWN){
1209  // Select RTC clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK.
1210  // By default the LSI is selected as source.
1211  /*
1212  rtc.setClockSource(STM32RTC::LSE_CLOCK);
1213  rtc.begin();
1214  LowPower.begin();
1215  */
1216 };
1217 void sleep_enable(){};
1218 void sleep_cpu(){
1219  //LowPower.deepSleep(1000);
1220 };
1221 void sleep_disable(){};
1222 
1223 #define WDTO_1S 1
1224 #define SLEEP_MODE_PWR_DOWN 1
1225 
1226 #endif
1227 
1228 
1229 #endif
#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
#define CONSTANTDATA_VALUE_LENGTH
Maximum lenght of value plus terminator for one constant data.
Definition: constantdata_config.h:36
#define CONSTANTDATA_BTABLE_LENGTH
Maximum lenght of btable code plus terminator that describe one constant data.
Definition: constantdata_config.h:30
#define ETHERNET_MAC_LENGTH
Length in bytes for mac address.
Definition: ethernet_config.h:96
#define ETHERNET_IP_LENGTH
Length in bytes for ip address.
Definition: ethernet_config.h:102
#define GSM_USERNAME_LENGTH
Length in bytes for username.
Definition: gsm_config.h:72
#define GSM_PASSWORD_LENGTH
Length in bytes for password.
Definition: gsm_config.h:78
#define GSM_APN_LENGTH
Length in bytes for apn.
Definition: gsm_config.h:66
#define STATIONSLUG_LENGTH
Length in bytes for station slug.
Definition: mqtt_config.h:102
#define BOARDSLUG_LENGTH
Length in bytes for board slug.
Definition: mqtt_config.h:108
#define NTP_SERVER_LENGTH
Length in bytes for ntp server data buffer.
Definition: ntp_config.h:30
#define IP_STACK_TIMEOUT_MS
IPStack timeout.
Definition: stima-config.h:493
#define USE_CONSTANTDATA_COUNT
Constantdata count.
Definition: stima-config.h:430
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 &params, 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 &params, 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 &params, 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 &params, 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 &params, 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 &params, 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