Hello,
I am trying to use a potentiometer and servo on two different Arduinos. The potentiometer is attached to the Nano and the Servo to the Uno R4. The goal is to have the Nano (Sender) record data from the Potentiometer and transmit it to the Uno R4 (Reciever). The R4 then takes the potentiometer information and converts it into degrees for the servo to rotate.
What I am having trouble with is getting the two Arduinos to connect to WiFi, as well as pulling the Uno R4 ip address.
Code for the Uno R4:
#include <WiFiS3.h>
#include <WiFiUdp.h>
#include <Servo.h>
const char* ssid = "***"; // Replace with your Wi-Fi SSID
const char* password = "***"; // Replace with your Wi-Fi password
const int udpPort = 1234;
WiFiUDP udp;
Servo servo;
const int servoPin = 9; // Servo connected to pin 9
char packetBuffer[8]; // Buffer to store received data
int potValue = 0;
int servoAngle = 0;
void setup() {
Serial.begin(115200);
servo.attach(servoPin);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("Arduino UNO R4 WiFi IP Address: ");
Serial.println(WiFi.localIP()); // Prints the assigned IP address
udp.begin(udpPort);
}
void loop() {
int packetSize = udp.parsePacket();
if (packetSize) {
int len = udp.read(packetBuffer, sizeof(packetBuffer) - 1);
if (len > 0) {
packetBuffer[len] = '\0'; // Null-terminate string
potValue = atoi(packetBuffer); // Convert received data to integer
servoAngle = map(potValue, 0, 1023, 0, 180); // Map to servo degrees
servo.write(servoAngle);
Serial.print("Received: ");
Serial.print(potValue);
Serial.print(" -> Servo Angle: ");
Serial.println(servoAngle);
}
}
}
Code for Nano:
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "***"; // Replace with your Wi-Fi SSID
const char* password = "***"; // Replace with your Wi-Fi password
const char* receiverIP = "192.168.1.100"; // Set this to the IP of the UNO R4 WiFi
const int udpPort = 1234; // UDP Port
WiFiUDP udp;
int potPin = A0; // Potentiometer connected to A0
int potValue = 0;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
void loop() {
potValue = analogRead(potPin); // Read potentiometer value (0-1023)
udp.beginPacket(receiverIP, udpPort);
udp.print(potValue);
udp.endPacket();
Serial.print("Sent: ");
Serial.println(potValue);
delay(20); // Short delay for low-latency transmission
}
Feel free to ask any questions as I am new to Arduino and don't know how much sense this makes.
the following code transmits/receives UDP datagrams between a pair of ESP32 modules
the datagrams contain a structure which has various data types
// UDP ESP32 WiFi ethernet transmit/receive structure
#include <WiFi.h>
#include <WiFiUdp.h> // for UDP
// network SSID (network name). and network password.
char ssid[] = "xxxxxxxxxxxxx"; //ESP32-Access-Point";
char pass[] = "zzzzzzzzzzzzz"; //"123456789";
#define nodeID 3 // <<< set up required node ID
struct __attribute__((packed)) Struct1 {
//struct Struct1 {
byte StructureID; // identifies the structure type
byte NodeID; // ID of transmitting node
int16_t seq; // sequence number
int32_t distance;
float voltage;
} struct1 = { 1, nodeID, 0, 25, 4.5 }, struct2;
// ***** IP of this machine and remote machine *********
IPAddress localIP(192, 168, 1, 167); // local static localIP address
IPAddress remoteIP(0, 0, 0, 0); // local static localIP address
unsigned int localPort = 999; //10000; // local port to listen on
unsigned int remotePort = 999; //10000; // remote port to transmiit too
// An WiFiUDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(2000);
while (!Serial)
;
Serial.println("\n\nUDP ESP32 WiFi transmit/receive structure ");
Serial.print("sizeof(struct1) ");
Serial.println(sizeof(struct1));
WiFi.mode(WIFI_STA); // Connect to Wifi network as a station
WiFi.begin(ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (Udp.begin(localPort)) // start UDP
Serial.print("\nWiFi UDP started ");
else Serial.println("UDP begin() failed!");
displayIPaddress(WiFi.localIP(), localPort);
}
void loop() {
// if Serial text entered transmit as a datagram
if (Serial.available()) {
while (Serial.available()) Serial.read();
// if no datagram has been receive request remote IP address
if (remoteIP[0] == 0) {
// get remote IP address
remoteIP = getIPaddress("\nenter remote IP address (e.g. 192.168.1.176)? ");
}
Serial.print("\nTransmitting datagram to ");
displayIPaddress(remoteIP, remotePort);
struct1.seq++; // increment packet sequence number
struct1.distance += 1; // setup test values
struct1.voltage += 1;
Serial.print(" Node ");
Serial.print(nodeID);
Serial.print(" seq number ");
Serial.print(struct1.seq);
Serial.print(" distance = ");
Serial.print(struct1.distance);
Serial.print(" voltage = ");
Serial.println(struct1.voltage);
Udp.beginPacket(remoteIP, remotePort); // send to remote localIP and port 999
Udp.write((const uint8_t*)&struct1, sizeof(struct1));
Udp.endPacket();
}
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.print(packetSize); // datagram packet size
Serial.print(" From ");
remoteIP = Udp.remoteIP(); // from localIP
Serial.print(remoteIP);
Serial.print(", port "); // from port
Serial.println(Udp.remotePort());
// if packet is correct size read it and display contents
if (packetSize == sizeof(struct2)) {
Udp.read((unsigned char*)&struct2, sizeof(struct2)); //packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.print("Contents: ");
Serial.print(" StructureID ");
Serial.print(struct2.StructureID);
Serial.print(" from Node ");
Serial.print(struct2.NodeID);
Serial.print(" seq number ");
Serial.print(struct2.seq);
Serial.print(" distance 1 = ");
Serial.print(struct2.distance);
Serial.print(" voltage 1 = ");
Serial.println(struct2.voltage);
checkSeqNumber(struct2.NodeID, struct2.seq);
}
}
delay(10);
}
// read IP address from keyboard and check it
IPAddress getIPaddress(const char* prompt) {
IPAddress ip;
while (1) { // read IP (end with new line)
Serial.print(prompt);
while (Serial.available() == 0) delay(10);
char text[40] = { 0 };
Serial.readBytesUntil('\n', (char*)text, 40);
for (int i = 0; i < 40; i++)
if (text[i] < ' ') text[i] = 0; // remove CR or LF
Serial.print(text);
if (ip.fromString(text)) break; // if IP OK break while
Serial.println(" invalid IP try again");
}
return ip;
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
Serial.print(address);
Serial.print(" port ");
Serial.println(port);
}
void displayMACaddress(byte address[]) {
Serial.print("MAC address ");
for (int i = 0; i < 6; i++) {
Serial.print("0x");
Serial.print(address[i], HEX);
if (i < 5) Serial.print(".");
}
Serial.println();
}
// check sequence number
void checkSeqNumber(int NodeID, int seq) {
static int seqNumber=0, seqErrors = 0;
if (seq != seqNumber) { // check for sequence error!
Serial.print(" seq number error expected ");
Serial.print(seqNumber);
Serial.print(" received ");
Serial.print(seq);
Serial.print(" seq errors ");
Serial.println(++seqErrors);
seqNumber = seq;
}
seqNumber++; // next sequence nunber expected
}
serial monitor of one of the ESP32 modules
UDP ESP32 WiFi transmit/receive structure
sizeof(struct1) 12
...................
WiFi UDP started IP 192.168.1.211 port 999
Received packet of size 12 From 192.168.1.70, port 999
Contents: StructureID 1 from Node 3 seq number 1 distance 1 = 26 voltage 1 = 5.50
seq number error expected 0 received 1 seq errors 1
Received packet of size 12 From 192.168.1.70, port 999
Contents: StructureID 1 from Node 3 seq number 2 distance 1 = 27 voltage 1 = 6.50
Received packet of size 12 From 192.168.1.70, port 999
Contents: StructureID 1 from Node 3 seq number 3 distance 1 = 28 voltage 1 = 7.50
Transmitting datagram to IP 192.168.1.70 port 999
Node 3 seq number 1 distance = 26 voltage = 5.50
Transmitting datagram to IP 192.168.1.70 port 999
Node 3 seq number 2 distance = 27 voltage = 6.50
Transmitting datagram to IP 192.168.1.70 port 999
Node 3 seq number 3 distance = 28 voltage = 7.50
I assume the code will require some modification for a UNO R4