Skip to content

Instantly share code, notes, and snippets.

@attilaolah
Last active September 24, 2017 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save attilaolah/43ef9d3dc01703299718 to your computer and use it in GitHub Desktop.
Save attilaolah/43ef9d3dc01703299718 to your computer and use it in GitHub Desktop.
HTTP JSON API: ENC28J60 + DHT11 + BMP180 + UNO
// HTTP JSON API.
#include <EtherCard.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BMP085_U.h>
// Ethernet:
// TCP/IP send/recv buffer:
byte Ethernet::buffer[512];
// Ethernet MAC address - must be unique on the network!
static byte myMAC[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
const char s200[] PROGMEM = "HTTP/1.0 200 OK\r\n";
const char s503[] PROGMEM = "HTTP/1.0 503 Service Unavailable\r\n";
const char headers[] PROGMEM = "Server: Arduino Uno\r\n"
"Content-Type: application/json\r\n"
"\r\n";
const char b503[] PROGMEM = "{\"error\":\"service_unavailable\"}";
// DHT11 Sensor:
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// BMP180 Sensor:
bool bmp_error;
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
void setup(){
if (!ether.begin(sizeof Ethernet::buffer, myMAC)) {
// OOPS, failed to initialise the ethernet board!
while(1);
}
if(!bmp.begin()) {
// OOPS, failed to initialiset he BMP180 sensor!
bmp_error = true;
}
ether.dhcpSetup();
}
void loop(){
// BMP180:
sensors_event_t event;
bmp.getEvent(&event);
// DHT11:
float humd = dht.readHumidity();
float temp = dht.readTemperature();
// Wait for an incoming TCP packet, but ignore its contents.
if (!ether.packetLoop(ether.packetReceive())) {
return;
}
// Check that all values are ready:
if (isnan(humd) || isnan(temp) || !(humd || temp) || !event.pressure) {
error_503();
return;
}
// Construct a response:
char body[4096];
int pos;
pos += sprintf(body+pos, "%s", "{\"BMP180\":{\"pressure\":");
pos += sprintf(body+pos, "%s", dtostrf(event.pressure, 0, 2, body+pos));
pos += sprintf(body+pos, "%s", "},\"DHT11\":{\"humidity\":");
pos += sprintf(body+pos, "%s", dtostrf(humd, 0, 2, body+pos));
pos += sprintf(body+pos, "%s", ",\"temperature\":");
pos += sprintf(body+pos, "%s", dtostrf(temp, 0, 2, body+pos));
pos += sprintf(body+pos, "%s", "}}");
respond(body, ++pos);
}
void respond(char *body, int size) {
unsigned int offset;
memcpy_P(ether.tcpOffset() + offset, s200, sizeof s200); offset += sizeof s200 - 1;
memcpy_P(ether.tcpOffset() + offset, headers, sizeof headers); offset += sizeof headers - 1;
memcpy (ether.tcpOffset() + offset, body, size); offset += size - 1;
ether.httpServerReply(offset);
}
void error_503() {
unsigned int offset;
memcpy_P(ether.tcpOffset() + offset, s503, sizeof s503); offset += sizeof s503 - 1;
memcpy_P(ether.tcpOffset() + offset, headers, sizeof headers); offset += sizeof headers - 1;
memcpy_P(ether.tcpOffset() + offset, b503, sizeof b503); offset += sizeof b503 - 1;
ether.httpServerReply(offset);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment