mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	Update Blynk and MQTT examples
This commit is contained in:
		| @@ -41,15 +41,8 @@ | ||||
| #include <TinyGsmClient.h> | ||||
| #include <BlynkSimpleSIM800.h> | ||||
|  | ||||
| // You should get Auth Token in the Blynk App. | ||||
| // Go to the Project Settings (nut icon). | ||||
| const char auth[] = "YourAuthToken"; | ||||
|  | ||||
| // 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 | ||||
|  | ||||
| // Hardware Serial on Mega, Leonardo, Micro | ||||
| #define SerialAT Serial1 | ||||
| @@ -58,12 +51,23 @@ const char pass[] = ""; | ||||
| //#include <SoftwareSerial.h> | ||||
| //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); | ||||
|  | ||||
| void setup() | ||||
| { | ||||
|   // Set console baud rate | ||||
|   Serial.begin(115200); | ||||
|   SerialMon.begin(115200); | ||||
|   delay(10); | ||||
|  | ||||
|   // Set GSM module baud rate | ||||
| @@ -72,12 +76,12 @@ void setup() | ||||
|  | ||||
|   // Restart takes quite some time | ||||
|   // To skip it, call init() instead of restart() | ||||
|   Serial.println("Initializing modem..."); | ||||
|   SerialMon.println("Initializing modem..."); | ||||
|   modem.restart(); | ||||
|  | ||||
|   String modemInfo = modem.getModemInfo(); | ||||
|   Serial.print("Modem: "); | ||||
|   Serial.println(modemInfo); | ||||
|   SerialMon.print("Modem: "); | ||||
|   SerialMon.println(modemInfo); | ||||
|  | ||||
|   // Unlock your SIM card with a PIN | ||||
|   //modem.simUnlock("1234"); | ||||
|   | ||||
| @@ -1,12 +1,14 @@ | ||||
| /************************************************************** | ||||
|  * | ||||
|  * 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 | ||||
|  * | ||||
|  * TinyGSM Getting Started guide: | ||||
|  *   http://tiny.cc/tiny-gsm-readme | ||||
|  * | ||||
|  * For more MQTT examples, see PubSubClient library | ||||
|  * | ||||
|  ************************************************************** | ||||
|  * Use Mosquitto client tools to work with MQTT | ||||
|  *   Ubuntu/Linux: sudo apt-get install mosquitto-clients | ||||
| @@ -38,11 +40,8 @@ | ||||
| #include <TinyGsmClient.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 | ||||
| #define SerialAT Serial1 | ||||
| @@ -51,16 +50,24 @@ const char pass[] = ""; | ||||
| //#include <SoftwareSerial.h> | ||||
| //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* topicLed = "GsmClientTest/led"; | ||||
| const char* topicInit = "GsmClientTest/init"; | ||||
| const char* topicLedStatus = "GsmClientTest/ledStatus"; | ||||
|  | ||||
| TinyGsm modem(SerialAT); | ||||
| TinyGsmClient client(modem); | ||||
| PubSubClient mqtt(client); | ||||
|  | ||||
| #define LED_PIN 13 | ||||
| int ledStatus = LOW; | ||||
|  | ||||
| @@ -70,7 +77,7 @@ void setup() { | ||||
|   pinMode(LED_PIN, OUTPUT); | ||||
|  | ||||
|   // Set console baud rate | ||||
|   Serial.begin(115200); | ||||
|   SerialMon.begin(115200); | ||||
|   delay(10); | ||||
|  | ||||
|   // Set GSM module baud rate | ||||
| @@ -79,30 +86,30 @@ void setup() { | ||||
|  | ||||
|   // Restart takes quite some time | ||||
|   // To skip it, call init() instead of restart() | ||||
|   Serial.println("Initializing modem..."); | ||||
|   SerialMon.println("Initializing modem..."); | ||||
|   modem.restart(); | ||||
|  | ||||
|   String modemInfo = modem.getModemInfo(); | ||||
|   Serial.print("Modem: "); | ||||
|   Serial.println(modemInfo); | ||||
|   SerialMon.print("Modem: "); | ||||
|   SerialMon.println(modemInfo); | ||||
|  | ||||
|   // Unlock your SIM card with a PIN | ||||
|   //modem.simUnlock("1234"); | ||||
|  | ||||
|   Serial.print("Waiting for network..."); | ||||
|   SerialMon.print("Waiting for network..."); | ||||
|   if (!modem.waitForNetwork()) { | ||||
|     Serial.println(" fail"); | ||||
|     SerialMon.println(" fail"); | ||||
|     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)) { | ||||
|     Serial.println(" fail"); | ||||
|     SerialMon.println(" fail"); | ||||
|     while (true); | ||||
|   } | ||||
|   Serial.println(" OK"); | ||||
|   SerialMon.println(" OK"); | ||||
|  | ||||
|   // MQTT Broker setup | ||||
|   mqtt.setServer(broker, 1883); | ||||
| @@ -110,13 +117,13 @@ void setup() { | ||||
| } | ||||
|  | ||||
| boolean mqttConnect() { | ||||
|   Serial.print("Connecting to "); | ||||
|   Serial.print(broker); | ||||
|   SerialMon.print("Connecting to "); | ||||
|   SerialMon.print(broker); | ||||
|   if (!mqtt.connect("GsmClientTest")) { | ||||
|     Serial.println(" fail"); | ||||
|     SerialMon.println(" fail"); | ||||
|     return false; | ||||
|   } | ||||
|   Serial.println(" OK"); | ||||
|   SerialMon.println(" OK"); | ||||
|   mqtt.publish(topicInit, "GsmClientTest started"); | ||||
|   mqtt.subscribe(topicLed); | ||||
|   return mqtt.connected(); | ||||
| @@ -140,11 +147,11 @@ void loop() { | ||||
| } | ||||
|  | ||||
| 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 | ||||
|   if (String(topic) == topicLed) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user