Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Created August 27, 2016 07:05
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 izumogeiger/f0b03e789c72e7053c6241d07f3c76fb to your computer and use it in GitHub Desktop.
Save izumogeiger/f0b03e789c72e7053c6241d07f3c76fb to your computer and use it in GitHub Desktop.
// DHT
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);
// gp2y
int measurePin = 0; //Connect dust sensor to Arduino A0 pin
int ledPower = 3; //Connect 3 led driver pins of dust sensor to Arduino D2
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
// hmc5883
#include <Wire.h> //I2C Arduino Library
#define HMC5883 0x1E //0011110b, I2C 7bit address of HMC5883
int x,y,z; //triple axis data
#define LED 4
#include <EtherCard.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = {
0x74,0x69,0x69,0x2D,0x30,0x32 };
static byte myip[] = {
192,168,1,203 };
byte Ethernet::buffer[500];
BufferFiller bfill;
void setup () {
Serial.begin(9600);
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
#if 1
ether.staticSetup(myip);
#else
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
#endif
// gp2y
pinMode(ledPower,OUTPUT);
//dht11
dht.begin();
//hmc5883
x = 0;
y = 0;
z = 0;
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(HMC5883); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
pinMode(LED,OUTPUT);
}
static word homePage() {
char tbuf[8];
char hbuf[8];
char dbuf[8];
char obuf[64];
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
h = 0;
t = 0;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
// gp2y
digitalWrite(LED,LOW);
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 5V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (5.0 / 1024.0);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = (0.17 * calcVoltage - 0.1) * 1000;
if (dustDensity < 0.0) {
dustDensity = 50.0;
}
if (dustDensity > 100.0) {
digitalWrite(LED,HIGH);
}
//hmc5883
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(HMC5883);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(HMC5883, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
dtostrf(h,2,0,hbuf);
dtostrf(t,2,0,tbuf);
dtostrf(dustDensity,5,1,dbuf);
sprintf(obuf,"%s,%s,%s,%d,%d,%d",hbuf,tbuf,dbuf,x,y,z);
Serial.println(obuf);
// response
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"$S"),obuf);
return bfill.position();
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) // check if valid tcp data is received
ether.httpServerReply(homePage()); // send web page data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment