DS18B20 TEMP sensor | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
DS18B20 TEMP sensor
No permission to create posts
April 6, 2012
1:45 am
Avatar
scottyratcliffe
New Member
Members
Forum Posts: 1
Member Since:
April 6, 2012
sp_UserOfflineSmall Offline

I have been trying to get temperature posted from multiple DS18B20 sensors, with no luck so far.

Here is the .pde for the onewire sensor

// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress ambient = { 0x28, 0x91, 0x9B, 0x49, 0x03, 0x00, 0x00, 0xC8 };
DeviceAddress ac = { 0x28, 0x27, 0xB2, 0x49, 0x03, 0x00, 0x00, 0x76 };
DeviceAddress seagate = { 0x28, 0x14, 0x9A, 0x49, 0x03, 0x00, 0x00, 0x04 };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(ambient, 10);
  sensors.setResolution(ac, 10);
  sensors.setResolution(seagate, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
 
  Serial.print("ambient is: ");
  printTemperature(ambient);
  Serial.print("\n\r");
  Serial.print("ac temp is: ");
  printTemperature(ac);
  Serial.print("\n\r");
  Serial.print("seagate temperature is: ");
  printTemperature(seagate);
  Serial.print("\n\r\n\r");
}

 

I pasted that in the basic thingspeak api like
/*

 ThingSpeak Client to Update Channel Feeds
 
 The ThingSpeak Client sketch is designed for the Arduino + Ethernet Shield.
 This sketch updates a channel feed with an analog input reading via the
 ThingSpeak API (http://community.thingspeak.com/documentation/)
 using HTTP POST.
 
 Getting Started with ThingSpeak:
 
    * Sign Up for New User Account - https://www.thingspeak.com/users/new
    * Create a New Channel by selecting Channels and then Create New Channel
    * Enter the Write API Key in this sketch under "ThingSpeak Settings"
 
 Created: January 25, 2011 by Hans Scharler (http://www.iamshadowlord.com)
 
 Additional Credits: Example sketches from Tom Igoe and David A. Mellis
 
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress ambient = { 0x28, 0x91, 0x9B, 0x49, 0x03, 0x00, 0x00, 0xC8 };
DeviceAddress ac = { 0x28, 0x27, 0xB2, 0x49, 0x03, 0x00, 0x00, 0x76 };
DeviceAddress seagate = { 0x28, 0x14, 0x9A, 0x49, 0x03, 0x00, 0x00, 0x04 };

// Local Network Settings
byte mac[]     = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
byte ip[]      = { 192, 168,   5,  249 };                // Must be unique on local network
byte gateway[] = { 192, 168,   5,   1 };
byte subnet[]  = { 255, 255, 255,   0 };

// ThingSpeak Settings
byte server[]  = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
String writeAPIKey = "C1SR65D8UOQZZMR8";    // Write API Key for a ThingSpeak Channel
const int updateInterval = 3000;        // Time interval in milliseconds to update ThingSpeak   
Client client(server, 80);

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int resetCounter = 0;

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
   // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(ambient, 10);
  sensors.setResolution(ac, 10);
  sensors.setResolution(seagate, 10);
  delay(1000);

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop()
{
  sensors.requestTemperatures();
 
  // Print Update Response to Serial Monitor
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
 
  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected)
  {
    Serial.println();
    Serial.println("...disconnected.");
    Serial.println();
    
    client.stop();
  }
 
  // Update ThingSpeak
  if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
  {
    updateThingSpeak("field1"=+String(ambient, DEC));
  }
 
  lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
  if (client.connect())
  {
    Serial.println("Connected to ThingSpeak...");
    Serial.println();
        
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.print(tsData);
    
    lastConnectionTime = millis();
    
    resetCounter = 0;
    
  }
  else
  {
    Serial.println("Connection Failed.");   
    Serial.println();
    
    resetCounter++;
    
    if (resetCounter >=5 ) {resetEthernetShield();}

    lastConnectionTime = millis();
  }
}

void resetEthernetShield()
{
  Serial.println("Resetting Ethernet Shield.");   
  Serial.println();
 
  client.stop();
  delay(1000);
 
  Ethernet.begin(mac, ip, gateway, subnet);
  delay(1000);
}

and i get

things_speak_issues.cpp: In function 'void loop()':
things_speak_issues:111: error: call of overloaded 'String(uint8_t [8], int)' is ambiguous
C:\Users\Scotty\Desktop\arduino-0023\hardware\arduino\cores\arduino/WString.h:39: note: candidates are: String::String(long unsigned int, int) <near match>
C:\Users\Scotty\Desktop\arduino-0023\hardware\arduino\cores\arduino/WString.h:38: note:                 String::String(long int, int) <near match>
C:\Users\Scotty\Desktop\arduino-0023\hardware\arduino\cores\arduino/WString.h:37: note:                 String::String(unsigned int, int) <near match>
C:\Users\Scotty\Desktop\arduino-0023\hardware\arduino\cores\arduino/WString.h:36: note:                 String::String(int, int) <near match>

 

any help would be nice?

April 17, 2012
6:16 pm
Avatar
Hans
Natick, MA

Admin
Forum Posts: 411
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

The "updateThingSpeak" function is expecting a string. 

Based on the error you are getting, the "String(uint8_t [8], int)" is not converting to a string.

The output of the one wire sensor function may not be a DEC, so you can't just convert it to a string like you have it.

I hope this points you in the right direction. I do not have any 1-wire experience and was hoping that the forum members had some input for you. What parts did you purchase to do the 1-wire? I might get them so I can replicate this and see what I find.

Another idea is that the sketch you are using is for Arduino 22. This might help to:

https://github.com/other/ThingSpeak-Arduino-Examples

Let us know what you find out.

June 4, 2012
6:22 am
Avatar
raklink
New Member
Members
Forum Posts: 2
Member Since:
June 4, 2012
sp_UserOfflineSmall Offline

Hi Scotty

I have a couple of DS18B20′s uploading my Solar Hot Water plus outside temps to ThingSpeak via Arduino. Code here:
https://sites.google.com/site/aussiepilot/Two%2018b20%20upload%20to%20thingspeak.txt?attredirects=0&d=1

External ThingSpeak link
https://www.thingspeak.com/channels/909?public=true

As per Han’s comment, I convert the DS18B20 output to integer value before I upload to ThingSpeak. As I am measuring from 0 Deg C through to 100 deg, I am happy rounding. This makes life easy !

As per code link above, to get Water Temp, I use
int WT = sensors.getTempC(WaterTemp);

Outside Temp
int OT = sensors.getTempC(OutsideTemp);

Then upload via

updateThingSpeak(“field1=”+String(WT, EC)+”&field2=”+String(OT, DEC));

You can see charts etc here
http://klinkfamilyadventures.blogspot.com.au/

Hope this helps

Regards
Rick

June 4, 2012
11:53 pm
Avatar
Hans
Natick, MA

Admin
Forum Posts: 411
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

Rick,

Great project! Thanks for providing your source code and the "how-to" part.

I went ahead and added your project to our blog:

http://community.thingspeak.com/2012/06/solar-hot-water-temperature-monitoring-with-arduino-thingspeak/

Let me know if you have any other info that you want to add or subtract.

Thanks again!

Hans

June 5, 2012
2:41 am
Avatar
raklink
New Member
Members
Forum Posts: 2
Member Since:
June 4, 2012
sp_UserOfflineSmall Offline

Hey, no problems Hans.

Hopefully it will make it easier for others to get their Temperature/Arduino/Thingspeak projects up and running. 

 

One small change to your blog post.

The blog code link you posted is for the small snippet of code to determine the ds18b20 unique address.
 

Probably worth changing to the link to the text file with full code listing

https://sites.google.com/site/aussiepilot/Two%2018b20%20upload%20to%20thingspeak.txt?attredirects=0&d=1

 

Thanks again for a great service in regards to ThingSpeak !!!

 

Rick

June 5, 2012
9:51 am
Avatar
Hans
Natick, MA

Admin
Forum Posts: 411
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

Updated... Thanks!

June 16, 2013
1:41 am
Avatar
turgo

Gold
Members
Forum Posts: 77
Member Since:
June 16, 2013
sp_UserOfflineSmall Offline

Here is how to convert a float to a string:

float temperature = 98.6;
char buffer[12];
String tempStr = dtostrf(temperature, 6,2,buffer);

October 11, 2015
12:11 am
Avatar
BSB

Silver
Members
Forum Posts: 19
Member Since:
October 10, 2015
sp_UserOfflineSmall Offline

@ Raklink
If you are still around just want to thank you for posting link to your code. It was almost what I wanted and needed only a minor tweak to get what i needed out of it.Cool

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online: cstapels
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

Team_mafia, icXu, emorphistechno, VTVMART, manimozhi, adamhebrew

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457