Skip to content

Instantly share code, notes, and snippets.

@art-enot
Created February 10, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save art-enot/8701ba2aa4339b85ba643b7a345dee14 to your computer and use it in GitHub Desktop.
Save art-enot/8701ba2aa4339b85ba643b7a345dee14 to your computer and use it in GitHub Desktop.
Remote control Arduino+EtherCard+DHT22
#include "DHT.h"
#include <EEPROM.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; // MAC Address должен быть уникальным в локальной сети
static byte myip[] = { 192,168,1,222 }; // Постоянный IP адресс нашей страницы
byte Ethernet::buffer[1000]; // Буфер, чем больше данных на web странице, тем больше требуется буфера
BufferFiller bfill;
// Начальные данные
int LedPins[] = {
2,3,5,6,7,8,9};
int t=0;
int h=0;
boolean PinStatus[7];
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"
"\r\n"
"<meta charset='UTF-8'>"
"<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
"<meta http-equiv='refresh' content='10'/>";
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>";
// Подключаем Ethernet порт HR911105A и датчик DHT22
void setup () {
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
Serial.println( "Failed to access Ethernet controller");
ether.staticSetup(myip);
for(int i = 0; i <= 7; i++)
{
pinMode(LedPins[i],OUTPUT);
PinStatus[i]=EEPROM.read(i);
digitalWrite(LedPins[i],PinStatus[i]);
}
dht.begin();
}
// Получаем данные от DHT22
static void ReadDHT22()
{
h = dht.readHumidity();
t = dht.readTemperature();
}
// Оформление Web страницы
static word homePage() {
bfill = ether.tcpOffset();
bfill.emit_p(PSTR("$F"
"<title>Гараж</title>"
"<p style=\"text-align: center;\"><br />Конвектор: <br> <span style=\"font-size: 4em;\"><a href=\"?ArduinoPIN2=$F\">$F</a></span>"),
http_OK,
PinStatus[0]?PSTR("off"):PSTR("on"),
PinStatus[0]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"));
bfill.emit_p(PSTR(
"<br><br>Температура: <br> <span style=\"font-size: 4em;\">$D C</span> <br /><br />Влажность:<br> <span style=\"font-size: 4em;\"> $D %</span></p>"),t, h);
return bfill.position();
}
void loop () {
delay(1); // Задержка
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) // check if valid tcp data is received
{
ReadDHT22();
bfill = ether.tcpOffset();
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
bfill.emit_p(http_Unauthorized);
}
else {
data += 5;
if (data[0] == ' ') {
homePage();
}
else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
PinStatus[0] = true;
digitalWrite(LedPins[0],PinStatus[0]);
EEPROM.write(0,PinStatus[0]); // записываем в ячейку EEPROM №0, текущее состояние LedPins[0].
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
PinStatus[0] = false;
digitalWrite(LedPins[0],PinStatus[0]);
EEPROM.write(0,PinStatus[0]);
bfill.emit_p(http_Found);
}
else {
// Page not found
bfill.emit_p(http_Unauthorized);
}
}
ether.httpServerReply(bfill.position()); // send http response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment