Hello all, I changed over to the DS18B20 library and it seems to work accept I get a NAN reading after a good reading. I was thinking it has to do with moving code from loop area to timer area? I stared at the code stupidly long enough. I’m not a programmer by any means. Any hints would be appreciated.
I would like to keep the multi drop code for future use.
#include <DS18B20.h>
#include <blynk.h>
#include <PowerShield.h>
#include <HX711ADC.h>
#include <SparkFunMAX17043.h>
#include <SparkCorePolledTimer.h>
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
unsigned long firstAvailable = 0;
int counter;
retained int retainedCounter = 0;
SparkCorePolledTimer updateTimer(5000); //Create a timer object and set it's timeout in milliseconds //TIMER
void OnTimer(void); //Prototype for timer callback method //TIMER
char auth[] = "************************"; //blynk
double hiveweight = 0.0; //scale
double cellVoltage = 0.0; //powershield
double stateOfCharge = 0.0; //powershield
#define DOUT 3 //scale
#define CLK 2 //scale
HX711ADC scale(DOUT, CLK); //scale
PowerShield batteryMonitor; //powershield
const int MAXRETRY = 3; //temp DS18B20 library
const int pinOneWire = D5; //temp DS18B20 library
const int pinLED = D7; //temp DS18B20 library
const uint32_t msSampleTime = 2500; //temp DS18B20 library
const uint32_t msPublishTime = 30000; //temp DS18B20 library
const int nSENSORS = 2; //temp DS18B20 library
DS18B20 ds18b20(pinOneWire); //temp DS18B20 library
retained uint8_t sensorAddresses[nSENSORS][8]; //temp DS18B20 library
float celsius[nSENSORS] = { NAN, NAN }; //temp DS18B20 library
double _temp;
void setup() {
updateTimer.SetCallback(OnTimer); //TIMER
Blynk.begin(auth); // Here your Arduino connects to the Blynk Cloud. //blynk
batteryMonitor.begin(); // This sets up the fuel gauge //powershield
batteryMonitor.quickStart(); //powershield
pinMode(pinLED, OUTPUT); //temp DS18B20 library
ds18b20.resetsearch(); // initialise for sensor search //temp DS18B20 library
for (int i = 0; i < nSENSORS; i++) { // try to read the sensor addresses //temp DS18B20 library
ds18b20.search(sensorAddresses[i]); // and if available store //temp DS18B20 library
} //temp DS18B20 library
scale.power_up(); // SCALE-
Serial.begin(9600);
delay(1000); // delay for everything to settle down
// zero factor from SparkFun_HX711_Calibration by sarahalmutlaq
// long zero_factor = scale.read_average(); //Get a baseline reading (for zero factor)
// Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
// Serial.println(zero_factor); //note the zero factor to set the offset
scale.set_scale(6538.f); //6538 TAL214 load cell,6218wii load cell // SCALE-this value is obtained by calibrating the scale with known weightssee the README for details, buttoncell5430,wiicell758;
// long zero_factor = 8483395; //set the zero_factor with number determined from "scale.read_average(20)"
// scale.set_offset(zero_factor); //Zero out the scale using a previously known zero_factor for a permanent weight on the scale
scale.tare(); // SCALE-reset the scale to 0, if your not setting the offset with "zero_factor
}
void loop() {
Serial.begin(9600);
bool wifiReady = WiFi.ready();
bool cloudReady = Particle.connected();
Serial.printlnf("wifi=%s cloud=%s counter=%d retainedCounter=%d", (wifiReady ? "on" : "off"), (cloudReady ? "on" : "off"),
counter++, retainedCounter++);
if (wifiReady) {
updateTimer.Update(); //TIMER, code executed at "void OnTimer(void) then continues here??
Blynk.run(); // All the Blynk Magic happens here... //blynk
// if (firstAvailable == 0) {
// firstAvailable = millis();
// }
// if (millis() - firstAvailable > 15000) {
// // After we've been up for 15 seconds, go to sleep. The delay is so the serial output gets written out before
// // sleeping.
// Serial.println("calling System.sleep(SLEEP_MODE_DEEP, 15 seconds)");
// delay(2);
// System.sleep(SLEEP_MODE_DEEP, 360); // deep sleep in seconds, 360sec=6min
// // The rest of the code here is not reached. SLEEP_MODE_DEEP causes the code execution to stop,
// // and when wake up occurs, it's like a reset where you start again with setup(), all variables are
// // cleared, etc.
// Serial.println("returned from sleep, should not occur");
// }
}
else {
firstAvailable = 0;
}
delay(1000);
}
void OnTimer(void) { //Handler for the timer, will be called automatically
Serial.begin(9600);
hiveweight = (scale.get_units(10)); // SCALE-
hiveweight = round(hiveweight * 10) / 10; // SCALE, round to first decimal place
Serial.println("HIVE WEIGHT LBS");
Serial.println(hiveweight); // SCALE-
//temp DS18B20 library
static uint32_t msSample = 0;
static uint32_t msPublish = 0;
if (millis() - msSample >= msSampleTime) { //temp DS18B20 library
msSample = millis();
for (int i = 0; i < nSENSORS; i++) {
float temp = getTemp(sensorAddresses[i]);
if (!isnan(temp)) celsius[i] = temp;
}
} //temp DS18B20 library
if (millis() - msPublish >= msPublishTime) {
msPublish = millis();
Serial.println("Publishing now.");
publishData();
}
}
double getTemp(uint8_t addr[8]) { //temp DS18B20 library
double _temp;
int i = 0;
do {
_temp = ds18b20.getTemperature(addr);
} while (!ds18b20.crcCheck() && MAXRETRY > i++);
if (i < MAXRETRY) {
_temp = ds18b20.convertToFahrenheit(_temp);
Serial.println(_temp);
}
else {
_temp = NAN;
Serial.println("Invalid reading"); //temp DS18B20 library
}
return _temp;
}
void publishData() {
char szInfo[64];
snprintf(szInfo, sizeof(szInfo), "%.1f �C, %.1f �C", celsius[0], celsius[1]);
// Particle.publish("dsTmp", szInfo, PRIVATE);
//temp DS18B20 library
cellVoltage = batteryMonitor.getVCell(); // Read the voltage of the LiPo //powershield
stateOfCharge = batteryMonitor.getSoC(); // Read the State of Charge of the LiPo //powershield
Serial.println("BATTERY");
Serial.println(stateOfCharge);
Serial.println("VOLTAGE");
Serial.println(cellVoltage);
Blynk.virtualWrite(V1, _temp); //BLYNK, photon BC, in office
Blynk.virtualWrite(V2, _temp); //BLYNK, photon BC
Blynk.virtualWrite(V5, hiveweight); //BLYNK, photon BC
Blynk.virtualWrite(V6, hiveweight); //BLYNK, photon BC
Blynk.virtualWrite(V9, cellVoltage); //BLYNK, photon BC
Blynk.virtualWrite(V10, cellVoltage); //BLYNK, photon BC
Blynk.virtualWrite(V13, stateOfCharge); //BLYNK, photon BC
Blynk.virtualWrite(V14, stateOfCharge); //BLYNK, photon BC
Blynk.virtualWrite(V17, _temp); //BLYNK, photon BC, in office
Blynk.virtualWrite(V18, _temp); //BLYNK, photon BC
Blynk.virtualWrite(V19, hiveweight); //BLYNK, photon BC
Blynk.virtualWrite(V20, hiveweight); //BLYNK, photon BC
Blynk.virtualWrite(V21, cellVoltage); //BLYNK, photon BC
Blynk.virtualWrite(V22, cellVoltage); //BLYNK, photon BC
Blynk.virtualWrite(V23, stateOfCharge); //BLYNK, photon BC
Blynk.virtualWrite(V24, stateOfCharge); //BLYNK, photon BC
Serial.println("sent it to BLYNK!");
}
This is the Serial output I get-
wifi=on cloud=on counter=1388 retainedCounter=5848
wifi=on cloud=on counter=1389 retainedCounter=5849
wifi=on cloud=on counter=1390 retainedCounter=5850
wifi=on cloud=on counter=1391 retainedCounter=5851
HIVE WEIGHT LBS
0.00
the temp is
70.70
the temp again
the temp is
nan
the temp again