Problemi con la libreria Wire

Salve a tutti,
mi chiamo Strabosky.. Seguo il forum da moltissimo tempo e mi appassiona molto il mondo di Arduino. Volevo porvi il mio quesito: sto facendo un progetto per una casa domotica ed utilizzo un arduino master e uno slave. Il funzionamento è questo: il master gestisce tutti gli output e le informazioni da internet, lo slave gestisce solo gli input fisici da bottone (interruttori di casa). Quando viene azionato un interruttore lo slave invia al master una stringa (ad esempio "Luce1Accesa") e il master accende la luce e invia tramite metodo GET ad una pagina php l'informazione al database.

Il problema di cui volevo parlarvi si verifica in maniera casuale e blocca completamente l'Arduino master, per sbloccarlo è inevitabile il reset.. Non so se è un problema della libreria Wire o vi è un errore nel codice.

Questi sono il codice del master e dello slave (non fate caso al codice molto disordinato):

//SERVER---------------------------------------------
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 50);
EthernetServer server(8082);
//Invio dati DB----------------------------------------
char myserver[] = "192.168.0.51";
EthernetClient cli;
String stringa;
String stringa2 = "";

//Cucina-----------------------------------------------
int S1stato[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int S1pin[9] = {22, 23, 24, 25, 26, 27, 28, 29, 30}; 
/* Luce, Luce, Luce, Luce, Tapparella, Tapparella, Tapparella, Tapparella, Elettrovalvola */

//Generale---------------------------------------------
String istruzioni[50] = {"S1L1=1", "S1L1=2", "S1L2=1", "S1L2=2", "S1L3=1", "S1L3=2", "S1L4=1", "S1L4=2", "S1T1=1", "S1T1=2", "S1T1=3", "S1T2=1", "S1T2=2", "S1T2=3"};
String comandotasto = "";
String comando = "";
int i;

void setup() 
{
  //Indirizzo Arduino------------------------------
  Wire.begin(1);
  //Leggo dati-------------------------------------
  Wire.onReceive(receiveEvent);

  Ethernet.begin(mac, ip);
  server.begin();
  //Ethernet delay
  W5100.setRetransmissionCount(1);
  //Stanza1
  for (i = 0; i < 9; i++)
  {
    pinMode(S1pin[i], OUTPUT);
  }
  pinMode(13, OUTPUT);
  //Stanza2

  //Stanza3

  //Stanza4

  //Stanza5
}
void receiveEvent(int howMany) 
{
  while (Wire.available() > 0) 
  {
    char c = Wire.read(); 
    comandotasto.concat(c);
  }
  if (comandotasto != "")
  {
    TastiFisici();
    //Lettura_sensori------------------------------------
    Sensori();
    //---------------------------------------------------
    inviodb();
  }
  comandotasto = "";
}
void loop() 
{
  internet();
  delay(10);
}
void attivazione()
{
  digitalWrite(13, S1stato[0]);
}
void internet()
{
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        comando.concat(c);
        for (i = 0; i < 14; i++)
        {
          if (comando.indexOf(istruzioni[i]) > -1)
          {
            IstruzioniInternet();
            //Risposta_arduino-------------------------------------
            client.println("HTTP/1.1 200 OK");
            client.println("Access-Control-Allow-Origin: *");
            client.println("Cache-Control: no-cache, no-store, max-age=0, must-revalidate");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  
            client.println();
            //-----------------------------------------------------
            client.stop();
            comando = "";
            break;
          }
        }
      }
    }
    delay(5);
    inviodb();
  }
}

