Last active
December 7, 2016 12:47
-
-
Save izumogeiger/69afee129d107c582821f0d1bcc68ac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
// bme280 | |
// SDA=A4 SCL=A5 | |
#define BME280_ADDRESS 0x76 | |
unsigned long int hum_raw,temp_raw,pres_raw; | |
signed long int t_fine; | |
double temp_act = 0.0, press_act = 0.0,hum_act=0.0; | |
uint16_t dig_T1; | |
int16_t dig_T2; | |
int16_t dig_T3; | |
uint16_t dig_P1; | |
int16_t dig_P2; | |
int16_t dig_P3; | |
int16_t dig_P4; | |
int16_t dig_P5; | |
int16_t dig_P6; | |
int16_t dig_P7; | |
int16_t dig_P8; | |
int16_t dig_P9; | |
int8_t dig_H1; | |
int16_t dig_H2; | |
int8_t dig_H3; | |
int16_t dig_H4; | |
int16_t dig_H5; | |
int8_t dig_H6; | |
void bme280_init() | |
{ | |
uint8_t osrs_t = 1; //Temperature oversampling x 1 | |
uint8_t osrs_p = 1; //Pressure oversampling x 1 | |
uint8_t osrs_h = 1; //Humidity oversampling x 1 | |
uint8_t mode = 3; //Normal mode | |
uint8_t t_sb = 5; //Tstandby 1000ms | |
uint8_t filter = 0; //Filter off | |
uint8_t spi3w_en = 0; //3-wire SPI Disable | |
uint8_t ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode; | |
uint8_t config_reg = (t_sb << 5) | (filter << 2) | spi3w_en; | |
uint8_t ctrl_hum_reg = osrs_h; | |
writeReg(0xF2,ctrl_hum_reg); | |
writeReg(0xF4,ctrl_meas_reg); | |
writeReg(0xF5,config_reg); | |
readTrim(); // | |
} | |
void readTrim() | |
{ | |
uint8_t data[32],i=0; // Fix 2014/04/06 | |
Wire.beginTransmission(BME280_ADDRESS); | |
Wire.write(0x88); | |
Wire.endTransmission(); | |
Wire.requestFrom(BME280_ADDRESS,24); // Fix 2014/04/06 | |
while(Wire.available()){ | |
data[i] = Wire.read(); | |
i++; | |
} | |
Wire.beginTransmission(BME280_ADDRESS); // Add 2014/04/06 | |
Wire.write(0xA1); // Add 2014/04/06 | |
Wire.endTransmission(); // Add 2014/04/06 | |
Wire.requestFrom(BME280_ADDRESS,1); // Add 2014/04/06 | |
data[i] = Wire.read(); // Add 2014/04/06 | |
i++; // Add 2014/04/06 | |
Wire.beginTransmission(BME280_ADDRESS); | |
Wire.write(0xE1); | |
Wire.endTransmission(); | |
Wire.requestFrom(BME280_ADDRESS,7); // Fix 2014/04/06 | |
while(Wire.available()){ | |
data[i] = Wire.read(); | |
i++; | |
} | |
dig_T1 = (data[1] << 8) | data[0]; | |
dig_T2 = (data[3] << 8) | data[2]; | |
dig_T3 = (data[5] << 8) | data[4]; | |
dig_P1 = (data[7] << 8) | data[6]; | |
dig_P2 = (data[9] << 8) | data[8]; | |
dig_P3 = (data[11]<< 8) | data[10]; | |
dig_P4 = (data[13]<< 8) | data[12]; | |
dig_P5 = (data[15]<< 8) | data[14]; | |
dig_P6 = (data[17]<< 8) | data[16]; | |
dig_P7 = (data[19]<< 8) | data[18]; | |
dig_P8 = (data[21]<< 8) | data[20]; | |
dig_P9 = (data[23]<< 8) | data[22]; | |
dig_H1 = data[24]; | |
dig_H2 = (data[26]<< 8) | data[25]; | |
dig_H3 = data[27]; | |
dig_H4 = (data[28]<< 4) | (0x0F & data[29]); | |
dig_H5 = (data[30] << 4) | ((data[29] >> 4) & 0x0F); // Fix 2014/04/06 | |
dig_H6 = data[31]; // Fix 2014/04/06 | |
} | |
void writeReg(uint8_t reg_address, uint8_t data) | |
{ | |
Wire.beginTransmission(BME280_ADDRESS); | |
Wire.write(reg_address); | |
Wire.write(data); | |
Wire.endTransmission(); | |
} | |
void readData() | |
{ | |
int i = 0; | |
uint32_t data[8]; | |
Wire.beginTransmission(BME280_ADDRESS); | |
Wire.write(0xF7); | |
Wire.endTransmission(); | |
Wire.requestFrom(BME280_ADDRESS,8); | |
while(Wire.available()){ | |
data[i] = Wire.read(); | |
i++; | |
} | |
pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); | |
temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); | |
hum_raw = (data[6] << 8) | data[7]; | |
} | |
signed long int calibration_T(signed long int adc_T) | |
{ | |
signed long int var1, var2, T; | |
var1 = ((((adc_T >> 3) - ((signed long int)dig_T1<<1))) * ((signed long int)dig_T2)) >> 11; | |
var2 = (((((adc_T >> 4) - ((signed long int)dig_T1)) * ((adc_T>>4) - ((signed long int)dig_T1))) >> 12) * ((signed long int)dig_T3)) >> 14; | |
t_fine = var1 + var2; | |
T = (t_fine * 5 + 128) >> 8; | |
return T; | |
} | |
unsigned long int calibration_P(signed long int adc_P) | |
{ | |
signed long int var1, var2; | |
unsigned long int P; | |
var1 = (((signed long int)t_fine)>>1) - (signed long int)64000; | |
var2 = (((var1>>2) * (var1>>2)) >> 11) * ((signed long int)dig_P6); | |
var2 = var2 + ((var1*((signed long int)dig_P5))<<1); | |
var2 = (var2>>2)+(((signed long int)dig_P4)<<16); | |
var1 = (((dig_P3 * (((var1>>2)*(var1>>2)) >> 13)) >>3) + ((((signed long int)dig_P2) * var1)>>1))>>18; | |
var1 = ((((32768+var1))*((signed long int)dig_P1))>>15); | |
if (var1 == 0) | |
{ | |
return 0; | |
} | |
P = (((unsigned long int)(((signed long int)1048576)-adc_P)-(var2>>12)))*3125; | |
if(P<0x80000000) | |
{ | |
P = (P << 1) / ((unsigned long int) var1); | |
} | |
else | |
{ | |
P = (P / (unsigned long int)var1) * 2; | |
} | |
var1 = (((signed long int)dig_P9) * ((signed long int)(((P>>3) * (P>>3))>>13)))>>12; | |
var2 = (((signed long int)(P>>2)) * ((signed long int)dig_P8))>>13; | |
P = (unsigned long int)((signed long int)P + ((var1 + var2 + dig_P7) >> 4)); | |
return P; | |
} | |
unsigned long int calibration_H(signed long int adc_H) | |
{ | |
signed long int v_x1; | |
v_x1 = (t_fine - ((signed long int)76800)); | |
v_x1 = (((((adc_H << 14) -(((signed long int)dig_H4) << 20) - (((signed long int)dig_H5) * v_x1)) + | |
((signed long int)16384)) >> 15) * (((((((v_x1 * ((signed long int)dig_H6)) >> 10) * | |
(((v_x1 * ((signed long int)dig_H3)) >> 11) + ((signed long int) 32768))) >> 10) + (( signed long int)2097152)) * | |
((signed long int) dig_H2) + 8192) >> 14)); | |
v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * ((signed long int)dig_H1)) >> 4)); | |
v_x1 = (v_x1 < 0 ? 0 : v_x1); | |
v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1); | |
return (unsigned long int)(v_x1 >> 12); | |
} | |
void bme280() | |
{ | |
signed long int temp_cal; | |
unsigned long int press_cal,hum_cal; | |
readData(); | |
temp_cal = calibration_T(temp_raw); | |
press_cal = calibration_P(pres_raw); | |
hum_cal = calibration_H(hum_raw); | |
temp_act = (double)temp_cal / 100.0; | |
press_act = (double)press_cal / 100.0; | |
hum_act = (double)hum_cal / 1024.0; | |
} | |
// 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; | |
float prevDensity = 0; | |
#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); | |
pinMode(LED,OUTPUT); | |
Wire.begin(); | |
bme280_init(); | |
} | |
void gp2y() | |
{ | |
// 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) { | |
prevDensity = dustDensity; | |
} | |
else { | |
dustDensity = prevDensity; | |
} | |
if (dustDensity > 75.0) { | |
digitalWrite(LED,HIGH); | |
} | |
} | |
static word homePage() { | |
char tbuf[9]; | |
char pbuf[9]; | |
char dbuf[9]; | |
char obuf[64]; | |
dtostrf(temp_act,8,2,tbuf); | |
dtostrf(press_act,8,2,pbuf); | |
dtostrf(dustDensity,8,2,dbuf); | |
sprintf(obuf,"%s,%s,%s",tbuf,pbuf,dbuf); | |
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 () { | |
#if 1 | |
word len = ether.packetReceive(); | |
word pos = ether.packetLoop(len); | |
if (pos) { // check if valid tcp data is received | |
bme280(); | |
gp2y(); | |
ether.httpServerReply(homePage()); // send web page data | |
} | |
#else | |
delay(1000); | |
bme280(); | |
Serial.print("TEMP : "); | |
Serial.print(temp_act); | |
Serial.print(" DegC PRESS : "); | |
Serial.print(press_act); | |
Serial.print(" hPa HUM : "); | |
Serial.print(hum_act); | |
Serial.print(" % "); | |
gp2y(); | |
Serial.print(dustDensity); | |
Serial.println(" ug/m2 "); | |
#endif | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require 'fileutils' | |
require 'net/https' | |
class Locked < StandardError | |
end | |
def lock(lock_file_path='/tmp/mix1.lock') | |
File.open(lock_file_path, 'w') do |lock_file| | |
if lock_file.flock(File::LOCK_EX|File::LOCK_NB) | |
yield | |
else | |
raise Locked | |
end | |
end | |
end | |
class Mix1Logger | |
def initialize | |
end | |
def get_line | |
uri = URI.parse('http://192.168.1.203') | |
http = Net::HTTP.new(uri.host,uri.port) | |
res = http.send_request('GET',uri.request_uri) | |
unless res.code == "200" | |
return nil | |
end | |
line = res.body | |
r = %r|^\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d\.]+)| | |
return nil unless r =~ line | |
v = [$1,$2,$3,line] | |
k = [:t,:p,:d,:line] | |
a = k.zip(v) | |
ret = Hash[a] | |
pp ret | |
ret | |
end | |
def xively(l) | |
uri = URI.parse('https://api.xively.com/v2/feeds/xxxxxxxxxxxx.xml') | |
https = Net::HTTP.new(uri.host,uri.port) | |
https.use_ssl = true | |
headers = { | |
'X-ApiKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', | |
'Content-Type' => 'application/xml' | |
} | |
data =<<-"EOS" | |
<?xml version="1.0" encoding="UTF-8"?> | |
<eeml> | |
<environment> | |
<data id="dht11_t"> | |
<current_value>#{l[:t]}</current_value> | |
</data> | |
<data id="pressure"> | |
<current_value>#{l[:p]}</current_value> | |
</data> | |
<data id="pm2_5"> | |
<current_value>#{l[:d]}</current_value> | |
</data> | |
</environment> | |
</eeml> | |
EOS | |
res = https.send_request('PUT',uri.request_uri,data,headers) | |
pp res | |
end | |
DELAY = 120 | |
def run | |
count = 0 | |
ds = [] | |
loop do | |
sleep DELAY | |
count += 1 | |
l = get_line | |
next unless l | |
now = Time.now | |
pp l | |
dir = File.join(ENV["HOME"],"mix1log",now.strftime("%Y"),now.strftime("%m")) | |
file = File.join(dir,now.strftime("%d")+".txt") | |
FileUtils.mkdir_p(dir) | |
mode = File.exists?(file) ? "a" : "w" | |
File.open(file,mode) do |io| | |
out = "#{now.strftime("%Y%m%d%H%M%S")},#{l[:line]}" | |
io.puts out | |
end | |
xively(l) | |
end | |
end | |
end | |
begin | |
lock do | |
pkg = Mix1Logger.new | |
pkg.run | |
end | |
rescue Locked | |
exit 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment