Skip to content

Instantly share code, notes, and snippets.

@glisha
Created June 30, 2012 18:10

Revisions

  1. glisha revised this gist Jul 19, 2012. 2 changed files with 7 additions and 3 deletions.
    8 changes: 6 additions & 2 deletions arduino_ds18b20.c
    Original file line number Diff line number Diff line change
    @@ -81,13 +81,17 @@ void loop(void) {
    lcd.print(Senzori[0].temp);

    lcd.setCursor(0,1);
    lcd.print("HW room: ");
    lcd.print("Outside: ");
    lcd.print(Senzori[1].temp);

    lcd.setCursor(0,2);
    lcd.print("Random: ");
    lcd.print("HW room: ");
    lcd.print(Senzori[2].temp);

    lcd.setCursor(0,3);
    lcd.print("Random: ");
    lcd.print(Senzori[3].temp);

    //Print current temp on LCD
    //for (i=0; i<attachedSensors; i++) {
    // lcd.setCursor(0,i);
    2 changes: 1 addition & 1 deletion readout_tocosm.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    import time

    feed_id="64655"
    sensors = {"28B535930013":"hardware_room","288AF85730019":"lounge_area","285BEF57300C7":"random_room"}
    sensors = {"28B535930013":"hardware_room","288AF85730019":"lounge_area","285BEF57300C7":"random_room","282576B0300C7":"outside"}
    cosm_api_key="0yaZCXxKg2UtrPKXwSinxVf0gHeSAKxQNnlGdmcvbXZPOD0g"

    #set up the serial and ask for data
  2. glisha revised this gist Jul 15, 2012. 1 changed file with 24 additions and 31 deletions.
    55 changes: 24 additions & 31 deletions readout_tocosm.py
    Original file line number Diff line number Diff line change
    @@ -1,56 +1,49 @@
    #!/usr/bin/env python2.7
    """
    Reads out the temp sensors from serial and posts them to https://cosm.com/feeds/64655/
    cron:
    */15 * * * * PYTHONUSERBASE=/home/kikadevices/temperature/env /home/kikadevices/temperature/temp_to_cosm.py >> /home/kikadevices/temperature/temp_to_cosm.log 2>&1
    """

    import serial
    import json
    import requests
    import time
    import exceptions

    sensors = {"28B535930013":"hardware_room","288AF85730019":"lounge_area","285BEF57300C7":"random_room"}
    feed_id="64655"
    cosm_api_key="COSM_API_KEY"
    sensors = {"28B535930013":"hardware_room","288AF85730019":"lounge_area","285BEF57300C7":"random_room"}
    cosm_api_key="0yaZCXxKg2UtrPKXwSinxVf0gHeSAKxQNnlGdmcvbXZPOD0g"

    #set up the serial and ask for data
    ser = serial.Serial("/dev/ttyUSB0")
    ser = serial.Serial("/dev/ttyUSB0", timeout=5)

    ser.flushInput()
    ser.write("g")
    time.sleep(10)
    ser.write("g")

    #current time will be the same for all uploads
    curr_time = time.strftime("%Y-%m-%dT%H:%M:%SZ%z", time.gmtime())

    #read the data
    while True:
    try:
    sensor_addr,curr_temp,readout_millis,curr_millis = ser.readline().split(",")

    # get sensor-room mapping
    datapoint_id=sensors[sensor_addr]

    if int(curr_millis)-int(readout_millis)>300000:
    raise exceptions.Warning("Haven't read new reading from %s for over 5 minutes.")

    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}]}

    r = requests.post(url, data=json.dumps(payload), headers=headers)

    print sensor_addr, json.dumps(payload)
    except KeyError:
    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

    except exceptions.Warning, e:
    print e
    if int(curr_millis)-int(readout_millis)>300000:
    print "Haven't read new reading from %s for over 5 minutes."
    continue

    except ValueError:
    #print "Done reading."
    break
    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)

  3. glisha revised this gist Jul 6, 2012. 1 changed file with 29 additions and 4 deletions.
    33 changes: 29 additions & 4 deletions arduino_ds18b20.c
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    #include <OneWire.h>
    #include <LiquidCrystal.h>

    // For every sensor found it outputs to serial:
    // SensorID,CurrentTemp,Readout time,Current time


    // Info at: http://wiki.spodeli.org/Хаклаб/Температура

    OneWire ds(10); // on pin 10
    OneWire ds(12); // on pin 12
    LiquidCrystal lcd(6, 7, 8, 9, 10, 11);

    struct SensorData {
    byte addr[8];
    @@ -19,6 +20,7 @@ byte attachedSensors = 0;

    void setup(void) {
    Serial.begin(9600);
    lcd.begin(20, 4);
    }

    void loop(void) {
    @@ -73,6 +75,29 @@ void loop(void) {
    if (attachedSensors < currentSensor)
    attachedSensors = currentSensor;

    //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("HW room: ");
    lcd.print(Senzori[1].temp);

    lcd.setCursor(0,2);
    lcd.print("Random: ");
    lcd.print(Senzori[2].temp);

    //Print current temp on LCD
    //for (i=0; i<attachedSensors; i++) {
    // lcd.setCursor(0,i);
    // for (byte j = 0; j < 8; j++) {
    // lcd.print(Senzori[i].addr[j],HEX);
    // }
    // lcd.print(": ");
    // lcd.print(Senzori[i].temp);
    //}

    if (Serial.available()) {
    byte a = Serial.read();
    for (i=0; i<attachedSensors; i++) {
    @@ -90,4 +115,4 @@ void loop(void) {
    }
    Serial.println();
    }
    }
    }
  4. glisha revised this gist Jun 30, 2012. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions readout_tocosm.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    #!/usr/bin/env python2.7
    """
    Reads out the temp sensors from serial and posts them to https://cosm.com/feeds/64655/
    cron:
    */15 * * * * PYTHONUSERBASE=/home/kikadevices/temperature/env /home/kikadevices/temperature/temp_to_cosm.py >> /home/kikadevices/temperature/temp_to_cosm.log 2>&1
    """

    import serial
    @@ -38,8 +42,7 @@

    r = requests.post(url, data=json.dumps(payload), headers=headers)

    print sensor_addr
    print json.dumps(payload)
    print sensor_addr, json.dumps(payload)
    except KeyError:
    print "Unknown sensor found %s" % sensor_addr
    continue
    @@ -49,5 +52,5 @@
    continue

    except ValueError:
    print "Done reading."
    #print "Done reading."
    break
  5. glisha created this gist Jun 30, 2012.
    93 changes: 93 additions & 0 deletions arduino_ds18b20.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #include <OneWire.h>

    // For every sensor found it outputs to serial:
    // SensorID,CurrentTemp,Readout time,Current time



    OneWire ds(10); // on pin 10

    struct SensorData {
    byte addr[8];
    float temp;
    unsigned long time;
    };

    SensorData Senzori[8];
    byte currentSensor = 0;
    byte attachedSensors = 0;

    void setup(void) {
    Serial.begin(9600);
    }

    void loop(void) {
    byte i;
    byte present = 0;
    byte data[12];
    byte addr[8];
    float celsius;

    //Search for the sensors
    if ( !ds.search(addr)) {
    currentSensor = 0;
    ds.reset_search();
    delay(250);
    return;
    }

    //CRC is not valid exit loop
    if (OneWire::crc8(addr, 7) != addr[7]) {
    return;
    }

    ds.reset();
    ds.select(addr);
    ds.write(0x44,1); // start conversion, with parasite power on at the end

    delay(1000); // maybe 750ms is enough, maybe not

    present = ds.reset();
    ds.select(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];
    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;

    Senzori[currentSensor].time = millis();
    Senzori[currentSensor].temp = celsius;
    memcpy(Senzori[currentSensor].addr, addr, 8);
    currentSensor++;
    if (attachedSensors < currentSensor)
    attachedSensors = currentSensor;

    if (Serial.available()) {
    byte a = Serial.read();
    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(',');
    Serial.print(Senzori[i].temp);
    Serial.print(',');
    Serial.print(Senzori[i].time);
    Serial.print(',');
    Serial.print(millis());
    Serial.println();
    }
    Serial.println();
    }
    }
    53 changes: 53 additions & 0 deletions readout_tocosm.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env python2.7
    """
    Reads out the temp sensors from serial and posts them to https://cosm.com/feeds/64655/
    """

    import serial
    import json
    import requests
    import time
    import exceptions

    sensors = {"28B535930013":"hardware_room","288AF85730019":"lounge_area","285BEF57300C7":"random_room"}
    feed_id="64655"
    cosm_api_key="COSM_API_KEY"

    #set up the serial and ask for data
    ser = serial.Serial("/dev/ttyUSB0")
    ser.flushInput()
    ser.write("g")

    #current time will be the same for all uploads
    curr_time = time.strftime("%Y-%m-%dT%H:%M:%SZ%z", time.gmtime())

    #read the data
    while True:
    try:
    sensor_addr,curr_temp,readout_millis,curr_millis = ser.readline().split(",")

    # get sensor-room mapping
    datapoint_id=sensors[sensor_addr]

    if int(curr_millis)-int(readout_millis)>300000:
    raise exceptions.Warning("Haven't read new reading from %s for over 5 minutes.")

    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}]}

    r = requests.post(url, data=json.dumps(payload), headers=headers)

    print sensor_addr
    print json.dumps(payload)
    except KeyError:
    print "Unknown sensor found %s" % sensor_addr
    continue

    except exceptions.Warning, e:
    print e
    continue

    except ValueError:
    print "Done reading."
    break