### WebClient Example Sketch Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md An example sketch demonstrating how to use the TinyGSM library to make HTTP requests to a web server. This requires a configured GSM module and an active internet connection. ```c++ #include #include // Define the serial port for the GSM module // For Arduino Uno/Nano: SoftwareSerial Serial1(10, 11); // RX, TX // For ESP32: use HardwareSerial // For other boards, check TinyGSM documentation #define SerialAT Serial // APN, username, and password for your mobile network #define APN "your_apn" #define USER_APN "your_username" #define PASS_APN "your_password" // Define the GSM module type (e.g., SIM800, SIM7000, BG96) // TinyGsm modem("your_apn", "your_username", "your_password"); void setup() { // Initialize serial for debug output Serial.begin(115200); Serial.println("TinyGSM WebClient Example"); // Initialize AT serial SerialAT.begin(115200); delay(3000); // Set modem serial port // modem.setSerial(Serial1); // Restart the GSM module Serial.println("Restarting modem..."); // modem.restart(); // Connect to the network Serial.println("Connecting to network..."); // if (!modem.waitForNetwork()) // { // Serial.println(" Failed to connect to network"); // return; // } Serial.println("Connected to network"); // Connect to the internet Serial.println("Connecting to internet..."); // if (!modem.gprsConnect(APN, USER_APN, PASS_APN)) // { // Serial.println(" Failed to connect to GPRS"); // return; // } Serial.println("Connected to GPRS"); // Make an HTTP GET request Serial.println("Making HTTP GET request..."); // TinyGsmClient client(modem); // client.connect("http://httpbin.org/get"); // if (client.connected()) { // client.print(String("GET /get HTTP/1.1\r\n")); // client.print(String("Host: httpbin.org\r\n")); // client.print(String("Connection: close\r\n\r\n")); // delay(1000); // // Read response // while (client.available()) { // Serial.write(client.read()); // } // client.stop(); // Serial.println("\n--- Request finished ---"); // } // else { // Serial.println("HTTP connection failed"); // } // Disconnect from GPRS // modem.gprsDisconnect(); // Serial.println("GPRS disconnected"); } void loop() { // Nothing to do here } ``` -------------------------------- ### TinyGSM Resource Usage Example Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md This example demonstrates the resource usage of the WebClient sketch for Arduino Uno using Software Serial. It highlights the low memory footprint of TinyGSM compared to other libraries. ```text Sketch uses 15022 bytes (46%) of program storage space. Maximum is 32256 bytes. Global variables use 574 bytes (28%) of dynamic memory, leaving 1474 bytes for local variables. Maximum is 2048 bytes. ``` -------------------------------- ### HTTP GET Request Formatting Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Use client.print with a single string for each header line to ensure correct HTTP request formatting. This prevents characters from being sent individually. ```cpp client.print(String("GET ") + resource + " HTTP/1.1\r\n"); ``` -------------------------------- ### Initialize Modem Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Initialize the GSM modem. `restart()` is generally more thorough than `init()` but takes longer. ```c++ modem.init() modem.restart() ``` -------------------------------- ### Create Multiple TinyGSM Client Instances Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Create multiple client instances for modules that support concurrent connections, assigning a unique ID to each. ```c++ TinyGsmClient clientX(modem, 0); TinyGsmClient clientY(modem, 1); ``` ```c++ TinyGsmClientSecure clientX(modem, 0); TinyGsmClientSecure clientY(modem, 1); ``` -------------------------------- ### Create Single TinyGSM Client Instance (Insecure) Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Create a standard TinyGSM client instance for a single connection. ```c++ TinyGsmClient client(modem); ``` -------------------------------- ### Connect to WiFi Network Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Connect the module to a WiFi network using the provided SSID and password. ```c++ modem.networkConnect(wifiSSID, wifiPass) ``` -------------------------------- ### Create TinyGSM Modem Instance Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Instantiate the TinyGSM modem object, typically linked to the serial communication port. ```c++ TinyGsm modem(SerialAT); ``` -------------------------------- ### Create Single TinyGSM Client Instance (Secure) Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Create a secure TinyGSM client instance for a single SSL/TLS connection, if supported by the module. ```c++ TinyGsmClientSecure client(modem); ``` -------------------------------- ### Auto-Bauding with TinyGSM Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Use TinyGsmAutoBaud for initial module connection and testing. Avoid using it in production environments; set a fixed baud rate with setBaud(#) instead. ```cpp TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX); ``` -------------------------------- ### Alternative HTTP Request Formatting Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Avoid sending HTTP request parts individually. Use client.print or client.write with buffers or Strings to send entire lines at once. ```cpp client.print("GET "); client.print(resource); client.println(" HTTP/1.1"); ``` -------------------------------- ### Wait for Network Registration Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Wait for the module to successfully register on the cellular network. This can take up to 600 seconds. ```c++ modem.waitForNetwork(600000L) ``` -------------------------------- ### Include TinyGSM Library Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Include the main TinyGSM client library in your Arduino sketch. ```c++ #include ``` -------------------------------- ### Define TinyGSM Modem Module Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Specify the particular modem module you are using. Only one definition should be active. ```c++ #define TINY_GSM_MODEM_SIM800 ``` -------------------------------- ### Connect TCP or SSL Client Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Connect the TinyGSM client to a specified server and port for TCP or SSL communication. ```c++ client.connect(server, port) ``` -------------------------------- ### Conditional TinyGsm Initialization with StreamDebugger Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md This snippet shows how to conditionally initialize TinyGsm with StreamDebugger if DUMP_AT_COMMANDS is defined, otherwise using a direct SerialAT connection. ```cpp #ifdef DUMP_AT_COMMANDS #include StreamDebugger debugger(SerialAT, SerialMon); TinyGsm modem(debugger); #else TinyGsm modem(SerialAT); #endif ``` -------------------------------- ### Dumping AT Commands with StreamDebugger Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Uncomment DUMP_AT_COMMANDS to use StreamDebugger for copying the entire AT command sequence to the main serial port. This helps in detailed diagnostics. ```cpp #define DUMP_AT_COMMANDS ``` -------------------------------- ### AT Command Debug Sketch Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md A utility sketch to test serial communication with your GSM module by sending AT commands. Hardware serial is recommended for this test. ```c++ #include #include "TinyGSM.h" // Set serial for AT commands (e.g. hardware serial) // Use SoftwareSerial for other boards #define SerialAT Serial // Your modem's serial port // SoftwareSerial mySerial(2, 3); // RX, TX void setup() { // Initialize serial for debug messages Serial.begin(115200); Serial.println("TinyGSM AT Debug"); // Initialize AT serial SerialAT.begin(115200); delay(3000); // Set modem serial port // mySerial.begin(9600); // gsm.setSerial(mySerial); } void loop() { // Send AT commands to the modem Serial.println("Sending AT command..."); SerialAT.println("AT\r"); // Read and print response from modem while (SerialAT.available()) { Serial.write(SerialAT.read()); } Serial.println("\n------------------------------\n"); delay(5000); } ``` -------------------------------- ### Setting Initial APN for GPRS Connection Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md If unable to register on the cellular network, try setting an initial APN using gprsConnect(APN). You may need to set it again after registration. ```cpp gprsConnect(APN) ``` -------------------------------- ### Establish GPRS/EPS Data Connection Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Establish a cellular data connection (GPRS or EPS) after successful network registration. For Digi XBees, this must be done before `waitForNetwork()`. ```c++ modem.gprsConnect(apn, gprsUser, gprsPass) modem.gprsConnect(apn) ``` -------------------------------- ### HTTP POST Request Formatting with Content Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Ensure a blank line separates headers from POST request content by adding an extra \r\n. This is a strict HTTP requirement. ```cpp client.print("....\r\n\r\n"); ``` -------------------------------- ### Enabling TinyGSM Library Debugging Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Define TINY_GSM_DEBUG before including the TinyGSM library to output debugging comments. This is useful for diagnosing library-related issues. ```cpp #define TINY_GSM_DEBUG SerialMon ``` -------------------------------- ### Unlock SIM Card Source: https://github.com/lewisxhe/tinygsm-fork/blob/master/README.md Unlock the SIM card if it is protected by a PIN. Replace `GSM_PIN` with your actual SIM PIN. ```c++ modem.simUnlock(GSM_PIN) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.