Skip to content

Commit e88e2da

Browse files
Merge branch 'master' of https://github.com/Kacp3r3/LiveObjects_SDK_for_Arduino into Kacp3r3-master
2 parents ebd86e7 + 550fc78 commit e88e2da

12 files changed

+1776
-1773
lines changed

src/LiveObjects.cpp

Lines changed: 0 additions & 1190 deletions
Large diffs are not rendered by default.

src/LiveObjects.h

Lines changed: 14 additions & 583 deletions
Large diffs are not rendered by default.

src/LiveObjectsBase.cpp

Lines changed: 423 additions & 0 deletions
Large diffs are not rendered by default.

src/LiveObjectsBase.h

Lines changed: 409 additions & 0 deletions
Large diffs are not rendered by default.

src/LiveObjectsCellular.cpp

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
#include "LiveObjectsCellular.h"
2+
3+
#if defined NBD || defined GSMD
4+
LiveObjectsCellular::LiveObjectsCellular()
5+
:
6+
LiveObjectsMKR()
7+
,m_Acces()
8+
,m_Scanner()
9+
,m_Sms()
10+
#ifdef GSMD
11+
,m_GPRSAcces()
12+
#endif
13+
{
14+
String num = SECRET_SERVER_MSISDN;
15+
for (int i = 0; i < num.length(); i+=2)
16+
{
17+
if ((i + 1) == num.length() && num.length() % 2 == 1)
18+
{
19+
num += num[i];
20+
num[i] = 'F';
21+
}
22+
else
23+
{
24+
char tmp = num[i + 1];
25+
num[i + 1] = num[i];
26+
num[i] = tmp;
27+
}
28+
}
29+
m_sNumber += ToHexT((char)(num.length() - 1));
30+
m_sNumber += "A1";
31+
m_sNumber += num;
32+
}
33+
34+
LiveObjectsCellular::~LiveObjectsCellular()
35+
{}
36+
37+
void LiveObjectsCellular::begin(Protocol p, Encoding s, bool bDebug)
38+
{
39+
LiveObjectsBase::begin(p,s,bDebug);
40+
if(p==MQTT)
41+
{
42+
switch(m_Security)
43+
{
44+
case TLS:
45+
#ifdef NBD
46+
m_pClient = new NBSSLClient();
47+
#elif defined GSMD
48+
m_pClient = new GSMSSLClient();
49+
#endif
50+
m_pMqttclient = new MqttClient(m_pClient);
51+
m_nPort = 8883;
52+
break;
53+
case NONE:
54+
#ifdef NBD
55+
m_pClient = new NBClient();
56+
#elif defined GSMD
57+
m_pClient = new GSMClient();
58+
#endif
59+
m_pMqttclient = new MqttClient(m_pClient);
60+
m_nPort = 1883;
61+
break;
62+
default:
63+
outputDebug(ERR,"Wrong mode! stopping...");
64+
while(true);
65+
}
66+
m_pMqttclient->onMessage(messageCallback);
67+
}
68+
else
69+
{
70+
if(SECRET_SERVER_MSISDN.length()<3)
71+
{
72+
outputDebug(ERR,"SERVER MSISDN is empty! Check arduino_secrets.h! Stopping here....");
73+
while(true);
74+
}
75+
}
76+
m_bInitialized = true;
77+
}
78+
void LiveObjectsCellular::connectNetwork()
79+
{
80+
//Set client id as IMEI
81+
if (!m_bInitialized)
82+
{
83+
outputDebug(WARN,"missing begin() call, calling with default protcol=MQTT, security protcol=TLS, debug=true");
84+
begin();
85+
}
86+
#ifdef NBD
87+
if(m_Acces.status()!= NB_READY)
88+
#elif defined GSMD
89+
if(m_Acces.status()!= GSM_READY)
90+
#endif
91+
{
92+
#ifdef NBD
93+
NBModem modem;
94+
#elif defined GSMD
95+
GSMModem modem;
96+
#endif
97+
if(modem.begin())
98+
{
99+
if(m_sMqttid.length()==0)
100+
{
101+
String imei="";
102+
for(int i=1;i<=3;i++)
103+
{
104+
imei=modem.getIMEI();
105+
if(imei.length()!=0) break;
106+
delay(100*i);
107+
}
108+
m_sMqttid = imei;
109+
}
110+
}
111+
else
112+
{
113+
outputDebug(ERR,"Failed to initialize modem!" );
114+
while(true);
115+
}
116+
117+
outputDebug(INFO,"Connecting to cellular network");
118+
#ifdef NBD
119+
while (m_Acces.begin(SECRET_PINNUMBER.c_str(), SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != NB_READY)
120+
outputDebug(TXT,".");
121+
outputDebug();
122+
#elif defined GSMD
123+
while ((m_Acces.begin(SECRET_PINNUMBER.c_str()) != GSM_READY)) outputDebug(TXT, ".");
124+
if(m_Protocol != SMS) while ((m_GPRSAcces.attachGPRS(SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != GPRS_READY)) outputDebug(TXT, ".");
125+
outputDebug();
126+
#endif
127+
outputDebug(INFO,"You're connected to the network");
128+
}
129+
if(m_nPort==8883){
130+
if (!m_bCertLoaded) {
131+
outputDebug(INFO,"Loading DigiCert Root CA certificate");
132+
MODEM.sendf("AT+USECMNG=0,0,\"%s\",%d", LO_ROOT_CERT.name, LO_ROOT_CERT.size);
133+
if (MODEM.waitForPrompt() != 1) {
134+
outputDebug(ERR,"Problem loading certificate!\nStopping here.");
135+
while (1) ;
136+
}
137+
else {
138+
MODEM.write(LO_ROOT_CERT.data, LO_ROOT_CERT.size);
139+
int ready;
140+
while (!MODEM.ready());
141+
m_bCertLoaded = true;
142+
outputDebug(INFO,"Certificate loaded");
143+
}
144+
}
145+
}
146+
if(m_Encoding==BINARY) MODEM.send("AT+CMGF=0");
147+
else MODEM.send("AT+CMGF=1");
148+
delay(200);
149+
}
150+
151+
152+
void LiveObjectsCellular::checkNetwork()
153+
{
154+
#ifdef NBD
155+
if(m_Acces.status()!= NB_READY)
156+
connectNetwork();
157+
#elif defined GSMD
158+
if(m_Acces.status()!= GSM_READY)
159+
connectNetwork();
160+
#endif
161+
if(m_Protocol == SMS)
162+
{
163+
String msg;
164+
if(m_Sms.available())
165+
{
166+
int c =0;
167+
while(true)
168+
{
169+
c = m_Sms.read();
170+
if(c==-1)break;
171+
msg+=(char)c;
172+
}
173+
String s;
174+
if(m_Encoding==TEXT) s = msg;
175+
else
176+
{
177+
s = parseCommand(msg);
178+
s = from7bit(s);
179+
}
180+
int spaceIndex = s.indexOf(' ');
181+
String cmd_name = s.substring(0,spaceIndex);
182+
s = s.substring(spaceIndex+1);
183+
outputDebug(INFO,"Received command: ",cmd_name);
184+
LiveObjects_command cmd(cmd_name,nullptr);
185+
int index = commands.find(&cmd);
186+
if(index >= 0 )
187+
{
188+
outputDebug(INFO,"Found command");
189+
commands[index]->callback(s,msg);
190+
}
191+
else outputDebug(INFO,"Unknown command");
192+
m_Sms.flush();
193+
}
194+
}
195+
}
196+
197+
String LiveObjectsCellular::parseCommand(String inputString)
198+
{
199+
String len; len += inputString[0]; len += inputString[1];
200+
int index = 2;
201+
index += 2 * strtol(len.c_str(), nullptr, 16);
202+
index += 2;
203+
len = ""; len += inputString[index]; index++; len += inputString[index]; index++;
204+
index += 2;
205+
index += (int)ceil(strtol(len.c_str(), nullptr, 16)/2.)*2;
206+
index += 2 * 9;
207+
len = ""; len += inputString[index]; index++; len += inputString[index]; index++;
208+
String retVal;
209+
retVal = inputString.substring(index);
210+
//outputDebug(INFO, "PARSED: ", retVal);
211+
return retVal;
212+
}
213+
214+
215+
void LiveObjectsCellular::disconnectNetwork()
216+
{
217+
outputDebug(INFO,"Disconnecting from cellular network...");
218+
m_Acces.shutdown();
219+
}
220+
221+
void LiveObjectsCellular::messageCallback(int msg)
222+
{
223+
LiveObjects::get().onMQTTmessage(msg);
224+
};
225+
226+
void LiveObjectsCellular::addNetworkInfo()
227+
{
228+
String strength= m_Scanner.getSignalStrength();
229+
String carrier = m_Scanner.getCurrentCarrier();
230+
if(m_Protocol == MQTT && m_Encoding==TEXT)
231+
{
232+
bool status;
233+
#ifdef NBD
234+
status = m_Acces.status() == NB_READY;
235+
#elif defined GSMD
236+
status = m_Acces.status() == GSM_READY;
237+
#endif
238+
addToPayload(easyDataPayload[JSONVALUE].createNestedObject("networkInfo"),"connection_status",status,"strength",strength,"carrier",carrier);
239+
}
240+
#ifdef NBD
241+
else addToStringPayload((m_Acces.status() == NB_READY),strength,carrier);
242+
#elif defined GSMD
243+
else addToStringPayload(m_Acces.status() == GSM_READY,strength,carrier);
244+
#endif
245+
}
246+
247+
void LiveObjectsCellular::sendData()
248+
{
249+
if(m_Protocol == MQTT) LiveObjectsBase::sendData();
250+
else
251+
{
252+
if(m_sPayload.length() > 160)
253+
{
254+
outputDebug(ERR,"Payload to big, skipping sending...");
255+
return;
256+
}
257+
else if (m_sPayload.length() < 1)
258+
{
259+
outputDebug(WARN,"Payload is empty, skipping...");
260+
return;
261+
}
262+
if(m_Encoding==TEXT)
263+
{
264+
outputDebug(INFO,"Publishing message: ", m_sPayload);
265+
if(m_Sms.beginSMS(SECRET_SERVER_MSISDN.c_str())!=1) outputDebug(ERR,"Error occured while sending SMS");
266+
m_Sms.print(m_sPayload);
267+
if(m_Sms.endSMS()!=1) outputDebug(ERR,"Error occured while sending SMS");
268+
}
269+
else
270+
{
271+
outputDebug(INFO,"Payload before conversion: ",m_sPayload);
272+
String msg;
273+
static String fixedStart = "001100";
274+
static String fixedInfo = "0000FF";
275+
msg+=fixedStart;
276+
msg+=m_sNumber;
277+
msg+= fixedInfo;
278+
msg+=ToHex((char)m_sPayload.length());
279+
msg+=to7bit(m_sPayload);
280+
int msgSize = (msg.length()-2)/2;
281+
msg+='\x1A';
282+
outputDebug(INFO,"MEssage len: ",msgSize);
283+
outputDebug(INFO,"Publishing message: ", msg);
284+
MODEM.sendf("AT+CMGS=%d\r", msgSize);
285+
delay(100);
286+
MODEM.sendf(msg.c_str());
287+
288+
}
289+
m_sPayload="";
290+
}
291+
}
292+
293+
#endif

src/LiveObjectsCellular.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "LiveObjectsMKR.h"
2+
#if defined ARDUINO_SAMD_MKRNB1500
3+
#define NBD
4+
#include <MKRNB.h>
5+
#elif defined ARDUINO_SAMD_MKRGSM1400
6+
#define GSMD
7+
#include <MKRGSM.h>
8+
#endif
9+
10+
#if defined NBD || defined GSMD
11+
class LiveObjectsCellular : public LiveObjectsMKR
12+
{
13+
public:
14+
static LiveObjectsCellular& get()
15+
{
16+
static LiveObjectsCellular g; return g;
17+
}
18+
19+
private:
20+
LiveObjectsCellular();
21+
~LiveObjectsCellular();
22+
LiveObjectsCellular(const LiveObjectsCellular&) = delete;
23+
LiveObjectsCellular& operator== (const LiveObjectsCellular&) = delete;
24+
public:
25+
void begin(Protocol p=MQTT, Encoding s=TEXT, bool bDebug=true) override;
26+
void addNetworkInfo() override;
27+
void sendData();
28+
private:
29+
String parseCommand(String inputString);
30+
void connectNetwork() override;
31+
void checkNetwork() override;
32+
void disconnectNetwork() override;
33+
static void messageCallback(int msg);
34+
private:
35+
#ifdef NBD
36+
NB m_Acces;
37+
NBScanner m_Scanner;
38+
NB_SMS m_Sms;
39+
#endif
40+
#ifdef GSMD
41+
GSM m_Acces;
42+
GPRS m_GPRSAcces;
43+
GSMScanner m_Scanner;
44+
GSM_SMS m_Sms;
45+
#endif
46+
String m_sNumber;
47+
};
48+
49+
typedef LiveObjectsCellular LiveObjects;
50+
51+
extern const String SECRET_SERVER_MSISDN;
52+
extern const String SECRET_PINNUMBER;
53+
extern const String SECRET_APN;
54+
extern const String SECRET_APN_USER;
55+
extern const String SECRET_APN_PASS;
56+
#endif

0 commit comments

Comments
 (0)