STIMA  3
Classes | Macros | Enumerations | Functions | Variables
stima.h File Reference
#include <stima_module.h>
#include "stima-config.h"
#include <typedef.h>
#include <registers.h>
#include <registers-master.h>
#include <debug.h>
#include <ArduinoLog.h>
#include <StreamUtils.h>
#include <i2c_config.h>
#include <json_config.h>
#include <ntp_config.h>
#include <constantdata_config.h>
#include <gsm_config.h>
#include <SPI.h>
#include <Wire.h>
#include <pcf8563.h>
#include <rmap_utility.h>
#include <TimeLib.h>
#include <SensorDriver.h>
#include <i2c_utility.h>
#include <arduinoJsonRPC.h>
#include <ethernet_config.h>
#include <Ethernet2.h>
#include <IWatchdog.h>
#include "STM32LowPower.h"
#include <STM32RTC.h>
#include <rtc.h>
Include dependency graph for stima.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  constantdata_t
 
struct  configuration_t
 EEPROM saved configuration. More...
 

Macros

#define WDTO_1S   1
 
#define SLEEP_MODE_PWR_DOWN   1
 

Enumerations

enum  state_t {
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END , INIT , TASKS_EXECUTION ,
  END , INIT , TASKS_EXECUTION , END ,
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END , INIT , TASKS_EXECUTION ,
  END , INIT , TASKS_EXECUTION , END ,
  INIT , TASKS_EXECUTION , END , REBOOT ,
  INIT , TASKS_EXECUTION , END , INIT ,
  TASKS_EXECUTION , END
}
 Main loop finite state machine. More...
 
enum  supervisor_state_t {
  SUPERVISOR_INIT , SUPERVISOR_CONNECTION_LEVEL_TASK , SUPERVISOR_WAIT_CONNECTION_LEVEL_TASK , SUPERVISOR_TIME_LEVEL_TASK ,
  SUPERVISOR_MANAGE_LEVEL_TASK , SUPERVISOR_TEST_SDCARD , SUPERVISOR_END , SUPERVISOR_WAIT_STATE
}
 Supervisor task finite state machine. More...
 
enum  ethernet_state_t {
  ETHERNET_INIT , ETHERNET_CONNECT , ETHERNET_OPEN_UDP_SOCKET , ETHERNET_END ,
  ETHERNET_WAIT_STATE
}
 Ethernet task finite state machine. More...
 
enum  sensors_reading_state_t {
  SENSORS_READING_INIT , SENSORS_READING_SETUP_CHECK , SENSORS_READING_POWER_OFF , SENSORS_READING_POWER_ON ,
  SENSORS_READING_SETUP , SENSORS_READING_PREPARE , SENSORS_READING_IS_PREPARED , SENSORS_READING_GET ,
  SENSORS_READING_IS_GETTED , SENSORS_READING_READ , SENSORS_READING_NEXT , SENSORS_READING_END ,
  SENSORS_READING_WAIT_STATE , SENSORS_READING_INIT , SENSORS_SETUP_CHECK , SENSORS_READING_PREPARE ,
  SENSORS_READING_IS_PREPARED , SENSORS_READING_GET , SENSORS_READING_IS_GETTED , SENSORS_READING_READ ,
  SENSORS_READING_NEXT , SENSORS_READING_END , SENSORS_READING_WAIT_STATE , SENSORS_READING_INIT ,
  SENSORS_READING_SETUP_CHECK , SENSORS_READING_POWER_OFF , SENSORS_READING_POWER_ON , SENSORS_READING_SETUP ,
  SENSORS_READING_PREPARE , SENSORS_READING_IS_PREPARED , SENSORS_READING_GET , SENSORS_READING_IS_GETTED ,
  SENSORS_READING_READ , SENSORS_READING_NEXT , SENSORS_READING_END , SENSORS_READING_WAIT_STATE ,
  SENSORS_READING_INIT , SENSORS_READING_PREPARE , SENSORS_READING_IS_PREPARED , SENSORS_READING_GET ,
  SENSORS_READING_IS_GETTED , SENSORS_READING_READ , SENSORS_READING_NEXT , SENSORS_READING_END ,
  SENSORS_READING_WAIT_STATE
}
 Sensors reading task finite state machine. More...
 