void inviodb()
{
  attivazione();
  if (cli.connect(myserver, 42511)) 
  {
    cli.print("GET /casa/update.php?");
    cli.print("stanze=");
    cli.print(stringa);
    cli.println(" HTTP/1.1");
    cli.print("Host: ");
    cli.println(myserver);
    cli.println( "Connection: close" );
    cli.println();
    cli.println();
    cli.stop();
  }
  stringa = "";
}
void IstruzioniInternet()
{
  //Luce1-----------------------------------------
  if (comando.indexOf("S1L1=1") > -1)
  {
    stringa = "S1;L1;A;";
    S1stato[0] = 1;
  }
  if (comando.indexOf("S1L1=2") > -1)
  {
    stringa = "S1;L1;S;";
    S1stato[0] = 0;
  }
  //Tapparella1-----------------------------------------
  if (comando.indexOf("S1T1=1") > -1) //-Sale
  {
    stringa = "S1;T1;SA;";
    S1stato[4] = 1;
    S1stato[5] = 0;
  }
  if (comando.indexOf("S1T1=2") > -1) //-Scende
  {
    stringa = "S1;T1;SC;";
    S1stato[4] = 0;
    S1stato[5] = 1;
  }
  if (comando.indexOf("S1T1=3") > -1) //-Ferma
  {
    stringa = "S1;T1;ST;";
    S1stato[4] = 0;
    S1stato[5] = 0;
  }
 
}
void TastiFisici()
{
  //Luce1-----------------------------------------
  if (comandotasto == "S1L1")
  {
    if (S1stato[0] == 0)
    {
      S1stato[0] = 1;
      stringa = "S1;L1;A;";
    }
    else
    {
      S1stato[0] = 0;
      stringa = "S1;L1;S;";
    }
  }
  //Luce2-----------------------------------------
  if (comandotasto == "S1L2")
  {
    if (S1stato[1] == 0)
    {
      S1stato[1] = 1;
      stringa = "S1;L2;A;";
    }
    else
    {
      S1stato[1] = 0;
      stringa = "S1;L2;S;";
    }
  }
  //Tapparella1-----------------------------------------
  if (comandotasto == "S1T1SA")
  {
    if (S1stato[4] == 0 && S1stato[5] == 0)
    {
      S1stato[4] = 1;
      stringa = "S1;T1;SA;";
    }
    else
    {
      S1stato[4] = 0;
      S1stato[5] = 0;
      stringa = "S1;T1;ST;";
    }
  }
  if (comandotasto == "S1T1SC")
  {
    if (S1stato[4] == 0 && S1stato[5] == 0)
    {
      S1stato[5] = 1;
      stringa = "S1;T1;SC;";
    }
    else
    {
      S1stato[4] = 0;
      S1stato[5] = 0;
      stringa = "S1;T1;ST;";
    }
  }
  //Tapparella2-----------------------------------------
  if (comandotasto == "S1T2SA")
  {
    if (S1stato[6] == 0 && S1stato[7] == 0)
    {
      S1stato[6] = 1;
      stringa = "S1;T2;SA;";
    }
    else
    {
      S1stato[6] = 0;
      S1stato[7] = 0;
      stringa = "S1;T2;ST;";
    }
  }
  if (comandotasto == "S1T2SC")
  {
    if (S1stato[6] == 0 && S1stato[7] == 0)
    {
      S1stato[7] = 1;
      stringa = "S1;T2;SC;";
    }
    else
    {
      S1stato[6] = 0;
      S1stato[7] = 0;
      stringa = "S1;T2;ST;";
    }
  }
}
void Sensori()
{
  if ((comandotasto.indexOf("T") > -1 && comandotasto.indexOf("U")) || comandotasto.indexOf("GM") > -1 || comandotasto.indexOf("GC") > -1)
  {
    stringa = comandotasto;
  }
}



//CLIENT------------------------------------------
#include <Wire.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 6
#define DHT11_PIN2 7
int contatore = 0;
//-------------------------------------------------

void setup() 
{
  Wire.begin(2);
  // put your setup code here, to run once:
  pinMode(22, INPUT);
  pinMode(23, INPUT);
  pinMode(28, INPUT);
}
int fermo[2] = {0, 0};

int S1pin[10] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
int S2pin[5] = {32, 33, 34, 35, 36};

String temp;
String temp2;

void loop() 
{
  //Inizio trasmissione con il server
  Wire.beginTransmission(1);
  S1L1();
  /*S1GAS();*/
  S1T1();
  S2T1();
  //Chiudo la trasmissione
  Wire.endTransmission();
  //Tempo attesa
  delay(100);
}
void S1L1()
{
  if (digitalRead(22) == HIGH && fermo[0] == 0)
  {
    Wire.write("S1L1");
    fermo[0] = 1;
  }
  if (digitalRead(22) == LOW && fermo[0] == 1)
  {
    fermo[0] = 0;
  }
}

void S1T1()
{
  contatore += 100;
  if (contatore == 10000)
  {
    int chk;
    chk = DHT.read(DHT11_PIN);

    temp = "S1;TE;"+String(DHT.temperature)+";U;"+String(DHT.humidity)+";";
    temp += temp2;
    char temperatura[100];
    temp.toCharArray(temperatura, 100);
    
    Wire.write(temperatura);
    contatore = 0;
  }
}
void S2T1()
{
  if (contatore == 9000)
  {
    int chk2;
    chk2 = DHT.read(DHT11_PIN2);

    temp2 = "S2;TE;"+String(DHT.temperature)+";U;"+String(DHT.humidity)+";";
  }
}

Ciao,
segui il forum da moltissimo tempo e non ti sei accorto che ... al primo post ci si deve presentare QUI (spiegando bene quali conoscenze hai di elettronica e di programmazione) e leggere con attenzione il REGOLAMENTO ? :wink:

Guglielmo

gpb01:
Ciao,
segui il forum da moltissimo tempo e non ti sei accorto che ... al primo post ci si deve presentare QUI (spiegando bene quali conoscenze hai di elettronica e di programmazione) e leggere con attenzione il REGOLAMENTO ? :wink:

Guglielmo

Scusami, preso da un momento di sconforto sono corso a scrivere qui il post.. Ora mi presento nell'apposito topic.

strabosky.

Up