Author Topic: Oak on Thingspeak (and particle.io) using DS18B20  (Read 6961 times)

exeng

  • Sr. Member
  • ****
  • Posts: 454
Oak on Thingspeak (and particle.io) using DS18B20
« on: March 05, 2016, 08:09:27 pm »
This is not a very detailed example. Just an example sketch that I cobbled together from some other examples. It posts temperature data from a DS18B20 to Thingspeak (and particle.io). I had an idle Thingspeak account and was itching to use it again since it supports logging. I was tripped up trying to use the Thingspeak library so I went off searching for a working example. Eventually, this will become my pool temp monitor.

Just an example for those that want to use Thingspeak. Credit goes here: http://www.arduinesp.com/thingspeak for the ESP8266 starting point. The DS18B20 code is derived from another DS18B20 example sketch that was floating around. I just added convert to degrees F and glued everything together to get a starting point.

Code follows:
Code: [Select]
/*Oak POSTing to Thingspeak and particle.io
 *
 * Known issue: First temperature reading on power up
 *              may be in error.
 */
#include <WiFiClient.h>
//#include <WiFiServer.h>
//#include <WiFiUdp.h>

#include <ESP8266WiFi.h>
//#include <DNSServer.h>
//#include <ESP8266WebServer.h>

#include <OneWire.h> // Using OneWire lib from Oak package
int DS18S20_Pin = 2; // DS18S20 Signal on pin 2
float tempF = 0;     // Temperature in degrees F
char tempFstring[6]; // Used to store a temperature value as a string
//Temperature chip i/o
OneWire ds(DS18S20_Pin);

int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiClient client;

unsigned long myChannelNumber = NNNNNN; // Put your channel number here
String myWriteAPIKey = "XXXXXXXXXXXXXXX"; // Put your API key here
const char * server = "api.thingspeak.com";

void setup()
{
  pinMode(1,OUTPUT);
  digitalWrite(1,LOW);
  while ( WiFi.status() != WL_CONNECTED) {
    digitalWrite(1,HIGH);
    status = WiFi.begin();
    // wait 10 seconds for connection:
    delay(10000);
    digitalWrite(1,LOW);
  }
}

void loop()
{
  tempF = getTemp();
  sprintf(tempFstring, "%d", (int)tempF); // Convert to string

  if(tempF <= -1000) {
    Particle.publish("DS18B20 Error", tempFstring, 60, PRIVATE);
  }
  else {
    Particle.publish("Temp (F)", tempFstring, 60, PRIVATE);
  }
  // POST to Thingspeak channel
  if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
    digitalWrite(1,HIGH);
    String postStr = myWriteAPIKey;
    postStr +="&field1=";
    postStr += String(tempFstring);
//    postStr +="&field2=";
//    postStr += String("72");
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+myWriteAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);

    client.stop();

    // thingspeak needs minimum 15 sec delay between updates
    delay(1000); // Pulse stretch LED
    digitalWrite(1,LOW);
   
    // Delay 1 minute
    for(int i=0; i<6; i++) {
     delay(10000); //Delay here to slow down updates to particle.io log
    }
  } // END if(client.connect())
  // Will appear as rapid blinking if no connection
  delay(200);
  digitalWrite(1,HIGH);
  delay(200);
  digitalWrite(1,LOW);
}

// OneWire DS18B20 temperature reader with conversion to degrees F
float getTemp(){
  //returns the temperature from one DS18S20 in DEG Fharenheit

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

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

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

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

  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;
 
  // Convert to degrees Fahernheit
  TemperatureSum = TemperatureSum * 9 / 5 + 32;
 
  return TemperatureSum;
} //END getTemp()
« Last Edit: March 05, 2016, 08:31:45 pm by exeng »

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Oak on Thingspeak (and particle.io) using DS18B20
« Reply #1 on: April 01, 2016, 03:29:37 am »
Only just saw this one exeng... nice work! I bolted the wifi setup and thingspeak posting code onto my oak that was doing particle publish already, and it works a treat... I can shut down another ESP8266 that had been doing thinkspeak posts for the last few months now as this will do both jobs now. Thanks! I'll put my modified code up here in a day or so, as it works a bit differently due to deepSleeping and also dealing with a some different DS18B20 sensors that need a little hand-holding to co-operate... (the 'P' variant IIRC... stupid things take longer to do temperature conversions, etc...)

Pete
« Last Edit: April 01, 2016, 03:31:47 am by pfeerick »