mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	Another update to examples
This commit is contained in:
		| @@ -67,27 +67,29 @@ | ||||
|  | ||||
| // Range to attempt to autobaud | ||||
| #define GSM_AUTOBAUD_MIN 9600 | ||||
| #define GSM_AUTOBAUD_MAX 38400 | ||||
| #define GSM_AUTOBAUD_MAX 115200 | ||||
|  | ||||
| // Add a reception delay - may be needed for a fast processor at a slow baud rate | ||||
| //#define TINY_GSM_YIELD() { delay(2); } | ||||
|  | ||||
| #define TINY_GSM_USE_GPRS true | ||||
| #define TINY_GSM_USE_WIFI false | ||||
| // Define how you're planning to connect to the internet | ||||
| #define TINY_GSM_USE_GPRS false | ||||
| #define TINY_GSM_USE_WIFI true | ||||
|  | ||||
| // set GSM PIN, if any | ||||
| #define GSM_PIN "" | ||||
|  | ||||
| // Your GPRS credentials | ||||
| // Leave empty, if missing user or pass | ||||
| const char apn[]  = "YourAPN"; | ||||
| // Your GPRS credentials, if any | ||||
| const char apn[] = "YourAPN"; | ||||
| const char gprsUser[] = ""; | ||||
| const char gprsPass[] = ""; | ||||
| const char wifiSSID[]  = "YourSSID"; | ||||
|  | ||||
| // Your WiFi connection credentials, if applicable | ||||
| const char wifiSSID[] = "YourSSID"; | ||||
| const char wifiPass[] = "YourWiFiPass"; | ||||
|  | ||||
| // MQTT details | ||||
| const char* broker = "test.mosquitto.org"; | ||||
| const char* broker = "broker.hivemq.com"; | ||||
|  | ||||
| const char* topicLed = "GsmClientTest/led"; | ||||
| const char* topicInit = "GsmClientTest/init"; | ||||
| @@ -96,6 +98,20 @@ const char* topicLedStatus = "GsmClientTest/ledStatus"; | ||||
| #include <TinyGsmClient.h> | ||||
| #include <PubSubClient.h> | ||||
|  | ||||
| // Just in case someone defined the wrong thing.. | ||||
| #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS | ||||
| #undef TINY_GSM_USE_GPRS | ||||
| #undef TINY_GSM_USE_WIFI | ||||
| #define TINY_GSM_USE_GPRS false | ||||
| #define TINY_GSM_USE_WIFI true | ||||
| #endif | ||||
| #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI | ||||
| #undef TINY_GSM_USE_GPRS | ||||
| #undef TINY_GSM_USE_WIFI | ||||
| #define TINY_GSM_USE_GPRS true | ||||
| #define TINY_GSM_USE_WIFI false | ||||
| #endif | ||||
|  | ||||
| #ifdef DUMP_AT_COMMANDS | ||||
|   #include <StreamDebugger.h> | ||||
|   StreamDebugger debugger(SerialAT, SerialMon); | ||||
| @@ -111,6 +127,42 @@ int ledStatus = LOW; | ||||
|  | ||||
| long lastReconnectAttempt = 0; | ||||
|  | ||||
| void mqttCallback(char* topic, byte* payload, unsigned int len) { | ||||
|   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) { | ||||
|     ledStatus = !ledStatus; | ||||
|     digitalWrite(LED_PIN, ledStatus); | ||||
|     mqtt.publish(topicLedStatus, ledStatus ? "1" : "0"); | ||||
|   } | ||||
| } | ||||
|  | ||||
| boolean mqttConnect() { | ||||
|   SerialMon.print("Connecting to "); | ||||
|   SerialMon.print(broker); | ||||
|  | ||||
|   // Connect to MQTT Broker | ||||
|   boolean status = mqtt.connect("GsmClientTest"); | ||||
|  | ||||
|   // Or, if you want to authenticate MQTT: | ||||
|   //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass"); | ||||
|  | ||||
|   if (status == false) { | ||||
|     SerialMon.println(" fail"); | ||||
|     return false; | ||||
|   } | ||||
|   SerialMon.println(" success"); | ||||
|   mqtt.publish(topicInit, "GsmClientTest started"); | ||||
|   mqtt.subscribe(topicLed); | ||||
|   return mqtt.connected(); | ||||
| } | ||||
|  | ||||
|  | ||||
| void setup() { | ||||
|   // Set console baud rate | ||||
|   SerialMon.begin(115200); | ||||
| @@ -125,7 +177,8 @@ void setup() { | ||||
|   SerialMon.println("Wait..."); | ||||
|  | ||||
|   // Set GSM module baud rate | ||||
|   SerialAT.begin(115200); | ||||
|   // TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX); | ||||
|   SerialAT.begin(9600); | ||||
|   delay(3000); | ||||
|  | ||||
|   // Restart takes quite some time | ||||
| @@ -135,7 +188,7 @@ void setup() { | ||||
|   // modem.init(); | ||||
|  | ||||
|   String modemInfo = modem.getModemInfo(); | ||||
|   SerialMon.print("Modem: "); | ||||
|   SerialMon.print("Modem Info: "); | ||||
|   SerialMon.println(modemInfo); | ||||
|  | ||||
| #if TINY_GSM_USE_GPRS | ||||
| @@ -145,7 +198,8 @@ void setup() { | ||||
|   } | ||||
| #endif | ||||
|  | ||||
| #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI | ||||
| #if TINY_GSM_USE_WIFI | ||||
|     // Wifi connection parameters must be set before waiting for the network | ||||
|   SerialMon.print(F("Setting SSID/password...")); | ||||
|   if (!modem.networkConnect(wifiSSID, wifiPass)) { | ||||
|     SerialMon.println(" fail"); | ||||
| @@ -172,7 +226,8 @@ void setup() { | ||||
|     SerialMon.println("Network connected"); | ||||
|   } | ||||
|  | ||||
| #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS | ||||
| #if TINY_GSM_USE_GPRS | ||||
|   // GPRS connection parameters are usually set after network registration | ||||
|     SerialMon.print(F("Connecting to ")); | ||||
|     SerialMon.print(apn); | ||||
|     if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { | ||||
| @@ -181,6 +236,10 @@ void setup() { | ||||
|       return; | ||||
|     } | ||||
|     SerialMon.println(" success"); | ||||
|  | ||||
|   if (modem.isGprsConnected()) { | ||||
|     SerialMon.println("GPRS connected"); | ||||
|   } | ||||
| #endif | ||||
|  | ||||
|   // MQTT Broker setup | ||||
| @@ -188,26 +247,6 @@ void setup() { | ||||
|   mqtt.setCallback(mqttCallback); | ||||
| } | ||||
|  | ||||
| boolean mqttConnect() { | ||||
|   SerialMon.print("Connecting to "); | ||||
|   SerialMon.print(broker); | ||||
|  | ||||
|   // Connect to MQTT Broker | ||||
|   boolean status = mqtt.connect("GsmClientTest"); | ||||
|  | ||||
|   // Or, if you want to authenticate MQTT: | ||||
|   //boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass"); | ||||
|  | ||||
|   if (status == false) { | ||||
|     SerialMon.println(" fail"); | ||||
|     return false; | ||||
|   } | ||||
|   SerialMon.println(" success"); | ||||
|   mqtt.publish(topicInit, "GsmClientTest started"); | ||||
|   mqtt.subscribe(topicLed); | ||||
|   return mqtt.connected(); | ||||
| } | ||||
|  | ||||
| void loop() { | ||||
|  | ||||
|   if (!mqtt.connected()) { | ||||
| @@ -226,18 +265,3 @@ void loop() { | ||||
|  | ||||
|   mqtt.loop(); | ||||
| } | ||||
|  | ||||
| void mqttCallback(char* topic, byte* payload, unsigned int len) { | ||||
|   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) { | ||||
|     ledStatus = !ledStatus; | ||||
|     digitalWrite(LED_PIN, ledStatus); | ||||
|     mqtt.publish(topicLedStatus, ledStatus ? "1" : "0"); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user