enum  time_state_t {
  TIME_INIT , TIME_SEND_ONLINE_REQUEST , TIME_WAIT_ONLINE_RESPONSE , TIME_SET_SYNC_NTP_PROVIDER ,
  TIME_SET_SYNC_RTC_PROVIDER , TIME_END , TIME_WAIT_STATE
}
 Time task finite state machine. More...
 
enum  rpc_state_t { RPC_INIT , RPC_EXECUTE , RPC_END }
 RPC task finite state machine. More...
 

Functions

JsonRPC streamRpc (false)
 Remote Procedure Call object.
 
void realreboot ()
 
time_t getSystemTime ()
 
void init_logging (void)
 Init logging system. More...
 
void init_power_down (uint32_t *time_ms, uint32_t debouncing_ms)
 Enter power down mode. More...
 
void init_wdt (uint8_t wdt_timer)
 Init watchdog. More...
 
void init_system (void)
 Init system. More...
 
void init_rpc (void)
 Register RPC. More...
 
void init_buffers (void)
 Init buffers. More...
 
void init_tasks (void)
 Init tasks variable and state. More...
 
void init_pins (void)
 Init hardware pins. More...
 
void init_wire (void)
 Init wire (i2c) library and performs checks on the bus. More...
 
void init_spi (void)
 Init SPI library. More...
 
void init_rtc (void)
 Init RTC module. More...
 
void init_timer1 (void)
 Init Timer1 module. More...
 
void start_timer (void)
 Start Timer1 module. More...
 
void stop_timer (void)
 Stop Timer1 module. More...
 
void interrupt_task_1s (void)
 1 seconds task. More...
 
void init_sensors (void)
 Create and setup sensors. More...
 
void print_configuration (void)
 Print current configuration. More...
 
void load_configuration (void)
 Load configuration from EEPROM. More...
 
void save_configuration (bool)
 Save configuration to EEPROM. More...
 
void set_default_configuration (void)
 Set default configuration to global configuration variable. More...
 
void setNextTimeForSensorReading (time_t *next_time, uint16_t time_s)
 
bool extractSensorsParams (JsonObject &params, char *driver, char *type, uint8_t *address, uint8_t *node)
 Extract sensor's parameter. More...
 
int configure (JsonObject &params, JsonObject &result)
 RPC configuration. More...
 
int prepare (JsonObject &params, JsonObject &result)
 RPC prepare. More...
 
int getjson (JsonObject &params, JsonObject &result)
 RPC get json. More...
 
int prepandget (JsonObject &params, JsonObject &result)
 RPC prepare and get json. More...
 
int reboot (JsonObject &params, JsonObject &result)
 RPC reboot. More...
 
void supervisor_task (void)
 Supervisor task. Manage RTC and NTP sync and open/close gsm and ethernet connection. More...
 
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. More...
 
void rtc_task (void)
 Real Time Clock task. Read RTC time and sync system time with it. More...
 
void time_task (void)
 Time task. Get time from NTP and sync RTC with it. More...
 
void ethernet_task (void)
 Ethernet task. Manage Ethernet operation. More...
 
void rtc_interrupt_handler (void)
 Real Time Clock interrupt handler. More...
 
HardwareSerial Serial1 (PB11, PB10)
 
void wdt_enable (int wdt_timer)
 
void wdt_reset ()
 
void wdt_disable ()
 
void power_adc_disable ()
 
void power_spi_disable ()
 
void power_timer0_disable ()
 
void power_timer1_disable ()
 
void power_timer2_disable ()
 
void power_adc_enable ()
 
void power_spi_enable ()
 
void power_timer0_enable ()
 
void power_timer1_enable ()
 
void power_timer2_enable ()
 
void set_sleep_mode (int SLEEP_MODE_PWR_DOWN)
 
void sleep_enable ()
 
void sleep_cpu ()
 
void sleep_disable ()
 

Variables

uint8_t sensors_count = 0
 
bool is_datetime_set
 A valid date and time is setted and usable by station.
 
bool have_to_reboot
 Request for a reboot as soon as possible.
 
configuration_t readable_configuration
 Configuration for this module.
 
configuration_t writable_configuration
 Configuration for this module.
 
volatile uint8_t ready_tasks_count
 
uint32_t awakened_event_occurred_time_ms
 
EthernetUDP eth_udp_client
 Ethernet UDP client structure.
 
EthernetClient eth_tcp_client
 Ethernet TCP client structure.
 
SensorDriver * sensors [SENSORS_MAX]
 
bool is_first_run
 If true, the first reading of the sensors was performed.
 
bool do_reset_first_run
 If true, the first reading of the sensors was performed.
 
bool is_first_test
 If true, the first reading of the sensors was performed.
 
bool is_test
 
bool is_time_set
 If true, the time was readed from rtc or ntp and was setted in system.
 
bool is_time_for_sensors_reading_updated
 If true, the next time has been calculated to read the sensors.
 
bool is_client_connected
 If true, the client (ethernet or gsm) was connected to socket (TCP or UDP).
 
bool is_client_udp_socket_open
 If true, the client (ethernet or gsm) was opened the UDP socket.
 
bool is_event_client_executed
 If true, the client has executed its task.
 
bool is_event_time_executed
 If true, the time task has executed.
 
bool do_ntp_sync
 If true, you must update the time from ntp.
 
time_t last_ntp_sync
 Last date and time when ntp sync was performed.
 
char json_sensors_data [SENSORS_MAX][JSON_BUFFER_LENGTH]
 buffer containing the data read by sensors in json text format.
 
char json_sensors_data_test [JSON_BUFFER_LENGTH]
 buffer containing the data read by sensors in json text format for test only.
 
volatile time_t system_time
 System time since 01/01/1970 00:00:00.
 
volatile time_t next_ptr_time_for_sensors_reading
 Next scheduled time (in seconds since 01/01/1970 00:0:00) for sensors reading.
 
volatile time_t next_ptr_time_for_testing_sensors
 Next scheduled time (in seconds since 01/01/1970 00:0:00) for sensors reading.
 
uint8_t sensor_reading_failed_count
 Counter for failed and skipped sensors.
 
volatile tmElements_t sensor_reading_time
 Date and time corresponding to the last reading of the sensors.
 
time_t ptr_time_data
 Readed data pointer stored on SD-Card for data send.
 
char stima_name [20]
 Name of this module.
 
state_t state
 
supervisor_state_t supervisor_state
 Supervisor task state.
 
ethernet_state_t ethernet_state
 Ethernet task state.
 
uint8_t i2c_error
 
time_state_t time_state
 Time task state.
 
sensors_reading_state_t sensors_reading_state
 
rpc_state_t rpc_state
 RPC task state.
 
bool is_event_supervisor
 Enable or disable the Supervisor task.
 
volatile bool is_event_sensors_reading
 
bool is_event_sensors_reading_rpc
 Enable or disable the Sensors reading task from RPC.
 
volatile bool is_event_rtc
 Enable or disable the Real Time Clock task.
 
volatile bool is_event_time
 Enable or disable the Time task.
 
bool is_event_ethernet
 Enable or disable the Ethernet task.
 
bool is_event_rpc
 Indicate if RPC is active or not.
 
STM32RTC & rtc = STM32RTC::getInstance()
 

Enumeration Type Documentation

◆ ethernet_state_t

Ethernet task finite state machine.

Enumerator
ETHERNET_INIT 

init task variables

ETHERNET_CONNECT 

begin ethernet operations

