Develop a circuit to regulate the input voltage with the following requirements:
be tolerant to reverse polarity
be able to handle an input of up to 20 Voltage ( regular car battery voltage is around 13-16V)
provide up to 2A (a requirement of the SIM808, in bad radio conditions more power is needed to transmit)
3.6V is required because it is the intersection between the working voltage ranges of both STM32 and SIM808
SIM808 range: 3.4V->4V
STM32F0 range: 1.8V->3.6V
At this point, there are 3 functions just to send commands the module. This creates confusion.
Create only one function to send commands to the module. @param char * cmd the command to be sent to the module
@rx_wait the time required by the module to process the command. wait this time before checking the receive buffer @param char * rx_buffer copy the reply of the module into this buffer @param uint8_t get_reply is used to tell the function if it should copy the reply into the buffer or not.
uint8_t send_command(char * cmd, uint8_t rx_wait, char * rx_buffer, uint8_t get_reply)
initialize the UART used to communicate with the module
initialize the debug UART
power on the module and check if it is powered on.
returns 1 if the module is correctly powered on. 0 otherwise.
To power on the module:
before trying to power on, check the power on status first. it could be that the module was turned before the mcu restarted.
STM32 GPIOB pin PB9 is connected to the power_on PIN of the SIM808
power_on pin must be pulled down for at least 1 second to enable the SIM808 module
Power on status can be monitored through STATUS pin of the SIM808 module that is connected to STM32 GPIOC PC14.
STATUS pin will go up when the power on procedure finishes
the function must try to power on the device 3 times if power on fails for some reason.
After power on, issue an "AT" string to allow the baud rate to be synchronized. This is necessary when you start up the module while auto-bauding is enabled. It is recommended to wait 3 to 5 seconds before sending the first AT character. Otherwise undefined characters might be returned.
Turn on LED1, which is connected to GPIOB PB12, to indicate the power on status of the module.
The watchdog timer is important to save the hardware/firmware in case of an infinite loop introduced by an unknown bug or a hardware issue.
Watch dog timer should be enabled and watch dog reset should be introduced in the code
Change the circuit so that the board can be powered by a battery.
Change the circuit to utilize the internal charging circuit inside the sim808.
Change the buck converter configuration so that the output voltage is 4V. and add a Linear regulator to drop voltage to for the STM32.
4V input will be necessary for the SIM808 if it is used to charge a battery.
Add a 2 pin JTS connector for battery input.
The program should collect position and speed data every 10 seconds and stores the new values if the traveled distance is superior than 10 meters and then sends the data to the server every minute
When the device is moving, this will result in 6 updates sent together every minute.
sim_mqtt_publish should include
The Client ID as an input with fixed length = 4 Bytes
The topic name with an maximum length of 64 Bytes
The message length which is variable with a maximum of 256
using global variable is not recommended as it can lead to bugs due to race conditions.
receive buffer should be encapsulated by a wrapper function.
the buffer should be static inside the function.
the HAL UART receive interrupt callback HAL_UART_RxCpltCallback() should insert the received byte into the buffer by calling a function instead of accessing the global variable.
The STM32 should be programmable through a USB.
To do so, add USB to UART circuit to interface with the STM32.
Add a button to the boot pin of the STM32.
Use the CP2102 IC or an equivalent.
The USB connection will be used to communicate with the STM32 UART or to program it without the need of SWD.
The current version of the PCB has a GPS antenna issue. The GPS signal is not strong enough for the SIM808 to get a GPS fix.
Probable root cause: lack of DC bias on the antenna line to activate the active GPS Antenna.
DC signal in the antenna line should be introduced and turned on and off using a transistor (or other switch) connected to a GPIO on the MCU. It should be activated and deactivated by software to control power consumption of the Active antenna.
Position data sent to the server must be encrypted.
A function to encrypt the position payload should be developed.
Encryption Algorithm AES-128
Key should be introduced in the code. Probably not a good a idea, because someone who can extract the firmware from the MCU can read the Encryption key. This can be enhanced in the future. Possible solution would be to enable read-out protection on the MCU.
Electronic Code Book chaining mode is enough. (for now)
In case the module freezes or connectivity is disrupted a reset could be useful.
Create a function void system_reset(); that resets the module then the stm32
STM32 GPIOF PF7 is connected to the Reset pin on the SIM808 module
Pull down the Reset pin at least 105ms to reset the module
once the SIM808 is reset with success then reset the MCU.
When power is turned off, the sim808 loses track of time. This means, when it is turned on again, it will take around 60-90 seconds to acquire a GPS fix. Hooking up the sim808 to a backup battery ensures it always has correct time. Which will speed up the Time To First Fix (TTFF) to around 10-15 seconds.
Add a battery socket or a large capacitor to the VRTC pin.
add a function sd_log(char * msg, char * file name); that logs a message into a text file on the SD card.
This function will be called after performing each step in the program.
The MCU and SIM808 have sleep modes.
The board should enter into sleep mode as much as possible to save power consumption.
it should also turn of the GPS Antenna DC bias.
The sim_tcp_send function below to send the TCP payload without checking if the underlying GPRS connection is still on. When the GPRS connection is disconnected, sim_tcp_send will fail with unpredictable behavior.
Note: GPRS connection could disconnected for several reasons:
a cell handover could go wrong
the network could disconnect the device
the cell is not reachable
In network_functions.c in function sim_tcp_send
after constructing the open tcp connection command, using strcat. the command is sent to the module to open a tcp connection,
in case the creation of connection fails the module sends CONNECT FAIL error. This error msg is not captured in the receive buffer.
Even when the connection has succeeded with CONNECT OK,
The only thing captured in the receive buffer is the "issued command"
the firmware should send a status report every T duration (30 min or 1h) to a status MQTT topic.
Report includes : board temperature - uptime - cell signal strength - visible GPS satellites - ping duration