Pilotage relais par interface web arduino uno + shield ethernet + carte 4 relais

Bonjour,

J'en profite pour me présenter, Alexis nouveau sur le forum et dans le milieu de l'arduino basé dans le sud ouest de la France, la ou on dis CHOCOLATINE :slight_smile:

Issus d'un bac électrotechnique, travaillant dans l'alarme et baignant dans le milieu du modélisme radiocommandé depuis plus de 20 ans.

je me plonge petit à petit dans le monde de l'arduino et toute son arborescence en vue de plusieurs projet domotique et surtout par intéressement.

Merci tout d'abord pour cette mine d'informations et j'espère a la longue pouvoir amener ma pierre a l'édifice et aider les débutants comme je le suit actuellement.

Venons en au sujet principale, je cherche à me perfectionner petit a petit dans le domaine en montant les différents projet que je trouve ci et la et en les modifiant par la suite pour me plonger dans la programmation.

je me retrouve bloqué sur un projet en cours d'arduino uno avec shield ethernet et carte 4 relais, j'ai repris le programme de J-M-L qui proposer de piloter une led et de modifier des variables avec ce même matos.
j'ai tenté de le modifier avec mes piètres connaissance en langage de programmation mais après injection je n'arrive a piloter que le relais que j'ai brancher sur la pin 8 (qui correspond a la led dans le programme de J-M-L), mes modifs affichent bien les deux autres relais mais quand je clique dessus sur l'interface web, rien ne se passe.
pourriez vous me dire ou j'ai merdé ou ce que j'ai oublier afin que j'avance un peu dans mon raisonnement ?

Voici le programme de J-M-L modifié par mes soins en pièce jointe.

Merci d'avance pour l'aide apporter dans mon apprentissage :slight_smile:

ethernetShieldDemoalex.ino (8.92 KB)

#include <SPI.h>
#include <Ethernet.h>

const byte SDCARD_PIN = 4;

const byte MacAddress[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
const byte MaxCommand = 30;
char urlCommand[MaxCommand + 1]; // +1 for the trailing '\0'

const unsigned int SERVER_PORT = 8081;
EthernetServer webServer(SERVER_PORT);


// ****************************************************************
// This is for our URL Parser. We are expecting an URL in the form
// of http://domain.com/x=v1,y=v2,z=v3
// x, y and z would be the labels of Interests and v1, v2, v3
// will be valued extracted by the parser
// ****************************************************************
const char * labelsOfInterest[] = {"LED", "relais 1", "relais 2"};
const unsigned int maxLabelsOfInterest = sizeof(labelsOfInterest) / sizeof(char*);
const byte relais1 = 4;
const byte relais2 = 6;
const byte LEDPin = 8;

void parseCommand()
{
  char * item;
  boolean labelFound;
  long int labelValue;
  int labelIndex;
  int commandLength = strlen(urlCommand);

  item = strtok(urlCommand, "=");

  while (item) {
    labelFound = false;
    for (int i = 0; i < maxLabelsOfInterest; ++i) {
      if (!strcmp(labelsOfInterest[i], item)) {
        item = strtok (NULL, ",");
        if (item != NULL) {
          labelFound = true;
          labelIndex = i;
          // then parse the value
          // we expect integers
          labelValue = atol(item);
          break; // ends the for loop as we found a label
        } // end if we had a value for the label
      } // end string compare
    } // end for

    // ****************************************************************
    // THIS IS WHERE YOU PERFORM YOUR ACTIONS FOR EACH VARIABLE FOUND
    // ****************************************************************

    if (labelFound) {
      switch (labelIndex) {
        case 0: // LED
          digitalWrite(LEDPin, (byte) labelValue);
          break;
        case 1: // relais1
          digitalWrite(relais1, (byte) labelValue);
          break;
        case 2: // relais2
          digitalWrite(relais2, (byte) labelValue);
          break;
      }
      // ****************************************************************
    } else {
      item = strtok (NULL, ","); // skip this label value, not recognized
    }

    item = strtok (NULL, "="); // got to next one - will modify urlCommand
  }
}



void sendHTTPResponse(EthernetClient& client)
{
  // send a standard http response header
  client.println(F("HTTP/1.1 200 OK"));
  client.println(F("Content-Type: text/html"));
  client.println(F("Connection: close"));  // the connection will be closed after completion of the response
  // client.println(F("Refresh: 30"));  // refresh the page automatically every 30 sec
  client.println();
  client.println(F("<!DOCTYPE HTML>"));

  // ****************************************************************
  // THIS IS WHERE YOU BUILD YOUR HTML PAGE
  // ****************************************************************

  client.println(F("<html><head><style>body {text-align:center;}</style></head>")); // body will be centered
  client.println(F("<body bgcolor='#F60707'>")); // page backgroud color

  // relais 1
  if (digitalRead(relais1) == LOW)
    client.print(F("<button onclick=\"location.href='/relais 1=1'\" type='button'>relais 1 ON</button>"));
  else
    client.print(F("<button onclick=\"location.href='/relais 1=0'\" type='button'>relais 1 OFF</button>"));


 // LED button
  if (digitalRead(LEDPin) == LOW)
    client.print(F("<button onclick=\"location.href='/LED=1'\" type='button'>LED ON</button>"));
  else
    client.print(F("<button onclick=\"location.href='/LED=0'\" type='button'>LED OFF</button>"));


  // relais 2
  if (digitalRead(relais2) == LOW)
    client.print(F("<button onclick=\"location.href='/relais 2=1'\" type='button'>relais 2 ON</button>"));
  else
    client.print(F("<button onclick=\"location.href='/relais 2=0'\" type='button'>relais 2 OFF</button>"));

  
  client.println(F("</center></body></html>"));
}



void handleCommand(EthernetClient& client)
{
  // the command is in urlCommand
  if (strlen(urlCommand) != 0) parseCommand();
  sendHTTPResponse(client);
}


void handleClient()
{
  boolean urlCommandFound = false;
  char httpHeader[MaxCommand + 1];
  byte httpHeaderIndex = 0;
  httpHeader[0] = '\0';
  urlCommand[0] = '\0';

  EthernetClient client = webServer.available();     // listen for incoming clients

  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // Serial.print(c); // if you want to see the HTTP request
        if (!urlCommandFound) {
          httpHeader[httpHeaderIndex++] = c;
          httpHeader[httpHeaderIndex] = '\0';
          if (httpHeaderIndex > MaxCommand - 1) httpHeaderIndex = MaxCommand - 1;
        }

        if (c == '\n' && currentLineIsBlank) {  // an http request ends with a blank line
          handleCommand(client);
          delay(5); // give the web browser time to receive the data
          client.stop(); // close the connection:
          break;
        }

        if (c == '\n') {
          currentLineIsBlank = true; // starting a new line
          if (!strncmp("GET / ", httpHeader, 5)) {
            strcpy(urlCommand, (httpHeader + 5)); // get rid of the "GET / "
            char * firstSpacePtr = strchr(urlCommand, ' ');
            if (firstSpacePtr) *firstSpacePtr = '\0'; // get rid of the " HTTP / 1.1 etc
            urlCommandFound = true;
          } else {
            httpHeaderIndex = 0;
            httpHeader[0] = '\0';
          }
        } else if (c != '\r') {
          currentLineIsBlank = false; // new character on the current line, ignore '\r'
        }
      }
    }
  }
}

void printServerAddress()
{
  Serial.print(F("connect to your arduino with http://"));
  Serial.print(Ethernet.localIP());
  if (SERVER_PORT != 8081) { // 80 is the default port so not needed in the URL
    Serial.print(F(":"));
    Serial.println(SERVER_PORT);
  }
}


boolean renewDHCPLease()
{
  boolean ok = true;
  switch (Ethernet.maintain())
  {
    case 1:
      //renewed fail
      Serial.println(F("Error: renewed fail"));
      ok = false;
      break;

    case 2:
      //renewed success
      Serial.println(F("Renewed success"));
      printServerAddress();
      break;

    case 3:
      //rebind fail
      ok = false;
      Serial.println(F("Error: rebind fail"));
      break;

    case 4:
      //rebind success
      Serial.println(F("Rebind success"));
      printServerAddress();
      break;

    default:
      //nothing happened
      break;
  }
  return ok;
}

// -------------------------------------

void setup() {
  Serial.begin(115200);

  // disable SD Card
  pinMode(SDCARD_PIN, OUTPUT);
  digitalWrite(SDCARD_PIN, HIGH);

  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, HIGH);

  pinMode(relais1, OUTPUT);
  digitalWrite(relais1, HIGH);

  pinMode(relais2, OUTPUT);
  digitalWrite(relais2, HIGH);

  if (Ethernet.begin(MacAddress) == 0) {
    Serial.println(F("NO DHCP"));
    while (true); // stop here
  }

  webServer.begin();
  printServerAddress();
}


void loop() {
  if (renewDHCPLease()) { // we still have an IP address
    handleClient();
  }
 

}