ETHERNET_OPEN_UDP_SOCKET 

open udp socket

ETHERNET_END 

performs end operations and deactivate task

ETHERNET_WAIT_STATE 

non-blocking waiting time

◆ rpc_state_t

RPC task finite state machine.

Enumerator
RPC_INIT 

init task variables

RPC_EXECUTE 

execute function loop

RPC_END 

performs end operations

◆ sensors_reading_state_t

enum sensors_reading_state_t

Sensors reading task finite state machine.

Enumerator
SENSORS_READING_INIT 

init task variables

SENSORS_SETUP_CHECK 

check errors and if required try a sensor setup

SENSORS_READING_PREPARE 

prepare sensor

SENSORS_READING_IS_PREPARED 

check if the sensor has been prepared

SENSORS_READING_GET 

read and get values from sensor

SENSORS_READING_IS_GETTED 

check if the sensor has been readed

SENSORS_READING_READ 

intermediate state (future implementation...)

SENSORS_READING_NEXT 

go to next sensor

SENSORS_READING_END 

performs end operations and deactivate task

SENSORS_READING_WAIT_STATE 

non-blocking waiting time

◆ state_t

enum state_t

Main loop finite state machine.

Enumerator
INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

INIT 

init tasks and sensors

TASKS_EXECUTION 

execute active tasks

END 

go to ENTER_POWER_DOWN or TASKS_EXECUTION

REBOOT 

reboot the machine

◆ supervisor_state_t

Supervisor task finite state machine.

Enumerator
SUPERVISOR_INIT 

init task variables

SUPERVISOR_CONNECTION_LEVEL_TASK 

enable hardware related tasks for doing connection

SUPERVISOR_WAIT_CONNECTION_LEVEL_TASK 

enable hardware related tasks for doing connection

SUPERVISOR_TIME_LEVEL_TASK 

enable time task for sync time with ntp server

SUPERVISOR_MANAGE_LEVEL_TASK 

enable tasks for manage data (mqtt)

SUPERVISOR_END 

performs end operations and deactivate task

SUPERVISOR_WAIT_STATE 

non-blocking waiting time

◆ time_state_t

Time task finite state machine.

Enumerator
TIME_INIT 

init task variables

TIME_SEND_ONLINE_REQUEST 

send ntp request

TIME_WAIT_ONLINE_RESPONSE 

wait ntp response

TIME_SET_SYNC_NTP_PROVIDER 

set ntp time

TIME_SET_SYNC_RTC_PROVIDER 

set rtc time

TIME_END 

performs end operations and deactivate task

TIME_WAIT_STATE 

non-blocking waiting time

Function Documentation

◆ configure()

int configure ( JsonObject &  params,
JsonObject &  result 
)

RPC configuration.

Parameters
*paramsjson's params
&resultjson's response
Returns
status code.

◆ ethernet_task()

void ethernet_task ( void  )

Ethernet task. Manage Ethernet operation.

Returns
void.

◆ extractSensorsParams()

bool extractSensorsParams ( JsonObject &  params,
char *  driver,
char *  type,
uint8_t *  address,
uint8_t *  node 
)

Extract sensor's parameter.

Parameters
[in]*paramsjson's params
[out]*driverdriver
[out]*typetype
[out]*addressaddress
[out]*nodenode
Returns
status code.

◆ getjson()

int getjson ( JsonObject &  params,
JsonObject &  result 
)

RPC get json.

Parameters
*paramsjson's params
&resultjson's response
Returns
status code.

◆ init_buffers()

void init_buffers ( )

Init buffers.

Returns
void.

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_write in readable_data_read

copy readable_data_2 in readable_data_1

copy readable_data_write in readable_data_read

◆ init_logging()

void init_logging ( )

Init logging system.

Returns
void.

◆ init_pins()

void init_pins ( )

Init hardware pins.

Returns
void.

◆ init_power_down()

void init_power_down ( uint32_t *  time_ms,
uint32_t  debouncing_ms 
)

