|
|
|
#include <Adafruit_SleepyDog.h> |
|
#include <SoftwareSerial.h> |
|
#include "Adafruit_FONA.h" |
|
#include "Adafruit_MQTT.h" |
|
#include "Adafruit_MQTT_FONA.h" |
|
|
|
#define FONA_APN "hologram" |
|
#define FONA_USERNAME "" |
|
#define FONA_PASSWORD "" |
|
|
|
// We default to using software serial. If you want to use hardware serial |
|
// (because softserial isnt supported) comment out the following three lines |
|
// and uncomment the HardwareSerial line |
|
#define FONA_RX 2 |
|
#define FONA_TX 3 |
|
#define FONA_RST 4 |
|
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); |
|
|
|
// Use this for FONA 800 and 808s |
|
Adafruit_FONA fona = Adafruit_FONA(FONA_RST); |
|
|
|
#define AIO_SERVER "io.adafruit.com" |
|
#define AIO_SERVERPORT 1883 |
|
// shhhhhh, these are secrets |
|
#define AIO_USERNAME "xxx" |
|
#define AIO_KEY "xxx" |
|
|
|
/************ Global State (you don't need to change this!) ******************/ |
|
|
|
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. |
|
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); |
|
|
|
// You don't need to change anything below this line! |
|
#define halt(s) { Serial.println(F( s )); while(1); } |
|
|
|
// FONAconnect is a helper function that sets up the FONA and connects to |
|
// the GPRS network. fonahelper.cpp must be included in the Arduino project. |
|
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); |
|
|
|
/********************/ |
|
|
|
// Setup a feed called 'photocell' for publishing. |
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> |
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); |
|
|
|
// How many transmission failures in a row we're willing to be ok with before reset |
|
uint8_t txfailures = 0; |
|
#define MAXTXFAILURES 3 |
|
|
|
void setup() { |
|
while (!Serial); |
|
|
|
// Watchdog is optional! |
|
Watchdog.enable(12000); |
|
|
|
Serial.begin(115200); |
|
|
|
Serial.println(F("Adafruit FONA MQTT demo")); |
|
|
|
Watchdog.reset(); |
|
delay(5000); // wait a few seconds to stabilize connection |
|
Watchdog.reset(); |
|
|
|
// Initialise the FONA module |
|
while (! FONAconnect(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) { |
|
Serial.println("Retrying FONA"); |
|
} |
|
|
|
Serial.println(F("Connected to Cellular!")); |
|
|
|
Watchdog.reset(); |
|
delay(5000); // wait a few seconds to stabilize connection |
|
Watchdog.reset(); |
|
} |
|
|
|
|
|
uint32_t x=0; |
|
unsigned long last_pub = 0; |
|
unsigned long pub_wait = 60000; |
|
|
|
void loop() { |
|
// Make sure to reset watchdog every loop iteration! |
|
Watchdog.reset(); |
|
|
|
// Ensure the connection to the MQTT server is alive (this will make the first |
|
// connection and automatically reconnect when disconnected). See the MQTT_connect |
|
// function definition further below. |
|
MQTT_connect(); |
|
|
|
Watchdog.reset(); |
|
|
|
if (millis() > last_pub + pub_wait) { |
|
// Now we can publish stuff! |
|
Serial.print(F("\nSending photocell val ")); |
|
Serial.print(x); |
|
Serial.print("..."); |
|
if (! photocell.publish(x++)) { |
|
Serial.println(F("Failed")); |
|
txfailures++; |
|
} else { |
|
Serial.println(F("OK!")); |
|
txfailures = 0; |
|
last_pub = millis(); |
|
} |
|
} |
|
|
|
delay(5000); |
|
|
|
// ping the server to keep the mqtt connection alive, only needed if we're not publishing |
|
//if(! mqtt.ping()) { |
|
// Serial.println(F("MQTT Ping failed.")); |
|
//} |
|
} |
|
|
|
|
|
// Function to connect and reconnect as necessary to the MQTT server. |
|
// Should be called in the loop function and it will take care if connecting. |
|
void MQTT_connect() { |
|
int8_t ret; |
|
|
|
// Stop if already connected. |
|
if (mqtt.connected()) { |
|
return; |
|
} |
|
|
|
Serial.print("Connecting to MQTT... "); |
|
|
|
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected |
|
Serial.println(mqtt.connectErrorString(ret)); |
|
Serial.println("Retrying MQTT connection in 5 seconds..."); |
|
mqtt.disconnect(); |
|
delay(5000); // wait 5 seconds |
|
} |
|
Serial.println("MQTT Connected!"); |
|
} |