Ena zanimivih idej za projekt je upravljanje relejev prek spleta. Spodaj je na voljo koda, ki deluje na arduinotu in ethernet modulu ENC28J60.

Koda je v originalu spisana za Nanode, ki ima ta čip že vgrajen na plošči, koda pa je na voljo tukaj: [Brainwagon.org] Web-based control of an RGB LED via the Nanode …

Koda je tukaj predvsem za to, da jo ne zgubim, če pa komu koristi, pa toliko bolje.
Releje se vklaplja in izklaplja preko spletnega vmesnika. V brskalnik je treba vnesti link

http://<ip arduinota>/<id pina>/0 do 255

#include <EtherCard.h>

//
// remote by Mark VandeWettering
//
// code changed for arduino by David Bajda [d4vid]
// compiled in arduino 1.6.11
// a reimplementation of the "RESTduino" idea, first published at:
// http://jasongullickson.posterous.com/restduino-arduino-hacking-for-the-rest-of-us
//
// I decided to rewrite this application because:
// 1. originally, it was written for the Wiznet based Ethernet Shield, but I have
// mostly Nanodes, based upon the ENC28J60
// 2. It was ported to the Ethershield library by Andrew Lindsay, but Andy has decided
// that he's decided not to continue development of that library, deferring to the
// EtherCard library being developed by JeeLabs
// 3. I thought there was a lot of cool additions that could be made! Okay, I haven't
// implemented any at the moment, but I will!
//
#define DEBUG 1
#define BUFFER_SIZE (900)

byte Ethernet::buffer[BUFFER_SIZE] ;
BufferFiller bfill;

const char okHeader[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"\r\n" ;

const char responseHeader[] PROGMEM =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Access-Control-Allow-Origin: *\r\n"
"\r\n" ;

void
homepage(BufferFiller&amp; buf)
{
buf.emit_p(PSTR(
"$F"
"" ""
"webserver remote"
"
<style type=\"text/css\">"<br />
    "body   { width: 640px ; }"<br />
    "</style>

"
"

" ""
"
<h1>webserver remote</h1>
"
"

"
"This webserver is running on a <a href="\&quot;http://nanode.eu\&quot;">Nanode</a>, an "
"Arduino-compatible microcontroller with web connectivity. It uses the &lt;a" "href=\"https://github.com/jcw/ethercard\"&gt;EtherCard library written by "
"<a href="\&quot;http://jeelabs.net/projects/cafe/wiki/EtherCard\&quot;">JeeLabs</a>. "
"Inspired by Jason Gullickson's <a href="\&quot;http://www.youtube.com/watch?v=X-s2se-34-g\&quot;">RESTduino</a>."
"

"
"

<hr />

"
"

Written by Mark VandeWettering

"
"

" ""
), okHeader) ;
}

&nbsp;

#define CODE_ERROR (-1)
#define CODE_WRITE (0)
#define CODE_READ (1)
#define CODE_ANALOG_WRITE (2)
#define CODE_INDEX (3)

int
cmdparse(const char *cmd, int &amp;pin, int &amp;val)
{
const char *cp = cmd+4 ;

if (*cp++ == '/') {
if (*cp == ' ') {
return CODE_INDEX ;
} else if (isdigit(*cp)) {
pin = atoi(cp) ;
while (isdigit(*cp)) cp++ ;
if (*cp == ' ') {
return CODE_READ ;
} else if (*cp == '/') {
cp++ ;
if (isdigit(*cp)) {
val = atoi(cp) ;
while (isdigit(*cp)) cp++ ;
if (*cp == ' ')
return CODE_ANALOG_WRITE ;
else
return CODE_ERROR ;
} else if (strncmp(cp, "HIGH", 4) == 0) {
cp += 4 ;
if (*cp == ' ') {
val = 1 ;
return CODE_WRITE ;
} else
return CODE_ERROR ;
} else if (strncmp(cp, "LOW", 3) == 0) {
cp += 3 ;
if (*cp == ' ') {
val = 0 ;
return CODE_WRITE ;
} else
return CODE_ERROR ;
}
} else
return CODE_ERROR ;
} else {
return CODE_ERROR ;
}
} else {
return CODE_ERROR ;
}
}

void
setup()
{
byte mac[6] ;

Serial.begin(9600) ;
Serial.println("\nbrainwagon remote webserver\n") ;

if (ether.begin(sizeof Ethernet::buffer, mac) == 0) {
Serial.println("Failed to access Ethernet controller.") ;
for (;;) ;
}

if (ether.dhcpSetup()) {
ether.printIp("IP: ", ether.myip) ;
ether.printIp("GW: ", ether.gwip) ;
ether.printIp("DNS: ", ether.dnsip) ;
} else {
Serial.println("DHCP failed.\n") ;
for (;;) ;
}
}

void
loop()
{
word len = ether.packetReceive() ;
word pos = ether.packetLoop(len) ;
int pin, val ;

if (pos) {
bfill = ether.tcpOffset() ;
char *data = (char *) Ethernet::buffer + pos ;
Serial.println(data) ;
switch (cmdparse(data, pin, val)) {
case CODE_READ:
pinMode(pin, INPUT) ;
bfill.emit_p(PSTR(
"$F"
"{\"$D\" : \"$S\"}"),
responseHeader,
pin, val ? "HIGH" : "LOW") ;
break ;
case CODE_WRITE:
pinMode(pin, OUTPUT) ;
digitalWrite(pin, val ? HIGH : LOW) ;
bfill.emit_p(PSTR(
"$F"), responseHeader) ;
break ;
case CODE_ANALOG_WRITE:
pinMode(pin, OUTPUT) ;
analogWrite(pin, val) ;
bfill.emit_p(PSTR(
"$F"), responseHeader) ;
break ;
case CODE_INDEX:
homepage(bfill) ;
break ;
case CODE_ERROR:
default:
bfill.emit_p(PSTR(
"HTTP/1.0 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"
<h1>404 Not Found</h1>
&nbsp;

")) ;
break ;
}
ether.httpServerReply(bfill.position()) ;
}
}

 


Dodaj odgovor

Vaš e-naslov ne bo objavljen. * označuje zahtevana polja