Skip to content

Instantly share code, notes, and snippets.

@spion
Forked from aleksmk/arduino_ds18b20.c
Created February 2, 2013 13:21
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 spion/4697343 to your computer and use it in GitHub Desktop.
Save spion/4697343 to your computer and use it in GitHub Desktop.
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <avr/delay.h>
#define CHANGERATE 5 // in which limits will the temperature change since the last reading, in degrees
#define INPUTPIN 5
// For every sensor found it outputs to serial:
// SensorID,CurrentTemp,Readout time,Current time
// Info at: http://wiki.spodeli.org/Хаклаб/Температура
OneWire ds(12); // 1Wire network on pin 12
LiquidCrystal lcd(6, 7, 8, 9, 10, 11);
int FirstRun = 1;
struct SensorData {
byte addr[8];
float temp;
unsigned long time;
unsigned int raw;
byte signBit;
};
SensorData Senzori[8]; // we define that we will have a maximum of 8 sensors attached to the network
byte currentSensor = 0;
byte attachedSensors = 0;
void setup(void) {
Serial.begin(9600);
lcd.begin(20, 4);
_delay_ms(100);
// search for sensors
cli();
while (ds.search(Senzori[attachedSensors].addr)) {
attachedSensors++;
}
sei();
}
void loop(void) {
byte i, signBit;
byte data[12];
float celsius;
for (currentSensor=0; currentSensor<attachedSensors; currentSensor++) {
//CRC is not valid, go to next address
if (OneWire::crc8(Senzori[currentSensor].addr, 7) != Senzori[currentSensor].addr[7]) {
continue;
}
ds.reset();
ds.select(Senzori[currentSensor].addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
_delay_ms(1000); // maybe 750ms is enough, maybe not
ds.reset();
ds.select(Senzori[currentSensor].addr);
ds.write(0xBE); // Read Scratchpad
//Get the temperature
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
signBit = (raw & 0x8000) >> 15; // Check if we have a negative temperature reading
if (signBit) {
raw = (raw ^ 0xFFFF) + 1; // If the reading is negative (signBit) - do a 2's compliment
}
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
celsius = (float)raw / 16.0;
// Add current data to struct
Senzori[currentSensor].time = millis();
// put the new sensor data into the structure, only if the current sensor redout is in the limit +/- CHANGERATE
if ((celsius <= Senzori[currentSensor].temp + CHANGERATE) && (celsius >= Senzori[currentSensor].temp - CHANGERATE) && !FirstRun )
Senzori[currentSensor].temp = celsius;
if (FirstRun)
Senzori[currentSensor].temp = celsius;
Senzori[currentSensor].raw = raw;
Senzori[currentSensor].signBit = signBit;
}
FirstRun=0;
//Print current temp on LCD static for 3 sensors
lcd.setCursor(0,0);
lcd.print("Lounge: ");
lcd.print(Senzori[0].temp);
lcd.setCursor(0,1);
lcd.print("Outside: ");
lcd.print(Senzori[1].temp);
lcd.setCursor(0,2);
lcd.print("HW room: ");
lcd.print(Senzori[2].temp);
lcd.setCursor(0,3);
lcd.print("Random: ");
lcd.print(Senzori[3].temp);
// If we get a ping from the PC, dump the data
if (Serial.available()) {
byte a = Serial.read();
if (a == 't') {
for (i=0; i<attachedSensors; i++) {
// First line is the address
for( byte j = 0; j < 8; j++) {
Serial.print(Senzori[i].addr[j], HEX);
}
Serial.print(',');
// Then the temperature
if (Senzori[i].signBit) Serial.print('-');
Serial.print(Senzori[i].temp);
Serial.print(',');
// Time of the temperature read-out
Serial.print(Senzori[i].time);
Serial.print(',');
// Current time
Serial.print(millis());
Serial.println();
}
}
else if (a == 'a') {
digitalRead(INPUTPIN) ? Serial.println("OPEN") : Serial.println("CLOSED");
}
Serial.println();
}
}
"""
Reads out the temp sensors from serial and posts them to https://cosm.com/feeds/64655/
"""
import serial
import json
import requests
import time
feed_id="64655"
cosm_api_key="MMB-ThRPU2Xwt59I8s9xWmcxoliSAKxIUGNUUDNVWHpuND0g"
sensors = {
"28B535930013" : "hardware_room",
"288AF85730019" : "lounge_area",
"285BEF57300C7" : "random_room",
"282576B0300C7" : "outside"
}
#set up the serial and ask for data
ser = serial.Serial("/dev/ttyUSB0", timeout=10)
ser.flushInput()
time.sleep(10)
ser.write("t")
#current time will be the same for all uploads
curr_time = time.strftime("%Y-%m-%dT%H:%M:%SZ%z", time.gmtime())
print curr_time
#read the data
while True:
line = ser.readline()
if not line or line == '\r\n':
break
sensor_addr,curr_temp,readout_millis,curr_millis = line.split(",")
# get sensor-room mapping
datapoint_id = sensors.get(sensor_addr)
if datapoint_id is None:
print "Unknown sensor found %s" % sensor_addr
continue
if int(curr_millis)-int(readout_millis)>300000:
print "Haven't read new reading from %s for over 5 minutes." % datapoint_id
continue
url="http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints" % (feed_id,datapoint_id)
headers = {'X-ApiKey':cosm_api_key}
payload={'datapoints': [{'at': curr_time, 'value': curr_temp}]}
print sensor_addr,json.dumps(payload)
r = requests.post(url, data=json.dumps(payload), headers=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment