Skip to content

Commit dc552ba

Browse files
committed
Fona testing
1 parent 0c77965 commit dc552ba

File tree

7 files changed

+398
-6
lines changed

7 files changed

+398
-6
lines changed

src/LiveObjects.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
#endif
1919

2020
/******************************************************************************
21-
ESP8266 BOARDS
21+
ESP8266 and ESP32 BOARDS
2222
******************************************************************************/
2323
#if defined ESP8266 || defined ESP32
2424
#include "LiveObjectsESP.h"
2525
#endif
2626

27+
/******************************************************************************
28+
Adafruit FONA
29+
******************************************************************************/
30+
#ifdef ARDUINO_AVR_FEATHER32U4
31+
#include "LiveObjectsFONA.h"
32+
#endif
33+
2734

2835
/******************************************************************************
2936
CLASS DRAFT BOARD

src/LiveObjectsBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void LiveObjectsBase::paramTyper(const String& name, char* variable, LiveObjects
3131
else
3232
addTypedParam(name, variable, type, T_CHAR, callback);
3333
}
34-
#if not defined ESP8266 && not defined ESP32
34+
#if not defined ESP8266 && not defined ESP32 && not defined ARDUINO_AVR_FEATHER32U4
3535
void LiveObjectsBase::paramTyper(const String& name, int* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback) {
3636
if (type == IMPLICIT)
3737
addTypedParam(name, variable, INTEGER, T_INT, callback);

src/LiveObjectsBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
******************************************************************************/
3737
#include <Arduino.h>
3838
#include <ArduinoJson.h>
39-
#include <ctime>
39+
#include <time.h>
4040
#include "LiveObjectsCert.h"
4141
#include "Utils.h"
4242

@@ -269,7 +269,7 @@ class LiveObjectsBase
269269
******************************************************************************/
270270
void paramTyper(const String& name, bool* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
271271
void paramTyper(const String& name, char* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
272-
#if not defined ESP8266 && not defined ESP32
272+
#if not defined ESP8266 && not defined ESP32 && not defined ARDUINO_AVR_FEATHER32U4
273273
void paramTyper(const String& name, int* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
274274
void paramTyper(const String& name, unsigned int* variable, LiveObjects_parameterType type, onParameterUpdateCallback callback);
275275
#endif

src/LiveObjectsCellular.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ void LiveObjectsCellular::sendData()
276276
if(m_Sms.beginSMS(SECRET_SERVER_MSISDN.c_str())!=1) outputDebug(ERR,"Error occured while sending SMS");
277277
m_Sms.print(m_sPayload);
278278
if(m_Sms.endSMS()!=1) outputDebug(ERR,"Error occured while sending SMS");
279-
outputDebug(INFO, "Nie tutaj");
280279
}
281280
else
282281
{

src/LiveObjectsESP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#if defined ESP8266D || defined ESP32D
44
LiveObjectsESP::LiveObjectsESP()
55
:
6-
m_pClient(nullptr)
6+
LiveObjectsBase()
7+
,m_pClient(nullptr)
78
,m_pMqttclient(nullptr)
89
{}
910

src/LiveObjectsFONA.cpp

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
#include "LiveObjectsFONA.h"
2+
#ifdef FONAD
3+
LiveObjectsFONA::LiveObjectsFONA()
4+
:
5+
LiveObjectsBase()
6+
,m_FonaSerial(FONA_TX, FONA_RX)
7+
,m_Fona(FONA_RST)
8+
,m_FonaMQTT(nullptr)
9+
,m_subCommands(nullptr)
10+
,m_subParameters(nullptr)
11+
{
12+
13+
String num = SECRET_SERVER_MSISDN;
14+
for (int i = 0; i < num.length(); i+=2)
15+
{
16+
if ((i + 1) == num.length() && num.length() % 2 == 1)
17+
{
18+
num += num[i];
19+
num[i] = 'F';
20+
}
21+
else
22+
{
23+
char tmp = num[i + 1];
24+
num[i + 1] = num[i];
25+
num[i] = tmp;
26+
}
27+
}
28+
m_sNumber += ToHexT((char)(num.length() - 1));
29+
m_sNumber += "A1";
30+
m_sNumber += num;
31+
}
32+
33+
LiveObjectsFONA::~LiveObjectsFONA()
34+
{
35+
if(m_subCommands!=nullptr)delete m_subCommands;
36+
if(m_subParameters!=nullptr) delete m_subParameters;
37+
if(m_FonaMQTT!=nullptr) delete m_FonaMQTT;
38+
}
39+
40+
41+
bool LiveObjectsFONA::connectFONA()
42+
{
43+
Watchdog.reset();
44+
45+
Serial.println(F("Initializing FONA....(May take 3 seconds)"));
46+
47+
m_FonaSerial.begin(4800); // if you're using software serial
48+
49+
if (!m_Fona.begin(m_FonaSerial)) { // can also try fona.begin(Serial1)
50+
Serial.println(F("Couldn't find FONA"));
51+
return false;
52+
}
53+
m_FonaSerial.println("AT+CMEE=2");
54+
Serial.println(F("FONA is OK"));
55+
Watchdog.reset();
56+
Serial.println(F("Checking for network..."));
57+
while (m_Fona.getNetworkStatus() != 1) {
58+
delay(500);
59+
}
60+
61+
Watchdog.reset();
62+
delay(5000); // wait a few seconds to stabilize connection
63+
Watchdog.reset();
64+
65+
//m_Fona.setGPRSNetworkSettings(SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str());
66+
67+
Serial.println(F("Disabling GPRS"));
68+
m_Fona.enableGPRS(false);
69+
70+
Watchdog.reset();
71+
delay(5000); // wait a few seconds to stabilize connection
72+
Watchdog.reset();
73+
74+
Serial.println(F("Enabling GPRS"));
75+
if (!m_Fona.enableGPRS(true)) {
76+
Serial.println(F("Failed to turn GPRS on"));
77+
return false;
78+
}
79+
Watchdog.reset();
80+
81+
return true;
82+
}
83+
84+
void LiveObjectsFONA::connectNetwork()
85+
{
86+
87+
if (!m_bInitialized)
88+
{
89+
outputDebug(WARN,"missing begin() call, calling with default protcol=MQTT, security protcol=TLS, debug=true");
90+
begin();
91+
}
92+
93+
while (! connectFONA())
94+
outputDebug(TXT,".");
95+
96+
97+
char imei[16];
98+
uint8_t len = m_Fona.getIMEI(imei);
99+
if(len>0) m_sMqttid=imei;
100+
else
101+
{
102+
outputDebug(ERR, "Cannot obtain sim card IMEI, stopping here...");
103+
while(true);
104+
}
105+
106+
if(m_nPort==8883)
107+
{
108+
if (!m_bCertLoaded)
109+
{
110+
outputDebug(INFO,"Loading DigiCert Root CA certificate");
111+
char send[128];
112+
sprintf(send,"AT+USECMNG=0,0,\"%s\",%d", LO_ROOT_CERT.name, LO_ROOT_CERT.size);
113+
//error check needed
114+
m_Fona.println(send);
115+
m_FonaSerial.write(LO_ROOT_CERT.data, LO_ROOT_CERT.size);
116+
m_bCertLoaded = true;
117+
outputDebug(INFO,"Certificate loaded");
118+
}
119+
}
120+
}
121+
122+
void LiveObjectsFONA::checkNetwork()
123+
{
124+
if(m_Fona.getNetworkStatus()!=1 || m_Fona.getNetworkStatus()!=5)
125+
connectNetwork();
126+
127+
Adafruit_MQTT_Subscribe *tmp=nullptr;
128+
while(tmp = m_FonaMQTT->readSubscription(1000))
129+
{
130+
if(tmp == &m_subCommands)
131+
{
132+
m_subLast = m_subCommands;
133+
commandManager(MQTT_SUBCMD);
134+
m_subLast = nullptr;
135+
}
136+
else if(tmp==&m_subParameters)
137+
{
138+
m_subLast = m_subParameters;
139+
configurationManager(MQTT_SUBCFG,1);
140+
m_subLast = nullptr;
141+
}
142+
}
143+
//ToDo parsing received vals from sms
144+
}
145+
146+
void LiveObjectsFONA::disconnectNetwork()
147+
{
148+
//m_Fona.disconnect()
149+
}
150+
151+
void LiveObjectsFONA::checkMQTT()
152+
{
153+
if(!m_FonaMQTT->connected())
154+
connectMQTT();
155+
}
156+
157+
void LiveObjectsFONA::connectMQTT()
158+
{
159+
if (SECRET_LIVEOBJECTS_API_KEY.length() != 32) // check if API key seems correct
160+
{
161+
outputDebug(ERR,"Please check your Live Objects API key (arduino_secrets.h file).\nStopping here.");
162+
while (1) ;
163+
}
164+
165+
outputDebug(INFO,"Connecting to mqtt broker ", MQTT_BROKER);
166+
while(m_FonaMQTT->connect()!=0)
167+
{
168+
outputDebug(TXT,".");
169+
m_FonaMQTT->disconnect();
170+
}
171+
outputDebug();
172+
173+
if(commands.size()>0) m_subCommands = new Adafruit_MQTT_Subscribe(m_FonaMQTT,MQTT_USER,MQTT_SUBCMD);
174+
if(parameters.size()>0) m_subParameters = new Adafruit_MQTT_Subscribe(m_FonaMQTT,MQTT_USER,MQTT_SUBCFG);
175+
176+
if (!m_bInitialMqttConfig)
177+
{
178+
configurationManager("");
179+
m_bInitialMqttConfig = true;
180+
}
181+
182+
}
183+
184+
void LiveObjectsFONA::disconnectMQTT()
185+
{
186+
outputDebug(INFO,"Closing MQTT connection...");
187+
m_FonaMQTT->disconnect();
188+
}
189+
190+
void LiveObjectsFONA::stopMQTT()
191+
{
192+
if(m_FonaMQTT!=nullptr)
193+
{
194+
disconnectMQTT();
195+
delete m_FonaMQTT;
196+
}
197+
}
198+
199+
void LiveObjectsFONA::sendMQTT(String& topic, JsonDocument& doc)
200+
{
201+
String payload;
202+
deserializeJson(doc,payload);
203+
m_FonaMQTT->publish(MQTT_PUBDATA, payload.c_str());
204+
}
205+
206+
void LiveObjectsFONA::sendMQTT(String& topic, String& doc)
207+
{
208+
m_FonaMQTT->publish(MQTT_PUBDATA, doc.c_str());
209+
}
210+
211+
void LiveObjectsFONA::deserializeMessage(JsonDocument& doc)
212+
{
213+
if(m_subLast!=nullptr)
214+
{
215+
String xd;
216+
for(int i=0;i<100;++i)
217+
{
218+
xd += (char)m_subLast->lastread[i];
219+
}
220+
serializeJson(doc,xd);
221+
}
222+
else
223+
{
224+
outputDebug(ERR, "Trying to read empty message!, stopping...");
225+
while(true);
226+
}
227+
}
228+
229+
void LiveObjectsFONA::begin(Protocol p, Encoding s, bool bDebug)
230+
{
231+
LiveObjectsBase::begin(p,s,bDebug);
232+
if(p==MQTT)
233+
{
234+
switch(m_Security)
235+
{
236+
case TLS:
237+
m_nPort = 8883;
238+
break;
239+
case NONE:
240+
m_nPort = 1883;
241+
break;
242+
default:
243+
outputDebug(ERR,"Wrong mode! stopping...");
244+
while(true);
245+
}
246+
m_FonaMQTT = new Adafruit_MQTT_FONA(&m_Fona, MQTT_BROKER, m_nPort, MQTT_USER, SECRET_LIVEOBJECTS_API_KEY.c_str());
247+
}
248+
else
249+
{
250+
if(SECRET_SERVER_MSISDN.length()<3)
251+
{
252+
outputDebug(ERR,"SERVER MSISDN is empty! Check arduino_secrets.h! Stopping here....");
253+
while(true);
254+
}
255+
}
256+
m_bInitialized = true;
257+
}
258+
259+
260+
void LiveObjectsFONA::sendData()
261+
{
262+
if(m_Protocol == MQTT) LiveObjectsBase::sendData();
263+
else
264+
{
265+
if(m_sPayload.length() > 160)
266+
{
267+
outputDebug(ERR,"Payload to big, skipping sending...");
268+
return;
269+
}
270+
else if (m_sPayload.length() < 1)
271+
{
272+
outputDebug(WARN,"Payload is empty, skipping...");
273+
return;
274+
}
275+
if(m_Encoding==TEXT)
276+
{
277+
outputDebug(INFO,"Publishing message: ", m_sPayload);
278+
m_Fona.sendSMS(SECRET_SERVER_MSISDN.c_str(), m_sPayload.c_str());
279+
}
280+
else
281+
{
282+
outputDebug(INFO,"Payload before conversion: ",m_sPayload);
283+
String msg;
284+
static String fixedStart = "001100";
285+
static String fixedInfo = "0000FF";
286+
msg+=fixedStart;
287+
msg+=m_sNumber;
288+
msg+= fixedInfo;
289+
msg+=ToHex((char)m_sPayload.length());
290+
msg+=to7bit(m_sPayload);
291+
int msgSize = (msg.length()-2)/2;
292+
msg+='\x1A';
293+
outputDebug(INFO,"Message len: ",msgSize);
294+
outputDebug(INFO,"Publishing message: ", msg);
295+
char cmd[30];
296+
sprintf(cmd,"AT+CMGS=%d\r",msgSize);
297+
m_Fona.print(cmd);
298+
delay(100);
299+
m_Fona.print(msg);
300+
301+
}
302+
m_sPayload="";
303+
}
304+
}
305+
306+
void LiveObjectsFONA::addPowerStatus()
307+
{
308+
309+
}
310+
311+
void LiveObjectsFONA::addNetworkInfo()
312+
{
313+
314+
}
315+
#endif

0 commit comments

Comments
 (0)