Je vous expose tout d'abord mon projet : je souhaiterai pouvoir commander une PWM via une page internet, en gros depuis mon smartphone ou mon ordinateur j'accède à cette page internet et je peux activer ou non la PWM (réglée en amont).
J'ai quelques connaissances en html et en prog arduino, mais pas avec la liaison ethernet..
Ma première question est donc : pour réaliser ma fonction est ce que mon arduino devra être client ou serveur ? Je pense que ça doit être serveur mais j'ai un doute..
La page internet devra-t-elle être stockée sur mon arduino ou non ? J'ai une micro sd sur le shield si besoin.
Si vous avez un petit bout de code qui pourrait m'aider à démarrer, ce serait sympa
D'accord merci, ça confirme ce que je pensais. Mais j'arrive pas à avancer sur mon code, j'ai cherché des exemples avec une fonction pour allumer une led mais ça bug..
Si quelqu'un a un bout de code ce serait parfait ! Merci
#include <SPI.h> // Libairie communicatin SPI avec le Shield éthernet #include <Ethernet.h> // Librairie communication Ethernet sur un réseau local
//On donne l’adresse MAC au shield Ethernet (ne doit pas être identique à une autre adresse MAC présente sur votre réseau)
byte MAC = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 }; // Adresse MAC du shield Ethernet
//On affecte l’adresse IP du Shield Ethernet
IPAddress IP(192,168,0, 25); // Adresse IP donné au Shield Ethernet
// On attribue la fonction serveur au Shield Ethernet sur le port 80
EthernetServer server(80); // Le Shield devient un serveur sur le port 80 (port HTTP)
void setup()
{
//Lancement connexion Ethernet avec l’adresse MAC et l’adresse IP
Serial.begin(9600); //Ethernet.begin(MAC, IP);
Ethernet.begin(MAC); //serveurHTTP.begin();
server.begin();
Serial.println("l adresse est : ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(“new client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println(“Refresh: 5”); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print(“analog input “);
client.print(analogChannel);
client.print(” is “);
client.print(sensorReading);
client.println(”
“);
}
client.println(””);
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println(“client disconnected”);
}
}
Voilà mon code, c'est globalement le code de test préfait par le logiciel.
Le Problème c'est que le programme ne m'affiche pas mon adresse, cela devrait s'afficher via ces deux lignes :
Serial.println("l adresse est : ");
Serial.println(Ethernet.localIP());
Désolé je connais pas les balises codes..
Quand je tape l'adresse dans un navigateur ça fait rien du tout, il met page indisponible..
J'essaye avec ta méthode dès demain !
J'ai essayé ta méthode Fredericzim, malheureusement le compilateur me retourne une erreur :
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:78:12: note: no known conversion for argument 1 from 'byte [4] {aka unsigned char [4]}' to 'long unsigned int'
call of overloaded 'println(byte [4])' is ambiguous
#include <SPI.h> // Libairie communicatin SPI avec le Shield éthernet
#include <Ethernet.h> // Librairie communication Ethernet sur un réseau local
//On donne l'adresse MAC au shield Ethernet (ne doit pas être identique à une autre adresse MAC présente sur votre réseau)
byte MAC[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 }; // Adresse MAC du shield Ethernet
//On affecte l'adresse IP du Shield Ethernet
IPAddress IP(192,168,0, 25); // Adresse IP donné au Shield Ethernet
// On attribue la fonction serveur au Shield Ethernet sur le port 80
EthernetServer serveurHTTP(80); // Le Shield devient un serveur sur le port 80 (port HTTP)
void setup()
{
//Lancement connexion Ethernet avec l'adresse MAC et l'adresse IP
Ethernet.begin(MAC, IP);
serveurHTTP.begin();
}
void loop()
{
}
J’obtient ceci via l’invite de commande le fichier joint
Je viens de trouver le problème... J'avais une carte sd insérée dans mon shield et du coup ça fonctionait pas.. Pourquoi je ne sais pas mais en l'enlevant ça fonctionne ! Si le programme tient sans sd tant mieux du coup !
J’ai encore une petite question, je connais pas trop le format pour écrire du html en arduino.
Voici mon code utilisé pour le moment :
/*
pwm project 11 07 2015
Arduino with Ethernet Shield
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int pwm = 3;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 25 }; // ip de l'arduino
//byte gateway[] = { 192, 168, 0, 254 }; // ip de ma box
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(pwm, OUTPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at "); //pour vérification
Serial.println(Ethernet.localIP());
}
void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
client.println("<TITLE>Distributeur plume</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Nourris ton petit chat !</H1>");
client.println("<hr />");
client.println("
");
client.println("<H2>Fais ton choix gros !</H2>");
client.println("
");
client.println("<a href=\"/?ration\"\">Donner une ration</a>");
client.println("<a href=\"/?clock\"\">Non connectee</a>
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?ration") >0){
analogWrite(pwm,255);
delay(2000);
analogWrite(pwm,0);
//envoyer un retour sur la page
}
if (readString.indexOf("?clock") >0){
analogWrite(pwm, 0);
}
//clearing string for next read
readString="";
}
}
}
}
}
ça fonctionne très bien mais je souhaiterai pouvoir avoir un retour lorsque la pwm est activée, est-il possible d’afficher quelque chose sur la page à la suite de mon delay, à l’endroit où j’ai écris : " //envoyer un retour sur la page " . Je ne peux pas utiliser client.println car le client est déconnecté, je ne trouve pas la solution…