You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/04.pro/carriers/portenta-max-carrier/tutorials/Cat M1 and MB IoT/assets/Connect-H7-to-Max-carrier.svg
Copy file name to clipboardExpand all lines: content/hardware/04.pro/carriers/portenta-max-carrier/tutorials/Cat M1 and MB IoT/assets/Sim-card-and-antenna-on-Max-carrier.svg
Copy file name to clipboardExpand all lines: content/hardware/04.pro/carriers/portenta-max-carrier/tutorials/catM1-and-NBIoT/content.md
+85-1Lines changed: 85 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ If you prefer to use one communication technology over the other, then this can
71
71
72
72
### Programming the Board
73
73
74
-
Now open the **NBWebClient** example, this is located inside the **MKRNB**. This sketch will connect the our setup to a website and print its content in the serial monitor.
74
+
Now open the **NBWebClient** example, this is located inside the **MKRNB**. The full sketch will also be included later in this tutorial. This sketch will connect the our setup to a website and print its content in the serial monitor.
75
75
76
76
This sketch uses a secret.h file to store sensitive information, like the PIN code for the SIM card. First we need to go to the **arduino_secrets.h** tab and enter our PIN code into the **Secret_pinnumber** variable.
77
77
@@ -85,6 +85,90 @@ When the sketch is uploaded, open the serial monitor to see the result. You will
85
85
86
86

87
87
88
+
### Full sketch
89
+
90
+
```arduino
91
+
92
+
// libraries
93
+
#include <MKRNB.h>
94
+
95
+
#include "arduino_secrets.h"
96
+
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
97
+
// PIN Number
98
+
const char PINNUMBER[] = SECRET_PINNUMBER;
99
+
100
+
// initialize the library instance
101
+
NBClient client;
102
+
GPRS gprs;
103
+
NB nbAccess;
104
+
105
+
// URL, path and port (for example: example.org)
106
+
char server[] = "example.org";
107
+
char path[] = "/";
108
+
int port = 80; // port 80 is the default for HTTP
109
+
110
+
void setup() {
111
+
// initialize serial communications and wait for port to open:
112
+
Serial.begin(9600);
113
+
while (!Serial) {
114
+
; // wait for serial port to connect. Needed for native USB port only
115
+
}
116
+
117
+
Serial.println("Starting Arduino web client.");
118
+
// connection state
119
+
boolean connected = false;
120
+
121
+
// After starting the modem with NB.begin()
122
+
// attach to the GPRS network with the APN, login and password
123
+
while (!connected) {
124
+
if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
125
+
(gprs.attachGPRS() == GPRS_READY)) {
126
+
connected = true;
127
+
} else {
128
+
Serial.println("Not connected");
129
+
delay(1000);
130
+
}
131
+
}
132
+
133
+
Serial.println("connecting...");
134
+
135
+
// if you get a connection, report back via serial:
136
+
if (client.connect(server, port)) {
137
+
Serial.println("connected");
138
+
// Make a HTTP request:
139
+
client.print("GET ");
140
+
client.print(path);
141
+
client.println(" HTTP/1.1");
142
+
client.print("Host: ");
143
+
client.println(server);
144
+
client.println("Connection: close");
145
+
client.println();
146
+
} else {
147
+
// if you didn't get a connection to the server:
148
+
Serial.println("connection failed");
149
+
}
150
+
}
151
+
152
+
void loop() {
153
+
// if there are incoming bytes available
154
+
// from the server, read them and print them:
155
+
if (client.available()) {
156
+
Serial.print((char)client.read());
157
+
}
158
+
159
+
// if the server's disconnected, stop the client:
160
+
if (!client.available() && !client.connected()) {
161
+
Serial.println();
162
+
Serial.println("disconnecting.");
163
+
client.stop();
164
+
165
+
// do nothing forevermore:
166
+
for (;;)
167
+
;
168
+
}
169
+
}
170
+
```
171
+
88
172
### Troubleshoot
89
173
90
174
If the code is not working, there are some common issues we can troubleshoot:
0 commit comments