Ora dovrei fare un'altra cosa ma ho un piccolo problema...dovrei far si che il mio arduino uno che lavora da client si comporti da server con la porta telnet e venga configurato magari questo arduino si connette con diversi IP ogni pulsante connesso ad arduino client va a comandare dei server con IP differenti sulla rete...dovrei vedere su tera term o putty la configurazione degli ip delle subnet e delle maschere...per maschere si intenderebbe "10101000" dove gli 1 indica su quali relay di quel server il mio client va ad agire riesco a visualizzare tutto ok...solo che non vedo la mask in configurazione binaria posto di seguito lo sketch
#include <SPI.h> // IMPORTA LIBRERIA SPI
#include <Ethernet.h> // IMPORTA LIBRERIA ETHERNET
#define BUTTONON 7 // PIN7 INPUT = BUTTON
#define BUTTONOFF 6 // PIN7 INPUT = BUTTON
#define dimBuff 36 //length of longest command string plus two spaces for CR + LF
char textBuff[dimBuff]; //someplace to put received text
int ts1st = HIGH; // ts1st LEGGE E MEMORIZZA IL LIVELLO DI BUTTON
int stato1 = HIGH; // stato1 MANTIENE LO STATO DI BUTTON
int ts2st = HIGH; // ts2st LEGGE E MEMORIZZA IL LIVELLO DI BUTTON
int stato2 = HIGH; // stato2 MANTIENE LO STATO DI BUTTON
int i = 0; // i CONTA LE VOLTE DI BUTTON ON PUSH
int j = 0; // j CONTA LE VOLTE DI BUTTON OFF PUSH
boolean conf = false;
unsigned long timeOfLastActivity;
int charsReceived = 0;
byte serverA[] = { 10, 10, 11, 17 };
byte serverB[] = { 10, 10, 11, 4 };
byte serverC[] = { 10, 10, 11, 5 };
byte serverD[] = { 10, 10, 11, 7 };
int mask = 4; //se gli passo 4 mi si vede 100 invece io vorrei vedere 00000100 se gli passo 00000100 mi si vede 10000000 p.s. l'ho passato anche come byte mask = 00000100; forse devo mettere [] ?
int Aserver1 = 10;
int Aserver2 = 10;
int Aserver3 = 11;
int Aserver4 = 17;
int subnet1 = 255;
int subnet2 = 255;
int subnet3 = 255;
int subnet4 = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x47 }; // MAC ETHERNET SHIELD
byte ip[] = {
10, 10, 11, 45 }; // IP CLIENT
byte server1[] = {
10, 10, 11, 17 }; // IP SERVER1
byte server2[] = {
10, 10, 11, 18 }; // IP SERVER2
byte gateway[] = {
10, 10, 11, 1 }; // INDIRIZZO ROUTER GATEWAY
byte subnet[] = {
255, 255, 255, 0 }; // SUBNET
EthernetClient client;
EthernetServer server = EthernetServer(23); // Telnet porta 23
void setup() {
Ethernet.begin(mac, ip, gateway, subnet); // INIZIALIZZA ETHERNET
Serial.begin(9600); // INIZIALIZZA LA SERIALE
pinMode(BUTTONON, INPUT); // IMPOSTA IL BUTTONON COME INPUT
pinMode(BUTTONOFF, INPUT); // IMPOSTA IL BUTTONOFF COME INPUT
}
void loop() {
if (server.available() && !conf ){
conf = true;
client = server.available();
client.println("\nConfigurazione Arduino Client");
client.println("\nPremere SHOW CNF per ottenere le CONFIGURAZIONI IP & MASK ");
printPrompt();
}
if (client.connected() && client.available()) getReceivedText();
ts1st = digitalRead(BUTTONON); // LEGGE E MEMORIZZA IL VALORE DI BUTTONON
ts2st = digitalRead(BUTTONOFF); // LEGGE E MEMORIZZA IL VALORE DI BUTTONOFF
if ( ts1st == HIGH && stato1 == HIGH );
else{
if ( ts1st == LOW && stato1 == HIGH ){ // PULSANTE PUSH FASE ON
stato1 = LOW; // IMPOSTA IL NUOVO STATO = LOW
delay(150); // RITARDO 150ms
i++; // INCREMENTA CONTEGGIO PUSH
Serial.print(" IL BOTTONE ON E' STATO PREMUTO "); // STAMPA IL NUMERO DI PUSH EFFETTUATI
Serial.print(i);
Serial.print(" VOLTE");
Serial.println();
if (client.connect(server1, 23)){
client.print("ON ");
client.stop();
}
else{
Serial.println("Connessione ON fallita"); // ALTRIMENTI
// STAMPA SU COM3 "Connessione ON fallita"
client.stop(); // CHIUDI CONNESSIONE
}
}
if ( ts1st == HIGH && stato1 == LOW ) // POSIZIONE DI RIPOSO FASE OFF
stato1 = HIGH; // AGGIORNA IL NUOVO STATO DI BUTTONON = HIGH
}
if ( ts2st == HIGH && stato2 == HIGH );
else{
if ( ts2st == LOW && stato2 == HIGH ){ // PULSANTE PUSH FASE ON
stato2 = LOW; // IMPOSTA IL NUOVO STATO = LOW
delay(150); // RITARDO 150ms
j++; // INCREMENTA CONTEGGIO PUSH
Serial.print(" IL BOTTONE OFF E' STATO PREMUTO "); // STAMPA IL NUMERO DI PUSH EFFETTUATI
Serial.print(j);
Serial.print(" VOLTE");
Serial.println();
if (client.connect(server1, 23)){
client.print("OFF");
client.stop();
}
else{
Serial.println("Connessione OFF fallita"); // ALTRIMENTI
// STAMPA SU COM3 "Connessione OFF fallita"
client.stop(); // CHIUDI CONNESSIONE
}
}
if ( ts2st == HIGH && stato2 == LOW ) // POSIZIONE DI RIPOSO FASE OFF
stato2 = HIGH; // AGGIORNA IL NUOVO STATO DI BUTTONOFF = HIGH
}
}
void printPrompt()
{
timeOfLastActivity = millis();
client.flush();
charsReceived = 0; //count of characters received
client.print("\n>");
}
void getReceivedText()
{
char c;
int charsWaiting;
// copy waiting characters into textBuff
//until textBuff full, CR received, or no more characters
charsWaiting = client.available();
do {
c = client.read();
textBuff[charsReceived] = c;
charsReceived++;
charsWaiting--;
}
while(charsReceived <= dimBuff && c != 0x0d && charsWaiting > 0);
//if CR found go look at received text and execute command
if(c == 0x0d) {
parseReceivedText();
// after completing command, print a new prompt
printPrompt();
}
// if textBuff full without reaching a CR, print an error message
if(charsReceived >= dimBuff) {
client.println();
printErrorMessage();
printPrompt();
}
// if textBuff not full and no CR, do nothing else;
// go back to loop until more characters are received
}
void parseReceivedText()
{
// look at first character and decide what to do
switch (textBuff[0]) {
case 'S' : showConfiguration(); break;
case 0x0d : break; //ignore a carriage return
default: printErrorMessage(); break;
}
}
void printErrorMessage()
{
client.println("\nComando sconosciuto !");
}
void showConfiguration()
{
if (textBuff[1] == 'H' && textBuff[2] == 'O' && textBuff[3] == 'W' && textBuff[4] == ' ' && textBuff[5] == 'C' && textBuff[6] == 'N' && textBuff[7] == 'F' ){
client.println();
client.print("\nServer A:");
client.print(" IP : ");
client.print(Aserver1);
client.print(".");
client.print(Aserver2);
client.print(".");
client.print(Aserver3);
client.print(".");
client.println(Aserver4);
client.print(" SUBNET : ");
client.print(subnet1);
client.print(".");
client.print(subnet2);
client.print(".");
client.print(subnet3);
client.print(".");
client.println(subnet4);
client.print(" MASK : ");
client.println(mask,BIN);
}
else
printErrorMessage();
}
si vede sia ip che subnet alla grande ma la mask si vede male.
Grazie