Browse Source

Another update to examples

v_master
Sara Damiano 5 years ago
parent
commit
3a8a2ada1a
8 changed files with 251 additions and 80 deletions
  1. +15
    -2
      examples/AllFunctions/AllFunctions.ino
  2. +2
    -3
      examples/BlynkClient/BlynkClient.ino
  3. +40
    -5
      examples/FileDownload/FileDownload.ino
  4. +33
    -6
      examples/HttpClient/HttpClient.ino
  5. +34
    -7
      examples/HttpsClient/HttpsClient.ino
  6. +71
    -47
      examples/MqttClient/MqttClient.ino
  7. +28
    -5
      examples/WebClient/WebClient.ino
  8. +28
    -5
      tools/Diagnostics/Diagnostics.ino

+ 15
- 2
examples/AllFunctions/AllFunctions.ino View File

@ -70,16 +70,29 @@
//#define SMS_TARGET "+380xxxxxxxxx" //#define SMS_TARGET "+380xxxxxxxxx"
//#define CALL_TARGET "+380xxxxxxxxx" //#define CALL_TARGET "+380xxxxxxxxx"
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID"; const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#if TINY_GSM_TEST_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#define TINY_GSM_TEST_GPRS false
#define TINY_GSM_TEST_CALL false
#define TINY_GSM_TEST_SMS false
#define TINY_GSM_TEST_USSD false
#define TINY_GSM_TEST_WIFI true
#endif
#if TINY_GSM_TEST_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#endif
#ifdef DUMP_AT_COMMANDS #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);


+ 2
- 3
examples/BlynkClient/BlynkClient.ino View File

@ -63,8 +63,7 @@
//SoftwareSerial SerialAT(2, 3); // RX, TX //SoftwareSerial SerialAT(2, 3); // RX, TX
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char user[] = ""; const char user[] = "";
const char pass[] = ""; const char pass[] = "";
@ -91,7 +90,7 @@ void setup()
modem.restart(); modem.restart();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN // Unlock your SIM card with a PIN


+ 40
- 5
examples/FileDownload/FileDownload.ino View File

@ -61,17 +61,19 @@
// Add a reception delay - may be needed for a fast processor at a slow baud rate // 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_YIELD() { delay(2); }
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false #define TINY_GSM_USE_WIFI false
// set GSM PIN, if any // set GSM PIN, if any
#define GSM_PIN "" #define GSM_PIN ""
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID"; const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
@ -82,6 +84,20 @@ const int port = 80;
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#include <CRC32.h> #include <CRC32.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
const char resource[] = "/TinyGSM/test_1k.bin"; const char resource[] = "/TinyGSM/test_1k.bin";
uint32_t knownCRC32 = 0x6f50d767; uint32_t knownCRC32 = 0x6f50d767;
uint32_t knownFileSize = 1024; // In case server does not send it uint32_t knownFileSize = 1024; // In case server does not send it
@ -101,6 +117,12 @@ void setup() {
SerialMon.begin(115200); SerialMon.begin(115200);
delay(10); delay(10);
// !!!!!!!!!!!
// Set your reset, enable, power pins here
// !!!!!!!!!!!
SerialMon.println("Wait...");
// Set GSM module baud rate // Set GSM module baud rate
SerialAT.begin(115200); SerialAT.begin(115200);
delay(3000); delay(3000);
@ -109,9 +131,10 @@ void setup() {
// To skip it, call init() instead of restart() // To skip it, call init() instead of restart()
SerialMon.println("Initializing modem..."); SerialMon.println("Initializing modem...");
modem.restart(); modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS #if TINY_GSM_USE_GPRS
@ -135,7 +158,8 @@ void printPercent(uint32_t readLength, uint32_t contentLength) {
void loop() { void loop() {
#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...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -162,7 +186,8 @@ void loop() {
SerialMon.println("Network connected"); 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(F("Connecting to "));
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -171,6 +196,10 @@ void loop() {
return; return;
} }
SerialMon.println(" success"); SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif #endif
SerialMon.print(F("Connecting to ")); SerialMon.print(F("Connecting to "));
@ -292,8 +321,14 @@ void loop() {
client.stop(); client.stop();
SerialMon.println(F("Server disconnected")); SerialMon.println(F("Server disconnected"));
#if TINY_GSM_USE_WIFI
modem.networkDisconnect();
SerialMon.println(F("WiFi disconnected"));
#endif
#if TINY_GSM_USE_GPRS
modem.gprsDisconnect(); modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected")); SerialMon.println(F("GPRS disconnected"));
#endif
float duration = float(timeElapsed) / 1000; float duration = float(timeElapsed) / 1000;


+ 33
- 6
examples/HttpClient/HttpClient.ino View File

@ -63,20 +63,26 @@
#define TINY_GSM_DEBUG SerialMon #define TINY_GSM_DEBUG SerialMon
//#define LOGGING // <- Logging is for the HTTP library //#define LOGGING // <- Logging is for the HTTP library
// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// Add a reception delay - may be needed for a fast processor at a slow baud rate // 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_YIELD() { delay(2); }
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false #define TINY_GSM_USE_WIFI false
// set GSM PIN, if any // set GSM PIN, if any
#define GSM_PIN "" #define GSM_PIN ""
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID"; const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
@ -88,6 +94,20 @@ const int port = 80;
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#include <ArduinoHttpClient.h> #include <ArduinoHttpClient.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 #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);
@ -111,7 +131,8 @@ void setup() {
SerialMon.println("Wait..."); SerialMon.println("Wait...");
// Set GSM module baud rate // Set GSM module baud rate
SerialAT.begin(115200);
// TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
SerialAT.begin(9600);
delay(3000); delay(3000);
// Restart takes quite some time // Restart takes quite some time
@ -121,7 +142,7 @@ void setup() {
// modem.init(); // modem.init();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS #if TINY_GSM_USE_GPRS
@ -134,7 +155,8 @@ void setup() {
void loop() { void loop() {
#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...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -161,7 +183,8 @@ void loop() {
SerialMon.println("Network connected"); 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(F("Connecting to "));
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -170,6 +193,10 @@ void loop() {
return; return;
} }
SerialMon.println(" success"); SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif #endif
SerialMon.print(F("Performing HTTP GET request... ")); SerialMon.print(F("Performing HTTP GET request... "));


+ 34
- 7
examples/HttpsClient/HttpsClient.ino View File

@ -55,20 +55,26 @@
#define TINY_GSM_DEBUG SerialMon #define TINY_GSM_DEBUG SerialMon
//#define LOGGING // <- Logging is for the HTTP library //#define LOGGING // <- Logging is for the HTTP library
// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// Add a reception delay - may be needed for a fast processor at a slow baud rate // 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_YIELD() { delay(2); }
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false #define TINY_GSM_USE_WIFI false
// set GSM PIN, if any // set GSM PIN, if any
#define GSM_PIN "" #define GSM_PIN ""
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID"; const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
@ -80,6 +86,20 @@ const int port = 443;
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#include <ArduinoHttpClient.h> #include <ArduinoHttpClient.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 #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);
@ -103,7 +123,8 @@ void setup() {
SerialMon.println("Wait..."); SerialMon.println("Wait...");
// Set GSM module baud rate // Set GSM module baud rate
SerialAT.begin(115200);
// TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
SerialAT.begin(9600);
delay(3000); delay(3000);
// Restart takes quite some time // Restart takes quite some time
@ -113,7 +134,7 @@ void setup() {
// modem.init(); // modem.init();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS #if TINY_GSM_USE_GPRS
@ -131,7 +152,8 @@ void setup() {
void loop() { void loop() {
#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...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -158,7 +180,8 @@ void loop() {
SerialMon.println("Network connected"); 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(F("Connecting to "));
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -167,10 +190,14 @@ void loop() {
return; return;
} }
SerialMon.println(" success"); SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif #endif
SerialMon.print(F("Performing HTTPS GET request... ")); SerialMon.print(F("Performing HTTPS GET request... "));
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
int err = http.get(resource); int err = http.get(resource);
if (err != 0) { if (err != 0) {
SerialMon.println(F("failed to connect")); SerialMon.println(F("failed to connect"));


+ 71
- 47
examples/MqttClient/MqttClient.ino View File

@ -67,27 +67,29 @@
// Range to attempt to autobaud // Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600 #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 // 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_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 // set GSM PIN, if any
#define GSM_PIN "" #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 gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
const char wifiSSID[] = "YourSSID";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
// MQTT details // MQTT details
const char* broker = "test.mosquitto.org";
const char* broker = "broker.hivemq.com";
const char* topicLed = "GsmClientTest/led"; const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init"; const char* topicInit = "GsmClientTest/init";
@ -96,6 +98,20 @@ const char* topicLedStatus = "GsmClientTest/ledStatus";
#include <TinyGsmClient.h> #include <TinyGsmClient.h>
#include <PubSubClient.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 #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);
@ -111,6 +127,42 @@ int ledStatus = LOW;
long lastReconnectAttempt = 0; 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() { void setup() {
// Set console baud rate // Set console baud rate
SerialMon.begin(115200); SerialMon.begin(115200);
@ -125,7 +177,8 @@ void setup() {
SerialMon.println("Wait..."); SerialMon.println("Wait...");
// Set GSM module baud rate // Set GSM module baud rate
SerialAT.begin(115200);
// TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
SerialAT.begin(9600);
delay(3000); delay(3000);
// Restart takes quite some time // Restart takes quite some time
@ -135,7 +188,7 @@ void setup() {
// modem.init(); // modem.init();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS #if TINY_GSM_USE_GPRS
@ -145,7 +198,8 @@ void setup() {
} }
#endif #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...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -172,7 +226,8 @@ void setup() {
SerialMon.println("Network connected"); 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(F("Connecting to "));
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -181,6 +236,10 @@ void setup() {
return; return;
} }
SerialMon.println(" success"); SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif #endif
// MQTT Broker setup // MQTT Broker setup
@ -188,26 +247,6 @@ void setup() {
mqtt.setCallback(mqttCallback); 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() { void loop() {
if (!mqtt.connected()) { if (!mqtt.connected()) {
@ -226,18 +265,3 @@ void loop() {
mqtt.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");
}
}

+ 28
- 5
examples/WebClient/WebClient.ino View File

@ -62,17 +62,20 @@
// Uncomment this if you want to use SSL // Uncomment this if you want to use SSL
//#define USE_SSL //#define USE_SSL
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true #define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false #define TINY_GSM_USE_WIFI false
// set GSM PIN, if any // set GSM PIN, if any
#define GSM_PIN "" #define GSM_PIN ""
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
// Server details // Server details
@ -81,6 +84,20 @@ const char resource[] = "/TinyGSM/logo.txt";
#include <TinyGsmClient.h> #include <TinyGsmClient.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 #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);
@ -120,7 +137,7 @@ void setup() {
// modem.init(); // modem.init();
String modemInfo = modem.getModemInfo(); String modemInfo = modem.getModemInfo();
SerialMon.print("Modem: ");
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo); SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS #if TINY_GSM_USE_GPRS
@ -133,7 +150,8 @@ void setup() {
void loop() { void loop() {
#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...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -160,7 +178,8 @@ void loop() {
SerialMon.println("Network connected"); 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(F("Connecting to "));
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -169,6 +188,10 @@ void loop() {
return; return;
} }
SerialMon.println(" success"); SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif #endif
SerialMon.print("Connecting to "); SerialMon.print("Connecting to ");


+ 28
- 5
tools/Diagnostics/Diagnostics.ino View File

@ -69,11 +69,12 @@
// set GSM PIN, if any // set GSM PIN, if any
#define GSM_PIN "" #define GSM_PIN ""
// Your GPRS credentials
// Leave empty, if missing user or pass
// Your GPRS credentials, if any
const char apn[] = "YourAPN"; const char apn[] = "YourAPN";
const char gprsUser[] = ""; const char gprsUser[] = "";
const char gprsPass[] = ""; const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID"; const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass"; const char wifiPass[] = "YourWiFiPass";
@ -83,6 +84,20 @@ const char resource[] = "/TinyGSM/logo.txt";
#include <TinyGsmClient.h> #include <TinyGsmClient.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 #ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h> #include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon); StreamDebugger debugger(SerialAT, SerialMon);
@ -111,8 +126,8 @@ void setup() {
SerialMon.println("Wait..."); SerialMon.println("Wait...");
// Set GSM module baud rate // Set GSM module baud rate
TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
// SerialAT.begin(115200);
// TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
SerialAT.begin(115200);
delay(3000); delay(3000);
} }
@ -147,6 +162,7 @@ void loop() {
#endif #endif
#if TINY_GSM_USE_WIFI #if TINY_GSM_USE_WIFI
// Wifi connection parameters must be set before waiting for the network
SerialMon.print(F("Setting SSID/password...")); SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) { if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail"); SerialMon.println(" fail");
@ -175,7 +191,8 @@ void loop() {
} }
SerialMon.println(F(" [OK]")); SerialMon.println(F(" [OK]"));
#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("Connecting to "); SerialMon.print("Connecting to ");
SerialMon.print(apn); SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
@ -233,8 +250,14 @@ void loop() {
client.stop(); client.stop();
SerialMon.println(F("Server disconnected")); SerialMon.println(F("Server disconnected"));
#if TINY_GSM_USE_WIFI
modem.networkDisconnect();
SerialMon.println(F("WiFi disconnected"));
#endif
#if TINY_GSM_USE_GPRS
modem.gprsDisconnect(); modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected")); SerialMon.println(F("GPRS disconnected"));
#endif
SerialMon.println(); SerialMon.println();
SerialMon.println(F("************************")); SerialMon.println(F("************************"));


Loading…
Cancel
Save