Domanda..
ok.. ho usato i digital input e funzionano. Usato l'ethernet sheld ho intuito che non devo usare i pin dal 10 al 13.
In riferimento al listato sottostante avrei bisogno di scrivere al posto del messaggio "contatto off" o "on" un messaggio molto semplice del tipo
1 char status - 0 = contatto aperto, 1= contatto chiuso
1 char Port - da 0 a 3
15 char ip address
Inoltre mi trovo in difficoltà in una serie di conversioni e sintassi.
Io le butto li poi chi ha tempo mi dica.. :
Come faccio a creare una struttura contenente le 4 porte (classe server) in modo da poterci ciclare semplicemente con un ciclo for?
Come faccio a buttare un array di byte, nello specifico l'indirizzo IP, in una stringa per passarla poi sul socket tramite il metodo write?
Scusate la banalità delle domande ma con la sintassi ANSI C(l'unica che conosco.. più o meno..) non ci sono saltato fuori
/*
Four Digital dry push botton ethernet server
A simple socket server for retry button status changing using digital input pins.
Arduino Ethernet SD shield platform is required.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Digital attached to pins 0 through 3
Protocol
For each port use a socket. To 5001 through 5004
When b
Created 24 Nov 2010
by Marco BRUNO, Ivrea, Italy
Rev 2010.02 25 Nov 2010 Marco BRUNO, Ivrea, Italy: Adding comments
*/
#include <SPI.h>
#include <Ethernet.h>
//#include <String.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/wdt.h>
#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {} // Cane da guardia, se il timer si inchioda riavvia arduino
// Per resettare alla bisogna via softwqware usare
// Reset_AVR();
//#include <Udp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = {192, 168, 1, 1};
byte subnet[] = {255, 255, 255, 0};
// Initialize the Ethernet server library
// with the IP address and port you want to use
Server port1(5001);
Server port2(5002);
Server port3(5003);
Server port4(5004);
int Button1State = 0; // Variable for reading the pushbuttons status
int Button2State = 0; // Variable for reading the pushbuttons status
int Button3State = 0; // Variable for reading the pushbuttons status
int Button4State = 0; // Variable for reading the pushbuttons status
int Button1StateOld = 0; //
int Button2StateOld = 0; //
int Button3StateOld = 0; //
int Button4StateOld = 0; //
String sIp="";
String sIpSep=".";
//itoa(int(ip[0]),sIP,10);
//+"."+string(ip[0])+"."+String(ip[0])+"."+String(ip[0]);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
port1.begin();
port2.begin();
port3.begin();
port4.begin();
// initialize the pushbutton pin as an input:
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(0, HIGH); // sets the LED on
digitalWrite(1, HIGH); // sets the LED on
digitalWrite(2, HIGH); // sets the LED on
digitalWrite(3, HIGH); // sets the LED on
}
void loop()
{
// Controllo su porta1 --------------------------------------
Button1State = digitalRead(0);
if (Button1State != Button1StateOld)
{
if (Button1State == HIGH)
{
port1.write("Conttato 1 On");
}
else
{
port1.write("Conttato 1 Off");
}
}
Button1StateOld=Button1State;
// Controllo su porta2 --------------------------------------
Button2State = digitalRead(1);
if (Button2State != Button2StateOld)
{
if (Button2State == HIGH)
{
port2.write("Contatto 2 On");
}
else
{
port2.write("Contatto 2 Off");
}
}
Button2StateOld=Button2State;
// Controllo su porta3 --------------------------------------
Button3State = digitalRead(2);
if (Button3State != Button3StateOld)
{
if (Button3State == HIGH)
{
port3.write("Contatto 3 On");
}
else
{
port3.write("Contatto 3 Off");
}
}
Button3StateOld=Button3State;
// Controllo su porta4 --------------------------------------
Button4State = digitalRead(3);
if (Button4State != Button4StateOld)
{
if (Button4State == HIGH)
{
port4.write("Contatto 4 On");
}
else
{
port4.write("Contatto 4 Off");
}
}
Button4StateOld=Button4State;
delay(10);
}
[code]/*
Four Digital dry push botton ethernet server
A simple socket server for retry button status changing using digital input pins.
Arduino Wiznet Ethernet shield platform is required.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Digital attached to pins 0 through 3
Protocol
For each port use a socket. To 5001 through 5004
When b
Created 24 Nov 2010
by Marco BRUNO, Ivrea, Italy
Rev 2010.02 25 Nov 2010 Marco BRUNO, Ivrea, Italy: Adding comments
*/
#include <SPI.h>
#include <Ethernet.h>
//#include <String.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/wdt.h>
#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {} // Cane da guardia, se il timer si inchioda riavvia arduino
// Per resettare alla bisogna via softwqware usare
// Reset_AVR();
//#include <Udp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = {192, 168, 1, 1};
byte subnet[] = {255, 255, 255, 0};
// Initialize the Ethernet server library
// with the IP address and port you want to use
Server port1(5001);
Server port2(5002);
Server port3(5003);
Server port4(5004);
int Button1State = 0; // Variable for reading the pushbuttons status
int Button2State = 0; // Variable for reading the pushbuttons status
int Button3State = 0; // Variable for reading the pushbuttons status
int Button4State = 0; // Variable for reading the pushbuttons status
int Button1StateOld = 0; //
int Button2StateOld = 0; //
int Button3StateOld = 0; //
int Button4StateOld = 0; //
String sIp="";
String sIpSep=".";
//itoa(int(ip[0]),sIP,10);
//+"."+string(ip[0])+"."+String(ip[0])+"."+String(ip[0]);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
port1.begin();
port2.begin();
port3.begin();
port4.begin();
// initialize the pushbutton pin as an input:
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(0, HIGH); // sets the LED on
digitalWrite(1, HIGH); // sets the LED on
digitalWrite(2, HIGH); // sets the LED on
digitalWrite(3, HIGH); // sets the LED on
}
void loop()
{
// Controllo su porta1 --------------------------------------
Button1State = digitalRead(0);
if (Button1State != Button1StateOld)
{
if (Button1State == HIGH)
{
port1.write("Conttato 1 On");
}
else
{
port1.write("Conttato 1 Off");
}
}
Button1StateOld=Button1State;
// Controllo su porta2 --------------------------------------
Button2State = digitalRead(1);
if (Button2State != Button2StateOld)
{
if (Button2State == HIGH)
{
port2.write("Contatto 2 On");
}
else
{
port2.write("Contatto 2 Off");
}
}
Button2StateOld=Button2State;
// Controllo su porta3 --------------------------------------
Button3State = digitalRead(2);
if (Button3State != Button3StateOld)
{
if (Button3State == HIGH)
{
port3.write("Contatto 3 On");
}
else
{
port3.write("Contatto 3 Off");
}
}
Button3StateOld=Button3State;
// Controllo su porta4 --------------------------------------
Button4State = digitalRead(3);
if (Button4State != Button4StateOld)
{
if (Button4State == HIGH)
{
port4.write("Contatto 4 On");
}
else
{
port4.write("Contatto 4 Off");
}
}
Button4StateOld=Button4State;
delay(10);
}