GIGA R1 WiFi Network Examples

Discover examples compatible with the WiFi library included in the GIGA Board Package.

The GIGA R1 WiFi has a Murata LBEE5KL1DX-883 radio module that allows you to connect to local Wi-Fi networks, and perform network operations. Protocols including HTTPS, MQTT, UDP are tested and supported, and in this article, you will find a number of examples that will get you started.

Wi-Fi support is enabled via the built-in

WiFi
library that is shipped with the Arduino Mbed OS GIGA Board Package. Installing the Board Package automatically installs the
WiFi
library.

The radio module also supports Bluetooth® Low Energy, which is supported via the ArduinoBLE library.

The easiest way to connect your board to the Internet is via the Arduino Cloud platform. Here you can configure, program, monitor and synchronize your devices without having to write any networking code.

Hardware & Software Needed

*The GIGA R1 WiFi has no built-in antenna, so connectivity will be very poor unless you connect an antenna.

Examples

Examples listed in this section have been tested and verified to work. Most examples requires you to input the

SSID
and
PASSWORD
for your local Wi-Fi network. As a standard practice, in all of our examples, we store this in a separate header file (called
arduino_secrets.h
).

You will need to create this file, or remove the

#include "arduino_secrets.h"
file at the top of each example. The file should contain:

1//arduino_secrets.h header file
2#define SECRET_SSID "yournetwork"
3#define SECRET_PASS "yourpassword"

Storing network & password in a separate file minimizes the risk of you accidentally sharing your Wi-Fi credentials.

WPA Connection

1/*
2 This example connects to an unencrypted WiFi network.
3 Then it prints the MAC address of the WiFi module,
4 the IP address obtained, and other network details.
5
6 Circuit:
7 * GIGA R1 WiFi
8
9 created 13 July 2010
10 by dlf (Metodo2 srl)
11 modified 31 May 2012
12 by Tom Igoe
13 modified 22 March 2023
14 by Karl Söderby
15 */
16
17
18#include <SPI.h>
19#include <WiFi.h>
20
21#include "arduino_secrets.h"
22///////please enter your sensitive data in the Secret tab/arduino_secrets.h
23char ssid[] = SECRET_SSID; // your network SSID (name)
24char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
25int status = WL_IDLE_STATUS; // the WiFi radio's status
26
27void setup() {
28 //Initialize serial and wait for port to open:
29 Serial.begin(9600);
30 while (!Serial) {
31 ; // wait for serial port to connect. Needed for native USB port only
32 }
33
34 // check for the WiFi module:
35 if (WiFi.status() == WL_NO_MODULE) {
36 Serial.println("Communication with WiFi module failed!");
37 // don't continue
38 while (true);
39 }
40
41 // attempt to connect to WiFi network:
42 while (status != WL_CONNECTED) {
43 Serial.print("Attempting to connect to WPA SSID: ");
44 Serial.println(ssid);
45 // Connect to WPA/WPA2 network:
46 status = WiFi.begin(ssid, pass);
47
48 // wait 10 seconds for connection:
49 delay(10000);
50 }
51
52 // you're connected now, so print out the data:
53 Serial.print("You're connected to the network");
54 printCurrentNet();
55 printWifiData();
56
57}
58
59void loop() {
60 // check the network connection once every 10 seconds:
61 delay(10000);
62 printCurrentNet();
63}
64
65void printWifiData() {
66 // print your board's IP address:
67 IPAddress ip = WiFi.localIP();
68 Serial.print("IP Address: ");
69 Serial.println(ip);
70 Serial.println(ip);
71
72 // print your MAC address:
73 byte mac[6];
74 WiFi.macAddress(mac);
75 Serial.print("MAC address: ");
76 printMacAddress(mac);
77}
78
79void printCurrentNet() {
80 // print the SSID of the network you're attached to:
81 Serial.print("SSID: ");
82 Serial.println(WiFi.SSID());
83
84 // print the MAC address of the router you're attached to:
85 byte bssid[6];
86 WiFi.BSSID(bssid);
87 Serial.print("BSSID: ");
88 printMacAddress(bssid);
89
90 // print the received signal strength:
91 long rssi = WiFi.RSSI();
92 Serial.print("signal strength (RSSI):");
93 Serial.println(rssi);
94
95 // print the encryption type:
96 byte encryption = WiFi.encryptionType();
97 Serial.print("Encryption Type:");
98 Serial.println(encryption, HEX);
99 Serial.println();
100}
101
102void printMacAddress(byte mac[]) {
103 for (int i = 5; i >= 0; i--) {
104 if (mac[i] < 16) {
105 Serial.print("0");
106 }
107 Serial.print(mac[i], HEX);
108 if (i > 0) {
109 Serial.print(":");
110 }
111 }
112 Serial.println();
113}

RTC / UDP / NTP Example

1/*
2 Udp NTP Client
3
4 Get the time from a Network Time Protocol (NTP) time server
5 Demonstrates use of UDP sendPacket and ReceivePacket
6 For more on NTP time servers and the messages needed to communicate with them,
7 see http://en.wikipedia.org/wiki/Network_Time_Protocol
8
9 created 4 Sep 2010
10 by Michael Margolis
11 modified 9 Apr 2012
12 by Tom Igoe
13 modified 28 Dec 2022
14 by Giampaolo Mancini
15
16This code is in the public domain.
17 */
18
19#include <WiFi.h>
20#include <WiFiUdp.h>
21#include <mbed_mktime.h>
22
23int status = WL_IDLE_STATUS;
24#include "arduino_secrets.h"
25///////please enter your sensitive data in the Secret tab/arduino_secrets.h
26char ssid[] = ""; // your network SSID (name)
27char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
28int keyIndex = 0; // your network key index number (needed only for WEP)
29
30unsigned int localPort = 2390; // local port to listen for UDP packets
31
32// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
33
34constexpr auto timeServer { "pool.ntp.org" };
35
36const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
37
38byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
39
40// A UDP instance to let us send and receive packets over UDP
41WiFiUDP Udp;
42
43constexpr unsigned long printInterval { 1000 };
44unsigned long printNow {};
45
46void setup()
47{
48 // Open serial communications and wait for port to open:
49 Serial.begin(9600);
50 while (!Serial) {
51 ; // wait for serial port to connect. Needed for native USB port only
52 }
53
54 // check for the WiFi module:
55 if (WiFi.status() == WL_NO_SHIELD) {
56 Serial.println("Communication with WiFi module failed!");
57 // don't continue
58 while (true)
59 ;
60 }
61
62 // attempt to connect to WiFi network:
63 while (status != WL_CONNECTED) {
64 Serial.print("Attempting to connect to SSID: ");
65 Serial.println(ssid);
66 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
67 status = WiFi.begin(ssid, pass);
68
69 // wait 10 seconds for connection:
70 delay(10000);
71 }
72
73 Serial.println("Connected to WiFi");
74 printWifiStatus();
75
76 setNtpTime();
77
78}
79
80void loop()
81{
82 if (millis() > printNow) {
83 Serial.print("System Clock: ");
84 Serial.println(getLocaltime());
85 printNow = millis() + printInterval;
86 }
87}
88
89void setNtpTime()
90{
91 Udp.begin(localPort);
92 sendNTPpacket(timeServer);
93 delay(1000);
94 parseNtpPacket();
95}
96
97// send an NTP request to the time server at the given address
98unsigned long sendNTPpacket(const char * address)
99{
100 memset(packetBuffer, 0, NTP_PACKET_SIZE);
101 packetBuffer[0] = 0b11100011; // LI, Version, Mode
102 packetBuffer[1] = 0; // Stratum, or type of clock
103 packetBuffer[2] = 6; // Polling Interval
104 packetBuffer[3] = 0xEC; // Peer Clock Precision
105 // 8 bytes of zero for Root Delay & Root Dispersion
106 packetBuffer[12] = 49;
107 packetBuffer[13] = 0x4E;
108 packetBuffer[14] = 49;
109 packetBuffer[15] = 52;
110
111 Udp.beginPacket(address, 123); // NTP requests are to port 123
112 Udp.write(packetBuffer, NTP_PACKET_SIZE);
113 Udp.endPacket();
114}
115
116unsigned long parseNtpPacket()
117{
118 if (!Udp.parsePacket())
119 return 0;
120
121 Udp.read(packetBuffer, NTP_PACKET_SIZE);
122 const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
123 const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
124 const unsigned long secsSince1900 = highWord << 16 | lowWord;
125 constexpr unsigned long seventyYears = 2208988800UL;
126 const unsigned long epoch = secsSince1900 - seventyYears;
127 set_time(epoch);
128
129#if defined(VERBOSE)
130 Serial.print("Seconds since Jan 1 1900 = ");
131 Serial.println(secsSince1900);
132
133 // now convert NTP time into everyday time:
134 Serial.print("Unix time = ");
135 // print Unix time:
136 Serial.println(epoch);
137
138 // print the hour, minute and second:
139 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
140 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
141 Serial.print(':');
142 if (((epoch % 3600) / 60) < 10) {
143 // In the first 10 minutes of each hour, we'll want a leading '0'
144 Serial.print('0');
145 }
146 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
147 Serial.print(':');
148 if ((epoch % 60) < 10) {
149 // In the first 10 seconds of each minute, we'll want a leading '0'
150 Serial.print('0');
151 }
152 Serial.println(epoch % 60); // print the second
153#endif
154
155 return epoch;
156}
157
158String getLocaltime()
159{
160 char buffer[32];
161 tm t;
162 _rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
163 strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
164 return String(buffer);
165}
166
167void printWifiStatus()
168{
169 // print the SSID of the network you're attached to:
170 Serial.print("SSID: ");
171 Serial.println(WiFi.SSID());
172
173 // print your board's IP address:
174 IPAddress ip = WiFi.localIP();
175 Serial.print("IP Address: ");
176 Serial.println(ip);
177
178 // print the received signal strength:
179 long rssi = WiFi.RSSI();
180 Serial.print("signal strength (RSSI):");
181 Serial.print(rssi);
182 Serial.println(" dBm");
183}

RTC / UDP / NTP Example (Timezone)

This example provides an option to set the timezone. As the received epoch is based on GMT time, you can input e.g.

-1
or
5
which represents the hours. The
timezone
variable is changed at the top of the example.

1/*
2 Udp NTP Client with Timezone Adjustment
3
4 Get the time from a Network Time Protocol (NTP) time server
5 Demonstrates use of UDP sendPacket and ReceivePacket
6 For more on NTP time servers and the messages needed to communicate with them,
7 see http://en.wikipedia.org/wiki/Network_Time_Protocol
8
9 created 4 Sep 2010
10 by Michael Margolis
11 modified 9 Apr 2012
12 by Tom Igoe
13 modified 28 Dec 2022
14 by Giampaolo Mancini
15 modified 29 Jan 2024
16 by Karl Söderby
17
18This code is in the public domain.
19 */
20
21#include <WiFi.h>
22#include <WiFiUdp.h>
23#include <mbed_mktime.h>
24
25int timezone = -1; //this is GMT -1.
26
27int status = WL_IDLE_STATUS;
28
29char ssid[] = "Flen"; // your network SSID (name)
30char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
31
32int keyIndex = 0; // your network key index number (needed only for WEP)
33
34unsigned int localPort = 2390; // local port to listen for UDP packets
35
36// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
37
38constexpr auto timeServer{ "pool.ntp.org" };
39
40const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
41
42byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
43
44// A UDP instance to let us send and receive packets over UDP
45WiFiUDP Udp;
46
47constexpr unsigned long printInterval{ 1000 };
48unsigned long printNow{};
49
50void setup() {
51 // Open serial communications and wait for port to open:
52 Serial.begin(9600);
53 while (!Serial) {
54 ; // wait for serial port to connect. Needed for native USB port only
55 }
56
57 // check for the WiFi module:
58 if (WiFi.status() == WL_NO_SHIELD) {
59 Serial.println("Communication with WiFi module failed!");
60 // don't continue
61 while (true)
62 ;
63 }
64
65 // attempt to connect to WiFi network:
66 while (status != WL_CONNECTED) {
67 Serial.print("Attempting to connect to SSID: ");
68 Serial.println(ssid);
69 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
70 status = WiFi.begin(ssid, pass);
71
72 // wait 10 seconds for connection:
73 delay(10000);
74 }
75
76 Serial.println("Connected to WiFi");
77 printWifiStatus();
78
79 setNtpTime();
80}
81
82void loop() {
83 if (millis() > printNow) {
84 Serial.print("System Clock: ");
85 Serial.println(getLocaltime());
86 printNow = millis() + printInterval;
87 }
88}
89
90void setNtpTime() {
91 Udp.begin(localPort);
92 sendNTPpacket(timeServer);
93 delay(1000);
94 parseNtpPacket();
95}
96
97// send an NTP request to the time server at the given address
98unsigned long sendNTPpacket(const char* address) {
99 memset(packetBuffer, 0, NTP_PACKET_SIZE);
100 packetBuffer[0] = 0b11100011; // LI, Version, Mode
101 packetBuffer[1] = 0; // Stratum, or type of clock
102 packetBuffer[2] = 6; // Polling Interval
103 packetBuffer[3] = 0xEC; // Peer Clock Precision
104 // 8 bytes of zero for Root Delay & Root Dispersion
105 packetBuffer[12] = 49;
106 packetBuffer[13] = 0x4E;
107 packetBuffer[14] = 49;
108 packetBuffer[15] = 52;
109
110 Udp.beginPacket(address, 123); // NTP requests are to port 123
111 Udp.write(packetBuffer, NTP_PACKET_SIZE);
112 Udp.endPacket();
113}
114
115unsigned long parseNtpPacket() {
116 if (!Udp.parsePacket())
117 return 0;
118
119 Udp.read(packetBuffer, NTP_PACKET_SIZE);
120 const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
121 const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
122 const unsigned long secsSince1900 = highWord << 16 | lowWord;
123 constexpr unsigned long seventyYears = 2208988800UL;
124 const unsigned long epoch = secsSince1900 - seventyYears;
125
126 const unsigned long new_epoch = epoch + (3600 * timezone); //multiply the timezone with 3600 (1 hour)
127
128 set_time(new_epoch);
129
130#if defined(VERBOSE)
131 Serial.print("Seconds since Jan 1 1900 = ");
132 Serial.println(secsSince1900);
133
134 // now convert NTP time into everyday time:
135 Serial.print("Unix time = ");
136 // print Unix time:
137 Serial.println(epoch);
138
139 // print the hour, minute and second:
140 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
141 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
142 Serial.print(':');
143 if (((epoch % 3600) / 60) < 10) {
144 // In the first 10 minutes of each hour, we'll want a leading '0'
145 Serial.print('0');
146 }
147 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
148 Serial.print(':');
149 if ((epoch % 60) < 10) {
150 // In the first 10 seconds of each minute, we'll want a leading '0'
151 Serial.print('0');
152 }
153 Serial.println(epoch % 60); // print the second
154#endif
155
156 return epoch;
157}
158
159String getLocaltime() {
160 char buffer[32];
161 tm t;
162 _rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
163 strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
164 return String(buffer);
165}
166
167void printWifiStatus() {
168 // print the SSID of the network you're attached to:
169 Serial.print("SSID: ");
170 Serial.println(WiFi.SSID());
171
172 // print your board's IP address:
173 IPAddress ip = WiFi.localIP();
174 Serial.print("IP Address: ");
175 Serial.println(ip);
176
177 // print the received signal strength:
178 long rssi = WiFi.RSSI();
179 Serial.print("signal strength (RSSI):");
180 Serial.print(rssi);
181 Serial.println(" dBm");
182}

Scan Networks

1/*
2 This example prints the board's MAC address, and
3 scans for available WiFi networks using the GIGA R1 WiFi board.
4 Every ten seconds, it scans again. It doesn't actually
5 connect to any network, so no encryption scheme is specified.
6
7 Circuit:
8 * GIGA R1 WiFi
9
10 created 13 July 2010
11 by dlf (Metodo2 srl)
12 modified 21 June 2012
13 by Tom Igoe and Jaymes Dec
14 modified 3 March 2023
15 by Karl Söderby
16 */
17
18#include <SPI.h>
19#include <WiFi.h>
20
21void setup() {
22 //Initialize serial and wait for port to open:
23 Serial.begin(9600);
24 while (!Serial) {
25 ; // wait for serial port to connect. Needed for native USB port only
26 }
27
28 // check for the WiFi module:
29 if (WiFi.status() == WL_NO_MODULE) {
30 Serial.println("Communication with WiFi module failed!");
31 // don't continue
32 while (true);
33 }
34
35 // print your MAC address:
36 byte mac[6];
37 WiFi.macAddress(mac);
38 Serial.print("MAC: ");
39 printMacAddress(mac);
40}
41
42void loop() {
43 // scan for existing networks:
44 Serial.println("Scanning available networks...");
45 listNetworks();
46 delay(10000);
47}
48
49void listNetworks() {
50 // scan for nearby networks:
51 Serial.println("** Scan Networks **");
52 int numSsid = WiFi.scanNetworks();
53 if (numSsid == -1) {
54 Serial.println("Couldn't get a WiFi connection");
55 while (true);
56 }
57
58 // print the list of networks seen:
59 Serial.print("number of available networks:");
60 Serial.println(numSsid);
61
62 // print the network number and name for each network found:
63 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
64 Serial.print(thisNet);
65 Serial.print(") ");
66 Serial.print(WiFi.SSID(thisNet));
67 Serial.print("\tSignal: ");
68 Serial.print(WiFi.RSSI(thisNet));
69 Serial.print(" dBm");
70 Serial.print("\tEncryption: ");
71 printEncryptionType(WiFi.encryptionType(thisNet));
72 }
73}
74
75void printEncryptionType(int thisType) {
76 // read the encryption type and print out the name:
77 switch (thisType) {
78 case ENC_TYPE_WEP:
79 Serial.println("WEP");
80 break;
81 case ENC_TYPE_TKIP:
82 Serial.println("WPA");
83 break;
84 case ENC_TYPE_CCMP:
85 Serial.println("WPA2");
86 break;
87 case ENC_TYPE_NONE:
88 Serial.println("None");
89 break;
90 case ENC_TYPE_AUTO:
91 Serial.println("Auto");
92 break;
93 case ENC_TYPE_UNKNOWN:
94 default:
95 Serial.println("Unknown");
96 break;
97 }
98}
99
100
101void printMacAddress(byte mac[]) {
102 for (int i = 5; i >= 0; i--) {
103 if (mac[i] < 16) {
104 Serial.print("0");
105 }
106 Serial.print(mac[i], HEX);
107 if (i > 0) {
108 Serial.print(":");
109 }
110 }
111 Serial.println();
112}

