The problem you’re seeing is because you have a print statement outside your closing brace of the if statement, so it will execute every time loop() runs. I’ve copied you code, but moved the closing brace to a more visible position so you can see it (you should be careful to line up your braces to make your code more readable),
Thanks, @Ric. That definitely helped. I’m soooo close, or so it appears. The only thing I’m not getting is an initial print of all the variables. When I heat up the sensors or add humidity to the RHT, I get the proper printout for each sensor but I don’t get an initial read. That doesn’t make sense to me as I’ve checked by doing a Serial.print of each variable (e.g.; tempF, tempFdelta, lasttempF) and without the IF statement I get the correct initial readings (i.e., I know the initial lasttempF is 0, etc.) I’ll add my entire code below. Any suggestions are greatly appreciated, not just on my initial reading issue but throughout. I’m a rookie and it’s a great learning experience.
#include <Ubidots.h>
#define TOKEN " "//PUT TOKEN HERE WHEN READY TO GO LIVE
#include <SparkFunRHT03.h>//included from the library for the RHT
//Ubidots ubidots(TOKEN);//uncomment when ready to go live
//FuelGauge fuel;//uncomment when ready to go live
const int RHT03_DATA_PIN = D1;//provdes temp & humidity inside van
RHT03 rht;//creates the RHT object
const int pinOut = A2;//provdes temp under van, near macerator (critical component)
const int pinFridge = A4;//you can guess this one
int tempF = 0;//temp inside van
int humidity = 0;//humidity inside van
int tempOut = 0;//outside temp reading
int tempFridge = 0;//yeah, that one
int SoC = 0;//fuel gauge
int lasttempF = 0;//I'll use these to see if we really need to upload data each time
int lasthumidity = 0;
int lasttempOut = 0;
int lasttempFridge = 0;
int lastSoC = 0;
int tempFdelta;
int humiditydelta;
int tempOutdelta;
int tempFridgedelta;
int SoCdelta;
void setup() {
pinMode(3, OUTPUT);//RHT - I only want sensors energized when Electron awake to save power
pinMode(4, OUTPUT);//OUT
pinMode(5, OUTPUT);//FRIDGE
rht.begin(RHT03_DATA_PIN);//initialize the RHT sensor
//ubidots.setMethod(TYPE_UDP);//uncomment out when ready to go live
Serial.begin(9600);//definitely worth checking if this works at my desk before going live
}
void loop() {
digitalWrite(3, HIGH);//power up the sensors
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(1000);//necessary delay for RHT to pass unstable status
int update = rht.update();//get temp and humidity readings from inside the van
int humidity = rht.humidity();
int tempF = rht.tempF();
tempOut = ((((((analogRead(pinOut) * (3.3/4095)) *1000.0) - 500)/10)*(9.0/5.0)) + 32.0);
tempFridge = ((((((analogRead(pinFridge) * (3.3/4095)) *1000.0) - 500)/10)*(9.0/5.0)) + 32.0);
//SoC = fuel.getSoC();//uncomment out when ready to go live
//Create logic so only significant readings get uploaded to cloud
//STILL NEED TO ADD UBIDOTS HANDLER TO EACH CONDITIONAL STATEMENT
tempFdelta = tempF-lasttempF;
tempFdelta = abs (tempFdelta);
if(tempFdelta>2){
Serial.printlnf("vanTemp %d\n", tempF);
lasttempF = tempF;
}
humiditydelta = humidity-lasthumidity;
humiditydelta = abs (humiditydelta);
if(humiditydelta>2){
Serial.printlnf("humidity %d\n", humidity);
lasthumidity = humidity;
}
tempOutdelta = tempOut-lasttempOut;
tempOutdelta = abs (tempOutdelta);
if(tempOutdelta>2){
Serial.printlnf("tempOut %d\n", tempOut);
lasttempOut = tempOut;
}
tempFridgedelta = tempFridge-lasttempFridge;
tempFridgedelta = abs (tempFridgedelta);
if(tempFridgedelta>2){
Serial.printlnf("fridge %d\n", tempFridge);
lasttempFridge = tempFridge;
}
//if(abs(SoC-lastSoC)>2){//uncomment and fix when ready to go live
//ubidots.sendAll();//uncomment out when ready to go live
digitalWrite(3, LOW);//power down the sensors
digitalWrite(4, LOW);
digitalWrite(5, LOW);
//System.sleep(BTN,FALLING,SLEEP_NETWORK_STANDBY,900);//uncomment when ready to go live
delay(5000);//comment out when ready to go live
}
Could it be that your IF statement is not true when your initial 0 readings are actually less than the new reading instead of greater than the new reading?
You will need to check if the new condition is greater than or less than 2 instead of just checking if the reading is greater than 2 only. Your IF statement is only true and only prints if the new reading is gerater than 2 but not if it's less than 2.
Look this over for trying to see if the new reading is Greater than or Less than 2 in the same IF statment.
@RWB - I’m not sure I get what you’re trying to tell me and it’s not for lack of trying. I’ve re-read your statement several times and I apologize but I don’t understand. The bad part is I suspect you are telling me what I need to know.
If I want the IF statement to be true in both conditions (>2 or <2), I think that’s the same as saying it’s true for any value other than 2, including 0 (i.e., it didn’t change.) My problem will be that if the temp rises any amount other than exactly 2 (or doesn’t change at all) I will get a reading. I only want to get a reading (after the initial reading) if the temp goes up by >2.
So I misspoke earlier. I think I’m misunderstanding what happens when the code first runs. When I start I have set lasttempF to 0. So my initial tempFdelta should equal tempF (whatever reading I just got) minus lasttempF (which I set to 0) = tempF. But I see now lasttempF has already changed to my current reading! In other words I expected the first print to read:
vanTemp 74
lasttempF 0
tempFdelta 74
Which would make my IF statement true. I’m missing something basic here.
I was just thinking that your If statement was only going to print IF the temp change was positive 2 but now I see its the Delta that your looking for the 2 degree change so it should print regardless if it’s 2 degrees + or - since your using the “abs” of the temp delta.
I copied your code, and tested it. I put an extra print statement at the top of loop(), and I saw my first temperature and humidity readings 1 second after that printed (as expected). So, it appears to me that your code works just fine.
I also put in a line that doesn’t start the serial until you press any key, otherwise it’s hard to catch any printouts the first time loop() runs. I think that’s your problem; you’re just missing that first print, so you won’t see anything until the temperature changes by more than 2 degrees.
Serial.begin(9600);
while(!Serial.available()) Particle.process(); // start the serial monitor, then press any key
I want to send out another thank you to @ScruffR, @Ric and @RWB for their help on this project. It’s been working great over the last couple of days and I’ve learned a lot through the process. I’m now ready to 3D print the enclosure and run my sensor wires to their proper locations (once it stops snowing.)
The logic is doing its job at keeping my data usage to a minimum. So far, I’m looking at between 1 and 2MB of data usage per month. This will all depend, of course, on how variable the temperatures are during the day: more swings, more data. But even at 5MB I’m currently looking at only $7/month.
One change I made was to leave the power always on to the sensors and not allow the Electron to sleep. I was having troubles getting accurate, consistent readings when the power would go on-and-off, even when I increased the “on” time. I’ll use more power, of course, but that’s immaterial as I can leave the unit plugged into the coach DC which recharges by a pretty massive solar system.
I was a little concerned about having to use Ubidots as my experience has been with Blynk and I love their dashboard format. I ended up finding Ubidots was very easy to learn and and it turns out if you just open the browser on your cell phone the dashboard is sufficient (you’ll have to click on the attachment to see the whole thing.)
Ubidots sure does make it easy to create a custom dashboard and then also setup SMS and Email alerts for basically free. That’s why I recommend it to everybody as an easy way to get started.
@Silverminer, how has your monitoring project been going? I’m in the same “boat” as you - all my Photon work has been with Blynk, and I’d like to keep all my projects there. How about an Electron in the RV doing a publish of grouped sensor every so often, maybe more often if something changes. Then a Photon at home that’s subscribing to that data and pushing it up to Blynk? It does add an extra “moving part”, but my home internet is pretty stable, and I on my hacked home security system I have that extra Photon up in my master bedroom, and use LED signaling class to control the RGB LED if a door is left open, or an alarm is going off. If the light is sold green (at night), then all is well. If it’s flashing yellow, somethings left open. Flashing red, something’s open when system is armed. Has worked out well and wife appreciates seeing the green light when we go up to bed. Using this publish/subscribe method should cut down on data usage - although that’s an assumption on my part, reading Particle’s literature leads me to believe a publish of some grouped data points (ie: 68.7,64,12.65,12.7) should be less data than making a call to ubidots?
Hope your project is going well. Let us know how it’s working out in practice!
The project has gone very well. Depending on weather changes and how often I travel outside the reception area for the Electron, I run about 1 MB of data per month. I occasionally get into a second 1 MB. I have the data usage limit set at 2 MB but I’ve never reached it.
I thought about using a Photon when I’m at home port but it’s pretty hard to justify the hassle and expense. At most the current setup costs me $4/month - $48/year. I could drop that substantially if I deactivated the SIM for 6 or so months out of the year. I’m too lazy for that and I kind of like the fact that I have all that data at my fingertips.
Good to know that you’ve had good luck with Electron to Ubidots and stayed under 2mb!
My “photon at home” idea doesn’t involve switching back and forth between an Electron and Photon on the RV. The Electron is always on the RV, the Photon is always home. The Electron publishes grouped sensor data, the Photon subscribes to that data and does stuff. In my case, I want the Photon to interact with the Blynk app on my phone. Since I’ve only got this half built and haven’t tested publishes vs. updating Blynk directly from an Electron, I could be wrong about the amount of data used. But, if publishing uses half the data that Blynk updates do, then my update frequency can double. Or, I can stay well under the 1mb/month, increasing update frequency when “emergencies” happen (a battery is quickly dying or something). I should have this master\slave setup working with an Electron in the next week or two, I’ll reply back if you’re at all interested…
Update on this project. This project was working great for four (4) years with Ubidots. (And I’ve always stayed below 2MB data!) Recently Ubidots got rid of Ubidots for Education and introduced Ubidots STEM. Now I can’t get my electron to send data to my Ubidots app. I’ve tried several things (possibly making things worse since it’s also been four (4) years since I did any programming!) If anyone has the time, would you mind looking to see if you notice any glaring problems with my latest code? Thanks!
#include <Ubidots.h>
//#define TOKEN "myoldtoken"//this was my old token
#include <SparkFunRHT03.h>//included from the library for the RHT
#ifndef TOKEN
#define TOKEN "mynewtoken" //this is the new Ubidots STEM token Feb 2021
#endif
//Ubidots ubidots(TOKEN, UBI_TCP); // Comment this line to use another protocol.
//Ubidots ubidots(TOKEN, UBI_HTTP); // Uncomment this line to use HTTP protocol.
Ubidots ubidots(TOKEN, UBI_UDP); // Uncomment this line to use UDP protocol
const int RHT03_DATA_PIN = D1;//provdes temp & humidity inside van
RHT03 rht;//creates the RHT object
const int pinOut = A2;//provides temp under van, near macerator (critical component)
const int pinFridge = A4;//you can guess this one
int tempF = 0;//temp inside van
int humidity = 0;//humidity inside van
int tempOut = 0;//outside temp reading
int tempFridge = 0;//yeah, that one
int SoC = 0;//fuel gauge
FuelGauge fuel;
int lasttempF = 0;//I'll use these to see if we really need to upload data each time
int lasthumidity = 0;
int lasttempOut = 0;
int lasttempFridge = 0;
int lastSoC = 0;
int tempFdelta;
int humiditydelta;
int tempOutdelta;
int tempFridgedelta;
int SoCdelta;
void setup() {
rht.begin(RHT03_DATA_PIN);//initialize the RHT sensor
//Serial.begin(9600);//definitely worth checking if this works at my desk before going live
//while(!Serial.available()) Particle.process(); // start the serial monitor, then press any key
}
void loop() {
int update = rht.update();//get temp and humidity readings from inside the van
int humidity = rht.humidity();
int tempF = rht.tempF();
tempF = (tempF-4);//calibration adjustment
tempOut = ((((((analogRead(pinOut) * (3.3/4095)) *1000.0) - 500)/10)*(9.0/5.0)) + 32.0);//calibrated
tempFridge = ((((((analogRead(pinFridge) * (3.3/4095)) *1000.0) - 500)/10)*(9.0/5.0)) + (32.0-6.0));
SoC = fuel.getSoC();
//Create logic so only significant readings get uploaded to cloud
tempFdelta = tempF-lasttempF;
tempFdelta = abs (tempFdelta);
if(tempFdelta>2 && tempF < 150){
//Serial.printlnf("vanTemp %d\n", tempF);
//Serial.printlnf("lasttempF %d\n", lasttempF);
//Serial.printlnf("tempFdelta %d\n", tempFdelta);
ubidots.add("t", tempF);
lasttempF = tempF;
}
humiditydelta = humidity-lasthumidity;
humiditydelta = abs (humiditydelta);
if(humiditydelta>2){
//Serial.printlnf("humidity %d\n", humidity);
ubidots.add("h", humidity);
lasthumidity = humidity;
}
tempOutdelta = tempOut-lasttempOut;
tempOutdelta = abs (tempOutdelta);
if(tempOutdelta>2 && tempOut < 150){
//Serial.printlnf("tempOut %d\n", tempOut);
ubidots.add("o", tempOut);
lasttempOut = tempOut;
}
tempFridgedelta = tempFridge-lasttempFridge;
tempFridgedelta = abs (tempFridgedelta);
if(tempFridgedelta>2 && tempFridge < 150){
//Serial.printlnf("fridge %d\n", tempFridge);
ubidots.add("f", tempFridge);
lasttempFridge = tempFridge;
}
SoCdelta = SoC-lastSoC;
SoCdelta = abs (SoCdelta);
if(SoCdelta>2){
//Serial.printlnf("SoC %d\n", SoC);
ubidots.add("b", SoC);
lastSoC = SoC;
}
ubidots.send();
delay(1200000);//20 minute delay
}```
Hi Tom,
perhaps the library was updated to point to the new endpoint (server) they are using. Since you did not mention if you updated the library (ubidots) to the latest, then I would recommend you do that.
At the same time, you can read more about doing the publish with a webhook here:
Thanks, Gustavo. Yes, I had updated the ubidots library thinking those might have changed but that didnt’ help. I was hoping not to reinvent the wheel but that might be what it’ll take. I’ll review the link. Thanks, again.
1- I followed the instructions on the link last week and it worked like a charm - and that - in one shot
2- if you use a webhook, the data is encrypted all the way to the Ubidots server. This might not be so important for a home project but still is a positive
I’m happy for you, haha. I just spent north of an hour going step-by-step through the instructions and can’t get mine to work. My program compiles but that’s likely just a coincidence. I’m going to have to find more time to start back through the instructions and get a better understanding of what I’m actually doing. Or just kill the project. I’m less worried about data encryption and more just want the stupid thing to work like it did before.