Make examples "wifi-ready"

This commit is contained in:
Sara Damiano
2019-05-23 11:14:23 -04:00
parent 22b7ae7198
commit dc2d4b5881
7 changed files with 171 additions and 88 deletions

View File

@@ -46,12 +46,23 @@
// #define TINY_GSM_MODEM_XBEE
// #define TINY_GSM_MODEM_SEQUANS_MONARCH
#include <TinyGsmClient.h>
#include <PubSubClient.h>
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// See the debugging, if wanted
#define TINY_GSM_DEBUG SerialMon
// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 38400
// Add a reception delay, if needed
#define TINY_GSM_YIELD() { delay(2); }
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
@@ -73,7 +84,17 @@ const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init";
const char* topicLedStatus = "GsmClientTest/ledStatus";
#include <TinyGsmClient.h>
#include <PubSubClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
PubSubClient mqtt(client);
@@ -83,12 +104,22 @@ int ledStatus = LOW;
long lastReconnectAttempt = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
// Set console baud rate
SerialMon.begin(115200);
delay(10);
// Set your reset, enable, power pins here
pinMode(LED_PIN, OUTPUT);
pinMode(20, OUTPUT);
digitalWrite(20, HIGH);
pinMode(23, OUTPUT);
digitalWrite(23, LOW);
SerialMon.println("Wait...");
// Set GSM module baud rate
SerialAT.begin(115200);
delay(3000);
@@ -97,6 +128,7 @@ void setup() {
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
@@ -105,20 +137,43 @@ void setup() {
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
#if TINY_GSM_USE_WIFI
SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail");
while (true);
delay(10000);
return;
}
SerialMon.println(" OK");
#endif
#if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
// The XBee must run the gprsConnect function BEFORE waiting for network!
modem.gprsConnect(apn, gprsUser, gprsPass);
#endif
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork(240000L)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" OK");
SerialMon.print("Connecting to ");
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
#if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, user, pass)) {
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
while (true);
delay(10000);
return;
}
SerialMon.println(" OK");
#endif
// MQTT Broker setup
mqtt.setServer(broker, 1883);