Skip to content

Instantly share code, notes, and snippets.

@TareObjects
Last active June 25, 2016 08:14
Show Gist options
  • Select an option

  • Save TareObjects/8be3068fb902a9a7bcb3 to your computer and use it in GitHub Desktop.

Select an option

Save TareObjects/8be3068fb902a9a7bcb3 to your computer and use it in GitHub Desktop.
#include <EtherCard.h>
#include <Wire.h>
#include <HDC1000.h>
#include <Adafruit_MPL115A2.h>
#define USE_DHCP 0
#define USE_DNS 0
//
// Ethernet configratios
//
byte Ethernet::buffer[700];
static byte MyMAC[] = { 0x20,0x15,0x08,0x15,0x17,0x05 };
#if USE_DHCP
#else
const uint8_t GWIP[] = {192, 168, 0, 1};
const uint8_t MyIP[] = {192, 168, 0, 42};
#endif
#if USE_DNS
const char HisName[] = "foo.bar.com";
#else
const char HisName[] = "192.168.0.72";
const uint8_t HisIP[] = {192, 168, 0, 72};
#endif
static void etherCallback (byte status, word off, word len) {
// Serial.println(">>>");
// Ethernet::buffer[off+300] = 0;
// Serial.print((const char*) Ethernet::buffer + off);
// Serial.println("...");
Serial.println("sent.");
}
//
// HDC1000
//
HDC1000 hdc1000(0x40, 4);
//
// MPL115A2
//
Adafruit_MPL115A2 mpl115a2;
//
// other control
//
int count = 1;
static uint32_t timer;
void setup () {
Serial.begin(57600);
Serial.println("setup started.");
// ethernet
//
if (ether.begin(sizeof Ethernet::buffer, MyMAC) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
#if USE_DHCP
#else
EtherCard::copyIp(ether.gwip, GWIP);
EtherCard::copyIp(ether.myip, MyIP);
#endif
#if USE_DNS
if (!ether.dnsLookup(hisname))
Serial.println("DNS failed");
#else
EtherCard::copyIp(ether.hisip, HisIP);
#endif
ether.hisport = 2999;
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
#if USE_DNS
ether.printIp("DNS: ", ether.dnsip);
#endif
ether.printIp("SRV: ", ether.hisip);
//
// HDC1000
hdc1000.begin();
//
// MPL115A2
mpl115a2.begin();
}
void loop () {
char buf[100], tbuf[10], hbuf[10];
float temperature;
float humidity;
float pressureHPA = 996;
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 5000;
temperature = hdc1000.getTemp();
dtostrf(temperature, 1, 2, tbuf);
humidity = hdc1000.getHumi();
dtostrf(humidity, 1, 2, hbuf);
pressureHPA = mpl115a2.getPressure() * 10;
Serial.println();
Serial.print("<<< POST ");
sprintf(buf, "{ \"temperature\":%s,\"humidity\":%s,\"pressure\":%d }",
tbuf, hbuf, (int)pressureHPA);
ether.httpPost(
PSTR("/weather"),
HisName,
PSTR("Content-type: application/json"),
buf,
etherCallback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment