Skip to content

Commit dfcff9e

Browse files
committed
Add example on how to use multiple connections
1 parent 0ea3956 commit dfcff9e

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* This example shows how to use the connection management features of the ArduinoCellular library
3+
* to maintain multiple HTTP and HTTPS clients.
4+
* This is useful if e.g. you want to use ArduinoIoTCloud or other services that require
5+
* a dedicated client while making custom HTTP requests to other servers.
6+
*
7+
* Instructions:
8+
* 1. Insert a SIM card with or without PIN code in the Arduino Pro 4G Module.
9+
* 2. Provide sufficient power to the Arduino Pro 4G Module. Ideally, use a 5V power supply
10+
* with a current rating of at least 2A and connect it to the VIN and GND pins.
11+
* 3. Specify the APN, login, and password for your cellular network provider.
12+
* 4. Upload the sketch to the connected Arduino board.
13+
* 5. Open the serial monitor to view the output.
14+
*
15+
* Initial author: Sebastian Romero
16+
*/
17+
18+
#include <Arduino.h>
19+
#include "ArduinoCellular.h"
20+
#include <ArduinoHttpClient.h>
21+
22+
#define SECRET_PINNUMBER ""
23+
#define SECRET_GPRS_LOGIN ""
24+
#define SECRET_GPRS_APN "gprs.swisscom.ch"
25+
#define SECRET_GPRS_PASSWORD ""
26+
27+
constexpr int SSL_PORT = 443;
28+
29+
ArduinoCellular cellular = ArduinoCellular();
30+
31+
void getResource(HttpClient &client, const char *resource) {
32+
Serial.println("Making GET request...");
33+
client.get(resource);
34+
int statusCode = client.responseStatusCode();
35+
36+
if (statusCode < 0) {
37+
Serial.print("Error Response Code: ");
38+
Serial.println(statusCode);
39+
return;
40+
}
41+
Serial.print("Response: ");
42+
Serial.println(client.responseBody());
43+
}
44+
45+
46+
void setup(){
47+
Serial.begin(115200);
48+
while (!Serial);
49+
50+
cellular.begin();
51+
52+
if(String(SECRET_PINNUMBER).length() > 0 && !cellular.unlockSIM(SECRET_PINNUMBER)){
53+
Serial.println("Failed to unlock SIM card.");
54+
while(true); // Stop here
55+
}
56+
57+
Serial.println("Connecting...");
58+
if(!cellular.connect(SECRET_GPRS_APN, SECRET_GPRS_LOGIN, SECRET_GPRS_PASSWORD)){
59+
Serial.println("Failed to connect to the network.");
60+
while(true); // Stop here
61+
}
62+
Serial.println("Connected!");
63+
64+
HttpClient client1 = cellular.getHTTPSClient("vsh.pp.ua", SSL_PORT);
65+
HttpClient client2 = cellular.getHTTPSClient("wttr.in", SSL_PORT);
66+
Serial.print("Managed client count: ");
67+
Serial.println(cellular.getManagedClientCount());
68+
69+
getResource(client1, "/TinyGSM/logo.txt");
70+
getResource(client2, "/?format=3");
71+
client1.stop();
72+
client2.stop();
73+
cellular.cleanup(); // Clean up connections after use
74+
75+
Serial.print("Managed client count: ");
76+
Serial.println(cellular.getManagedClientCount());
77+
}
78+
79+
void loop(){}

0 commit comments

Comments
 (0)