Storing Weather data from CM2302 sensor onto SD card file doesn't save anything.
mundaneeffort
Posts: 29
Hello,
I am working on a small project right now to try and store weather data collected onto an SD card, where eventually the file could be run through a matlab program and plot the weather over time. I'm having a problem right now figuring out how to get the SD card to actually write the data to a text file, when I run the program an .ATA file gets created, but it contains no actual data. I was wondering if anyone had any idea where I might be going wrong. I know the dht22 library functions converts the integer values into floats, but I am wondering where the raw data is actually stored, or even the decimal converted data. I'm guessing it is in some array somewhere that I could access?
I am working on a small project right now to try and store weather data collected onto an SD card, where eventually the file could be run through a matlab program and plot the weather over time. I'm having a problem right now figuring out how to get the SD card to actually write the data to a text file, when I run the program an .ATA file gets created, but it contains no actual data. I was wondering if anyone had any idea where I might be going wrong. I know the dht22 library functions converts the integer values into floats, but I am wondering where the raw data is actually stored, or even the decimal converted data. I'm guessing it is in some array somewhere that I could access?
#include "simpletools.h" #include "dht22.h" int DO = 22, CLK = 23, DI = 24, CS = 25; // SD card pins on Propeller BOE int NUM = 0; // ------ Main Program ------ int main() { int n; sd_mount(DO, CLK, DI, CS); // Mount SD card FILE* fp = fopen("TempHumData.txt", "w"); // Open a file for writing while (NUM < 3) { int n = dht22_read(5); fwrite(&n, 400,15,fp); print("%s%03.2f%s\r", "The temperature is ", ((float) dht22_getTemp(FAHRENHEIT)) / 10.0, " degrees F"); print("%s%03.2f%s\r", "Humidity is ", ((float) dht22_getHumidity()) / 10.0, "%"); pause(2000); NUM = NUM + 1; } fclose(fp); fp = fopen("TempHumData.txt", "w"); fread(&n, 400, 15, fp); print(".2f%", n); fclose(fp); }
Comments
Figured out what I need for now, perhaps my phrasing was too ambiguous. Creating an array of the values myself and using fprintf to write them as decimal integers solved my problem.
I still have some random bits of code in here I was using for debugging purposes, but it works!
I attached pictures of both the terminal log and data file I wrote too. Hope this helps someone in the future.
Cheers!
first of all welcome to the forum.
Second, it is sort of funny that one finds a solution shortly after writing a post, happens quite often to me. Just describing the problem leads to a solution.
Enjoy!
Mike
Yea it's weird how it works like that. It helps to get the problem out of your head and onto something you can look at.
I just realized I need a time stamp too, I'm wondering what my options are there.
The safest way is to have a file of sufficient length already written with zeros or some other known value, and then open and re-write the file with records as needed. This way, all your data is there (perhaps excluding the last) when a power fail or reset for some other reason happens.
If you know your file is contiguous, you can just do a low level sector write.
I'm only writing 2 floats to the text file everytime I run, I've changed the fp = fopen("TempHumData.txt", "w") to fp = fopen("TempHumData.txt", "a") so that the data appends rather than overwrites it and closes.
Right now I'm working on how to get the propeller to take a sample every 30 minutes, append the data file and then close.
I know sleep would be easy but, I think this poses power consumption problems as I plan on running this with a battery pack outside for a few days in a row at least.
I think using waitcnt and clkfreq is what I'm looking for, here's a really brief pseudo code idea:
-Place gauge outside and run, manually record real time. (Not programming )
-Create 10 minute delay using waitcnt and clkfreq, reset clock and add 1 to a variable
-If variable == 2; run data collection loop, reset clock and reset variable to 0
I'm not concerned with hyper accurate time readings (like it's fine if I take the temperature and humidity at 12:02 rather than at 12), just with accidentally creating an overflow problem with my timer and not actually getting the reading done. I think the waitcnt register maxes out at around 50 seconds real time before resetting. Perhaps I could just make my program count higher...
EDIT: Lowering the clock speed though! Hmmmm..
source: https://forums.parallax.com/discussion/147677/timing-using-a-propeller-as-a-stopwatch
This seems like a tasty fix, gotta investigate lowering the clock speed.
My assembly is weak and I can't find a good example to change up the clock frequency using inline assembly that I can wrap my head around.
Just using sleep it is! For now...
Essentially I just sleep for 30 minutes, and though this is an increased power draw compared to waitcnt it should still last days powered from 4 AA batteries. I wrapped everything up in a while loop and reset the count after 30 minutes.
Seems simple and effective so far, I was wondering if anyone has any knowledge of long term clock issues with sleep. Like does the clock get messed up over time. I tested it over 30 minutes and it executed just fine as written below, I just worry if this will continue to function properly over multiple days.
Also, should I repost the finished project with pictures and complete code when I'm done with it in a new thread?
You will need to keep the xtal oscillator running for accuracy, but you can lower the run frequency to 5MHz (xtal * 1) and use waitcnt to lower again. IIRC overflow is ~53s at 80MHz, so 16 x 53s gives around 14min, so try using 10mins and perform every third count.
You dont think sleep will work if I'm not concerned with power management.
I have a 4 AA battery pack, should last a few days I think. I'm not hyper concerned with the clock accuracy either. If the weather is taken at 12:01 rather than 12 its not a huge deal.
I think this should work. I timed an hour and a half and got three measurements and it stored correctly.
Would I need inline assembly to get that clock frequency down? I looked at the propeller manual but didn't really understand the code I was seeing.
Also not sure what sleep translates to.
You will need help from someone with C experience.
Good luck!
Awesome, I'm looking to redo some of the project to make it more robust over the summer and will definitely check out that thread.
https://forums.parallax.com/discussion/146492/thermoprop-a-3-station-temperature-humidity-data-logger#latest
A great project IMHO!