I continue to play with the new Photon2 module. Today was starting to work through what it takes to migrate my main Boron based code to the Photon2. Some things worked some things didn’t. As I investigate each item I’ll make a post in the community.
Issue 2: A standard DS18B20 temperature sensor does not work as tested on the latest deviceOS 5.3.2.
So I also use a DS18B20 standard run of the mill temperature sensor. When trying out to Photon2 on the carrier board, I noticed I was not getting any data for temperature. I’m not sure if this is an issue with the DS18B20 library or something else. I went back to the basics of a breadboard, a DS18B20 and the example sketch from the DS18B20 library and it just returns ‘nan’.
Here is the sketch:
#include "Particle.h"
#include <DS18B20.h>
#include <math.h>
void setup();
void loop();
void publishData();
void getTemp();
const int MAXRETRY = 10;
const uint32_t msSAMPLE_INTERVAL = 2500;
const uint32_t msMETRIC_PUBLISH = 10000;
const int16_t dsVCC = D2;
const int16_t dsData = D3;
const int16_t dsGND = D4;
// Sets Pin D3 as data pin and the only sensor on bus
DS18B20 ds18b20(dsData, true);
char szInfo[64];
double celsius;
double fahrenheit;
uint32_t msLastMetric;
uint32_t msLastSample;
void setup() {
Serial.begin(115200);
pinMode(dsGND, OUTPUT);
digitalWrite(dsGND, LOW);
pinMode(dsVCC, OUTPUT);
digitalWrite(dsVCC, HIGH);
delay(1000);
}
void loop() {
if (millis() - msLastSample >= msSAMPLE_INTERVAL){
getTemp();
}
if (millis() - msLastMetric >= msMETRIC_PUBLISH){
Serial.println("Publishing now.");
publishData();
}
}
void publishData(){
sprintf(szInfo, "%2.2f", fahrenheit);
Particle.publish("dsTmp", szInfo, PRIVATE);
msLastMetric = millis();
}
void getTemp(){
float _temp;
int i = 0;
do {
_temp = ds18b20.getTemperature();
} while (!ds18b20.crcCheck() && MAXRETRY > i++);
if (i < MAXRETRY) {
celsius = _temp;
fahrenheit = ds18b20.convertToFahrenheit(_temp);
Serial.println(fahrenheit);
}
else {
celsius = fahrenheit = NAN;
Serial.println("Invalid reading");
}
msLastSample = millis();
}
Here’s the basic hardware configuration:
Particle Boron using the same pin D3 for data: Works properly. Returns the correct temperature
Particle Photon2 using the same pin D3 for data: Does not work. Returns “NAN”
I spent a little time poking through the library. The OneWire.H has reference to PLATFORM_ID for something about setting fast pin access (I’m not sure what this is). Is there something in the library that needs to account for P2 or what will it take to allow P2s to work with DS18B20.
I also tested the two different example sketches in the OneWire Library Address Scanner and DS18x20 and both worked on Boron but neither one found any OneWire device on P2.
What am I doing wrong or what needs to change???
@ScruffR - Looks like you publish the original library. Any ideas or things I can try?