Wi-Fi Chat Server

1/*
2 Chat Server
3
4 A simple server that distributes any incoming messages to all
5 connected clients. To use, telnet to your device's IP address and type.
6 You can see the client's input in the serial monitor as well.
7
8 This example is written for a network using WPA encryption. For
9 WEP or WPA, change the WiFi.begin() call accordingly.
10
11
12 Circuit:
13 * GIGA R1 WiFi
14
15 created 18 Dec 2009
16 by David A. Mellis
17 modified 31 May 2012
18 by Tom Igoe
19 modified 23 March 2023
20 by Karl Söderby
21 */
22
23#include <SPI.h>
24#include <WiFi.h>
25
26#include "arduino_secrets.h"
27///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28char ssid[] = SECRET_SSID; // your network SSID (name)
29char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30
31int keyIndex = 0; // your network key index number (needed only for WEP)
32
33int status = WL_IDLE_STATUS;
34
35WiFiServer server(23);
36
37boolean alreadyConnected = false; // whether or not the client was connected previously
38
39void setup() {
40 //Initialize serial and wait for port to open:
41 Serial.begin(9600);
42 while (!Serial) {
43 ; // wait for serial port to connect. Needed for native USB port only
44 }
45
46 // check for the WiFi module:
47 if (WiFi.status() == WL_NO_MODULE) {
48 Serial.println("Communication with WiFi module failed!");
49 // don't continue
50 while (true);
51 }
52
53
54 // attempt to connect to WiFi network:
55 while (status != WL_CONNECTED) {
56 Serial.print("Attempting to connect to SSID: ");
57 Serial.println(ssid);
58 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
59 status = WiFi.begin(ssid, pass);
60
61 // wait 10 seconds for connection:
62 delay(10000);
63 }
64
65 // start the server:
66 server.begin();
67 // you're connected now, so print out the status:
68 printWifiStatus();
69}
70
71
72void loop() {
73 // wait for a new client:
74 WiFiClient client = server.available();
75
76
77 // when the client sends the first byte, say hello:
78 if (client) {
79 if (!alreadyConnected) {
80 // clear out the input buffer:
81 client.flush();
82 Serial.println("We have a new client");
83 client.println("Hello, client!");
84 alreadyConnected = true;
85 }
86
87 if (client.available() > 0) {
88 // read the bytes incoming from the client:
89 char thisChar = client.read();
90 // echo the bytes back to the client:
91 server.write(thisChar);
92 // echo the bytes to the server as well:
93 Serial.write(thisChar);
94 }
95 }
96}
97
98
99void printWifiStatus() {
100 // print the SSID of the network you're attached to:
101 Serial.print("SSID: ");
102 Serial.println(WiFi.SSID());
103
104 // print your board's IP address:
105 IPAddress ip = WiFi.localIP();
106 Serial.print("IP Address: ");
107 Serial.println(ip);
108
109 // print the received signal strength:
110 long rssi = WiFi.RSSI();
111 Serial.print("signal strength (RSSI):");
112 Serial.print(rssi);
113 Serial.println(" dBm");
114}

Web Client

1/*
2 Web client
3
4 This sketch connects to a website (http://www.google.com)
5 using the WiFi module.
6
7 This example is written for a network using WPA encryption. For
8 WEP or WPA, change the WiFi.begin() call accordingly.
9
10 This example is written for a network using WPA encryption. For
11 WEP or WPA, change the WiFi.begin() call accordingly.
12
13 Circuit:
14 * GIGA R1 WiFi
15
16 created 13 July 2010
17 by dlf (Metodo2 srl)
18 modified 31 May 2012
19 by Tom Igoe
20 modified 21 March 2023
21 by Karl Söderby
22
23 */
24
25
26#include <SPI.h>
27#include <WiFi.h>
28
29#include "arduino_secrets.h"
30///////please enter your sensitive data in the Secret tab/arduino_secrets.h
31char ssid[] = SECRET_SSID; // your network SSID (name)
32char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
33int keyIndex = 0; // your network key index number (needed only for WEP)
34
35int status = WL_IDLE_STATUS;
36// if you don't want to use DNS (and reduce your sketch size)
37// use the numeric IP instead of the name for the server:
38//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
39char server[] = "www.google.com"; // name address for Google (using DNS)
40
41// Initialize the Ethernet client library
42// with the IP address and port of the server
43// that you want to connect to (port 80 is default for HTTP):
44WiFiClient client;
45
46void setup() {
47 //Initialize serial and wait for port to open:
48 Serial.begin(9600);
49 while (!Serial) {
50 ; // wait for serial port to connect. Needed for native USB port only
51 }
52
53 // check for the WiFi module:
54 if (WiFi.status() == WL_NO_MODULE) {
55 Serial.println("Communication with WiFi module failed!");
56 // don't continue
57 while (true);
58 }
59
60 // attempt to connect to WiFi network:
61 while (status != WL_CONNECTED) {
62 Serial.print("Attempting to connect to SSID: ");
63 Serial.println(ssid);
64 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
65 status = WiFi.begin(ssid, pass);
66
67 // wait 10 seconds for connection:
68 delay(10000);
69 }
70 Serial.println("Connected to WiFi");
71 printWifiStatus();
72
73 Serial.println("\nStarting connection to server...");
74 // if you get a connection, report back via serial:
75 if (client.connect(server, 80)) {
76 Serial.println("connected to server");
77 // Make a HTTP request:
78 client.println("GET /search?q=arduino HTTP/1.1");
79 client.println("Host: www.google.com");
80 client.println("Connection: close");
81 client.println();
82 }
83}
84
85void loop() {
86 // if there are incoming bytes available
87 // from the server, read them and print them:
88 while (client.available()) {
89 char c = client.read();
90 Serial.write(c);
91 }
92
93 // if the server's disconnected, stop the client:
94 if (!client.connected()) {
95 Serial.println();
96 Serial.println("disconnecting from server.");
97 client.stop();
98
99 // do nothing forevermore:
100 while (true);
101 }
102}
103
104
105void printWifiStatus() {
106 // print the SSID of the network you're attached to:
107 Serial.print("SSID: ");
108 Serial.println(WiFi.SSID());
109
110 // print your board's IP address:
111 IPAddress ip = WiFi.localIP();
112 Serial.print("IP Address: ");
113 Serial.println(ip);
114
115 // print the received signal strength:
116 long rssi = WiFi.RSSI();
117 Serial.print("signal strength (RSSI):");
118 Serial.print(rssi);
119 Serial.println(" dBm");
120}

Wi-Fi SSL Client

1/*
2This example creates a client object that connects and transfers
3data using always SSL.
4
5It is compatible with the methods normally related to plain
6connections, like client.connect(host, port).
7
8Circuit
9* GIGA R1 WiFi
10
11Written by Arturo Guadalupi
12in November 2015
13
14modified 22 March 2023
15by Karl Söderby
16*/
17
18#include <SPI.h>
19#include <WiFi.h>
20#include <WiFiSSLClient.h>
21
22#include "arduino_secrets.h"
23///////please enter your sensitive data in the Secret tab/arduino_secrets.h
24char ssid[] = SECRET_SSID; // your network SSID (name)
25char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
26int keyIndex = 0; // your network key index number (needed only for WEP)
27
28int status = WL_IDLE_STATUS;
29// if you don't want to use DNS (and reduce your sketch size)
30// use the numeric IP instead of the name for the server:
31//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
32char server[] = "www.google.com"; // name address for Google (using DNS)
33
34// Initialize the Ethernet client library
35// with the IP address and port of the server
36// that you want to connect to (port 80 is default for HTTP):
37WiFiSSLClient client;
38
39void setup() {
40 //Initialize serial and wait for port to open:
41 Serial.begin(9600);
42 while (!Serial) {
43 ; // wait for serial port to connect. Needed for native USB port only
44 }
45
46 // check for the WiFi module:
47 if (WiFi.status() == WL_NO_MODULE) {
48 Serial.println("Communication with WiFi module failed!");
49 // don't continue
50 while (true);
51 }
52
53 // attempt to connect to WiFi network:
54 while (status != WL_CONNECTED) {
55 Serial.print("Attempting to connect to SSID: ");
56 Serial.println(ssid);
57 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
58 status = WiFi.begin(ssid, pass);
59
60 // wait 10 seconds for connection:
61 delay(10000);
62 }
63 Serial.println("Connected to WiFi");
64 printWiFiStatus();
65
66 Serial.println("\nStarting connection to server...");
67 // if you get a connection, report back via serial:
68 if (client.connect(server, 443)) {
69 Serial.println("connected to server");
70 // Make a HTTP request:
71 client.println("GET /search?q=arduino HTTP/1.1");
72 client.println("Host: www.google.com");
73 client.println("Connection: close");
74 client.println();
75 }
76}
77
78void loop() {
79 // if there are incoming bytes available
80 // from the server, read them and print them:
81 while (client.available()) {
82 char c = client.read();
83 Serial.write(c);
84 }
85
86 // if the server's disconnected, stop the client:
87 if (!client.connected()) {
88 Serial.println();
89 Serial.println("disconnecting from server.");
90 client.stop();
91
92 // do nothing forevermore:
93 while (true);
94 }
95}
96
97
98void printWiFiStatus() {
99 // print the SSID of the network you're attached to:
100 Serial.print("SSID: ");
101 Serial.println(WiFi.SSID());
102
103 // print your board's IP address:
104 IPAddress ip = WiFi.localIP();
105 Serial.print("IP Address: ");
106 Serial.println(ip);
107
108 // print the received signal strength:
109 long rssi = WiFi.RSSI();
110 Serial.print("signal strength (RSSI):");
111 Serial.print(rssi);
112 Serial.println(" dBm");
113}

UDP Send / Receive

1/*
2WiFi UDP Send and Receive String
3
4 This sketch waits for a UDP packet on localPort using the WiFi module.
5 When a packet is received an Acknowledge packet is sent to the client on port remotePort
6
7 Circuit:
8 * GIGA R1 WiFi
9
10 created 30 December 2012
11 by dlf (Metodo2 srl)
12
13 modified 3 March 2023
14 by Karl Söderby
15 */
16
17
18#include <SPI.h>
19#include <WiFi.h>
20#include <WiFiUdp.h>
21
22int status = WL_IDLE_STATUS;
23#include "arduino_secrets.h"
24///////please enter your sensitive data in the Secret tab/arduino_secrets.h
25char ssid[] = SECRET_SSID; // your network SSID (name)
26char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
27int keyIndex = 0; // your network key index number (needed only for WEP)
28
29unsigned int localPort = 2390; // local port to listen on
30
31char packetBuffer[256]; //buffer to hold incoming packet
32char ReplyBuffer[] = "acknowledged"; // a string to send back
33
34WiFiUDP Udp;
35
36void setup() {
37 //Initialize serial and wait for port to open:
38 Serial.begin(9600);
39 while (!Serial) {
40 ; // wait for serial port to connect. Needed for native USB port only
41 }
42
43 // check for the WiFi module:
44 if (WiFi.status() == WL_NO_MODULE) {
45 Serial.println("Communication with WiFi module failed!");
46 // don't continue
47 while (true);
48 }
49
50 // attempt to connect to WiFi network:
51 while (status != WL_CONNECTED) {
52 Serial.print("Attempting to connect to SSID: ");
53 Serial.println(ssid);
54 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
55 status = WiFi.begin(ssid, pass);
56
57 // wait 10 seconds for connection:
58 delay(10000);
59 }
60 Serial.println("Connected to WiFi");
61 printWifiStatus();
62
63 Serial.println("\nStarting connection to server...");
64 // if you get a connection, report back via serial:
65 Udp.begin(localPort);
66}
67
68void loop() {
69
70 // if there's data available, read a packet
71 int packetSize = Udp.parsePacket();
72 if (packetSize) {
73 Serial.print("Received packet of size ");
74 Serial.println(packetSize);
75 Serial.print("From ");
76 IPAddress remoteIp = Udp.remoteIP();
77 Serial.print(remoteIp);
78 Serial.print(", port ");
79 Serial.println(Udp.remotePort());
80
81 // read the packet into packetBufffer
82 int len = Udp.read(packetBuffer, 255);
83 if (len > 0) {
84 packetBuffer[len] = 0;
85 }
86 Serial.println("Contents:");
87 Serial.println(packetBuffer);
88
89 // send a reply, to the IP address and port that sent us the packet we received
90 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
91 Udp.write(ReplyBuffer);
92 Udp.endPacket();
93 }
94}
95
96
97void printWifiStatus() {
98 // print the SSID of the network you're attached to:
99 Serial.print("SSID: ");
100 Serial.println(WiFi.SSID());
101
102 // print your board's IP address:
103 IPAddress ip = WiFi.localIP();
104 Serial.print("IP Address: ");
105 Serial.println(ip);
106
107 // print the received signal strength:
108 long rssi = WiFi.RSSI();
109 Serial.print("signal strength (RSSI):");
110 Serial.print(rssi);
111 Serial.println(" dBm");
112}

Web Server

1/*
2 WiFi Web Server
3
4 A simple web server that shows the value of the analog input pins.
5
6 This example is written for a network using WPA encryption. For
7 WEP or WPA, change the WiFi.begin() call accordingly.
8
9 Circuit:
10 * GIGA R1 WiFi
11 * (optional) analog inputs connected on A0-A5
12
13 created 13 July 2010
14 by dlf (Metodo2 srl)
15 modified 31 May 2012
16 by Tom Igoe
17 modified 3 March 2023
18 by Karl Söderby
19 */
20
21#include <SPI.h>
22#include <WiFi.h>
23
24
25#include "arduino_secrets.h"
26///////please enter your sensitive data in the Secret tab/arduino_secrets.h
27char ssid[] = SECRET_SSID; // your network SSID (name)
28char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
29int keyIndex = 0; // your network key index number (needed only for WEP)
30
31int status = WL_IDLE_STATUS;
32
33WiFiServer server(80);
34
35void setup() {
36 //Initialize serial and wait for port to open:
37 Serial.begin(9600);
38 while (!Serial) {
39 ; // wait for serial port to connect. Needed for native USB port only
40 }
41
42 // check for the WiFi module:
43 if (WiFi.status() == WL_NO_MODULE) {
44 Serial.println("Communication with WiFi module failed!");
45 // don't continue
46 while (true);
47 }
48
49 // attempt to connect to WiFi network:
50 while (status != WL_CONNECTED) {
51 Serial.print("Attempting to connect to SSID: ");
52 Serial.println(ssid);
53 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
54 status = WiFi.begin(ssid, pass);
55
56 // wait 10 seconds for connection:
57 delay(10000);
58 }
59 server.begin();
60 // you're connected now, so print out the status:
61 printWifiStatus();
62}
63
64
65void loop() {
66 // listen for incoming clients
67 WiFiClient client = server.available();
68 if (client) {
69 Serial.println("new client");
70 // an HTTP request ends with a blank line
71 boolean currentLineIsBlank = true;
72 while (client.connected()) {
73 if (client.available()) {
74 char c = client.read();
75 Serial.write(c);
76 // if you've gotten to the end of the line (received a newline
77 // character) and the line is blank, the HTTP request has ended,
78 // so you can send a reply
79 if (c == '\n' && currentLineIsBlank) {
80 // send a standard HTTP response header
81 client.println("HTTP/1.1 200 OK");
82 client.println("Content-Type: text/html");
83 client.println("Connection: close"); // the connection will be closed after completion of the response
84 client.println("Refresh: 5"); // refresh the page automatically every 5 sec
85 client.println();
86 client.println("<!DOCTYPE HTML>");
87 client.println("<html>");
88 // output the value of each analog input pin
89 for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
90 int sensorReading = analogRead(analogChannel);
91 client.print("analog input ");
92 client.print(analogChannel);
93 client.print(" is ");
94 client.print(sensorReading);
95 client.println("<br />");
96 }
97 client.println("</html>");
98 break;
99 }
100 if (c == '\n') {
101 // you're starting a new line
102 currentLineIsBlank = true;
103 } else if (c != '\r') {
104 // you've gotten a character on the current line
105 currentLineIsBlank = false;
106 }
107 }
108 }
109 // give the web browser time to receive the data
110 delay(1);
111
112 // close the connection:
113 client.stop();
114 Serial.println("client disconnected");
115 }
116}
117
118
119void printWifiStatus() {
120 // print the SSID of the network you're attached to:
121 Serial.print("SSID: ");
122 Serial.println(WiFi.SSID());
123
124 // print your board's IP address:
125 IPAddress ip = WiFi.localIP();
126 Serial.print("IP Address: ");
127 Serial.println(ip);
128
129 // print the received signal strength:
130 long rssi = WiFi.RSSI();
131 Serial.print("signal strength (RSSI):");
132 Serial.print(rssi);
133 Serial.println(" dBm");
134}

Web Server AP Mode

1/*
2 WiFi Web Server LED Blink
3
4 A simple web server that lets you blink an LED via the web.
5 This sketch will create a new access point (with no password).
6 It will then launch a new server and print out the IP address
7 to the Serial Monitor. From there, you can open that address in a web browser
8 to turn on and off the LED on pin 13.
9
10 If the IP address of your board is yourAddress:
11 http://yourAddress/H turns the LED on
12 http://yourAddress/L turns it off
13
14 created 25 Nov 2012
15 by Tom Igoe
16 adapted to WiFi AP by Adafruit
17
18 modified 22 March 2023
19 by Karl Söderby
20 */
21
22#include <SPI.h>
23#include <WiFi.h>
24#include "arduino_secrets.h"
25///////please enter your sensitive data in the Secret tab/arduino_secrets.h
26char ssid[] = SECRET_SSID; // your network SSID (name)
27char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
28int keyIndex = 0; // your network key index number (needed only for WEP)
29
30int status = WL_IDLE_STATUS;
31WiFiServer server(80);
32
33void setup() {
34 //Initialize serial and wait for port to open:
35 Serial.begin(9600);
36 while (!Serial) {
37 ; // wait for serial port to connect. Needed for native USB port only
38 }
39
40 Serial.println("Access Point Web Server");
41
42 pinMode(LED_RED, OUTPUT);
43 pinMode(LED_GREEN, OUTPUT);
44 pinMode(LED_BLUE, OUTPUT);
45
46 // check for the WiFi module:
47 if (WiFi.status() == WL_NO_MODULE) {
48 Serial.println("Communication with WiFi module failed!");
49 // don't continue
50 while (true)
51 ;
52 }
53
54 // by default the local IP address will be 192.168.4.1
55 // you can override it with the following:
56 // WiFi.config(IPAddress(10, 0, 0, 1));
57
58 // print the network name (SSID);
59 Serial.print("Creating access point named: ");
60 Serial.println(ssid);
61
62 // Create open network. Change this line if you want to create an WEP network:
63 status = WiFi.beginAP(ssid, pass);
64 if (status != WL_AP_LISTENING) {
65 Serial.println("Creating access point failed");
66 // don't continue
67 while (true)
68 ;
69 }
70
71 // wait 10 seconds for connection:
72 delay(10000);
73
74 // start the web server on port 80
75 server.begin();
76
77 // you're connected now, so print out the status
78 printWiFiStatus();
79}
80
81
82void loop() {
83 // compare the previous status to the current status
84 if (status != WiFi.status()) {
85 // it has changed update the variable
86 status = WiFi.status();
87
88 if (status == WL_AP_CONNECTED) {
89 // a device has connected to the AP
90 Serial.println("Device connected to AP");
91 } else {
92 // a device has disconnected from the AP, and we are back in listening mode
93 Serial.println("Device disconnected from AP");
94 }
95 }
96
97 WiFiClient client = server.available(); // listen for incoming clients
98
99 if (client) { // if you get a client,
100 Serial.println("new client"); // print a message out the serial port
101 String currentLine = ""; // make a String to hold incoming data from the client
102 while (client.connected()) { // loop while the client's connected
103 delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
104 if (client.available()) { // if there's bytes to read from the client,
105 char c = client.read(); // read a byte, then
106 Serial.write(c); // print it out the serial monitor
107 if (c == '\n') { // if the byte is a newline character
108
109 // if the current line is blank, you got two newline characters in a row.
110 // that's the end of the client HTTP request, so send a response:
111 if (currentLine.length() == 0) {
112 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
113 // and a content-type so the client knows what's coming, then a blank line:
114 client.println("HTTP/1.1 200 OK");
115 client.println("Content-type:text/html");
116 client.println();
117
118 // the content of the HTTP response follows the header:
119 client.print("Click <a href=\"/HR\">here</a> turn the RED LED on<br>");
120 client.print("Click <a href=\"/LR\">here</a> turn the RED LED off<br>");
121 client.print("Click <a href=\"/HG\">here</a> turn the GREEN LED ON<br>");
122 client.print("Click <a href=\"/LG\">here</a> turn the GREEN LED off<br>");
123 client.print("Click <a href=\"/BH\">here</a> turn the BLUE LED on<br>");
124 client.print("Click <a href=\"/BL\">here</a> turn the BLUE LED off<br>");
125
126 // The HTTP response ends with another blank line:
127 client.println();
128 // break out of the while loop:
129 break;
130 } else { // if you got a newline, then clear currentLine:
131 currentLine = "";
132 }
133 } else if (c != '\r') { // if you got anything else but a carriage return character,
134 currentLine += c; // add it to the end of the currentLine
135 }
136
137 // Check to see if the client request (turns ON/OFF the different LEDs)
138 if (currentLine.endsWith("GET /HR")) {
139 digitalWrite(LED_RED, LOW);
140 }
141 if (currentLine.endsWith("GET /LR")) {
142 digitalWrite(LED_RED, HIGH);
143 }
144 if (currentLine.endsWith("GET /HG")) {
145 digitalWrite(LED_GREEN, LOW);
146 }
147 if (currentLine.endsWith("GET /LG")) {
148 digitalWrite(LED_GREEN, HIGH);
149 }
150 if (currentLine.endsWith("GET /BH")) {
151 digitalWrite(LED_BLUE, LOW);
152 }
153 if (currentLine.endsWith("GET /BL")) {
154 digitalWrite(LED_BLUE, HIGH);
155 }
156 }
157 }
158 // close the connection:
159 client.stop();
160 Serial.println("client disconnected");
161 }
162}
163
164void printWiFiStatus() {
165 // print the SSID of the network you're attached to:
166 Serial.print("SSID: ");
167 Serial.println(WiFi.SSID());
168
169 // print your WiFi shield's IP address:
170 IPAddress ip = WiFi.localIP();
171 Serial.print("IP Address: ");
172 Serial.println(ip);
173
174 // print where to go in a browser:
175 Serial.print("To see this page in action, open a browser to http://");
176 Serial.println(ip);
177}

MQTT

The GIGA R1 supports MQTT (Message Queuing Telemetry), a popular communication protocol for IoT devices. You will find below two examples: publisher and subscriber. The publisher sketch simply publishes a value to a channel, whereas the subscriber sketch subscribes to that value, achieving a very basic communication line between two boards, over the Internet.

Publisher

1/*
2MQTT Simple Publish (GIGA R1 WiFi / Portenta H7)
3
4This sketch demonstrates how to publish data
5to an MQTT broker (test.mosquitto.org).
6Data is NOT encrypted and this sketch is for
7prototyping purposes only.
8
9created 23 March 2023
10by Karl Söderby
11*/
12
13#include <ArduinoMqttClient.h>
14#include <WiFi.h>
15#include "arduino_secrets.h"
16
17///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18char ssid[] = SECRET_SSID; // your network SSID (name)
19char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20
21
22//create the objects
23WiFiClient wifiClient;
24MqttClient mqttClient(wifiClient);
25
26//define broker, port and topic
27const char broker[] = "test.mosquitto.org";
28int port = 1883;
29const char topic[] = "real_unique_topic";
30
31//set interval for sending messages (milliseconds)
32const long interval = 5000;
33unsigned long previousMillis = 0;
34
35int count = 0;
36
37void setup() {
38 //Initialize serial and wait for port to open:
39 Serial.begin(9600);
40 while (!Serial) {
41 ; // wait for serial port to connect. Needed for native USB port only
42 }
43
44 // attempt to connect to Wifi network:
45 Serial.print("Attempting to connect to WPA SSID: ");
46 Serial.println(ssid);
47 while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
48 // failed, retry
49 Serial.print(".");
50 delay(5000);
51 }
52
53 Serial.println("You're connected to the network");
54 Serial.println();
55
56 Serial.print("Attempting to connect to the MQTT broker: ");
57 Serial.println(broker);
58
59 if (!mqttClient.connect(broker, port)) {
60 Serial.print("MQTT connection failed! Error code = ");
61 Serial.println(mqttClient.connectError());
62
63 while (1);
64 }
65
66 Serial.println("You're connected to the MQTT broker!");
67 Serial.println();
68}
69
70void loop() {
71 // call poll() regularly to allow the library to send MQTT keep alives which
72 // avoids being disconnected by the broker
73 mqttClient.poll();
74 unsigned long currentMillis = millis();
75
76 //send a message every X second (based on interval)
77 if (currentMillis - previousMillis >= interval) {
78 previousMillis = currentMillis;
79
80 //read value from A0
81 int Rvalue = analogRead(A0);
82
83 //print to serial monitor
84 Serial.print("Sending message to topic: ");
85 Serial.println(topic);
86 Serial.println(Rvalue);
87 Serial.println();
88
89 //publish the message to the specific topic
90 mqttClient.beginMessage(topic);
91 mqttClient.print(Rvalue);
92 mqttClient.endMessage();
93
94 }
95}

Subscriber

1/*
2MQTT Simple Subscribe (GIGA R1 WiFi / Portenta H7)
3
4This sketch demonstrates how to subscribe to a topic
5from an MQTT broker (test.mosquitto.org).
6Data is NOT encrypted and this sketch is for
7prototyping purposes only.
8
9created 23 March 2023
10by Karl Söderby
11*/
12
13#include <ArduinoMqttClient.h>
14#include <WiFi.h>
15#include "arduino_secrets.h"
16
17///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18char ssid[] = SECRET_SSID; // your network SSID
19char pass[] = SECRET_PASS; // your network password
20
21//create the objects
22WiFiClient wifiClient;
23MqttClient mqttClient(wifiClient);
24
25//define broker, port and topic
26const char broker[] = "test.mosquitto.org";
27int port = 1883;
28const char topic[] = "real_unique_topic";
29
30void setup() {
31 //Initialize serial and wait for port to open:
32 Serial.begin(9600);
33 while (!Serial) {
34 ; // wait for serial port to connect. Needed for native USB port only
35 }
36 // attempt to connect to Wifi network:
37 Serial.print("Attempting to connect to SSID: ");
38 Serial.println(ssid);
39 while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
40 // failed, retry
41 Serial.print(".");
42 delay(5000);
43 }
44
45 Serial.println("You're connected to the network");
46 Serial.println();
47
48 Serial.print("Attempting to connect to the MQTT broker: ");
49 Serial.println(broker);
50
51 if (!mqttClient.connect(broker, port)) {
52 Serial.print("MQTT connection failed! Error code = ");
53 Serial.println(mqttClient.connectError());
54
55 while (1);
56 }
57
58 Serial.println("You're connected to the MQTT broker!");
59 Serial.println();
60
61 // set the message receive callback
62 mqttClient.onMessage(onMqttMessage);
63
64 Serial.print("Subscribing to topic: ");
65 Serial.println(topic);
66 Serial.println();
67
68 // subscribe to a topic
69 mqttClient.subscribe(topic);
70
71 // topics can be unsubscribed using:
72 // mqttClient.unsubscribe(topic);
73
74 Serial.print("Topic: ");
75 Serial.println(topic);
76
77 Serial.println();
78}
79
80void loop() {
81 // call poll() regularly to allow the library to receive MQTT messages and
82 // send MQTT keep alives which avoids being disconnected by the broker
83 mqttClient.poll();
84}
85
86void onMqttMessage(int messageSize) {
87 // we received a message, print out the topic and contents
88 Serial.println("Received a message with topic '");
89 Serial.print(mqttClient.messageTopic());
90 Serial.print("', length ");
91 Serial.print(messageSize);
92 Serial.println(" bytes:");
93
94 // use the Stream interface to print the contents
95 while (mqttClient.available()) {
96 Serial.print((char)mqttClient.read());
97 }
98 Serial.println();
99 Serial.println();
100}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.