-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdafruitIO_MQTT_DemoShort.ino
157 lines (128 loc) · 4.55 KB
/
AdafruitIO_MQTT_DemoShort.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/
*
* Original Author: Timothy Woo (www.botletics.com)
* Github: https://github.com/botletics/SIM7000-LTE-Shield
* Last Updated: 1/7/2021
* License: GNU GPL v3.0
* Changes made by William Burk to enable use with Arduino Uno.
* Code focused to work with SIM7000, on Verizon, and with Adafruit IO.
*/
#include "Adafruit_FONA.h" // https://github.com/botletics/SIM7000-LTE-Shield/tree/master/Code
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_FONA.h"
#define SIMCOM_7000
/************************* PIN DEFINITIONS *********************************/
#define FONA_PWRKEY 6
#define FONA_RST 7
#define FONA_TX 10 // Microcontroller RX
#define FONA_RX 11 // Microcontroller TX
#define samplingRate 60 // The time we want to delay after each post (in seconds)
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA_LTE fona = Adafruit_FONA_LTE();
/************************* MQTT SETUP *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "YourUserName"
#define AIO_KEY "YourAdafruitKey"
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
uint8_t txfailures = 0;
Adafruit_MQTT_Publish feed_charging = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/charging");
#include <Wire.h>
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
char imei[16] = {0}; // Use this for device ID
uint8_t type;
uint16_t battLevel = 0; // Battery voltage in mV
uint8_t counter = 0;
char battBuff[6] = "3.345"; // Starting value for testing
void setup() {
Serial.begin(9600);
Serial.println(F("*** SIMComExample ***"));
pinMode(FONA_RST, OUTPUT);
digitalWrite(FONA_RST, HIGH); // Default state
fona.powerOn(FONA_PWRKEY); // Power on the module
moduleSetup(); // Establishes first-time serial comm and prints IMEI
fona.setFunctionality(1); // AT+CFUN=1
fona.setNetworkSettings(F("vzwinternet")); // For Verizon SIM card
if (!fona.enableGPRS(false)) Serial.println(F("Faildisdata"));
while (!fona.enableGPRS(true)) {
Serial.println(F("F en data"));
delay(2000); // Retry every 2s
}
Serial.println(F("En data!"));
}
void loop() {
while (!netStatus()) {
Serial.println(F("Failed"));
delay(2000); // Retry every 2s
}
Serial.println(F("Conn cell net"));
battLevel = readVcc();
dtostrf(battLevel,4,0,battBuff);
MQTT_connect();
MQTT_publish_checkSuccess(feed_charging, battBuff);
Serial.print(F("Wafor ")); Serial.print(samplingRate); Serial.println(F(" ses\r\n"));
delay(samplingRate * 1000UL); // Delay
}
void moduleSetup() {
fonaSS.begin(115200); // Default SIM7000 shield baud rate
Serial.println(F("C96"));
fonaSS.println("AT+IPR=9600"); // Set baud rate
delay(100); // Short pause to let the command run
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("t find FONA"));
while (1); // Don't proceed if it couldn't find the device
}
type = fona.type();
Serial.println(F("FONA is OK"));
Serial.print(F("Found "));
switch (type) {
case SIM7000:
Serial.println(F("SIM7000")); break;
default:
Serial.println(F("???")); break;
}
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("Module IMEI: "); Serial.println(imei);
}
}
float readVcc() {
// Read battery voltage
if (!fona.getBattVoltage(&battLevel)) Serial.println(F("Failbatt"));
else Serial.print(F("b = ")); Serial.print(battLevel); Serial.println(F(" mV"));
return battLevel;
}
bool netStatus() {
int n = fona.getNetworkStatus();
Serial.print(F("Net stat ")); Serial.print(n); Serial.print(F(": "));
if (!(n == 1 || n == 5)) return false;
else return true;
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.println("Con MQTT ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retg MQTTn 5 ses...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}
void MQTT_publish_checkSuccess(Adafruit_MQTT_Publish &feed, const char *feedContent) {
Serial.println(F("Sending data..."));
if (! feed.publish(feedContent)) {
Serial.println(F("Failed"));
txfailures++;
}
else {
Serial.println(F("OK!"));
txfailures = 0;
}
}