first commit
This commit is contained in:
163
ATMEGA_ETHERNET_DALLAS/ATMEGA_ETHERNET_DALLAS.ino
Normal file
163
ATMEGA_ETHERNET_DALLAS/ATMEGA_ETHERNET_DALLAS.ino
Normal file
@@ -0,0 +1,163 @@
|
||||
// Ethercard REL example
|
||||
#include <OneWire.h>
|
||||
#include <EEPROM.h>
|
||||
#include <DallasTemperature.h>
|
||||
|
||||
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
|
||||
|
||||
#if STATIC
|
||||
// ethernet interface ip address
|
||||
static byte myip[] = { 192,168,0,200 };
|
||||
// gateway ip address
|
||||
static byte gwip[] = { 192,168,0,1 };
|
||||
#endif
|
||||
|
||||
// ethernet mac address - must be unique on your network
|
||||
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
|
||||
|
||||
|
||||
#define BUFFER_SIZE 500
|
||||
byte Ethernet::buffer[BUFFER_SIZE];
|
||||
BufferFiller bfill;
|
||||
|
||||
#define REL 5 // define REL pin
|
||||
bool RELStatus = false;
|
||||
float temp;
|
||||
char temp_str[10];
|
||||
|
||||
#define ONE_WIRE_BUS 4
|
||||
OneWire oneWire(ONE_WIRE_BUS);
|
||||
DallasTemperature sensors(&oneWire);
|
||||
|
||||
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>";
|
||||
|
||||
void homePage()
|
||||
{
|
||||
String entete = "<meta http-equiv='refresh' content='10'/>"
|
||||
"<title>Temp Server</title>"
|
||||
"<body bgcolor=""#99CCFF"">\n";
|
||||
|
||||
String table = entete + "<table>";
|
||||
table += "<TR><TD>Pin</TD><TD>Value</TD></TR>\n";
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
table += "<TR><TD>";
|
||||
table += String(i);
|
||||
table += "</TD><TD>";
|
||||
table += String(digitalRead(i));
|
||||
table += "</TD></TR>\n";
|
||||
|
||||
}
|
||||
|
||||
table += "<\/table>\n";
|
||||
|
||||
//table += "<img src=\"http://www.ac-grenoble.fr/ien.vienne1-2/spip/IMG/bmp_Image004.bmp\">";
|
||||
|
||||
String pied = "<\/body>";
|
||||
|
||||
// html += "<img src=\"http://www.ac-grenoble.fr/ien.vienne1-2/spip/IMG/bmp_Image004.bmp\">";
|
||||
|
||||
Serial.println(table);
|
||||
|
||||
//bfill.emit_p(String.toCharArray(html));
|
||||
int len = table.length() + 1;
|
||||
char tmp[len];
|
||||
//Serial.println(tmp);
|
||||
table.toCharArray(tmp, len);
|
||||
bfill.emit_p(PSTR("$F$F"),http_OK,tmp);
|
||||
|
||||
// bfill.emit_p(PSTR("$F"
|
||||
// "<meta http-equiv='refresh' content='10'/>"
|
||||
// "<title>Temp Server</title>"
|
||||
// "<body bgcolor=""#99CCFF"">"
|
||||
// "<center>"
|
||||
// "<br />"
|
||||
// "<font size=""7"">"
|
||||
// "Temperature: <b>"
|
||||
// "$S °C"
|
||||
// "<br />"
|
||||
// "<br />"
|
||||
// "<img src=\"http://www.ac-grenoble.fr/ien.vienne1-2/spip/IMG/bmp_Image004.bmp\">"
|
||||
// "</b>"
|
||||
// "Relay Status: <a href=\"?REL=$F\">$F</a><b>"), http_OK, temp_str, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
pinMode(REL, OUTPUT);
|
||||
if (ether.begin(BUFFER_SIZE, mymac) == 0)
|
||||
Serial.println("Cannot initialise ethernet.");
|
||||
else
|
||||
Serial.println("Ethernet initialised.");
|
||||
//ether.staticSetup(myip);
|
||||
#if STATIC
|
||||
ether.staticSetup(myip, gwip);
|
||||
#else
|
||||
if (!ether.dhcpSetup())
|
||||
Serial.println("DHCP failed");
|
||||
#endif
|
||||
ether.printIp("IP: ", ether.myip);
|
||||
ether.printIp("GW: ", ether.gwip);
|
||||
ether.printIp("DNS: ", ether.dnsip);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
digitalWrite(REL, RELStatus); // write to REL digital output
|
||||
delay(1); // necessary for my system
|
||||
word len = ether.packetReceive(); // check for ethernet packet
|
||||
word pos = ether.packetLoop(len); // check for tcp packet
|
||||
sensors.requestTemperatures();
|
||||
temp = (sensors.getTempCByIndex(0));
|
||||
dtostrf(temp, 6, 2, temp_str);
|
||||
|
||||
if (pos) {
|
||||
bfill = ether.tcpOffset();
|
||||
char *data = (char *) Ethernet::buffer + pos;
|
||||
|
||||
if (strncmp("GET /", data, 5) != 0) {
|
||||
// Unsupported HTTP request
|
||||
// 304 or 501 response would be more appropriate
|
||||
bfill.emit_p(http_Unauthorized);
|
||||
}
|
||||
else {
|
||||
data += 5;
|
||||
|
||||
if (data[0] == ' ') {
|
||||
Serial.println("Homepage");
|
||||
// Return home page
|
||||
homePage();
|
||||
}
|
||||
else if (strncmp("?REL=on ", data, 8) == 0) {
|
||||
// Set RELStatus true and redirect to home page
|
||||
RELStatus = true;
|
||||
bfill.emit_p(http_Found);
|
||||
}
|
||||
else if (strncmp("?REL=off ", data, 9) == 0) {
|
||||
// Set RELStatus false and redirect to home page
|
||||
RELStatus = false;
|
||||
bfill.emit_p(http_Found);
|
||||
}
|
||||
else {
|
||||
// Page not found
|
||||
bfill.emit_p(http_Unauthorized);
|
||||
}
|
||||
}
|
||||
ether.httpServerReply(bfill.position()); // send http response
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user