is it possible to get 2 Arduino nano 33 iot's to communicate using wifi if so how ?
cjharrison33:
is it possible to get 2 Arduino nano 33 iot's to communicate using wifi
Yes.
cjharrison33:
if so how ?
There are multiple options. You can take a look at the examples under the File > Examples > WiFiNINA menu, the tutorials that accompany them here:
https://www.arduino.cc/en/Tutorial/LibraryExamples#WiFiNINA
and the library reference:
I'll provide an example that builds on the "WiFiUdpSendReceiveString" example, using UDP multicast.
Here's the sketch to run on one of the Arduino boards, which waits for a packet to arrive, and then responds "acknowledged" to the packet sender:
#include <WiFiNINA.h>
#include <WiFiUdp.h>
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect
// 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:
int status = WL_IDLE_STATUS;
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...");
Udp.beginMulticast(IPAddress(226, 1, 1, 1), 3000);
}
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());
char packetBuffer[255]; //buffer to hold incoming packet
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, sizeof(packetBuffer) / sizeof(packetBuffer[0]);
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("acknowledged");
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");
}
Here's the sketch for the other Arduino board, which sends a packet "hello" to the multicast address every three seconds and prints packets it receives to the Serial Monitor:
#include <WiFiNINA.h>
#include <WiFiUdp.h>
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
unsigned int localPort = 2390; // local port to listen on
WiFiUDP Udp;
unsigned long timestamp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect
// 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:
int status = WL_IDLE_STATUS;
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...");
Udp.begin(localPort);
}
void loop() {
// periodically send a message
if (millis() - timestamp > 3000) {
Udp.beginPacket(IPAddress(226, 1, 1, 1), 3000);
Udp.write("hello");
Udp.endPacket();
timestamp = millis();
}
// 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());
char packetBuffer[255]; //buffer to hold incoming packet
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, sizeof(packetBuffer) / sizeof(packetBuffer[0]);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}
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");
}
and if you want to use TCP instead of UDP then read here
Try as I can, I haven't been successful with UDP operations using a WeMOS board with a ESP8266 chip. The nano 33 might be a different beast. The example UDP code I've tried with my board just doesn't seem to work. TCP no problem. So far a head scratcher.
I tested the code I posted, as well as the stock "WiFiUdpSendReceiveString" example sketch, with a Nano 33 IoT and a MKR WiFi 1010 (because I don't have two Nano 33 IoTs, but same difference).
I've only done webserver stuff with the ESP8266.