Hello,
Im trying to run a tipping bucket rain gauge, Ds18b20, and monitor the power using fuel gauge all in the same time. When I run rain gauge and temp it works fine, when I run rain gauge and fuel it works fine. When I run all three, the boron turn solid white.
Side problem- for some reason once in a blue the rain gauge will log rain but there is no rain. For example it indoor right now and it will log 2 to 5 tip, sometimes more. For days it was running fine but when I power off and on sometime it acts up again. This could be a hardware issues. Im getting new rain gauges tomorrow. But it could be code issue and im just not seeing it.
Im new to coding so im sure I did something wrong. Im hoping someone smarter then me will see the issue and help get this running properly. Here is the code. Thank you very much for your time
// This #include statement was automatically added by the Particle IDE.
#include <DS18B20.h>
// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>
#include <math.h>
#ifndef TOKEN
#define TOKEN "A1E-" // Put here your Ubidots TOKEN
#endif
FuelGauge fuel;
char data[64];
Ubidots ubidots(TOKEN, UBI_EDUCATIONAL, UBI_TCP); //Use "UBI_EDUCATIONAL" when using ubidots education
// Each time we loop through the main loop, we check to see if it's time to capture the sensor readings
unsigned int sensorCapturePeriod = 100;
unsigned int timeNextSensorReading;
// Each time we loop through the main loop, we check to see if it's time to publish the data we've collected
unsigned int publishPeriod = 60000;
unsigned int timeNextPublish;
//STARTUP(WiFi.selectAntenna(ANT_AUTO)) //use exteral (WIFI ONLY) antenna if connected
DS18B20 ds18b20(D2, true); //Sets Pin D2 for Water Temp Sensor and
void setup() {
Serial.begin(115200);
ubidots.setDebug(true); // Comment this line for printing debug messages
initializeRainGauge();
//Solar Power Managment
//PMIC pmic; //Initalize the PMIC class so you can call the Power Management functions below.
//pmic.setChargeCurrent(0,0,1,0,0,0); //Set charging current to 1024mA (512 + 512 offset)
//pmic.setInputVoltageLimit(4840); //Set the lowest input voltage to 4.84 volts. This keeps my 5v solar panel from operating below 4.84 volts.
// Schedule the next sensor reading and publish events
timeNextSensorReading = millis() + sensorCapturePeriod;
timeNextPublish = millis() + publishPeriod;
}
void loop() {
// Capture any sensors that need to be polled (temp, humidity, pressure, wind vane)
// The rain and wind speed sensors use interrupts, and so data is collected "in the background"
if(timeNextSensorReading <= millis()) {
captureTemp();
// Schedule the next sensor reading
timeNextSensorReading = millis() + sensorCapturePeriod;
}
// Publish the data collected to Particle and to ThingSpeak
if(timeNextPublish <= millis()) {
// Get the data to be published
float tempF = getAndResetTempF();
float rainInches = getAndResetRainInches();
float voltage = fuel.getVCell();
float SoC = fuel.getSoC();
// Publish the data
publishToParticle(tempF,rainInches,voltage,SoC);
// Schedule the next publish event
timeNextPublish = millis() + publishPeriod;
}
delay(10);
}
void publishToParticle(float tempF, float rainInches,float voltage,float SoC) {
//Particle.publish("Rain", String::format(" %0.2f in",rainInches),60 , PRIVATE);
snprintf(data, sizeof(data), "%0.1f°F,%.2f in,%.2f V,%.2f %%",tempF,rainInches,voltage,SoC);
Particle.publish("TP1", data, PRIVATE);
//Ubidots
ubidots.add("Rain", rainInches);
ubidots.add("Temp", tempF);
ubidots.add("Volts", voltage);
ubidots.add("SoC", SoC);
ubidots.send();
}
//===========================================================================
// Temp DS18B20
//===========================================================================
float tempFTotal = 0.0;
unsigned int tempFReadingCount = 0;
void captureTemp() {
float celsius = ds18b20.getTemperature();
float tempF = ds18b20.convertToFahrenheit(celsius);
//If the result is reasonable, add it to the running mean
if(tempF > -50 && tempF < 150)
{
// Add the observation to the running sum, and increment the number of observations
tempFTotal += tempF;
tempFReadingCount++;
}
return;
}
float getAndResetTempF()
{
if(tempFReadingCount == 0) {
return 0;
}
float result = tempFTotal/float(tempFReadingCount);
tempFTotal = 0.0;
tempFReadingCount = 0;
return result;
}
//===========================================================================
// Rain Guage
//===========================================================================
int RainPin = D4;
volatile unsigned int rainEventCount;
unsigned int lastRainEvent;
float RainScaleInches = 0.01; // Each pulse is .01 inches of rain
void initializeRainGauge() {
pinMode(RainPin, INPUT_PULLUP);
rainEventCount = 0;
lastRainEvent = 0;
attachInterrupt(RainPin, handleRainEvent, FALLING);
return;
}
void handleRainEvent() {
// Count rain gauge bucket tips as they occur
// Activated by the magnet and reed switch in the rain gauge, attached to input D2
unsigned int timeRainEvent = millis(); // grab current time
// ignore switch-bounce glitches less than 10mS after initial edge
if(timeRainEvent - lastRainEvent < 10) {
return;
}
rainEventCount++; //Increase this minute's amount of rain
lastRainEvent = timeRainEvent; // set up for next event
}
float getAndResetRainInches()
{
float result = RainScaleInches * float(rainEventCount);
rainEventCount = 0;
return result;
}