24 #include <PubSubClient.h>
26 #define topic_deep_sleep "/DeepSleep"
28 #define topic_voltage "/Voltage"
29 #define topic_mAh "/mAh"
30 #define topic_mAhLowPower "/mAhLowPower"
31 #define topic_alive "/Alive"
32 #define topic_rssi "/RSSI"
34 #define topic_power_on "/PowerOn"
35 #define topic_gps_enabled "/GpsEnabled"
36 #define topic_send_on_move_every "/SendOnMoveEverySec"
37 #define topic_send_on_non_move_every "/SendOnNonMoveEverySec"
38 #define topic_send_every "/SendEverySec"
40 #define topic_temperature "/BME280/Temperature"
41 #define topic_humidity "/BME280/Humidity"
42 #define topic_pressure "/BME280/Pressure"
44 #define topic_signal_quality "/Gsm/SignalQuality"
45 #define topic_batt_level "/Gsm/BattLevel"
46 #define topic_batt_volt "/Gsm/BattVolt"
48 #define topic_gps "/Gps"
49 #define topic_gps_distance "/GpsDistance"
54 class MyMqtt :
protected PubSubClient
60 static void mqttCallback(
char* topic, byte* payload,
unsigned int len);
69 bool myPublish(String subTopic, String value);
85 : PubSubClient(client)
88 , publishInProgress(false)
109 MyDbg((String) F(
"MyMqtt::subscribe: [") + topic + F(
"]"),
true);
110 return PubSubClient::subscribe(topic.c_str());
124 if (value.length() > 0) {
128 MyDbg((String) F(
"MyMqtt::publish: [") + topic + F(
"]=[") + value + F(
"]"),
true);
129 ret = PubSubClient::publish(topic.c_str(), value.c_str(),
true);
154 MyDbg(F(
"MQTT:begin"),
true);
176 if (!PubSubClient::connected()) {
177 for (
int i = 0; !PubSubClient::connected() && i < 5; i++) {
178 MyDbg(F(
"Attempting MQTT connection..."),
true);
182 #ifdef SIM808_CONNECTED
187 MyDbg(F(
" connected"),
true);
189 MyDbg((String) F(
" Mqtt failed, rc = ") + String(PubSubClient::state()),
true);
190 MyDbg(F(
" Try again in 5 seconds"),
true);
192 MyDbg(F(
"."),
true,
false);
196 if (PubSubClient::connected()) {
199 MyDbg(F(
"Attempting MQTT publishing"),
true);
204 #ifndef SIM808_CONNECTED
211 #ifdef SIM808_CONNECTED
223 MyDbg(F(
"mqtt published"),
true);
237 if (topic == NULL || payload == NULL || len <= 0 || len > 200) {
241 String strTopic = String((
char*)topic);
244 MyDbg((String) F(
"Message arrived [") + strTopic + F(
"]:[ "),
true);
245 if (len)
MyDbg((
char *) payload,
true);
248 if (MyMqtt::g_myOptions) {
String formatInterval(long secs)
#define topic_power_on
switch power on/off
#define topic_send_on_move_every
mqtt send interval on moving
#define topic_deep_sleep
Deep sleep on/off.
#define topic_mAh
Power consumption.
#define topic_send_every
mqtt sending interval
bool isGpsEnabled
Is the gps part of the sim808 active?
#define topic_send_on_non_move_every
mqtt sending interval on non moving
#define topic_voltage
Power supply voltage.
String mqttUser
MQTT user.
class MyData::RtcData rtcData
Data to store in the RTC memory.
double getLowPowerPowerConsumption()
MyOptions myOptions
The global options.
void MyDbg(String info, bool fromWebServer=false, bool newline=true)
long mqttLastSentTime
Last mqtt sent timestamp.
bool powerOn
Is the GSM power from the DC-DC modul switched on?
double temperature
Current BME280 temperature.
MyGps lastGps
Last known gps location without timeout.
String mqttPassword
MQTT password.
bool myPublish(String subTopic, String value)
MyData & myData
Reference to the data.
double getPowerConsumption()
String WifiGetRssiAsQuality(int rssi)
#define topic_temperature
Temperature.
#define topic_batt_level
Battery level of the gsm modul.
void MyDelay(long millisDelay)
double pressure
Current BME280 pressure.
String signalQuality
Quality of the signal.
#define topic_gps
Gps longitude, latitude, altitude, moving speed.
static void mqttCallback(char *topic, byte *payload, unsigned int len)
String batteryVolt
Battery volt of the sim808 module.
String mqttServer
MQTT server url.
long mqttSendCount
How many time the mqtt data successfully sent.
#define topic_signal_quality
Signal quality.
long mqttPort
MQTT server port.
bool getAsGpsJson(char *gpsJson)
#define topic_mAhLowPower
Power consumption in low power.
long secondsSincePowerOn()
MyMqtt(Client &client, MyOptions &options, MyData &data)
bool mySubscribe(String subTopic)
bool isMoving
Is moving recognized.
double voltage
Current supply voltage.
String batteryLevel
Battery level of the sim808 module.
long lastMqttPublishSec
Timestamp from the last send.
MyOptions & myOptions
Reference to the options.
#define topic_gps_distance
Gps distance to last position.
String mqttName
MQTT server name.
static MyOptions * g_myOptions
Static option pointer for the callback function.
bool isGsmActive
Is the sim808 modul connected to a gsm network?
#define topic_rssi
Wifi conection quality.
MyTime time
The received gps utc time.
#define topic_pressure
Pressure.
bool secondsElapsed(long &lastCheckSec, const long &intervalSec)
double humidity
Current BME280 humidity.
MyData myData
The global collected data.
#define topic_humidity
Humidity.
#define topic_batt_volt
Battery volt of the gsm modul.
long mqttSendOnNonMoveEverySec
Send data interval to MQTT server on non moving.
bool isDeepSleepEnabled
Should the system go into deepsleep if needed.
long mqttSendOnMoveEverySec
Send data interval to MQTT server on moving.
#define topic_gps_enabled
switch gps on/off
bool publishInProgress
Are we publishing right now.
double movingDistance
Minimum distance for moving flag.
#define topic_alive
Alive time in sec.