Hi zusammen,
ich bin gerade dabei eine Steuerung für meine Fussbodenheizung zu bauen. Dabei soll ein Atmega dem anderen über eine Ethernet Verbindung sagen wann er das Relais ein - bzw. ausschalten soll.
Aus der EtherCard.h Library habe ich die beiden Beispiele WebServer und WebClient ein wenig angepasst, damit der Erste Atmega 328 , der als Webserver läuft, als Status 1 oder 0 auf seiner HTML Website anzeigt.
Nur leider scheitere ich gerade daran, diese Info auf dem 2. Atmega auszuwerten und in eine variable (integer oder ähnliches) zu schreiben. Auf der Seriellen Konsole wird mir der Website Inhalt schon richtig ausgegeben.
(HTTP/1.0 503 Service Unavailable
Content-Type: text/html
Retry-After: 600
Vielleicht könnt ihr mir ja helfen
Vielen Dank und viele Grüße
Peter
anbei noch der Code vom Client:
// Demo using DHCP and DNS to perform a web client request.
// 2011-06-08 <jc@wippler.nl>
//
// License: GPLv2
#include <EtherCard.h>
char URL;
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x78,0x69,0x69,0x2D,0x30,0x37 };
byte Ethernet::buffer[700];
static uint32_t timer;
const char website[] PROGMEM = "192.168.10.199";
// called when the client request is complete
static void my_callback (byte status, word off, word len) {
Serial.println(">>>");
Ethernet::buffer[off+300] = 0;
Serial.print((const char*) Ethernet::buffer + off);
Serial.println("...");
}
void setup () {
Serial.begin(57600);
Serial.println(F("\n[webClient]"));
// Change 'SS' to your Slave Select pin, if you arn't using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, 8) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
#if 1
// if website is a string containing an IP address instead of a domain name,
// then use it directly. Note: the string can not be in PROGMEM.
char websiteIP[] = "http://192.168.10.199/";
ether.parseIp(ether.hisip, websiteIP);
#else
// or provide a numeric IP address instead of a string
byte hisip[] = {http://192.168.10.199/};
ether.copyIp(ether.hisip, hisip);
#endif
ether.printIp("SRV: ", ether.hisip);
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 5000;
Serial.println();
Serial.print("<<< REQ ");
ether.browseUrl(PSTR("/foo/"), "bar", website, my_callback);
Serial.println();
Serial.println("UUUU");
}
}
und vom Server:
#include <EtherCard.h>
#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)
// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,10,199 };
// gateway ip address
static byte gwip[] = { 192,168,10,1 };
// LED to control output
int ledPin10 = 7;
byte Ethernet::buffer[700];
char const page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Sers"
"</title></head>"
"<body>"
"Status: 1
"
"The syntax: http://192.168.0.XX/?LED10=OFF or ON"
"</em></p>"
"</body>"
"</html>"
;
void setup () {
pinMode(ledPin10, OUTPUT);
Serial.begin(9600);
Serial.println("Trying to get an IP...");
Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac, 8) == 0)
{
Serial.println( "Failed to access Ethernet controller");
}
else
{
Serial.println("Ethernet controller access: OK");
}
;
#if STATIC
Serial.println( "Getting static IP.");
if (!ether.staticSetup(myip, gwip)){
Serial.println( "could not get a static IP");
blinkLed(); // blink forever to indicate a problem
}
#else
Serial.println("Setting up DHCP");
if (!ether.dhcpSetup()){
Serial.println( "DHCP failed");
blinkLed(); // blink forever to indicate a problem
}
#endif
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
// IF LED10=ON turn it ON
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=ON") != 0) {
Serial.println("Received ON command");
digitalWrite(ledPin10, HIGH);
}
// IF LED10=OFF turn it OFF
if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=OFF") != 0) {
Serial.println("Received OFF command");
digitalWrite(ledPin10, LOW);
}
// show some data to the user
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
void blinkLed(){
while (true){
digitalWrite(ledPin10, HIGH);
delay(500);
digitalWrite(ledPin10, LOW);
delay(500);
}
}
