Skip to Content
Dan k
Chen
ATWINC1500 WiFi UDP Communication
Explorations Dan Chen / December 30, 2016

Working in progress

//////////////////////////////////////////////////////////////////////

http://www.atmel.com/devices/atwinc1500.aspx

 

Test your I/O setup – No networking yet

Wire servo on A2, Pot on Pin A1.

When you run the sketch below you should be able to turn the 10k potentiometer to control the servo.

#include #include <Servo.h>

Servo myservo;  // create servo object to control a servo
int senPin = A1;
int servoPin = A2;
int senVal = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
}

void loop() {

  senVal = analogRead(senPin);
  Serial.print("senVal: ");
  Serial.println(senVal, DEC);
  senVal = map(senVal, 10, 104, 10, 170);
  myservo.write(senVal);
  delay(15);
}

Test UDP + WiFi Connection with ARWINC1500

 

On you Mac

Download Packet Sender for your Mac

https://packetsender.com/

Find out your IP address by Option + Click the WIFI icon on the upper status bar

 

Download and Install this Library

https://github.com/adafruit/Adafruit_WINC1500

Run the Example > Adafruit_WINC1500 > WiFiUdpSendReceiveString >
just to make sure that your Wifi is working by changing the following 2 lines

char ssid[] = “SkyNet”; // your network SSID (name)
char pass[] = “password”; // your network password

 

Serial Monitor

After uploading the sketch, it should say something like this in Serial

Connected to wifi
SSID: SkyNet
IP Address: 192.168.13.4
signal strength (RSSI):-30 dBm

Starting connection to server…

 

Test UDP Connection

Download Packet Sender for your Mac
https://packetsender.com/

# Run the WiFiUdpSendReceiveString Example
# Open Serial Monitor
# Get the IP address from Serial Monitor
# Paste the IP Address in Address field in Packet Sender
# Use port 2390
# In Packet Send, Change TCP to UDP
# Type Some ASCII message then check HEX field to cover the string
# Click Send

You should see the message in Serial Monitor

https://youtu.be/AspSdeDboaM

iPhone UDP

# Download UDP Test Tool https://itunes.apple.com/us/app/udp-test-tool/id469617393?mt=8
# Type IP address from your Serial Monitor into the “Sending data to:” field
# Use port 2390
# Type some message then click Send

 

 

TIP: Some router allows you to assign internal IP address. You can also do port mapping or DDNS, that way you are not limited to only internal network.

 

Send Potentiometer Data via UDP to your Computer & iPhone

 

The following code gets the sensor data then sends out via UDP

Download
WiFiUdpSendSensorData.ino

/*
 WiFi UDP Send and Receive String
This sketch wait an UDP packet on localPort using a WiFi shield.
 When a packet is received an Acknowledge packet is sent to the client on port remotePort
Circuit:
 * WiFi shield attached
created 30 December 2012
 by dlf (Metodo2 srl) + Dan Chen
*/

#include <SPI.h>
#include <Adafruit_WINC1500.h>
#include <Adafruit_WINC1500Udp.h>
// Define the WINC1500 board connections below.
// If you're following the Adafruit WINC1500 board
// guide you don't need to modify these:
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC and comment this out
// The SPI pins of the WINC1500 (SCK, MOSI, MISO) should be
// connected to the hardware SPI port of the Arduino.
// On an Uno or compatible these are SCK = #13, MISO = #12, MOSI = #11.
// On an Arduino Zero use the 6-pin ICSP header, see:
// https://www.arduino.cc/en/Reference/SPI
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
// Or just use hardware SPI (SCK/MOSI/MISO) and defaults, SS -> #10, INT -> #7, RST -> #5, EN -> 3-5V
//Adafruit_WINC1500 WiFi;
int counter=1;
int status = WL_IDLE_STATUS;
char ssid[] = "SkyNet"; // your network SSID (name)
char pass[] = "password"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
unsigned int localPort = 2390; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "acknowledged"; // a string to send back
Adafruit_WINC1500UDP Udp;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int senPin = A1;
int servoPin = A2;
int senVal = 0;
 
void setup() {
#ifdef WINC_EN
 pinMode(WINC_EN, OUTPUT);
 digitalWrite(WINC_EN, HIGH);
#endif
//Initialize serial and wait for port to open:
 Serial.begin(9600);
 
 myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
/*
 while (!Serial) {
 ; // wait for serial port to connect. Needed for native USB port only
 }
 */
// check for the presence of the shield:
 if (WiFi.status() == WL_NO_SHIELD) {
 Serial.println("WiFi shield not present");
 // 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(5000);
 }
 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();
 
 
 messageSender();

 /*
 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());

 String stringOne = "Message: ";
 stringOne += counter;
char charBuf[50];
 stringOne.toCharArray(charBuf, 50);

 Udp.write(charBuf);
 Udp.endPacket();
 counter++;
}
 */
}
void messageSender() {
 
 senVal = analogRead(senPin);
 Serial.print("senVal: ");
 Serial.println(senVal, DEC);
 // map the sensor data for servo
 senVal = map(senVal, 0, 1024, 10, 170);
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
 // Computer or Iphone's IP address and Port
 Udp.beginPacket("192.168.13.3", 55056);
 
 String stringOne = "Sensor Data: ";
 stringOne += senVal;
char charBuf[50];
 stringOne.toCharArray(charBuf, 50);
Udp.write(charBuf);
//Udp.write(ReplyBuffer);
 Udp.endPacket();
 
 
 delay(500);

 
}
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 the received signal strength:
 long rssi = WiFi.RSSI();
 Serial.print("signal strength (RSSI):");
 Serial.print(rssi);
 Serial.println(" dBm");
}

ATWINC1500 sends sensor data to Computer via UDP – DEMO

void messageSender()
This function sends out the sensor data via UDP

Udp.beginPacket(“192.168.13.3”, 55056);
Sends the sensor to certain IP address and port.

I used Packet Sender on my Mac to get UDP data

https://packetsender.com/

 

DAN K CHEN © 2024