Тема: ENC28J60 - терморегулятор
#include <EtherCard.h>
#include <OneWire.h>//https://github.com/PaulStoffregen/OneWire/archive/master.zip
#include <DallasTemperature.h>
#include <EEPROM.h>
OneWire oneWire(A0);
DallasTemperature t(&oneWire);
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 10,42,0,100 };
byte Ethernet::buffer[1000];
float temp;
int reg,temp0,temp1,a;
const byte gis = 1; // гистерезис 1 градус
BufferFiller bfill;
String line;
static word homePage() {
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"<html><head><meta charset='UTF-8'>"
"<meta http-equiv='refresh' content='10'/>"
"<style>.tab1 {background-color:#F5F5F5;border-radius: 5px;margin: auto;}</style></head>"
"<br><TABLE class='tab1' align='center' width='470' BORDER='1' cellspacing='0' cellpadding='10'>"
"<td><center><big><b>Терморегулятор DS18B20</b></big></td><tr><td><center>"
"Температура: <b>$D.$D </b>°C"
"</td><tr><td align='center'>Установка температуры регулирования (0...125°C)"
"<form action='' method='GET'>"
"<br><input type='text' name='reg' autocomplete='off' size='1'></input> "
"<button>Подтвердить</button></form></td><tr>"
"<td><center>Температура регулировки: <b>$D °C</b></td>"
"</table>"
"</center></body></html> "
),temp0,temp1,reg);
return bfill.position();
}
void setup () {
Serial.begin(9600);
pinMode(2, OUTPUT); // D2 управление реле нагревательного элемента
ether.begin(sizeof Ethernet::buffer, mymac, 10);
ether.staticSetup(myip);
reg = EEPROM.read(0);
t.begin();
t.setResolution(9);
}
void loop () {
t.requestTemperatures();
temp = t.getTempCByIndex(0);
temp0 = int(temp);
temp1 = (temp-temp0)*100;
word pos = ether.packetLoop(ether.packetReceive());
if (pos){ // check if valid tcp data is received
char *data = (char *) Ethernet::buffer + pos;
line = data;a = line.indexOf("?reg=");if(a>0){reg = line.substring(a+5, a+8).toInt();
if(reg<0){reg=0;}if(reg>125){reg=125;}EEPROM.update(0,reg);}
if(reg >= temp + gis){digitalWrite(2,HIGH);}
if(reg <= temp - gis){digitalWrite(2,LOW);}
ether.httpServerReply(homePage());
}}