|
@ -1,12 +1,14 @@ |
|
|
/**************************************************************
|
|
|
/**************************************************************
|
|
|
* |
|
|
* |
|
|
* For this example, you need to install PubSubClient library: |
|
|
* For this example, you need to install PubSubClient library: |
|
|
* https://github.com/knolleary/pubsubclient/releases/latest
|
|
|
|
|
|
|
|
|
* https://github.com/knolleary/pubsubclient
|
|
|
* or from http://librarymanager/all#PubSubClient
|
|
|
* or from http://librarymanager/all#PubSubClient
|
|
|
* |
|
|
* |
|
|
* TinyGSM Getting Started guide: |
|
|
* TinyGSM Getting Started guide: |
|
|
* http://tiny.cc/tiny-gsm-readme
|
|
|
* http://tiny.cc/tiny-gsm-readme
|
|
|
* |
|
|
* |
|
|
|
|
|
* For more MQTT examples, see PubSubClient library |
|
|
|
|
|
* |
|
|
************************************************************** |
|
|
************************************************************** |
|
|
* Use Mosquitto client tools to work with MQTT |
|
|
* Use Mosquitto client tools to work with MQTT |
|
|
* Ubuntu/Linux: sudo apt-get install mosquitto-clients |
|
|
* Ubuntu/Linux: sudo apt-get install mosquitto-clients |
|
@ -38,11 +40,8 @@ |
|
|
#include <TinyGsmClient.h>
|
|
|
#include <TinyGsmClient.h>
|
|
|
#include <PubSubClient.h>
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
|
|
|
// Your GPRS credentials
|
|
|
|
|
|
// Leave empty, if missing user or pass
|
|
|
|
|
|
const char apn[] = "YourAPN"; |
|
|
|
|
|
const char user[] = ""; |
|
|
|
|
|
const char pass[] = ""; |
|
|
|
|
|
|
|
|
// Set serial for debug console (to the Serial Monitor, default speed 115200)
|
|
|
|
|
|
#define SerialMon Serial
|
|
|
|
|
|
|
|
|
// Use Hardware Serial on Mega, Leonardo, Micro
|
|
|
// Use Hardware Serial on Mega, Leonardo, Micro
|
|
|
#define SerialAT Serial1
|
|
|
#define SerialAT Serial1
|
|
@ -51,16 +50,24 @@ const char pass[] = ""; |
|
|
//#include <SoftwareSerial.h>
|
|
|
//#include <SoftwareSerial.h>
|
|
|
//SoftwareSerial SerialAT(2, 3); // RX, TX
|
|
|
//SoftwareSerial SerialAT(2, 3); // RX, TX
|
|
|
|
|
|
|
|
|
TinyGsm modem(SerialAT); |
|
|
|
|
|
TinyGsmClient client(modem); |
|
|
|
|
|
PubSubClient mqtt(client); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Your GPRS credentials
|
|
|
|
|
|
// Leave empty, if missing user or pass
|
|
|
|
|
|
const char apn[] = "YourAPN"; |
|
|
|
|
|
const char user[] = ""; |
|
|
|
|
|
const char pass[] = ""; |
|
|
|
|
|
|
|
|
|
|
|
// MQTT details
|
|
|
const char* broker = "test.mosquitto.org"; |
|
|
const char* broker = "test.mosquitto.org"; |
|
|
|
|
|
|
|
|
const char* topicLed = "GsmClientTest/led"; |
|
|
const char* topicLed = "GsmClientTest/led"; |
|
|
const char* topicInit = "GsmClientTest/init"; |
|
|
const char* topicInit = "GsmClientTest/init"; |
|
|
const char* topicLedStatus = "GsmClientTest/ledStatus"; |
|
|
const char* topicLedStatus = "GsmClientTest/ledStatus"; |
|
|
|
|
|
|
|
|
|
|
|
TinyGsm modem(SerialAT); |
|
|
|
|
|
TinyGsmClient client(modem); |
|
|
|
|
|
PubSubClient mqtt(client); |
|
|
|
|
|
|
|
|
#define LED_PIN 13
|
|
|
#define LED_PIN 13
|
|
|
int ledStatus = LOW; |
|
|
int ledStatus = LOW; |
|
|
|
|
|
|
|
@ -70,7 +77,7 @@ void setup() { |
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
|
|
|
|
|
|
// Set console baud rate
|
|
|
// Set console baud rate
|
|
|
Serial.begin(115200); |
|
|
|
|
|
|
|
|
SerialMon.begin(115200); |
|
|
delay(10); |
|
|
delay(10); |
|
|
|
|
|
|
|
|
// Set GSM module baud rate
|
|
|
// Set GSM module baud rate
|
|
@ -79,30 +86,30 @@ void setup() { |
|
|
|
|
|
|
|
|
// Restart takes quite some time
|
|
|
// Restart takes quite some time
|
|
|
// To skip it, call init() instead of restart()
|
|
|
// To skip it, call init() instead of restart()
|
|
|
Serial.println("Initializing modem..."); |
|
|
|
|
|
|
|
|
SerialMon.println("Initializing modem..."); |
|
|
modem.restart(); |
|
|
modem.restart(); |
|
|
|
|
|
|
|
|
String modemInfo = modem.getModemInfo(); |
|
|
String modemInfo = modem.getModemInfo(); |
|
|
Serial.print("Modem: "); |
|
|
|
|
|
Serial.println(modemInfo); |
|
|
|
|
|
|
|
|
SerialMon.print("Modem: "); |
|
|
|
|
|
SerialMon.println(modemInfo); |
|
|
|
|
|
|
|
|
// Unlock your SIM card with a PIN
|
|
|
// Unlock your SIM card with a PIN
|
|
|
//modem.simUnlock("1234");
|
|
|
//modem.simUnlock("1234");
|
|
|
|
|
|
|
|
|
Serial.print("Waiting for network..."); |
|
|
|
|
|
|
|
|
SerialMon.print("Waiting for network..."); |
|
|
if (!modem.waitForNetwork()) { |
|
|
if (!modem.waitForNetwork()) { |
|
|
Serial.println(" fail"); |
|
|
|
|
|
|
|
|
SerialMon.println(" fail"); |
|
|
while (true); |
|
|
while (true); |
|
|
} |
|
|
} |
|
|
Serial.println(" OK"); |
|
|
|
|
|
|
|
|
SerialMon.println(" OK"); |
|
|
|
|
|
|
|
|
Serial.print("Connecting to "); |
|
|
|
|
|
Serial.print(apn); |
|
|
|
|
|
|
|
|
SerialMon.print("Connecting to "); |
|
|
|
|
|
SerialMon.print(apn); |
|
|
if (!modem.gprsConnect(apn, user, pass)) { |
|
|
if (!modem.gprsConnect(apn, user, pass)) { |
|
|
Serial.println(" fail"); |
|
|
|
|
|
|
|
|
SerialMon.println(" fail"); |
|
|
while (true); |
|
|
while (true); |
|
|
} |
|
|
} |
|
|
Serial.println(" OK"); |
|
|
|
|
|
|
|
|
SerialMon.println(" OK"); |
|
|
|
|
|
|
|
|
// MQTT Broker setup
|
|
|
// MQTT Broker setup
|
|
|
mqtt.setServer(broker, 1883); |
|
|
mqtt.setServer(broker, 1883); |
|
@ -110,13 +117,13 @@ void setup() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
boolean mqttConnect() { |
|
|
boolean mqttConnect() { |
|
|
Serial.print("Connecting to "); |
|
|
|
|
|
Serial.print(broker); |
|
|
|
|
|
|
|
|
SerialMon.print("Connecting to "); |
|
|
|
|
|
SerialMon.print(broker); |
|
|
if (!mqtt.connect("GsmClientTest")) { |
|
|
if (!mqtt.connect("GsmClientTest")) { |
|
|
Serial.println(" fail"); |
|
|
|
|
|
|
|
|
SerialMon.println(" fail"); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
Serial.println(" OK"); |
|
|
|
|
|
|
|
|
SerialMon.println(" OK"); |
|
|
mqtt.publish(topicInit, "GsmClientTest started"); |
|
|
mqtt.publish(topicInit, "GsmClientTest started"); |
|
|
mqtt.subscribe(topicLed); |
|
|
mqtt.subscribe(topicLed); |
|
|
return mqtt.connected(); |
|
|
return mqtt.connected(); |
|
@ -140,11 +147,11 @@ void loop() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void mqttCallback(char* topic, byte* payload, unsigned int len) { |
|
|
void mqttCallback(char* topic, byte* payload, unsigned int len) { |
|
|
Serial.print("Message arrived ["); |
|
|
|
|
|
Serial.print(topic); |
|
|
|
|
|
Serial.print("]: "); |
|
|
|
|
|
Serial.write(payload, len); |
|
|
|
|
|
Serial.println(); |
|
|
|
|
|
|
|
|
SerialMon.print("Message arrived ["); |
|
|
|
|
|
SerialMon.print(topic); |
|
|
|
|
|
SerialMon.print("]: "); |
|
|
|
|
|
SerialMon.write(payload, len); |
|
|
|
|
|
SerialMon.println(); |
|
|
|
|
|
|
|
|
// Only proceed if incoming message's topic matches
|
|
|
// Only proceed if incoming message's topic matches
|
|
|
if (String(topic) == topicLed) { |
|
|
if (String(topic) == topicLed) { |
|
|