Ejemplo n.º 1
0
        public void Display(ArrayList displayList, DS1307 clock, Schedule schedule)
        {
            var now = clock.Get();

            displayList.Add("Time: " + now.ToString("U") + "\r\n");
            displayList.Add("Heater Schedule Today: ");
            schedule.GetDayTimeSlots(now.DayOfWeek, displayList);
            displayList.Add("\r\n");
            displayList.Add("Heater Status: ");

            if (schedule.GetHeaterStatus(now) == true)
            {
                displayList.Add("ON");
            }
            else
            {
                displayList.Add("OFF");
            }

            if (schedule.WaterHeaterManualOverride)
            {
                displayList.Add(" [manual override]");
            }
            else
            {
                displayList.Add(" [scheduled]");
            }

            displayList.Add("\r\n");
        }
Ejemplo n.º 2
0
            public void synch()
            {
                var RTC_Clock = new DS1307();

                //RTC_Clock.Set(new DateTime(2018, 6, 18, 20, 00, 00)); // (year, month, day, hour, minute, second)

                RTC_Clock.Halt(false);  /* To make shure RTC is running */

                while (true)
                {
                    try
                    {
                        DateTime DateTimeNTP = NTPTime("pool.ntp.org", -180);

                        if (!(DateTimeNTP.Year == 1900))
                        {
                            // Synch RTC nd system clock from NTPTime
                            Debug.Print("Synch OK!");
                            Debug.Print("Internet Time " + DateTimeNTP.ToString());
                            Debug.Print("System Clock  Before Synch " + DateTime.Now.ToString());
                            Utility.SetLocalTime(DateTimeNTP);
                            Debug.Print("System Clock After Synch " + DateTime.Now.ToString());

                            //Debug.Print("RTC Before " + RTC_Clock.Get().ToString());
                            RTC_Clock.Halt(true);
                            RTC_Clock.Set(DateTimeNTP);
                            RTC_Clock.Halt(false);
                            Debug.Print("RTC After Synch " + RTC_Clock.Get().ToString());
                        }
                        else
                        {
                            Debug.Print("NTP socket failure!  Check Internet connection");
                            Debug.Print("Synch system Clock using RTC");
                            Utility.SetLocalTime(RTC_Clock.Get());
                            Debug.Print("System Clock " + DateTime.Now.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Print(ex.ToString());
                        Debug.Print("General Failure!  Verify internet connection and RTC");
                    }
                    /* sleep for 1 minute */
                    Thread.Sleep(60000);
                }
            }
Ejemplo n.º 3
0
        public static void LogTemperature(object obj)
        {
            LedRed.Write(true);
            var tickStart = Utility.GetMachineTime().Ticks;
            var now       = Clock.Get();

            ThermoCouple.Read();
            var elapsedMs    = (int)((Utility.GetMachineTime().Ticks - tickStart) / TimeSpan.TicksPerMillisecond);
            var date         = AddZeroPrefix(now.Year) + "/" + AddZeroPrefix(now.Month) + "/" + AddZeroPrefix(now.Day);
            var time         = AddZeroPrefix(now.Hour) + ":" + AddZeroPrefix(now.Minute) + ":" + AddZeroPrefix(now.Second) + ":" + AddZeroPrefix(elapsedMs);
            var celsius      = Shorten(ThermoCouple.Celsius.ToString());
            var farenheit    = Shorten(ThermoCouple.Farenheit.ToString());
            var latestRecord = date + "," + time + "," + celsius + "," + farenheit;

            Debug.Print(latestRecord);
            try {
                if (CardDetect.Read() == false)
                {
                    var filename = SdMountPoint + BuildTemperatureLogFilename(now);
                    if (File.Exists(filename) == false)
                    {
                        using (var tempLogFile = new StreamWriter(filename, true)) {
                            tempLogFile.WriteLine("date,time,celsius,fahrenheit");
                        }
                    }
                    using (var tempLogFile = new StreamWriter(filename, true)) {
                        if (Buffer.Count != 0)
                        {
                            foreach (var bufferedLine in Buffer)
                            {
                                tempLogFile.WriteLine(bufferedLine);
                            }
                            Buffer.Clear();
                        }
                        tempLogFile.WriteLine(latestRecord);
                        tempLogFile.Flush();
                    }
                }
                else
                {
                    LogLine("No card in reader. Buffering record.");
                    Buffer.Add(latestRecord);
                }
                LedRed.Write(false);
            } catch (OutOfMemoryException e) {
                LogLine("Memory full. Clearing buffer.");
                Buffer.Clear();
            } catch (IOException e) {
                LogLine("IO error. Resetting peripherals.");
                Buffer.Add(latestRecord);
                ResetPeripherals.Set();
            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            var clock = new DS1307();

            // Set the clock to some arbitrary date / time
            clock.Set(new DateTime(2011, 1, 2, 20, 20, 20));

            // Make sure the clock is running
            clock.Halt(false);

            // Test reading RTC clock registers
            Debug.Print("Before Halt: " + clock.Get().ToString());

            // Halt the clock for 3 seconds
            clock.Halt(true);
            Thread.Sleep(3000);

            // Should be the same time as "Before Halt"
            Debug.Print("After Halt for 3 seconds (should be the same time): " + clock.Get().ToString());

            // Resume the clock
            clock.Halt(false);

            // Sleep for another 1.5 second
            Thread.Sleep(1500);

            // Should be just one second later since the clock's oscillator was resumed
            Debug.Print("Resumed Clock (should be time + ~1 sec): " + clock.Get().ToString());

            // Requires an oscilloscope or an interrupt handler on the microcontroller to see the effects
            TestSquareWaves(ref clock);

            // Test writing to arbitrary registers
            Debug.Print("Writing distinct RAM registers (writing the register number to itself)");
            for (byte I = DS1307.DS1307_RAM_START_ADDRESS; I <= DS1307.DS1307_RAM_END_ADDRESS; I++)
            {
                clock.WriteRegister(I, I);
            }

            // Test reading from arbitrary registers
            Debug.Print("Reading distinct RAM registers (the registers and values read should be the same)");
            for (byte I = DS1307.DS1307_RAM_START_ADDRESS; I <= DS1307.DS1307_RAM_END_ADDRESS; I++)
            {
                Debug.Print(I.ToString() + ": " + clock[I].ToString());
            }

            // Test writing to the RAM as a single block
            //-------------01234567890123456789012345678901234567890123456789012345
            string Text = "[There are 56 bytes in the RTC RAM buffer and that's it]";

            Debug.Print("Writing string: " + Text + " (Length=" + Text.Length.ToString() + ") to the RAM as a block.");
            var ram = new byte[DS1307.DS1307_RAM_SIZE];

            // Copy the string to the ram buffer
            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                ram[I] = (byte)Text[I];
            }
            // Write it to the RAM in the clock
            clock.SetRAM(ram);

            // Zero out the ram buffer
            ram = null;
            // Zero out the string
            Text = null;

            // Test reading from the RAM as a single block
            Debug.Print("Reading from the RAM as a block...");
            ram = clock.GetRAM();

            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                Text += (char)ram[I];
            }

            Debug.Print("RAM: " + Text + " (Length=" + Text.Length.ToString() + ")");

            // Sleep another 5 seconds before exiting
            Thread.Sleep(5 * 1000);

            // Reset the clock & RAM
            clock.Set(new DateTime(2011, 2, 17, 21, 36, 00));

            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                ram[I] = (byte)0;
            }
            // Write it to the RAM in the clock
            clock.SetRAM(ram);
        }
Ejemplo n.º 5
0
        public static void Main()
        {
            const int millisecIncrement    = 100;
            const int millisecElapsedLimit = 60000;

            var millisecCounter = 0;
            var currentHeat     = HighHeat;

            var currentHeaterStatus  = false;
            var previousHeaterStatus = true;
            var initialize           = true;

            _ledHighHeat.SetDutyCycle(0);
            _ledLowHeat.SetDutyCycle(0);

            InitializeClock();

            Log("\r\nWater Heater Controller v1.0\r\n");
            Log("Initializing...");
            LoadSchedule();

            PowerServo(true);
            Log("Centering servo");
            _servo.Center();
            Log("Setting heater on high heat by default");
            _servo.Move(Center, currentHeat);
            Log("Running...");
            PowerServo(false);

            while (true)
            {
                if (_serialUI.SerialErrorReceived == true)
                {
                    _serialUI.Dispose();
                    _serialUI     = new SerialUserInterface();
                    _showMainMenu = true;
                    Log("Serial error received: serial UI object recycled");
                }

                if (_showMainMenu == true)
                {
                    _showMainMenu = false;
                    MainMenu(null);
                }

                millisecCounter += millisecIncrement;

                if (millisecCounter >= millisecElapsedLimit || _scheduleChange == true)
                {
                    millisecCounter     = 0;
                    currentHeaterStatus = _schedule.GetHeaterStatus(_clock.Get());
                }

                if (currentHeaterStatus != previousHeaterStatus || _scheduleChange == true || initialize == true)
                {
                    Log("Heater state change");

                    _ledOverride.Write(_schedule.WaterHeaterManualOverride);

                    if (currentHeaterStatus)
                    {
                        Log("Setting heater on high");
                        PowerServo(true);
                        _servo.Move(currentHeat, HighHeat);
                        PowerServo(false);
                        _ledHighHeat.SetDutyCycle(50);
                        _ledLowHeat.SetDutyCycle(0);
                        currentHeat = HighHeat;
                    }
                    else
                    {
                        Log("Setting heater on low");
                        PowerServo(true);
                        _servo.Move(currentHeat, LowHeat);
                        PowerServo(false);
                        _ledHighHeat.SetDutyCycle(0);
                        _ledLowHeat.SetDutyCycle(50);
                        currentHeat = LowHeat;
                    }
                    previousHeaterStatus = currentHeaterStatus;
                    _scheduleChange      = false;
                    initialize           = false;
                }

                if (_shutdown)
                {
                    Log("Shutting down");
                    Log("Moving servo to center...");
                    _ledHighHeat.SetDutyCycle(50);
                    _ledLowHeat.SetDutyCycle(50);
                    PowerServo(true);
                    _servo.Move(currentHeat, Center);
                    PowerServo(false);

                    Log("Shutdown complete");
                    Log("Cycle power to restart.");

                    var dutyCycle = 0;
                    var direction = 1;

                    while (true)
                    {
                        _ledHighHeat.SetDutyCycle((uint)dutyCycle);
                        _ledLowHeat.SetDutyCycle((uint)dutyCycle);
                        dutyCycle += direction;
                        if (dutyCycle == 50)
                        {
                            direction = -1;
                        }
                        else if (dutyCycle == 0)
                        {
                            direction = 1;
                        }
                        Thread.Sleep(50);
                    }
                }

                Thread.Sleep(millisecIncrement);
            }
        }