Enter power down mode.

Parameters
time_mspointer to a variable to save the last instant you entered power down.
debouncing_msdelay to power down.
Returns
void.

◆ init_rpc()

void init_rpc ( void  )

Register RPC.

Returns
void.

◆ init_rtc()

void init_rtc ( )

Init RTC module.

Returns
void.

◆ init_sensors()

void init_sensors ( )

Create and setup sensors.

Returns
void.

◆ init_spi()

void init_spi ( )

Init SPI library.

Returns
void.

◆ init_system()

void init_system ( )

Init system.

Returns
void.

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

main loop state

◆ init_tasks()

void init_tasks ( )

Init tasks variable and state.

Returns
void.

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

no tasks ready

no tasks ready

no tasks ready

reset samples_count value

no tasks ready

no tasks ready

no tasks ready

no tasks ready

◆ init_timer1()

void init_timer1 ( void  )

Init Timer1 module.

Returns
void.

◆ init_wdt()

void init_wdt ( uint8_t  wdt_timer)

Init watchdog.

Parameters
wdt_timera time value for init watchdog (WDTO_xxxx).
Returns
void.

◆ init_wire()

void init_wire ( )

Init wire (i2c) library and performs checks on the bus.

Returns
void.

clear the I2C bus first before calling Wire.begin()

clear the I2C bus first before calling Wire.begin()

◆ interrupt_task_1s()

void interrupt_task_1s ( void  )

1 seconds task.

Returns
void.

◆ load_configuration()

void load_configuration ( )

Load configuration from EEPROM.

Returns
void.

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

read configuration from eeprom

◆ prepandget()

int prepandget ( JsonObject &  params,
JsonObject &  result 
)

RPC prepare and get json.

Parameters
*paramsjson's params
&resultjson's response
Returns
status code.

◆ prepare()

int prepare ( JsonObject &  params,
JsonObject &  result 
)

RPC prepare.

Parameters
*paramsjson's params
&resultjson's response
Returns
status code.

◆ print_configuration()

void print_configuration ( )

Print current configuration.

Returns
void.

◆ reboot()

int reboot ( JsonObject &  params,
JsonObject &  result 
)

RPC reboot.

Parameters
*paramsjson's params
&resultjson's response
Returns
status code.

◆ rtc_interrupt_handler()

void rtc_interrupt_handler ( void  )

Real Time Clock interrupt handler.

Returns
void.

◆ rtc_task()

void rtc_task ( void  )

Real Time Clock task. Read RTC time and sync system time with it.

Returns
void.

◆ save_configuration()

void save_configuration ( bool  is_default)

Save configuration to EEPROM.

Parameters
is_defaultif true save default configuration; if false save current configuration.
Returns
void.

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

write configuration to eeprom

◆ sensors_reading_task()

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.

Parameters
do_prepareif true, execute the prepare sensor's procedure
do_getif true, execute the get sensor's procedure
*driversensor's driver
*typesensor's type
*addresssensor's address
*nodesensor's node
*sensor_indexsensor's index
*wait_timesensor's wait time
Returns
void.

◆ set_default_configuration()

void set_default_configuration ( void  )

Set default configuration to global configuration variable.

Returns
void.

◆ start_timer()

void start_timer ( void  )

Start Timer1 module.

Returns
void.

< Normal timer operation

< 1:1024 prescaler

< Pre-load timer counter register

< Clear interrupt overflow flag register

< Enable overflow interrupt

◆ stop_timer()

void stop_timer ( void  )

Stop Timer1 module.

Returns
void.

< Stop

< Disable overflow interrupt

< Clear interrupt overflow flag register

< Pre-load timer counter register

◆ supervisor_task()

void supervisor_task ( void  )

Supervisor task. Manage RTC and NTP sync and open/close gsm and ethernet connection.

Returns
void.

◆ time_task()

void time_task ( void  )

Time task. Get time from NTP and sync RTC with it.

Returns
void.