Update Blynk and MQTT examples
This commit is contained in:
@@ -41,15 +41,8 @@
|
|||||||
#include <TinyGsmClient.h>
|
#include <TinyGsmClient.h>
|
||||||
#include <BlynkSimpleSIM800.h>
|
#include <BlynkSimpleSIM800.h>
|
||||||
|
|
||||||
// You should get Auth Token in the Blynk App.
|
// Set serial for debug console (to the Serial Monitor, default speed 115200)
|
||||||
// Go to the Project Settings (nut icon).
|
#define SerialMon Serial
|
||||||
const char auth[] = "YourAuthToken";
|
|
||||||
|
|
||||||
// Your GPRS credentials
|
|
||||||
// Leave empty, if missing user or pass
|
|
||||||
const char apn[] = "YourAPN";
|
|
||||||
const char user[] = "";
|
|
||||||
const char pass[] = "";
|
|
||||||
|
|
||||||
// Hardware Serial on Mega, Leonardo, Micro
|
// Hardware Serial on Mega, Leonardo, Micro
|
||||||
#define SerialAT Serial1
|
#define SerialAT Serial1
|
||||||
@@ -58,12 +51,23 @@ const char pass[] = "";
|
|||||||
//#include <SoftwareSerial.h>
|
//#include <SoftwareSerial.h>
|
||||||
//SoftwareSerial SerialAT(2, 3); // RX, TX
|
//SoftwareSerial SerialAT(2, 3); // RX, TX
|
||||||
|
|
||||||
|
|
||||||
|
// Your GPRS credentials
|
||||||
|
// Leave empty, if missing user or pass
|
||||||
|
const char apn[] = "YourAPN";
|
||||||
|
const char user[] = "";
|
||||||
|
const char pass[] = "";
|
||||||
|
|
||||||
|
// You should get Auth Token in the Blynk App.
|
||||||
|
// Go to the Project Settings (nut icon).
|
||||||
|
const char auth[] = "YourAuthToken";
|
||||||
|
|
||||||
TinyGsm modem(SerialAT);
|
TinyGsm modem(SerialAT);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
// 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
|
||||||
@@ -72,12 +76,12 @@ 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: ");
|
SerialMon.print("Modem: ");
|
||||||
Serial.println(modemInfo);
|
SerialMon.println(modemInfo);
|
||||||
|
|
||||||
// Unlock your SIM card with a PIN
|
// Unlock your SIM card with a PIN
|
||||||
//modem.simUnlock("1234");
|
//modem.simUnlock("1234");
|
||||||
|
@@ -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
|
// Set serial for debug console (to the Serial Monitor, default speed 115200)
|
||||||
// Leave empty, if missing user or pass
|
#define SerialMon Serial
|
||||||
const char apn[] = "YourAPN";
|
|
||||||
const char user[] = "";
|
|
||||||
const char pass[] = "";
|
|
||||||
|
|
||||||
// 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: ");
|
SerialMon.print("Modem: ");
|
||||||
Serial.println(modemInfo);
|
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 ");
|
SerialMon.print("Connecting to ");
|
||||||
Serial.print(apn);
|
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 ");
|
SerialMon.print("Connecting to ");
|
||||||
Serial.print(broker);
|
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 [");
|
SerialMon.print("Message arrived [");
|
||||||
Serial.print(topic);
|
SerialMon.print(topic);
|
||||||
Serial.print("]: ");
|
SerialMon.print("]: ");
|
||||||
Serial.write(payload, len);
|
SerialMon.write(payload, len);
|
||||||
Serial.println();
|
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) {
|
||||||
|
Reference in New Issue
Block a user