From 3fd0d4219ebe4cef0e8865e9e30c901691db9de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20S=C3=B6derby?= <35461661+karlsoderby@users.noreply.github.com> Date: Fri, 24 Mar 2023 08:12:06 +0100 Subject: [PATCH 1/3] GIGA WiFi examples Add all examples that have been tested and verified with the GIGA board. --- .../tutorials/giga-wifi/giga-wifi.md | 1397 +++++++++++++++++ 1 file changed, 1397 insertions(+) create mode 100644 content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md new file mode 100644 index 0000000000..135a8b26cd --- /dev/null +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md @@ -0,0 +1,1397 @@ +--- +title: GIGA R1 WiFi Network Examples +description: Discover examples compatible with the WiFi library included in the GIGA core. +author: Karl Söderby +tags: [Wi-Fi, Web Server, AP Mode, SSL, UDP] +--- + +The [GIGA R1 WiFi](/hardware/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 Core](https://github.com/arduino/ArduinoCore-mbed). Installing the core automatically installs the `WiFi` library. + +The radio module also supports Bluetooth® Low Energy, which is supported via the [ArduinoBLE](https://www.arduino.cc/reference/en/libraries/arduinoble/) library. + +***The easiest way to connect your board to the Internet is via the [Arduino IoT Cloud](https://create.arduino.cc/iot/) platform. Here you can configure, program, monitor and synchronize your devices without having to write any networking code.*** + +## Hardware & Software Needed + +- [GIGA R1 WiFi](/hardware/giga-r1) + \*antenna (included with the board). +- [Arduino IDE](https://www.arduino.cc/en/software) + +***\*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: + +```arduino +//arduino_secrets.h header file +#define SECRET_SSID "yournetwork" +#define SECRET_PASS "yourpassword" +``` + +***Storing network & password in a separate file minimizes the risk of you accidentially sharing your Wi-Fi credentials.*** + +### WPA Connection + +```arduino +/* + This example connects to an unencrypted WiFi network. + Then it prints the MAC address of the WiFi module, + the IP address obtained, and other network details. + + Circuit: + * GIGA R1 WiFi + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + modified 22 March 2023 + by Karl Söderby + */ + + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int status = WL_IDLE_STATUS; // the WiFi radio's status + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + +} + +void loop() { + // check the network connection once every 10 seconds: + delay(10000); + printCurrentNet(); +} + +void printWifiData() { + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + Serial.println(ip); + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + printMacAddress(mac); +} + +void printCurrentNet() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + printMacAddress(bssid); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption, HEX); + Serial.println(); +} + +void printMacAddress(byte mac[]) { + for (int i = 5; i >= 0; i--) { + if (mac[i] < 16) { + Serial.print("0"); + } + Serial.print(mac[i], HEX); + if (i > 0) { + Serial.print(":"); + } + } + Serial.println(); +} +``` + +### Wi-Fi RTC Example + +```arduino +/* + This example connects to an unencrypted WiFi network. + Then it prints the MAC address of the WiFi module, + the IP address obtained, and other network details. + + Circuit: + * GIGA R1 WiFi + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + modified 22 March 2023 + by Karl Söderby + */ + + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int status = WL_IDLE_STATUS; // the WiFi radio's status + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + +} + +void loop() { + // check the network connection once every 10 seconds: + delay(10000); + printCurrentNet(); +} + +void printWifiData() { + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + Serial.println(ip); + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + printMacAddress(mac); +} + +void printCurrentNet() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + printMacAddress(bssid); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption, HEX); + Serial.println(); +} + +void printMacAddress(byte mac[]) { + for (int i = 5; i >= 0; i--) { + if (mac[i] < 16) { + Serial.print("0"); + } + Serial.print(mac[i], HEX); + if (i > 0) { + Serial.print(":"); + } + } + Serial.println(); +} +``` + +### Scan Networks + +```arduino +/* + This example prints the board's MAC address, and + scans for available WiFi networks using the GIGA R1 WiFi board. + Every ten seconds, it scans again. It doesn't actually + connect to any network, so no encryption scheme is specified. + + Circuit: + * GIGA R1 WiFi + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 21 June 2012 + by Tom Igoe and Jaymes Dec + modified 3 March 2023 + by Karl Söderby + */ + +#include +#include + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC: "); + printMacAddress(mac); +} + +void loop() { + // scan for existing networks: + Serial.println("Scanning available networks..."); + listNetworks(); + delay(10000); +} + +void listNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + int numSsid = WiFi.scanNetworks(); + if (numSsid == -1) { + Serial.println("Couldn't get a WiFi connection"); + while (true); + } + + // print the list of networks seen: + Serial.print("number of available networks:"); + Serial.println(numSsid); + + // print the network number and name for each network found: + for (int thisNet = 0; thisNet < numSsid; thisNet++) { + Serial.print(thisNet); + Serial.print(") "); + Serial.print(WiFi.SSID(thisNet)); + Serial.print("\tSignal: "); + Serial.print(WiFi.RSSI(thisNet)); + Serial.print(" dBm"); + Serial.print("\tEncryption: "); + printEncryptionType(WiFi.encryptionType(thisNet)); + } +} + +void printEncryptionType(int thisType) { + // read the encryption type and print out the name: + switch (thisType) { + case ENC_TYPE_WEP: + Serial.println("WEP"); + break; + case ENC_TYPE_TKIP: + Serial.println("WPA"); + break; + case ENC_TYPE_CCMP: + Serial.println("WPA2"); + break; + case ENC_TYPE_NONE: + Serial.println("None"); + break; + case ENC_TYPE_AUTO: + Serial.println("Auto"); + break; + case ENC_TYPE_UNKNOWN: + default: + Serial.println("Unknown"); + break; + } +} + + +void printMacAddress(byte mac[]) { + for (int i = 5; i >= 0; i--) { + if (mac[i] < 16) { + Serial.print("0"); + } + Serial.print(mac[i], HEX); + if (i > 0) { + Serial.print(":"); + } + } + Serial.println(); +} +``` + +### Wi-Fi Chat Server + +```arduino +/* + Chat Server + + A simple server that distributes any incoming messages to all + connected clients. To use, telnet to your device's IP address and type. + You can see the client's input in the serial monitor as well. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the WiFi.begin() call accordingly. + + + Circuit: + * GIGA R1 WiFi + + created 18 Dec 2009 + by David A. Mellis + modified 31 May 2012 + by Tom Igoe + modified 23 March 2023 + by Karl Söderby + */ + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +WiFiServer server(23); + +boolean alreadyConnected = false; // whether or not the client was connected previously + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // start the server: + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); +} + + +void loop() { + // wait for a new client: + WiFiClient client = server.available(); + + + // when the client sends the first byte, say hello: + if (client) { + if (!alreadyConnected) { + // clear out the input buffer: + client.flush(); + Serial.println("We have a new client"); + client.println("Hello, client!"); + alreadyConnected = true; + } + + if (client.available() > 0) { + // read the bytes incoming from the client: + char thisChar = client.read(); + // echo the bytes back to the client: + server.write(thisChar); + // echo the bytes to the server as well: + Serial.write(thisChar); + } + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} +``` + +### Web Client + +```arduino +/* + Web client + + This sketch connects to a website (http://www.google.com) + using the WiFi module. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the WiFi.begin() call accordingly. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the WiFi.begin() call accordingly. + + Circuit: + * GIGA R1 WiFi + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + modified 21 March 2023 + by Karl Söderby + + */ + + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) +char server[] = "www.google.com"; // name address for Google (using DNS) + +// Initialize the Ethernet client library +// with the IP address and port of the server +// that you want to connect to (port 80 is default for HTTP): +WiFiClient client; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to WiFi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.1"); + client.println("Host: www.google.com"); + client.println("Connection: close"); + client.println(); + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while (true); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} +``` + +### Wi-Fi SSL Client + +```arduino +/* +This example creates a client object that connects and transfers +data using always SSL. + +It is compatible with the methods normally related to plain +connections, like client.connect(host, port). + +Circuit +* GIGA R1 WiFi + +Written by Arturo Guadalupi +in November 2015 + +modified 22 March 2023 +by Karl Söderby +*/ + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) +char server[] = "www.google.com"; // name address for Google (using DNS) + +// Initialize the Ethernet client library +// with the IP address and port of the server +// that you want to connect to (port 80 is default for HTTP): +WiFiSSLClient client; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to WiFi"); + printWiFiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 443)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.1"); + client.println("Host: www.google.com"); + client.println("Connection: close"); + client.println(); + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while (true); + } +} + + +void printWiFiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} +``` + +### UDP Send / Receive + +```arduino +/* +WiFi UDP Send and Receive String + + This sketch waits for a UDP packet on localPort using the WiFi module. + When a packet is received an Acknowledge packet is sent to the client on port remotePort + + Circuit: + * GIGA R1 WiFi + + created 30 December 2012 + by dlf (Metodo2 srl) + + modified 3 March 2023 + by Karl Söderby + */ + + +#include +#include +#include + +int status = WL_IDLE_STATUS; +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +unsigned int localPort = 2390; // local port to listen on + +char packetBuffer[256]; //buffer to hold incoming packet +char ReplyBuffer[] = "acknowledged"; // a string to send back + +WiFiUDP Udp; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to WiFi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + Udp.begin(localPort); +} + +void loop() { + + // if there's data available, read a packet + int packetSize = Udp.parsePacket(); + if (packetSize) { + Serial.print("Received packet of size "); + Serial.println(packetSize); + Serial.print("From "); + IPAddress remoteIp = Udp.remoteIP(); + Serial.print(remoteIp); + Serial.print(", port "); + Serial.println(Udp.remotePort()); + + // read the packet into packetBufffer + int len = Udp.read(packetBuffer, 255); + if (len > 0) { + packetBuffer[len] = 0; + } + Serial.println("Contents:"); + Serial.println(packetBuffer); + + // send a reply, to the IP address and port that sent us the packet we received + Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); + Udp.write(ReplyBuffer); + Udp.endPacket(); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} +``` + +### Web Server + +```arduino +/* + WiFi Web Server + + A simple web server that shows the value of the analog input pins. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the WiFi.begin() call accordingly. + + Circuit: + * GIGA R1 WiFi + * (optional) analog inputs connected on A0-A5 + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + modified 3 March 2023 + by Karl Söderby + */ + +#include +#include + + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +WiFiServer server(80); + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); +} + + +void loop() { + // listen for incoming clients + WiFiClient client = server.available(); + if (client) { + Serial.println("new client"); + // an HTTP request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + Serial.write(c); + // if you've gotten to the end of the line (received a newline + // character) and the line is blank, the HTTP request has ended, + // so you can send a reply + if (c == '\n' && currentLineIsBlank) { + // send a standard HTTP response header + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println("Connection: close"); // the connection will be closed after completion of the response + client.println("Refresh: 5"); // refresh the page automatically every 5 sec + client.println(); + client.println(""); + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + int sensorReading = analogRead(analogChannel); + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(sensorReading); + client.println("
"); + } + client.println(""); + break; + } + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + // give the web browser time to receive the data + delay(1); + + // close the connection: + client.stop(); + Serial.println("client disconnected"); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} +``` + + +### Web Server AP Mode + +```arduino +/* + WiFi Web Server LED Blink + + A simple web server that lets you blink an LED via the web. + This sketch will create a new access point (with no password). + It will then launch a new server and print out the IP address + to the Serial Monitor. From there, you can open that address in a web browser + to turn on and off the LED on pin 13. + + If the IP address of your board is yourAddress: + http://yourAddress/H turns the LED on + http://yourAddress/L turns it off + + created 25 Nov 2012 + by Tom Igoe + adapted to WiFi AP by Adafruit + + modified 22 March 2023 + by Karl Söderby + */ + +#include +#include +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +WiFiServer server(80); + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.println("Access Point Web Server"); + + pinMode(LED_RED, OUTPUT); + pinMode(LED_GREEN, OUTPUT); + pinMode(LED_BLUE, OUTPUT); + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true) + ; + } + + // by default the local IP address will be 192.168.4.1 + // you can override it with the following: + // WiFi.config(IPAddress(10, 0, 0, 1)); + + // print the network name (SSID); + Serial.print("Creating access point named: "); + Serial.println(ssid); + + // Create open network. Change this line if you want to create an WEP network: + status = WiFi.beginAP(ssid, pass); + if (status != WL_AP_LISTENING) { + Serial.println("Creating access point failed"); + // don't continue + while (true) + ; + } + + // wait 10 seconds for connection: + delay(10000); + + // start the web server on port 80 + server.begin(); + + // you're connected now, so print out the status + printWiFiStatus(); +} + + +void loop() { + // compare the previous status to the current status + if (status != WiFi.status()) { + // it has changed update the variable + status = WiFi.status(); + + if (status == WL_AP_CONNECTED) { + // a device has connected to the AP + Serial.println("Device connected to AP"); + } else { + // a device has disconnected from the AP, and we are back in listening mode + Serial.println("Device disconnected from AP"); + } + } + + WiFiClient client = server.available(); // listen for incoming clients + + if (client) { // if you get a client, + Serial.println("new client"); // print a message out the serial port + String currentLine = ""; // make a String to hold incoming data from the client + while (client.connected()) { // loop while the client's connected + delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served. + if (client.available()) { // if there's bytes to read from the client, + char c = client.read(); // read a byte, then + Serial.write(c); // print it out the serial monitor + if (c == '\n') { // if the byte is a newline character + + // if the current line is blank, you got two newline characters in a row. + // that's the end of the client HTTP request, so send a response: + if (currentLine.length() == 0) { + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) + // and a content-type so the client knows what's coming, then a blank line: + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println(); + + // the content of the HTTP response follows the header: + client.print("Click here turn the RED LED on
"); + client.print("Click here turn the RED LED off
"); + client.print("Click here turn the GREEN LED ON
"); + client.print("Click here turn the GREEN LED off
"); + client.print("Click here turn the BLUE LED on
"); + client.print("Click here turn the BLUE LED off
"); + + // The HTTP response ends with another blank line: + client.println(); + // break out of the while loop: + break; + } else { // if you got a newline, then clear currentLine: + currentLine = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine + } + + // Check to see if the client request (turns ON/OFF the different LEDs) + if (currentLine.endsWith("GET /HR")) { + digitalWrite(LED_RED, LOW); + } + if (currentLine.endsWith("GET /LR")) { + digitalWrite(LED_RED, HIGH); + } + if (currentLine.endsWith("GET /HG")) { + digitalWrite(LED_GREEN, LOW); + } + if (currentLine.endsWith("GET /LG")) { + digitalWrite(LED_GREEN, HIGH); + } + if (currentLine.endsWith("GET /BH")) { + digitalWrite(LED_BLUE, LOW); + } + if (currentLine.endsWith("GET /BL")) { + digitalWrite(LED_BLUE, HIGH); + } + } + } + // close the connection: + client.stop(); + Serial.println("client disconnected"); + } +} + +void printWiFiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print where to go in a browser: + Serial.print("To see this page in action, open a browser to http://"); + Serial.println(ip); +} +``` + +## 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 + +```arduino +/* +MQTT Simple Publish (GIGA R1 WiFi / Portenta H7) + +This sketch demonstrates how to publish data +to an MQTT broker (test.mosquitto.org). +Data is NOT encrypted and this sketch is for +prototyping purposes only. + +created 23 March 2023 +by Karl Söderby +*/ + +#include +#include +#include "arduino_secrets.h" + +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + + +//create the objects +WiFiClient wifiClient; +MqttClient mqttClient(wifiClient); + +//define broker, port and topic +const char broker[] = "test.mosquitto.org"; +int port = 1883; +const char topic[] = "real_unique_topic"; + +//set interval for sending messages (milliseconds) +const long interval = 5000; +unsigned long previousMillis = 0; + +int count = 0; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // attempt to connect to Wifi network: + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { + // failed, retry + Serial.print("."); + delay(5000); + } + + Serial.println("You're connected to the network"); + Serial.println(); + + Serial.print("Attempting to connect to the MQTT broker: "); + Serial.println(broker); + + if (!mqttClient.connect(broker, port)) { + Serial.print("MQTT connection failed! Error code = "); + Serial.println(mqttClient.connectError()); + + while (1); + } + + Serial.println("You're connected to the MQTT broker!"); + Serial.println(); +} + +void loop() { + // call poll() regularly to allow the library to send MQTT keep alives which + // avoids being disconnected by the broker + mqttClient.poll(); + unsigned long currentMillis = millis(); + + //send a message every X second (based on interval) + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + + //read value from A0 + int Rvalue = analogRead(A0); + + //print to serial monitor + Serial.print("Sending message to topic: "); + Serial.println(topic); + Serial.println(Rvalue); + Serial.println(); + + //publish the message to the specific topic + mqttClient.beginMessage(topic); + mqttClient.print(Rvalue); + mqttClient.endMessage(); + + } +} +``` + +### Subscriber + +```arduino +/* +MQTT Simple Subscribe (GIGA R1 WiFi / Portenta H7) + +This sketch demonstrates how to subscribe to a topic +from an MQTT broker (test.mosquitto.org). +Data is NOT encrypted and this sketch is for +prototyping purposes only. + +created 23 March 2023 +by Karl Söderby +*/ + +#include +#include +#include "arduino_secrets.h" + +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID +char pass[] = SECRET_PASS; // your network password + +//create the objects +WiFiClient wifiClient; +MqttClient mqttClient(wifiClient); + +//define broker, port and topic +const char broker[] = "test.mosquitto.org"; +int port = 1883; +const char topic[] = "real_unique_topic"; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + // attempt to connect to Wifi network: + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + while (WiFi.begin(ssid, pass) != WL_CONNECTED) { + // failed, retry + Serial.print("."); + delay(5000); + } + + Serial.println("You're connected to the network"); + Serial.println(); + + Serial.print("Attempting to connect to the MQTT broker: "); + Serial.println(broker); + + if (!mqttClient.connect(broker, port)) { + Serial.print("MQTT connection failed! Error code = "); + Serial.println(mqttClient.connectError()); + + while (1); + } + + Serial.println("You're connected to the MQTT broker!"); + Serial.println(); + + // set the message receive callback + mqttClient.onMessage(onMqttMessage); + + Serial.print("Subscribing to topic: "); + Serial.println(topic); + Serial.println(); + + // subscribe to a topic + mqttClient.subscribe(topic); + + // topics can be unsubscribed using: + // mqttClient.unsubscribe(topic); + + Serial.print("Topic: "); + Serial.println(topic); + + Serial.println(); +} + +void loop() { + // call poll() regularly to allow the library to receive MQTT messages and + // send MQTT keep alives which avoids being disconnected by the broker + mqttClient.poll(); +} + +void onMqttMessage(int messageSize) { + // we received a message, print out the topic and contents + Serial.println("Received a message with topic '"); + Serial.print(mqttClient.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + // use the Stream interface to print the contents + while (mqttClient.available()) { + Serial.print((char)mqttClient.read()); + } + Serial.println(); + Serial.println(); +} +``` \ No newline at end of file From ce145ce9c60f55cece99a85ea6214f93595eb6fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20S=C3=B6derby?= <35461661+karlsoderby@users.noreply.github.com> Date: Fri, 24 Mar 2023 08:18:46 +0100 Subject: [PATCH 2/3] Update giga-wifi.md --- .../boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md index 135a8b26cd..73b9abea86 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md @@ -32,7 +32,7 @@ You will need to create this file, or remove the `#include "arduino_secrets.h"` #define SECRET_PASS "yourpassword" ``` -***Storing network & password in a separate file minimizes the risk of you accidentially sharing your Wi-Fi credentials.*** +***Storing network & password in a separate file minimizes the risk of you accidentally sharing your Wi-Fi credentials.*** ### WPA Connection From 0c1ac1b42930e7b382698050312480397eed30a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20S=C3=B6derby?= <35461661+karlsoderby@users.noreply.github.com> Date: Fri, 24 Mar 2023 09:17:25 +0100 Subject: [PATCH 3/3] Add feature + expand cs section --- .../10.mega/boards/giga-r1-wifi/features.md | 6 +++++- .../tutorials/cheat-sheet/cheat-sheet.md | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/features.md b/content/hardware/10.mega/boards/giga-r1-wifi/features.md index 7fcd974a6e..1e80bd39a4 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/features.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/features.md @@ -27,7 +27,7 @@ The STM32H747XI has two cores that can be programmed separately (M4/M7). You can The GIGA R1 WiFi comes with a Murata LBEE5KL1DX-883 radio module for Wi-Fi/Bluetooth® communication. - + @@ -47,5 +47,9 @@ Learn how to connect a camera via the dedicated connector. + +The GIGA R1 WiFi is compatible with the Arduino IoT Cloud platform. Build IoT projects in just minutes! + + diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md index 058c97f604..59eee61fdf 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md @@ -161,18 +161,29 @@ This can be very useful, as this flash storage **does not get deleted when you u ***Note: In this configuration, the USB serial port used for serial communication with the computer is occupied, so you won't be able to send or read information in the serial monitor. **This includes uploading new sketches. To upload a new sketch you need to put the GIGA R1 in DFU mode by double pressing the RST button.*** -## Wi-Fi® / Bluetooth® LE +## Radio Module ![Murata LBEE5KL1DX-883 radio module + antenna connector.](assets/wifi.png) -The Wi-Fi / Bluetooth® module onboard the GIGA R1 WiFi is the Murata LBEE5KL1DX-883. This module does not come with a built-in antenna, but an external antenna is included when purchasing the board. +The Wi-Fi® / Bluetooth® module onboard the GIGA R1 WiFi is the Murata LBEE5KL1DX-883. This module does not come with a built-in antenna, but an external antenna is included when purchasing the board. The antenna connector (see image above) is located right next to the USB-C connector, and is of a **U.FL.** type. +### Wi-Fi® + +Wi-Fi® on the GIGA R1 WiFi is supported via the `WiFi` library. This library is included in the core, so it is automatically installed when installing the core. + +To use the Wi-Fi® features on this board, please refer to the [GIGA R1 WiFi Network Examples](/tutorials/giga-r1-wifi/giga-wifi) guide. + +***The easiest way to connect your board to the Internet is via the [Arduino IoT Cloud](https://create.arduino.cc/iot/) platform. Here you can configure, program, monitor and synchronize your devices without having to write any networking code.*** + +### Bluetooth® Low Energy + To use the BLE features on this board, refer to the [ArduinoBLE library documentation](https://reference.arduino.cc/reference/en/libraries/arduinoble/). -### Ethernet -If you want to add Ethernet connectivity to your project, there are many many ways of doing that, one of the easiest ways is to use the [Arduino Ethernet Shield Rev2](/hardware/ethernet-shield-rev2). +## Ethernet + +If you want to add Ethernet connectivity to your project, the [Arduino Ethernet Shield Rev2](/hardware/ethernet-shield-rev2) is compatible. In the shield's documentation, you will find a series of examples. ## Audio Jack