Hi, I have a problem with the following code. I’m using a Lemur board and a TinyRTC module on I2C along with a DS18B20 One Wire temp. sensor. Using SDK 4.3 and VS 2015.
The code all works thanks in large part to this forum. The program is a simple data logger for time and temp. which will eventually form part of a larger system. When I say it runs, it does so for just over one hour and them stops working altogether. I had originally set a Thread.Sleep period of 10 minutes, but changed it to the system of 5 secs. in a loop of 120 iterations.
It records to SD card the time and temp every 10 minutes and stops just over one hour. If I change the loop delay to record data every 5 minutes it still stops at just over one hour. I need it to run continuously. Is there something about .NetMF I am missing?
Program
using System;
using System.IO;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using StablePoint.Hardware.OneWire.Devices;
using StablePoint.Hardware.OneWire;
using GHI.Pins;
using GHI.IO.Storage;
namespace RTC
{
public class Program
{
public static void Main()
{
int i = 0;
OutputPort LED1 = new OutputPort((Cpu.Pin)FEZLemur.Gpio.Led1, true);
OutputPort LED2 = new OutputPort((Cpu.Pin)FEZLemur.Gpio.Led2, true);
InputPort mySwitch = new InputPort((Cpu.Pin)FEZLemur.Gpio.Ldr1, false, Port.ResistorMode.PullUp);
SDCard sd_card = new SDCard();
sd_card.Mount();
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
FileStream FileHandle = new FileStream(rootDirectory + @ "\data.csv", FileMode.Create);
RTC.RTC_1307 RTC_time = new RTC.RTC_1307();
var oneWirePin = ((Cpu.Pin)FEZLemur.Gpio.D8);
var oneWireBus = new Bus(oneWirePin);
var devices = oneWireBus.ScanForDevices(Family.Ds18B20);
Debug.Print(devices.Length + " DS18B20 sensor(s) found.");
//Set the date and time
// RTC_time.SetDateTime(new DateTime(2015, 12, 16, 11, 03, 30));
var ds18B20 = new Ds18B20(devices[0]);
ds18B20.SetResolution(Ds18B20.ConversionResolution.HalfDegree);
LED2.Write(true);
int myHour = RTC_time.GetDateTime().Hour;
int myMinute = RTC_time.GetDateTime().Minute;
int mySecond = RTC_time.GetDateTime().Second;
int myTimeStart = (myHour * 60 * 60) + (myMinute * 60) + mySecond;
while (mySwitch.Read())
{
i = 0;
while(i<=120 && mySwitch.Read())
{
LED1.Write(!LED1.Read());
Debug.Print(i + " " + "In short loop");
i++;
Thread.Sleep(5000);
}
myHour = RTC_time.GetDateTime().Hour;
myMinute = RTC_time.GetDateTime().Minute;
mySecond = RTC_time.GetDateTime().Second;
int myTimeFinish = (myHour * 60 * 60) + (myMinute * 60) + mySecond;
int myTotalTime = myTimeFinish - myTimeStart;
if (ds18B20.UpdateValues() == true)
{
string temp = ds18B20.Temperature.ToString() + "°C";
byte[] data = Encoding.UTF8.GetBytes(RTC_time.GetDateTime().ToString("G") + "," + 1 + "," + myTotalTime.ToString() + "," + temp + "\n");
FileHandle.Write(data, 0, data.Length);
Debug.Print("Recorded" + " " + myTotalTime.ToString() + " " + RTC_time.GetDateTime().ToString("G") + " " + temp);
}
else
{
//something's wrong. May happen with longer cables
Debug.Print("...failure! Maybe try again?");
}
}
LED2.Write(false);
FileHandle.Close();
Debug.Print("End");
sd_card.Unmount();
while(true)
{
Debug.Print("Loop");
Thread.Sleep(1000);
}
}
}
}
RTC Class
using System;
using Microsoft.SPOT.Hardware;
namespace RTC
{
class RTC_1307 : IDisposable
{
private I2CDevice I2C;
I2CDevice.I2CTransaction[] xaction;
ushort DS1307_Address = 0x68;
public void Dispose()
{
I2C.Dispose();
xaction = null;
}
public RTC_1307()
{
//Create I2C object
I2CDevice.Configuration conf = new I2CDevice.Configuration(DS1307_Address, 100);
I2C = new I2CDevice(conf);
}
public DateTime GetDateTime()
{
xaction = new I2CDevice.I2CTransaction[2];
xaction[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00 });
byte[] ReturnedDateTime = new byte[7];
xaction[1] = I2CDevice.CreateReadTransaction(ReturnedDateTime);
if (I2C.Execute(xaction, 1000) == 0)
{
new Exception("Failed to send I2C data");
}
int sec = bcdToDec(ReturnedDateTime[0]) & 0x7f;
int min = bcdToDec(ReturnedDateTime[1]);
int hour = bcdToDec(ReturnedDateTime[2]) & 0x3f;
int dayofweek = bcdToDec(ReturnedDateTime[3]);
int dayofmonth = bcdToDec(ReturnedDateTime[4]);
int month = bcdToDec(ReturnedDateTime[5]);
int year = bcdToDec(ReturnedDateTime[6]) + 2000;
DateTime dt = new DateTime(year, month, dayofmonth, hour, min, sec);
return dt;
}
public void SetDateTime(DateTime datetime)
{
xaction = new I2CDevice.I2CWriteTransaction[1];
byte[] sb = new byte[8] { 0x00,
decToBcd(datetime.Second),
decToBcd(datetime.Minute),
decToBcd(datetime.Hour),
decToBcd((int)datetime.DayOfWeek),
decToBcd(datetime.Day),
decToBcd(datetime.Month),
decToBcd(datetime.Year - 2000)
};
xaction[0] = I2CDevice.CreateWriteTransaction(sb);
if (I2C.Execute(xaction, 1000) == 0)
{
new Exception("Failed to send I2C data");
}
}
private byte decToBcd(int val)
{
return (byte)((val / 10 * 16) + (val % 10));
}
private byte bcdToDec(byte val)
{
return (byte)((val / 16 * 10) + (val % 16));
}
}
}