Salve,
dopo mille peripezie sono riuscito a volte e non sempre a far funzionare facendo cosi' (non riesco ad allegare gli sketch in altro modo per cui chiedo scusa e chiedo anche come fare):
Trasmissione:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAB, 0xDE, 0x05, 0xFC, 0xC8 };
IPAddress ip(192, 168, 1, 180); //centrale di comando
IPAddress remote1IP(192, 168, 1, 181); //pos. lavanderia
IPAddress remote2IP(192, 168, 1, 182); //pos. cucina/camera post.
IPAddress remote3IP(192, 168, 1, 183); //pos. cameretta
IPAddress remote4IP(192, 168, 1, 184); //pos. camera fronte
IPAddress remote5IP(192, 168, 1, 185); //pos. salotto
IPAddress remote6IP(192, 168, 1, 186); //pos. serra
IPAddress remote7IP(192, 168, 1, 187); //pos. garage
IPAddress remote8IP(192, 168, 1, 188);
unsigned int localPort = 8889; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char stringa;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
pinMode(6,INPUT_PULLUP);
}
void loop() {
stringa ="";
if (digitalRead(6) == LOW){
stringa = 74;
}else{
stringa = 69;
}
Udp.beginPacket(remote1IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote2IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote3IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote4IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote5IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote6IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote7IP, 8888);
Udp.write(stringa);
Udp.endPacket();
Udp.beginPacket(remote8IP, 8888);
Udp.write(stringa);
Udp.endPacket();
delay(10);
}
Ricezione con W5100:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <String.h>
int i;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAB, 0xDE, 0xA7, 0x3F, 0xEB};
IPAddress ip(192, 168, 1, 185);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
String ric ;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
pinMode(2,OUTPUT);
Serial.begin(9600);
}
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 remote = Udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
ric =(packetBuffer);
for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++);
packetBuffer[i] = 0;
Serial.println(packetSize);
if (ric == "J"){
digitalWrite (2,HIGH);
}else{
digitalWrite (2,LOW);
}
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(10);
// Udp.flush();
}
Ricezione con ENC26j80:
/*
* UIPEthernet UdpServer example.
*
* UIPEthernet is a TCP/IP stack that can be used with a enc28j60 based
* Ethernet-shield.
*
* UIPEthernet uses the fine uIP stack by Adam Dunkels <adam@sics.se>
*
* -----------------
*
* This UdpServer example sets up a udp-server at 192.168.0.6 on port 5000.
* send packet via upd to test
*
* Copyright (C) 2013 by Norbert Truchsess (norbert.truchsess@t-online.de)
*/
#include <UIPEthernet.h>
EthernetUDP udp;
void setup() {
pinMode (3,OUTPUT);
Serial.begin(9600);
uint8_t mac[6] = {0xDE,0xAB,0xDE,0x17,0x74,0x30};
Ethernet.begin(mac,IPAddress(192,168,1,184));
int success = udp.begin(8888);
Serial.print("initialize: ");
Serial.println(success ? "success" : "failed");
}
void loop() {
//check for new udp-packet:
int size = udp.parsePacket();
if (size > 0) {
do
{
char* msg = (char*)malloc(size+1);
int len = udp.read(msg,size+1);
msg[len]=0;
if (strcmp(msg , "J")){
digitalWrite(3,LOW);}
else{
digitalWrite(3,HIGH);
}
Serial.print("received: '");
Serial.print(msg);
free(msg);
}
while ((size = udp.available())>0);
//finish reading this packet:
udp.flush();
Serial.println("'");
int success;
do
{
Serial.print("remote ip: ");
Serial.println(udp.remoteIP());
Serial.print("remote port: ");
Serial.println(udp.remotePort());
//send new packet back to ip/port of client. This also
//configures the current connection to ignore packets from
//other clients!
success = udp.beginPacket(udp.remoteIP(),udp.remotePort());
Serial.print("beginPacket: ");
Serial.println(success ? "success" : "failed");
//beginPacket fails if remote ethaddr is unknown. In this case an
//arp-request is send out first and beginPacket succeeds as soon
//the arp-response is received.
}
while (!success);
success = udp.println("hello world from arduino");
Serial.print("bytes written: ");
Serial.println(success);
success = udp.endPacket();
Serial.print("endPacket: ");
Serial.println(success ? "success" : "failed");
udp.stop();
//restart with new connection to receive packets from other clients
Serial.print("restart connection: ");
Serial.println (udp.begin(8888) ? "success" : "failed");
}
}
Premetto che le mie conoscenze in questo campo sono veramente piccole e che questi skech sono scopiazzati a destra e sinistra.
Per quanto riguarda il Broadcasting non riesco a mettere insieme niente perche' non sono riuscito a trovare niente sulle funzioni e la sintassi per poterle richiamare.
Ho provato a mettere in pratica quei 2 esempi ma visto che non ho la sintassi giusta mi escono 3 errori di compilazione diversi ( tra l'altro che non ho copiato).
Le prove da me fatte sono con 4 schede 3 in ricezione ed una in trasmissione.
Solo una con W5100 funziona e l'altre ricevono oppure interpretano 3/4/5 caratteri anziche' uno.
Comunque anche la scheda con ENC28j60 riceve il messaggio ma giustamente non accende il led.
Forse mi sono avventurato in una cosa piu' grossa delle mie competenze.
Comunque grazie e sicuramente con il vostro aiuto qualcosa riusciro' a compicciare