Pages: [1]   Go Down
Author Topic: WiFi Shield, Pacchetti UDP  (Read 404 times)
0 Members and 1 Guest are viewing this topic.
Italy
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Salve sto cercando di comunicare in locale con un programma c# e arduino munito di wifi shield. Non riesco a far leggere pacchetti UDP ad Arduino ma neanche a mandarlo ecco qua il codice di prova limitato al minimo solo per questa funzione.

Code:
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
char ssid[] = "MySSID";     //  your network SSID (name)
char pass[] = "PW";    // your network password

unsigned int localPort = 34567;      // local port to listen on

char packetBuffer[255]; //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);

  boolean shieldOn = true;
 
    // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    shieldOn = false;
  }

  if(shieldOn)
  {
      // attempt to connect using WPA2 encryption:
    Serial.println("Attempting to connect to WPA network...");
    status = WiFi.begin(ssid, pass);
 
    // if you're not connected, stop here:
    if ( status != WL_CONNECTED) {
      Serial.println("Couldn't get a wifi connection");
    }
    // if you are connected, print out info about the connection:
    else {
      Serial.println("Connected to network");
      Serial.print("SSID: ");
      Serial.println(WiFi.SSID());
     
        // print the received signal strength:
      long rssi = WiFi.RSSI();
      Serial.print("signal strength (RSSI):");
      Serial.print(rssi);
      Serial.println(" dBm");
    }

    // print your WiFi shield's IP address:
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
  }
 
    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();
  Serial.print("Packetsize: ");
  Serial.println(packetSize);
 
  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();
   }

   delay(5000);
}

C#

Code:
UdpClient server = new UdpClient("192.168.1.246", 34567);

int z = server.Send(Encoding.ASCII.GetBytes("ARDUINOARDUINOARDUINOARDUINOARDUINO"), Encoding.ASCII.GetBytes("ARDUINOARDUINOARDUINOARDUINOARDUINO").Length);
Console.WriteLine(z);

L'Ip della shield lo controllo sempre.
Logged

There are 10 types of people in the world: Those who understand binary, and those who don't.

Italy
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

up
Logged

There are 10 types of people in the world: Those who understand binary, and those who don't.

Pages: [1]   Go Up
Jump to: