#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define REQ_BUF_SZ 20
#define DHT11_PIN 7
dht11 DHT;
File webFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 20);
EthernetServer server(80);
void setup() {
Ethernet.begin(mac, ip);
server.begin();
(!SD.begin(4));
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
if (StrContains(HTTP_req, "GET / ")) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<link rel='shortcut icon' href='favicon.ico' type='image/x-icon'>");
client.println("<meta http-equiv='refresh' content='10'/>");
client.println("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>");
client.println("<title>Данные с датчиков</title>");
int smoke_gas = 0; //пин на котором подключен MQ-2
int sensorReading = analogRead(smoke_gas);
client.print("<img src='/flame.png'/>Датчик дыма = ");
client.print(sensorReading);
client.println("<br />");
int chk;
chk = DHT.read(DHT11_PIN);
client.print("<img src='temp.png' />Температура = ");
client.print(DHT.temperature);
client.print(" °C<br/>");
client.print("<img src='humid.png /'>Влажность = ");
client.print(DHT.humidity);
client.print(" %");
client.print("</br>");
client.print("</br>");
client.print("<form action='https://tehnopage.ru 'target='_blank'><button type='submit' >Tehnopage.ru</button></form>");
client.println("</html>");
} else if (StrContains(HTTP_req, "GET /temp.png")) {
webFile = SD.open("temp.png");
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /humid.png")) {
webFile = SD.open("humid.png");
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /flame.png")) {
webFile = SD.open("flame.png");
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
else if (StrContains(HTTP_req, "GET /favicon.ico")) {
webFile = SD.open("favicon.ico");
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println();
}
}
if (webFile) {
while (webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}