Apply VSC auto format to the code and indent macros for readability

This commit is contained in:
1technophile committed 2019-12-14 17:14:36 +01:00
1 parent f7ec81ffaa
commit 7f9d8d18da
26 files changed
+4512 -3692

No files matched your search

+129 -104
View File
@@ -33,13 +33,14 @@
#include <A6lib.h> // library for controling A6 or A7 module
// Instantiate the library with TxPin, RxPin.
A6lib A6l(_2G_TX_PIN, _2G_RX_PIN); //D6 to A6 RX, D7 to A6 TX
A6lib A6l(_2G_TX_PIN, _2G_RX_PIN); //D6 to A6 RX, D7 to A6 TX
int unreadSMSLocs[50] = {0};
int unreadSMSNum = 0;
SMSmessage sms;
void setup2G(){
void setup2G()
{
trc(F("_2G_TX_PIN "));
trc(_2G_TX_PIN);
@@ -49,115 +50,139 @@ void setup2G(){
trc(F("Zgateway2G setup done "));
}
void setupGSM(bool deleteSMS){
trc(F("Init 2G module: "));
trc(_2G_PWR_PIN);
delay(1000);
// Power-cycle the module to reset it.
A6l.powerCycle(_2G_PWR_PIN);
trc(F("waiting for network connection"));
A6l.blockUntilReady(_2G_MODULE_BAUDRATE);
trc(F("A6/A7 gsm ready"));
signalStrengthAnalysis();
delay(1000);
// deleting all sms
if (deleteSMS){
if (A6l.deleteSMS(1,4) == A6_OK ) {
trc(F("delete SMS OK"));
}else{
trc(F("delete SMS KO"));
}
void setupGSM(bool deleteSMS)
{
trc(F("Init 2G module: "));
trc(_2G_PWR_PIN);
delay(1000);
// Power-cycle the module to reset it.
A6l.powerCycle(_2G_PWR_PIN);
trc(F("waiting for network connection"));
A6l.blockUntilReady(_2G_MODULE_BAUDRATE);
trc(F("A6/A7 gsm ready"));
signalStrengthAnalysis();
delay(1000);
// deleting all sms
if (deleteSMS)
{
if (A6l.deleteSMS(1, 4) == A6_OK)
{
trc(F("delete SMS OK"));
}
}
void signalStrengthAnalysis(){
int signalStrength = 0;
signalStrength = A6l.getSignalStrength();
trc(F("Signal strength: "));
trc(signalStrength);
if (signalStrength < _2G_MIN_SIGNAL || signalStrength > _2G_MAX_SIGNAL ) {
trc(F("Signal too low restart the module"));
setupGSM(false); // if we are below or above a threshold signal we relaunch the setup of GSM module
}
}
bool _2GtoMQTT(){
// Get the memory locations of unread SMS messages.
unreadSMSNum = A6l.getUnreadSMSLocs(unreadSMSLocs, 512);
trc(F("Creating SMS buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject& SMSdata = jsonBuffer.createObject();
for (int i = 0; i < unreadSMSNum; i++) {
trc(F("New message at index: "));
trc(unreadSMSNum);
sms = A6l.readSMS(unreadSMSLocs[i]);
SMSdata.set("message", (char *)sms.message.c_str());
SMSdata.set("date", (char *)sms.date.c_str());
SMSdata.set("phone", (char *)sms.number.c_str());
A6l.deleteSMS(unreadSMSLocs[i]); // we delete the SMS received
trc(F("Adv data 2GtoMQTT"));
pub(subject2GtoMQTT,SMSdata);
return true;
}
return false;
}
#ifdef simpleReceiving
void MQTTto2G(char * topicOri, char * datacallback) {
String data = datacallback;
String topic = topicOri;
if (cmpToMainTopic(topicOri,subjectMQTTto2G)) {
trc(F("MQTTto2G data analysis"));
// 2G DATA ANALYSIS
String phone_number = "";
int pos0 = topic.lastIndexOf(_2GPhoneKey);
if (pos0 != -1) {
pos0 = pos0 + strlen(_2GPhoneKey);
phone_number = topic.substring(pos0);
trc(F("MQTTto2G phone ok"));
trc(phone_number);
trc(F("MQTTto2G sms"));
trc(data);
if (A6l.sendSMS(phone_number,data) == A6_OK ) {
trc(F("SMS OK"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}else{
trc(F("SMS KO"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
}else{
trc(F("MQTTto2G Fail reading phone number"));
}
else
{
trc(F("delete SMS KO"));
}
}
}
void signalStrengthAnalysis()
{
int signalStrength = 0;
signalStrength = A6l.getSignalStrength();
trc(F("Signal strength: "));
trc(signalStrength);
if (signalStrength < _2G_MIN_SIGNAL || signalStrength > _2G_MAX_SIGNAL)
{
trc(F("Signal too low restart the module"));
setupGSM(false); // if we are below or above a threshold signal we relaunch the setup of GSM module
}
}
bool _2GtoMQTT()
{
// Get the memory locations of unread SMS messages.
unreadSMSNum = A6l.getUnreadSMSLocs(unreadSMSLocs, 512);
trc(F("Creating SMS buffer"));
StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer;
JsonObject &SMSdata = jsonBuffer.createObject();
for (int i = 0; i < unreadSMSNum; i++)
{
trc(F("New message at index: "));
trc(unreadSMSNum);
sms = A6l.readSMS(unreadSMSLocs[i]);
SMSdata.set("message", (char *)sms.message.c_str());
SMSdata.set("date", (char *)sms.date.c_str());
SMSdata.set("phone", (char *)sms.number.c_str());
A6l.deleteSMS(unreadSMSLocs[i]); // we delete the SMS received
trc(F("Adv data 2GtoMQTT"));
pub(subject2GtoMQTT, SMSdata);
return true;
}
return false;
}
#ifdef simpleReceiving
void MQTTto2G(char *topicOri, char *datacallback)
{
String data = datacallback;
String topic = topicOri;
if (cmpToMainTopic(topicOri, subjectMQTTto2G))
{
trc(F("MQTTto2G data analysis"));
// 2G DATA ANALYSIS
String phone_number = "";
int pos0 = topic.lastIndexOf(_2GPhoneKey);
if (pos0 != -1)
{
pos0 = pos0 + strlen(_2GPhoneKey);
phone_number = topic.substring(pos0);
trc(F("MQTTto2G phone ok"));
trc(phone_number);
trc(F("MQTTto2G sms"));
trc(data);
if (A6l.sendSMS(phone_number, data) == A6_OK)
{
trc(F("SMS OK"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS OK"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
else
{
trc(F("SMS KO"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS KO"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
}
else
{
trc(F("MQTTto2G Fail reading phone number"));
}
}
}
#endif
#ifdef jsonReceiving
void MQTTto2G(char * topicOri, JsonObject& SMSdata) {
if (cmpToMainTopic(topicOri,subjectMQTTto2G)){
const char * sms = SMSdata["message"];
const char * phone = SMSdata["phone"];
trc(F("MQTTto2G json data analysis"));
if (sms && phone) {
trc(F("MQTTto2G sms & phone ok"));
trc(sms);
if (A6l.sendSMS(String(phone),String(sms)) == A6_OK ) {
trc(F("SMS OK"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}else{
trc(F("SMS KO"));
pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
}else{
trc(F("MQTTto2G failed json read"));
void MQTTto2G(char *topicOri, JsonObject &SMSdata)
{
if (cmpToMainTopic(topicOri, subjectMQTTto2G))
{
const char *sms = SMSdata["message"];
const char *phone = SMSdata["phone"];
trc(F("MQTTto2G json data analysis"));
if (sms && phone)
{
trc(F("MQTTto2G sms & phone ok"));
trc(sms);
if (A6l.sendSMS(String(phone), String(sms)) == A6_OK)
{
trc(F("SMS OK"));
// Acknowledgement to the GTW2G topic
pub(subjectGTW2GtoMQTT, "SMS OK"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
else
{
trc(F("SMS KO"));
pub(subjectGTW2GtoMQTT, "SMS KO"); // we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
}
}
else
{
trc(F("MQTTto2G failed json read"));
}
}
}
#endif
#endif