Skip to content

Instantly share code, notes, and snippets.

@alexishida
Created October 12, 2016 02:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexishida/316f52c2d31597a43239188049cd5915 to your computer and use it in GitHub Desktop.
Save alexishida/316f52c2d31597a43239188049cd5915 to your computer and use it in GitHub Desktop.
WS REST using ENC28J60 and DHT11
#include <EtherCard.h>
#include <DHT.h>
#define DHTPIN A1 // pino que estamos conectado
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Ethernet interface ip address
static byte myip[] = { 192, 168, 0, 115 };
// Gateway ip address
static byte gwip[] = { 192, 168, 0, 240 };
// DNS ip address
static byte dnsip[] = { 192, 168, 0, 240 };
// Subnet mask
static byte mask[] = { 255, 255, 255, 0 };
// Ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
// TCP/IP send and receive buffer
byte Ethernet::buffer[1000];
BufferFiller bfill;
const char okHeader[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: application/json\r\n"
"Pragma: no-cache\r\n"
"\r\n" ;
const char failHeader[] PROGMEM =
"HTTP/1.0 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Pragma: no-cache\r\n"
"\r\n" ;
const char responseHeader[] PROGMEM =
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n"
"Access-Control-Allow-Origin: *\r\n"
"\r\n" ;
int tipo_requisicao = 0;
void setup() {
// Open a serial connection for debugging
Serial.begin(57600);
Serial.println("\n[Debugger]");
// Initialize the Ethernet interface
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
// Setup IP statically
ether.staticSetup(myip, gwip, dnsip, mask);
// Print IP information out to serial line
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
dht.begin();
}
static word homePage() {
bfill = ether.tcpOffset();
if (tipo_requisicao == 1) {
float t = dht.readTemperature();
char json_temp[50];
String json_str_temp = "{ \"temperatura\": " + String(t) + "}";
json_str_temp.toCharArray(json_temp, 50) ;
bfill.emit_p(PSTR("$S"), json_temp, responseHeader);
return bfill.position();
}
else if (tipo_requisicao == 2) {
float h = dht.readHumidity();
char json_humi[50];
String json_str_humi = "{ \"humidade\": " + String(h) + "}";
json_str_humi.toCharArray(json_humi, 50) ;
bfill.emit_p(PSTR("$S"), json_humi, responseHeader);
return bfill.position();
}
else {
bfill.emit_p(PSTR("Pagina nao encontrada"), failHeader);
return bfill.position();
}
}
void WebServer() {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (strstr((char *)Ethernet::buffer + pos, "GET /DHT11/TEMPERATURA") != 0) {
tipo_requisicao = 1;
}
else if (strstr((char *)Ethernet::buffer + pos, "GET /DHT11/HUMIDADE") != 0) {
tipo_requisicao = 2;
}
else {
tipo_requisicao = 0;
}
if (pos)
{
ether.httpServerReply(homePage());
}
}
void loop() {
WebServer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment