From 1488caedce21b29a7c282c1e1280a3b47f286842 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Wed, 2 Aug 2023 14:02:52 +0200 Subject: [PATCH 1/2] Add UNO R4 WiFi CodeBlocks --- .../adc-resolution/adc-resolution.md | 2 + .../boards/uno-r4-wifi/tutorials/can/can.md | 75 +- .../boards/uno-r4-wifi/tutorials/dac/dac.md | 166 +- .../uno-r4-wifi/tutorials/eeprom/eeprom.md | 40 +- .../r4-wifi-getting-started.md | 2 + .../boards/uno-r4-wifi/tutorials/rtc/rtc.md | 2 + .../tutorials/wifi-examples/wifi-examples.md | 1559 +---------------- 7 files changed, 36 insertions(+), 1810 deletions(-) diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/adc-resolution/adc-resolution.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/adc-resolution/adc-resolution.md index 965d7bce5e..61e8c87383 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/adc-resolution/adc-resolution.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/adc-resolution/adc-resolution.md @@ -5,6 +5,8 @@ tags: - ADC - 14-bit author: 'Karl Söderby' +hardware: + - hardware/02.hero/boards/uno-r4-wifi --- In this tutorial you will learn how to change the analog-to-digital converter (ADC) on an **Arduino UNO R4 WiFi** board. By default, the resolution is set to 10-bit, which can be updated to both 12-bit (0-4096) and 14-bit (0-16383) resolutions for improved accuracy on analog readings. diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/can/can.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/can/can.md index d37f735828..e083022bab 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/can/can.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/can/can.md @@ -4,6 +4,8 @@ description: 'Learn how to send messages using the CAN bus on the UNO R4 WiFi.' tags: - CAN author: 'Karl Söderby' +hardware: + - hardware/02.hero/boards/uno-r4-wifi --- In this tutorial you will learn how to use the CAN controller on the **Arduino UNO R4 WiFi** board. The CAN controller is embedded in the UNO R4 WiFi's microcontroller (RA4M1). CAN is a serial protocol that is mainly used in the automotive industry. @@ -25,7 +27,7 @@ The goals of this tutorial are: - CAN transceiver module * - Jumper wires -* In this tutorial, we are using a SN65HVD230 breakout module. +\* In this tutorial, we are using a SN65HVD230 breakout module. ## Controller Area Network (CAN) @@ -79,80 +81,13 @@ CanMsg msg(CAN_ID, sizeof(msg_data), msg_data); After you have crafted a CAN message, we can send it off, by using the `CAN.write()` method. The following example creates a CAN message that increases each time `void loop()` is executed. -```arduino -#include - -static uint32_t const CAN_ID = 0x20; - -void setup() -{ - Serial.begin(115200); - while (!Serial) { } - - if (!CAN.begin(CanBitRate::BR_250k)) - { - Serial.println("CAN.begin(...) failed."); - for (;;) {} - } -} - -static uint32_t msg_cnt = 0; - -void loop() -{ - /* Assemble a CAN message with the format of - * 0xCA 0xFE 0x00 0x00 [4 byte message counter] - */ - uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0}; - memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt)); - CanMsg msg(CAN_ID, sizeof(msg_data), msg_data); - - /* Transmit the CAN message, capture and display an - * error core in case of failure. - */ - if (int const rc = CAN.write(msg); rc < 0) - { - Serial.print ("CAN.write(...) failed with error code "); - Serial.println(rc); - for (;;) { } - } - - /* Increase the message counter. */ - msg_cnt++; - - /* Only send one message per second. */ - delay(1000); -} -``` + ### CAN Read To read an incoming CAN message, first use `CAN.available()` to check if data is available, before using `CAN.read()` to read the message. -```arduino -#include - -void setup() -{ - Serial.begin(115200); - while (!Serial) { } - - if (!CAN.begin(CanBitRate::BR_250k)) - { - Serial.println("CAN.begin(...) failed."); - for (;;) {} - } -} - -void loop() -{ - if (CAN.available()) - { - CanMsg const msg = CAN.read(); - Serial.println(msg); - } -} -``` + ## Summary diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/dac/dac.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/dac/dac.md index 9dcbd4d65c..236ef9432a 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/dac/dac.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/dac/dac.md @@ -54,26 +54,7 @@ The waveform is being stored as samples in an array, and with every loop of the Open a new sketch and paste the following code into your window. -```arduino -#include "analogWave.h" - -analogWave wave(DAC); - -int freq = 10; // in hertz, change accordingly - -void setup() { - Serial.begin(115200); - pinMode(A5, INPUT); - wave.sine(freq); -} - -void loop() { - freq = map(analogRead(A5), 0, 1024, 0, 10000); - Serial.println("Frequency is now " + String(freq) + " hz"); - wave.freq(freq); - delay(1000); -} -``` + ## Testing It Out Once you have uploaded the code to the board, it should start generating a sine wave oscillation on the DAC, that depending on the frequency could be used to produce sound on a piezo buzzer or speaker. If you have an oscilloscope at hand, connecting its probe to the DAC output might be an interesting exercise so see what the wave looks like. @@ -87,149 +68,12 @@ Now that you know your setup is working, you can experiment further with differe ### Frere Jacques This one for example plays the melody of Frere Jacques: -```arduino - /* - DAC Melody player - - Generates a series of tones from MIDI note values - using the Uno R4 DAC and the AnalogWave Library. - The melody is "Frere Jacques" - -circuit: - * audio amp (LM386 used for testing) input+ attached to A0 - * audio amp input- attached to ground - * 4-8-ohm speaker attached to amp output+ - * Potentiometer connected to pin A5 - - created 13 Feb 2017 - modified 3 Jul 2023 - by Tom Igoe -*/ -#include "analogWave.h" -analogWave wave(DAC); - -#define NOTE_A4 69 // MIDI note value for middle A -#define FREQ_A4 440 // frequency for middle A - -// the tonic, or first note of the key signature for the song: -int tonic = 65; -// the melody sequence. Note values are relative to the tonic: -int melody[] = {1, 3, 5, 1, - 1, 3, 5, 1, - 5, 6, 8, 5, 6, 8, - 8, 10, 8, 6, 5, 1, - 8, 10, 8, 6, 5, 1, - 1, -4, 1, - 1, -4, 1 - }; -// the rhythm sequence. Values are 1/note, e.g. 4 = 1/4 note: -int rhythm[] = {4, 4, 4, 4, - 4, 4, 4, 4, - 4, 4, 2, - 4, 4, 2, - 8, 8, 8, 8, 4, 4, - 8, 8, 8, 8, 4, 4, - 4, 4, 2, - 4, 4, 2 - }; -// which note of the melody to play: -int noteCounter = 0; - -int bpm = 120; // beats per minute -// duration of a beat in ms -float beatDuration = 60.0 / bpm * 1000; - -void setup() { -// start the sine wave generator: - wave.sine(10); -} - -void loop() { - // current note is an element of the array: - int currentNote = melody[noteCounter] + tonic; - // play a note from the melody: - // convert MIDI note number to frequency: - float frequency = FREQ_A4 * pow(2, ((currentNote - NOTE_A4) / 12.0)); - - // all the notes in this are sixteenth notes, - // which is 1/4 of a beat, so: - float noteDuration = beatDuration * (4.0 / rhythm[noteCounter]); - // turn the note on: - wave.freq(frequency); - // tone(speakerPin, frequency, noteDuration * 0.85); - // keep it on for the appropriate duration: - delay(noteDuration * 0.85); - wave.stop(); - delay(noteDuration * 0.15); - // turn the note off: - // noTone(speakerPin); - // increment the note number for next time through the loop: - noteCounter++; - // keep the note in the range from 0 - 32 using modulo: - noteCounter = noteCounter % 32; - -} -``` + ### MIDI Piano Notes -This sketch will break down the potentiometer input to steps, that are translated to the 88 MIDI notes that represent the keys on a piano. - -```arduino -/* - Plays a tone in response to a potentiometer - formula from https://newt.phys.unsw.edu.au/jw/notes.html - and https://en.wikipedia.org/wiki/MIDI_tuning_standard: - - the MIDI protocol divides the notes of an equal-tempered scale into - 128 possible note values. Middle A is MIDI note value 69. There is - a formula for converting MIDI note numbers (0-127) to pitches. This sketch - reduces that to the notes 21 - 108, which are the 88 keys found on a piano: - - frequency = 440 * ((noteNumber - 69) / 12.0)^2 - - You can see this applied in the code below. - - circuit: - * audio amp (LM386 used for testing) input+ attached to A0 - * audio amp input- attached to ground - * 4-8-ohm speaker attached to amp output+ - * Potentiometer connected to pin A5 - - created 18 Dec 2018 - modified 3 Jul 2023 - by Tom Igoe -*/ - -// include the AnalogWave library: -#include "analogWave.h" -analogWave wave(DAC); - -// middle A is the reference frequency for an -// equal-tempered scale. Set its frequency and note value: -#define NOTE_A4 69 // MIDI note value for middle A -#define FREQ_A4 440 // frequency for middle A - -const int speakerPin = A0; // the pin number for the speaker -void setup() { - Serial.begin(9600); - wave.sine(10); -} -void loop() { - // convert sensor reading to 21 - 108 range - // which is the range of MIDI notes on an 88-key keyboard - // (from A0 to C8): - int sensorReading = analogRead(A5); - int noteValue = map(sensorReading, 0, 1023, 21, 108); - // then convert to frequency: - float frequency = FREQ_A4 * pow(2, ((noteValue - NOTE_A4) / 12.0)); - int freq = int(frequency); - // turn the speaker on: - wave.freq(freq); - Serial.print("note value: "+ String(noteValue) + " freq: "); - Serial.println(freq); - delay(500); -} -``` +This sketch will break down the potentiometer input into steps, that are translated to the 88 MIDI notes that represent the keys on a piano. + + ## Conclusion By following this tutorials you've experimented with the DAC on the Arduino UNO R4 boards and used it to first generate a sine wave, and then to explore the possibilities of analog output by testing out various examples. diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md index b0b9895790..00b304617f 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md @@ -5,6 +5,8 @@ tags: - RTC - Alarm author: 'Karl Söderby' +hardware: + - hardware/02.hero/boards/uno-r4-wifi --- In this tutorial you will learn how to access the EEPROM (memory) on an **Arduino UNO R4 WiFi** board. The EEPROM is embedded in the UNO R4 WiFi's microcontroller (RA4M1). @@ -42,49 +44,19 @@ EEPROM.read(0); //reads first byte There are several more methods available when working with EEPROM, and you can read more about this in the [A Guide to EEPROM](https://docs.arduino.cc/learn/programming/eeprom-guide). -***Please note: EEPROM is a type of memory with limited amount of write cycles. Be cautious when writing to this memory as you may significantly reduce the lifespan of this memory.*** +***Please note: EEPROM is a type of memory with a limited amount of write cycles. Be cautious when writing to this memory as you may significantly reduce the lifespan of this memory.*** ### EEPROM Write A minimal example on how to **write** to the EEPROM can be found below: -```arduino -#include - -int addr = 0; -byte value = 100; - -void setup() { - EEPROM.write(addr, val); -} -void loop(){ -} -``` + ### EEPROM Read -A minimal example on how to **read** from the EEPROM can be found below: - -```arduino -#include - -int address = 0; -byte value; - -void setup() { - Serial.begin(9600); - value = EEPROM.read(addr); - while (!Serial) { - ; - } +A minimal example of how to **read** from the EEPROM can be found below: - Serial.print("Address 0: "); - Serial.println(addr); -} - -void loop() { -} -``` + ## Summary diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/r4-wifi-getting-started/r4-wifi-getting-started.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/r4-wifi-getting-started/r4-wifi-getting-started.md index 57b409fe87..519e83920e 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/r4-wifi-getting-started/r4-wifi-getting-started.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/r4-wifi-getting-started/r4-wifi-getting-started.md @@ -2,6 +2,8 @@ title: Getting Started with UNO R4 WiFi description: A step-by-step guide to install the board package needed for the UNO R4 WiFi board. author: Hannes Siebeneicher +hardware: + - hardware/02.hero/boards/uno-r4-wifi tags: [UNO R4 WiFi, Installation, IDE] --- diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/rtc/rtc.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/rtc/rtc.md index a809bc312c..4259e1ff23 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/rtc/rtc.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/rtc/rtc.md @@ -5,6 +5,8 @@ tags: - RTC - Alarm author: 'Karl Söderby' +hardware: + - hardware/02.hero/boards/uno-r4-wifi --- In this tutorial you will learn how to access the real-time clock (RTC) on an **Arduino UNO R4 WiFi** board. The RTC is embedded in the UNO R4 WiFi's microcontroller (RA4M1). diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/wifi-examples/wifi-examples.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/wifi-examples/wifi-examples.md index e822a71ace..8828f8cd36 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/wifi-examples/wifi-examples.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/wifi-examples/wifi-examples.md @@ -2,6 +2,8 @@ title: UNO R4 WiFi Network Examples description: Discover examples compatible with the WiFi library included in the UNO R4 core. author: Jacob Hylén +hardware: + - hardware/02.hero/boards/uno-r4-wifi tags: [Wi-Fi, Web Server, AP Mode, SSL, UDP] --- @@ -29,1570 +31,37 @@ 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 accidentally sharing your Wi-Fi® credentials.*** +***Storing your network & password in a separate file minimizes the risk of you accidentally sharing your Wi-Fi® credentials.*** ### Access Point -```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 - */ - -#include "WiFiS3.h" - -#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 led = LED_BUILTIN; -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, OUTPUT); // set the LED pin mode - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // by default the local IP address will be 192.168.4.1 - // you can override it with the following: - WiFi.config(IPAddress(192,48,56,2)); - - // 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 to 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 LED on

"); - client.print("

Click here turn the 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 was "GET /H" or "GET /L": - if (currentLine.endsWith("GET /H")) { - digitalWrite(led, HIGH); // GET /H turns the LED on - } - if (currentLine.endsWith("GET /L")) { - digitalWrite(led, LOW); // GET /L turns the LED off - } - } - } - // 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); - -} -``` ### Connect With WPA -```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. - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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); - - // 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 NINA module. - Every ten seconds, it scans again. It doesn't actually - connect to any network, so no encryption scheme is specified. - - Circuit: - * Arduino UNO R4 WiFi - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 21 Junn 2012 - by Tom Igoe and Jaymes Dec - */ - - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } -} - -void loop() { - byte mac[6]; - // scan for existing networks: - Serial.println("Scanning available networks..."); - listNetworks(); - WiFi.macAddress(mac); - Serial.println(); - Serial.print("Your MAC Address is: "); - printMacAddress(mac); - 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(" Signal: "); - Serial.print(WiFi.RSSI(thisNet)); - Serial.print(" dBm"); - Serial.print(" Encryption: "); - 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_WPA: - Serial.println("WPA"); - break; - case ENC_TYPE_WPA2: - Serial.println("WPA2"); - break; - case ENC_TYPE_WPA3: - Serial.print("WPA3"); - 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(); -} -``` + ### Scan Networks (Advanced) -```arduino -/* - This example prints the board's MAC address, and - scans for available WiFi networks using the NINA module. - Every ten seconds, it scans again. It doesn't actually - connect to any network, so no encryption scheme is specified. - BSSID and WiFi channel are printed - - Circuit: - * Arduino UNO R4 WiFi - - This example is based on ScanNetworks - - created 1 Mar 2017 - by Arturo Guadalupi -*/ - - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // scan for existing networks: - Serial.println(); - Serial.println("Scanning available networks..."); - listNetworks(); - - // print your MAC address: - byte mac[6]; - WiFi.macAddress(mac); - Serial.print("MAC: "); - printMacAddress(mac); -} - -void loop() { - delay(10000); - // scan for existing networks: - Serial.println("Scanning available networks..."); - listNetworks(); -} - -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 + 1); - Serial.print(") "); - Serial.print("Signal: "); - Serial.print(WiFi.RSSI(thisNet)); - Serial.print(" dBm"); - Serial.print("\tChannel: "); - Serial.print(WiFi.channel(thisNet)); - byte bssid[6]; - Serial.print("\t\tBSSID: "); - printMacAddress(WiFi.BSSID(thisNet, bssid)); - Serial.print("\tEncryption: "); - printEncryptionType(WiFi.encryptionType(thisNet)); - Serial.print("\t\tSSID: "); - Serial.println(WiFi.SSID(thisNet)); - Serial.flush(); - } - Serial.println(); -} - -void printEncryptionType(int thisType) { - // read the encryption type and print out the name: - switch (thisType) { - case ENC_TYPE_WEP: - Serial.print("WEP"); - break; - case ENC_TYPE_WPA: - Serial.print("WPA"); - break; - case ENC_TYPE_WPA2: - Serial.print("WPA2"); - break; - case ENC_TYPE_WPA3: - Serial.print("WPA3"); - break; - case ENC_TYPE_NONE: - Serial.print("None"); - break; - case ENC_TYPE_AUTO: - Serial.print("Auto"); - break; - case ENC_TYPE_UNKNOWN: - default: - Serial.print("Unknown"); - break; - } -} - -void print2Digits(byte thisByte) { - if (thisByte < 0xF) { - Serial.print("0"); - } - Serial.print(thisByte, HEX); -} - -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(); -} -``` + ### Simple Webserver -```arduino -/* - WiFi Web Server LED Blink - - A simple web server that lets you blink an LED via the web. - This sketch will print the IP address of your WiFi module (once connected) - to the Serial Monitor. From there, you can open that address in a web browser - to turn on and off the LED_BUILTIN. - - If the IP address of your board is yourAddress: - http://yourAddress/H turns the LED on - http://yourAddress/L turns it off - - This example is written for a network using WPA encryption. For - WEP or WPA, change the WiFi.begin() call accordingly. - - Circuit: - * Arduino UNO R4 WiFi - * LED attached to pin 9 - - created 25 Nov 2012 - by Tom Igoe - */ - -#include "WiFiS3.h" - -#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 led = LED_BUILTIN; -int status = WL_IDLE_STATUS; -WiFiServer server(80); - -void setup() { - Serial.begin(9600); // initialize serial communication - pinMode(led, OUTPUT); // set the LED pin mode - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // attempt to connect to WiFi network: - while (status != WL_CONNECTED) { - Serial.print("Attempting to connect to Network named: "); - Serial.println(ssid); // print the network name (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(); // start the web server on port 80 - printWifiStatus(); // you're connected now, so print out the status -} - - -void loop() { - 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 - 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 to 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 LED on

"); - client.print("

Click here turn the 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 was "GET /H" or "GET /L": - if (currentLine.endsWith("GET /H")) { - digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on - } - if (currentLine.endsWith("GET /L")) { - digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off - } - } - - } - // 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"); - // print where to go in a browser: - Serial.print("To see this page in action, open a browser to http://"); - Serial.println(ip); -} -``` - -### SPI At Shell -```arduino -#include "Arduino.h" -#include "SPIProtocol.h" - -uint16_t sizec = 0; -uint8_t txBuffer[4096]; -uint8_t rxBuffer[4096]; - -//void isrRead() { -// readflag = true; -//} - -#ifdef ARDUINO_PORTENTA_C33 - -#ifndef NINA_GPIO0 -#define NINA_GPIO0 (100) -#endif - -#ifndef NINA_RESETN -#define NINA_RESETN (101) -#endif - -#else - - -#ifndef NINA_GPIO0 -#define NINA_GPIO0 (28) -#endif - -#ifndef NINA_RESETN -#define NINA_RESETN (29) -#endif - -#endif -void setup() { - Serial.begin(115200); - - pinMode(NINA_GPIO0, OUTPUT); - pinMode(NINA_RESETN, OUTPUT); - - digitalWrite(NINA_GPIO0, HIGH); - delay(100); - digitalWrite(NINA_RESETN, HIGH); - digitalWrite(NINA_RESETN, LOW); - digitalWrite(NINA_RESETN, HIGH); - - spiBegin(); -} - - -void loop() { - while (Serial.available()) { - txBuffer[sizec] = Serial.read(); - sizec++; - } - if (sizec > 0) { - Serial.println("sending"); - sizec *= 8; - writeToSlave(txBuffer, &sizec); - sizec = 0; - } - uint8_t raedableByte = readFromSlave(rxBuffer); - if (raedableByte > 0) { - for (int i = 0; i < raedableByte; i++) { - Serial.print(char(rxBuffer[i])); - } - } -} -``` + ### 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: - * Arduino UNO R4 WiFi - - created 18 Dec 2009 - by David A. Mellis - modified 31 May 2012 - by Tom Igoe - - */ - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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"); -} -``` + ### Wi-Fi® UDP NTP Client -```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: - * Arduino UNO R4 WiFi - - created 18 Dec 2009 - by David A. Mellis - modified 31 May 2012 - by Tom Igoe - - */ - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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"); -} -``` + ### Wi-Fi® UDP Send Receive String - -```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: - * Arduino UNO R4 WiFi - - created 18 Dec 2009 - by David A. Mellis - modified 31 May 2012 - by Tom Igoe - - */ - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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"); -} -``` + ### Wi-Fi® 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. - - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ - - -#include "WiFiS3.h" - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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); - } - - 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(); - } -} - -/* just wrap the received data up to 80 columns in the serial print*/ -/* -------------------------------------------------------------------------- */ -void read_response() { -/* -------------------------------------------------------------------------- */ - uint32_t received_data_num = 0; - while (client.available()) { - /* actual data reception */ - char c = client.read(); - /* print data to serial port */ - Serial.print(c); - /* wrap data to 80 columns*/ - received_data_num++; - if(received_data_num % 80 == 0) { - Serial.println(); - } - } -} - -/* -------------------------------------------------------------------------- */ -void loop() { -/* -------------------------------------------------------------------------- */ - read_response(); - - // 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® Web Client Repeating -```arduino -/* - Repeating WiFi Web Client - - This sketch connects to a a web server and makes a request - using a WiFi equipped Arduino board. - - created 23 April 2012 - modified 31 May 2012 - by Tom Igoe - modified 13 Jan 2014 - by Federico Vanzati - - http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating - This code is in the public domain. - */ - -#include "WiFiS3.h" - - -#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; - -// Initialize the WiFi client library -WiFiClient client; - -// server address: -char server[] = "example.org"; -//IPAddress server(64,131,82,241); - -unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds -const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds - -/* -------------------------------------------------------------------------- */ -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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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); - } - // you're connected now, so print out the status: - printWifiStatus(); -} - -/* just wrap the received data up to 80 columns in the serial print*/ -/* -------------------------------------------------------------------------- */ -void read_request() { -/* -------------------------------------------------------------------------- */ - uint32_t received_data_num = 0; - - while (client.available()) { - /* actual data reception */ - char c = client.read(); - /* print data to serial port */ - Serial.print(c); - /* wrap data to 80 columns*/ - received_data_num++; - if(received_data_num % 80 == 0) { - - } - - } -} - -/* -------------------------------------------------------------------------- */ -void loop() { -/* -------------------------------------------------------------------------- */ - // if there's incoming data from the net connection. - // send it out the serial port. This is for debugging - // purposes only: - read_request(); - - // if ten seconds have passed since your last connection, - // then connect again and send data: - if (millis() - lastConnectionTime > postingInterval) { - httpRequest(); - } - -} - -// this method makes a HTTP connection to the server: -/* -------------------------------------------------------------------------- */ -void httpRequest() { -/* -------------------------------------------------------------------------- */ - // close any connection before send a new request. - // This will free the socket on the NINA module - client.stop(); - - // if there's a successful connection: - if (client.connect(server, 80)) { - Serial.println("connecting..."); - // send the HTTP GET request: - client.println("GET / HTTP/1.1"); - client.println("Host: example.org"); - client.println("User-Agent: ArduinoWiFi/1.1"); - client.println("Connection: close"); - client.println(); - // note the time that the connection was made: - lastConnectionTime = millis(); - } else { - // if you couldn't make a connection: - Serial.println("connection failed"); - } -} - -/* -------------------------------------------------------------------------- */ -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® 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: - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - - */ - -#include "WiFiS3.h" - - - -#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); - } - - String fv = WiFi.firmwareVersion(); - if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - Serial.println("Please upgrade the firmware"); - } - - // 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"); -} -``` + \ No newline at end of file From cd3dc8a7f6d9db7e393f3d5588b9e13b14852608 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Thu, 3 Aug 2023 14:25:35 +0200 Subject: [PATCH 2/2] Update eeprom.md --- .../uno-r4-wifi/tutorials/eeprom/eeprom.md | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md index 00b304617f..5d60780db4 100644 --- a/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md +++ b/content/hardware/02.hero/boards/uno-r4-wifi/tutorials/eeprom/eeprom.md @@ -50,13 +50,43 @@ There are several more methods available when working with EEPROM, and you can r A minimal example on how to **write** to the EEPROM can be found below: - +```arduino +#include + +int addr = 0; +byte value = 100; + +void setup() { + EEPROM.write(addr, value); +} +void loop(){ +} +``` ### EEPROM Read A minimal example of how to **read** from the EEPROM can be found below: - +```arduino +#include + +int addr = 0; +byte value; + +void setup() { + Serial.begin(9600); + value = EEPROM.read(addr); + while (!Serial) { + + } + + Serial.print("Address 0: "); + Serial.println(value); +} + +void loop() { +} +``` ## Summary