COSM data upload w/ds18b20 temp sensor help.

The topic on number crunching

COSM data upload w/ds18b20 temp sensor help.

Postby makiyoung » Fri Jun 09, 2017 10:11 am

I will post the code below. I am trying to read the temp from a ds18b20 temp sensor, which I can do and display to a serial lcd, I am now trying to upload that data to COSM. Can't quite get it right. Most of this code is bits and pieces from various examples over the web. If Im close please point me in the right direction,
include <OneWire.h>
include <SPI.h>
include <Ethernet.h>
include <HttpClient.h>
include <Cosm.h>

// COSM API KEY char cosmKey[] = "MY API KEY IS HERE";

//Define strings for data stream IDs CosmDatastream datastreams[] = { CosmDatastream(temperature, strlen(temperature), DATASTREAM_FLOAT), };

// COSM DATASTREAM FEEDS CosmFeed feed(FEED ID IS HERE, datastreams, 1 /* number of datastreams */);

EthernetClient client; CosmClient cosmclient(client);

int DS18S20_Pin = 8; //DS18S20 Signal pin on digital 8

//Temperature chip i/o OneWire ds(DS18S20_Pin); // on digital pin 8

void setup(void) { Serial.begin(9600); Serial.println("Starting datastream upload to COSM...");

while (Ethernet.begin(mac) != 1) { Serial.println("Error getting IP address via DHCP, trying again..."); delay(15000); } }

void loop(void) { float temperature = getTemp(); serial.print("Reading sensors "); datastream[0].setFloat(temperature);

Serial.println("Uploading it to Cosm"); int ret = cosmclient.put(feed, cosmKey); Serial.print("cosmclient.put returned "); Serial.println(ret);

Serial.println(); delay(15000); }

float getTemp(){ //returns the temperature from one DS18S20 in DEG Celsius

byte data[12]; byte addr[8];

if ( !ds.search(addr)) { //no more sensors on chain, reset search ds.reset_search(); return -1000; }

if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.println("CRC is not valid!"); return -1000; }

if ( addr[0] != 0x10 && addr[0] != 0x28) { Serial.print("Device is not recognized"); return -1000; }

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

byte present = ds.reset(); ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); }

ds.reset_search();

byte MSB = data[1]; byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB); //using two's compliment float TemperatureSum = tempRead / 16;

return TemperatureSum;
makiyoung
 
Posts: 4
Joined: Wed Jun 07, 2017 9:26 am

Return to Microcontrollers & Embedded

Who is online

Users browsing this forum: No registered users and 1 guest