Main Site Documentation

Programmable thermostat settings, need some advice


#1

Guys,

One of the functions I’m implementing on a Cobra is a programmable thermostat. A friend of mine suggested me to use Hashtables to store the various times/temps but I can’t figure out the time part. Also, I’m not sure how to persist this to a file on SD storage.
Here’s the code i have so far:


    private double Setpoint()
    {
      Hashtable _Mon = new Hashtable();
      _Mon.Add("07:00", 21.00);
      _Mon.Add("09:00", 20.00);
      _Mon.Add("19:00", 21.00);
      _Mon.Add("23:00", 16.00);

      Hashtable _Tue = new Hashtable();
      _Tue.Add("07:00", 21.00);
      _Tue.Add("09:00", 20.00);
      _Tue.Add("19:00", 21.00);
      _Tue.Add("23:00", 16.00);

      Hashtable _Wed = new Hashtable();
      _Wed.Add("07:00", 21.00);
      _Wed.Add("09:00", 20.00);
      _Wed.Add("19:00", 21.00);
      _Wed.Add("23:00", 16.00);

      Hashtable _Thu = new Hashtable();
      _Thu.Add("07:00", 21.00);
      _Thu.Add("09:00", 20.00);
      _Thu.Add("19:00", 21.00);
      _Thu.Add("23:00", 16.00);

      Hashtable _Fri = new Hashtable();
      _Fri.Add("07:00", 21.00);
      _Fri.Add("09:00", 20.00);
      _Fri.Add("19:00", 21.00);
      _Fri.Add("23:00", 16.00);

      Hashtable _Sat = new Hashtable();
      _Sat.Add("09:00", 21.00);
      _Sat.Add("23:00", 16.00);

      Hashtable _Sun = new Hashtable();
      _Sun.Add("09:00", 21.00);
      _Sun.Add("23:00", 16.00);

      Hashtable _weekdays = new Hashtable();
      _weekdays.Add("Monday", _Mon);
      _weekdays.Add("Tuesday", _Tue);
      _weekdays.Add("Wednesday", _Wed);
      _weekdays.Add("Thursday", _Thu);
      _weekdays.Add("Friday", _Fri);
      _weekdays.Add("Saturday", _Sat);
      _weekdays.Add("Sunday", _Sun);

      Hashtable today = _weekdays[DateTime.Now.DayOfWeek.ToString()] as Hashtable;
      double setpoint = 0.0;
      foreach (DictionaryEntry time in today)
      {
        // Find the setpoint in today's hashtable depending on the current time
      }
      Console.WriteLine("setpoint= " + setpoint);
      return setpoint;
    }

For example, if it is now Wednesday 14:45h, setpoint should be 20.00
From 23:00 till 7:00 it should return 16.00

Any suggestion or improvement is very welcome. I’m not a C# guru, but trying to learn as fast as I can.

Thanks in advance


#2

Chris used hash tables in his clock project if I remember right.
Maybe it will help you understand/ modify.

http://files.chrisseto.com/62W


#3

Thanks Foekie, will have a look.


#4

As far as i can see, he’s using Arrays.


#5

Sorry, then this was not the correct project of him. He has a project with hash tables though.

This is the topic where mike helped him creating them:
http://www.tinyclr.com/forum/1/289/


#6

ok, i found a very nice TimeRange class on the internet: http://www.webdevbros.net/2007/08/24/timerange-object-for-c/

I had to change the way i declare the times in the hastable to:


      //There's a hashtable for every weekday, but today is wednesday ;)
      Hashtable _Wed = new Hashtable();
      _Wed.Add("0:00-7:00", 21.00);
      _Wed.Add("7:00-19:00", 20.00);
      _Wed.Add("19:00-23:00", 21.00);
      _Wed.Add("23:00-24:00", 16.00);

      Hashtable _weekdays = new Hashtable();
      _weekdays.Add("Monday", _Mon);
      _weekdays.Add("Tuesday", _Tue);
      _weekdays.Add("Wednesday", _Wed);
      _weekdays.Add("Thursday", _Thu);
      _weekdays.Add("Friday", _Fri);
      _weekdays.Add("Saturday", _Sat);
      _weekdays.Add("Sunday", _Sun);

and then, I’m able to get the correct value by doing:


      Hashtable today = _weekdays[DateTime.Now.DayOfWeek.ToString()] as Hashtable;
      double setpoint = 0.0;
      foreach (DictionaryEntry time in today)
      {
        TimeRange tr = null;
        tr = TimeRange.Parse(time.Key.ToString());
        if (tr.IsIn(new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 0))) setpoint = (double)time.Value;
      }
      return setpoint;

Hope it’s of any use to someone else.

Any idea how i can store the hashtables to a file? Or will I be better of creating an xml file?


#7

Looking over the SDK - Microsoft.SPOT.Reflection has both Serialize() and Deserialize() methods available. You likely could use those to put your objects into a file stream to the miniSD/SD card.

Or as you mentioned, you could use XML and the good thing is that the files are human readable as well. This is good because you could then just pop the card into a PC and review/edit the files simply outside of the FEZ. Otherwise you’d need take your code and create a desktop version of the software to do this.


#8

mhectorgato,

thanks for the advice. This is only a small part of a much bigger project i’m working on: http://www.tinyclr.com/forum/7/992/.

Plan is to set all the thermostat settings on the touch screen GUI, and send them over to the Cobra.


#9

[quote]This is the topic where mike helped him creating them:
[/quote]

In the referenced post I did not not actually implement a hashtable. I developed a class which had a hashtable type interface. The number of entries in the table, and the frequency of access, did not require an actual hashtable,.


#10

Well I’m not sure how that would all fit together, but I would imagine that you could use the Serialize/Deserialize to pass the objects (and their data) between devices or straight to a file.


#11

if your going to convert the hash table to xml to store the data on sd, then surely it makes sense to bin the hash table completely and just use xml all the way though ?? <<< not a suggestion more of a question based on other posts


#12

Assuming the temperature set time granularity is15 minutes, you could store the temperatures in a byte array.

24 hours/day * 4 setting/hour * 7 days/week = 672 bytes.

You can read and write the array to a SD.

Knowing the day of week, the hour, the minute, you could index into the array to find the termperature setting.

KEEP IT SIMPLE! :smiley: