Skip to content

Commit 881e107

Browse files
committed
[20%] progress - sending data to Lo
1 parent dc552ba commit 881e107

File tree

7 files changed

+228
-403
lines changed

7 files changed

+228
-403
lines changed

src/LiveObjects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
Adafruit FONA
2929
******************************************************************************/
3030
#ifdef ARDUINO_AVR_FEATHER32U4
31-
#include "LiveObjectsFONA.h"
31+
#include "LiveObjectsAVRFona.h"
3232
#endif
3333

3434

src/LiveObjectsAVRFona.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "LiveObjectsAVRFona.h"
2+
3+
LiveObjectsFona::LiveObjectsFona()
4+
:
5+
m_Fona(FONA_RST)
6+
,m_FonaSerial(FONA_TX,FONA_RX)
7+
,m_FonaMQTT(nullptr)
8+
,m_sClientID("Fona")
9+
,m_nPort(1883)
10+
{
11+
12+
}
13+
14+
void LiveObjectsFona::begin(Protocol p, Encoding e, bool d)
15+
{
16+
17+
}
18+
19+
void LiveObjectsFona::loop()
20+
{
21+
Adafruit_MQTT_Subscribe *subscription;
22+
while ((subscription = m_FonaMQTT->readSubscription(5000))) {
23+
//if (subscription == &onoffbutton) {
24+
// Serial.print(F("Got: "));
25+
// Serial.println((char *)onoffbutton.lastread);
26+
//}
27+
}
28+
}
29+
30+
void LiveObjectsFona::connect()
31+
{
32+
while (! FONAconnect()) {
33+
Serial.println("Retrying FONA");
34+
}
35+
Serial.println(F("Connected to Cellular!"));
36+
delay(5000); // wait a few seconds to stabilize connection
37+
connectMQTT();
38+
}
39+
40+
void LiveObjectsFona::addToPayload(String x, int y)
41+
{
42+
43+
}
44+
45+
void LiveObjectsFona::sendData()
46+
{
47+
m_FonaMQTT->publish("dev/data","{ \"value\": { \"hello\" : \"world\" }}");
48+
}
49+
50+
bool LiveObjectsFona::FONAconnect()
51+
{
52+
Watchdog.reset();
53+
54+
Serial.println(F("Initializing FONA....(May take 3 seconds)"));
55+
56+
m_FonaSerial.begin(4800); // if you're using software serial
57+
58+
if (!m_Fona.begin(m_FonaSerial)) { // can also try fona.begin(Serial1)
59+
Serial.println(F("Couldn't find FONA"));
60+
return false;
61+
}
62+
m_FonaSerial.println("AT+CMEE=2");
63+
Serial.println(F("FONA is OK"));
64+
Watchdog.reset();
65+
Serial.println(F("Checking for network..."));
66+
while (m_Fona.getNetworkStatus() != 1) {
67+
delay(500);
68+
}
69+
70+
Watchdog.reset();
71+
delay(5000); // wait a few seconds to stabilize connection
72+
Watchdog.reset();
73+
74+
m_Fona.setGPRSNetworkSettings(F("internet"), F(""), F(""));
75+
76+
//Serial.println(F("Disabling GPRS"));
77+
//fona.enableGPRS(false);
78+
79+
Watchdog.reset();
80+
delay(5000); // wait a few seconds to stabilize connection
81+
Watchdog.reset();
82+
83+
Serial.println(F("Enabling GPRS"));
84+
if (!m_Fona.enableGPRS(true)) {
85+
Serial.println(F("Failed to turn GPRS on"));
86+
return false;
87+
}
88+
Watchdog.reset();
89+
90+
return true;
91+
}
92+
93+
void LiveObjectsFona::connectMQTT()
94+
{
95+
m_FonaMQTT = new Adafruit_MQTT_FONA(&m_Fona,MQTT_BROKER, m_nPort,m_sClientID.c_str(), MQTT_USER,SECRET_LIVEOBJECTS_API_KEY.c_str());
96+
int8_t ret;
97+
98+
// Stop if already connected.
99+
if (m_FonaMQTT->connected()) {
100+
return;
101+
}
102+
103+
Serial.print("Connecting to MQTT... ");
104+
105+
while ((ret = m_FonaMQTT->connect()) != 0) { // connect will return 0 for connected
106+
Serial.println(m_FonaMQTT->connectErrorString(ret));
107+
Serial.println("Retrying MQTT connection in 5 seconds...");
108+
m_FonaMQTT->disconnect();
109+
delay(5000); // wait 5 seconds
110+
}
111+
Serial.println("MQTT Connected!");
112+
}

src/LiveObjectsAVRFona.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#pragma once
2+
3+
/******************************************************************************
4+
DEFAULT VALUES FOR LIVEOBJECTS
5+
******************************************************************************/
6+
#ifdef ARDUINO_ARCH_AVR
7+
#define PAYLOAD_DATA_SIZE 256
8+
#else
9+
#define PAYLOAD_DATA_SIZE 1024
10+
#endif
11+
#define KEEP_ALIVE_NETWORK 1000
12+
#define SW_REVISION "1.8.0"
13+
14+
15+
/******************************************************************************
16+
LiveObjects MQTT constants
17+
******************************************************************************/
18+
#define MQTT_BROKER "liveobjects.orange-business.com"
19+
#define MQTT_USER "json+device"
20+
#define MQTT_PUBDATA "dev/data"
21+
#define MQTT_PUBDATA_BINARY "dev/v1/data/binary"
22+
#define MQTT_SUBCFG "dev/cfg/upd"
23+
#define MQTT_PUBCFG "dev/cfg"
24+
#define MQTT_SUBCMD "dev/cmd"
25+
#define MQTT_PUBCMD "dev/cmd/res"
26+
27+
/******************************************************************************
28+
JSON payload constants
29+
******************************************************************************/
30+
#define JSONCID "cid"
31+
#define JSONCFG "cfg"
32+
#define JSONCFGVALUE "v"
33+
#define JSONCFGTYPE "t"
34+
#define JSONMODEL "model"
35+
#define JSONVALUE "value"
36+
#define JSONMODELNAME "Orange_Pollution_Shield"
37+
38+
39+
#include <Adafruit_SleepyDog.h>
40+
#include <SoftwareSerial.h>
41+
#include <Adafruit_FONA.h>
42+
#include <Adafruit_MQTT.h>
43+
#include <Adafruit_MQTT_FONA.h>
44+
#include <ArduinoJson.h>
45+
46+
#ifdef ARDUINO_AVR_FEATHER32U4
47+
#define FONA_RX 9
48+
#define FONA_TX 8
49+
#define FONA_RST 4
50+
#endif
51+
//Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, "dev/cmd");
52+
#define SW_REVISION "1.8.0"
53+
#define PAYLOAD_DATA_SIZE 256
54+
55+
enum Protocol
56+
{
57+
MQTT,
58+
SMS
59+
//,LORA
60+
};
61+
62+
enum Security
63+
{
64+
NONE
65+
,TLS
66+
//,DTLS
67+
};
68+
69+
enum Encoding
70+
{
71+
BINARY
72+
,TEXT
73+
};
74+
75+
76+
class LiveObjectsFona
77+
{
78+
public:
79+
static LiveObjectsFona& get()
80+
{
81+
static LiveObjectsFona f;
82+
return f;
83+
}
84+
private:
85+
LiveObjectsFona();
86+
LiveObjectsFona(const LiveObjectsFona&) = delete;
87+
LiveObjectsFona& operator==(const LiveObjectsFona&) = delete;
88+
89+
public:
90+
void begin(Protocol p, Encoding e, bool d);
91+
void loop();
92+
void connect();
93+
void addToPayload(String x, int y);
94+
void sendData();
95+
bool FONAconnect();
96+
97+
private:
98+
void connectMQTT();
99+
100+
private:
101+
Adafruit_FONA m_Fona;
102+
SoftwareSerial m_FonaSerial;
103+
Adafruit_MQTT_FONA* m_FonaMQTT;
104+
StaticJsonDocument<PAYLOAD_DATA_SIZE> m_Payload;
105+
String m_sClientID;
106+
uint16_t m_nPort;
107+
};
108+
extern const String SECRET_LIVEOBJECTS_API_KEY;
109+
110+
typedef LiveObjectsFona LiveObjects;

src/LiveObjectsBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
/******************************************************************************
55
DEFAULT VALUES FOR LIVEOBJECTS
66
******************************************************************************/
7+
#ifdef ARDUINO_ARCH_AVR
8+
#define PAYLOAD_DATA_SIZE 256
9+
#else
710
#define PAYLOAD_DATA_SIZE 1024
11+
#endif
812
#define KEEP_ALIVE_NETWORK 1000
913
#define SW_REVISION "1.8.0"
1014

0 commit comments

Comments
 (0)