EthernetRelayNew.ino
À propos du fichier
- Type de fichier
- Fichier INO de 5 Ko (text/html)
- Confidentialité
- Fichier public, envoyé le 10 mai 2016 à 14:46, depuis l'adresse IP 195.83.x.x (France)
- Sécurité
- Ne contient aucun Virus ou Malware connus - Dernière vérification: 12 heures
- Statistiques
- La présente page de téléchargement a été vue 1433 fois depuis l'envoi du fichier
- Page de téléchargement
-
Aperçu du fichier
/*
---------------------------------------------------------
- Web control relay with Arduino -
---------------------------------------------------------
Ethernet module "ENC28J60" is connected to the Arduino :
VCC - 3.3V Arduino
GND - GND Arduino
SCK - Pin 13 Arduino
SO - Pin 12 Arduino
SI - Pin 11 Arduino
CS - Pin 10 Arduino
The relay is connected to the Arduino so:
VCC - 5V Arduino
In1 - Pin 2 Arduino
In2 - Pin 3 Arduino
...
In7 - Pin 8 Arduino
In8 - Pin 9 Arduino
GND - GND Arduino
---------------------------------------------------------
*/
#include <EtherCard.h>
// Define number of relays (MAX 8)
#define NB_PINS 8
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,0,200 };
// Declare PinStatus and LedPins before BufferFiller declaration
boolean PinStatus[NB_PINS];
int LedPins[NB_PINS];
// buffer[500] for 4 relays
// increase size buffer for more relays
byte Ethernet::buffer[800];
BufferFiller bfill;
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
//---------------------------------------------------------
// Web page
void homePage()
{
bfill.emit_p(PSTR("$F"
"<title>ArduinoPIN Webserver</title>"
"<p align=center><font size=10>"
),
http_OK);
for(int i = 0; i <= NB_PINS-1; i++)
{
bfill.emit_p(PSTR(
"R$D: <a href='?P$D=$F' style=color:$F>$F</a><br />"
),
i+1,
i+1,
PinStatus[i]?PSTR("off"):PSTR("on"),
PinStatus[i]?PSTR("green"):PSTR("red"),
PinStatus[i]?PSTR("ON"):PSTR("OFF"));
}
bfill.emit_p(PSTR(
"</font></p>"
));
}
//---------------------------------------------------------
void setup()
{
Serial.begin(9600);
Serial.print("Setup ");
for(int i = 0; i <= NB_PINS-1; i++)
{
// Pins on Arduino begin from PIN 2 to PIN 9
LedPins[i] = i+2;
}
// if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
// and change it (CS-pin) to pin 10.
Serial.println(F("\nConnecting..."));
if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0);
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup());
Serial.println(F("DHCP failed"));
Serial.print("Connected\n");
ether.printIp("My Router IP: ", ether.myip);
//ether.staticSetup(myip);
ether.printIp("My SET IP: ", ether.myip);
// Debug
Serial.print("\n");
Serial.print("|-------------------------------------------|\n");
Serial.print("| Actions | LedPin / PinStatus |\n");
Serial.print("|-------------------------------------------|\n");
Serial.print("| Init | ");
for(int i = 0; i <= NB_PINS-1; i++)
{
// Initialization of pin mode and pin status
pinMode(LedPins[i],OUTPUT);
digitalWrite (LedPins[i],HIGH);
PinStatus[i]=false;
// Debug
Serial.print(LedPins[i]);
Serial.print("/");
Serial.print(PinStatus[i]);
Serial.print(" ");
}
Serial.print("\n");
}
//---------------------------------------------------------
void loop()
{
delay(1);
word len = ether.packetReceive(); // check for ethernet packet
word pos = ether.packetLoop(len); // check for tcp packet
if (pos) {
bfill = ether.tcpOffset();
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
bfill.emit_p(http_Unauthorized);
}
else {
data += 5;
int relay=data[2] - 48; // ASCII code for the character '0' is decimal 48
if (data[0] == ' ') {
// If change on home page, execute function.
homePage();
//Debug
for (int i = 0; i <= NB_PINS-1; i++)digitalWrite(LedPins[i],!PinStatus[i]);
Serial.print("| F5 | ");
} else if( relay>0 && relay<=NB_PINS ) {
data += 4;
if (strncmp("on ", data, 2) == 0) {
PinStatus[relay-1] = true;
bfill.emit_p(http_Found);
// Debug
Serial.print("| R");
Serial.print(relay-1);
Serial.print(" ON | ");
} else if (strncmp("off ", data, 3) == 0) {
PinStatus[relay-1] = false;
bfill.emit_p(http_Found);
// Debug
Serial.print("| R");
Serial.print(relay-1);
Serial.print(" OFF | ");
}
}
else {
// Page not found
bfill.emit_p(http_Unauthorized);
}
// Debug
for(int i = 0; i <= NB_PINS-1; i++)
{
Serial.print(LedPins[i]);
Serial.print("/");
Serial.print(PinStatus[i]);
Serial.print(" ");
}
Serial.print("\n");
}
ether.httpServerReply(bfill.position()); // send http response
}
}
Partager le fichier EthernetRelayNew.ino sur le Web et les réseaux sociaux:
Télécharger le fichier EthernetRelayNew.ino
Télécharger EthernetRelayNew.ino