public static void Main() { var ds1307 = new DS1307(); //Comment the following line to not set the time to DS1307 board again ds1307.SetTime( Year: 2018, Month: 01, Day: 23, Hour: 16, Minute: 59, Second: 0 ); while (true) { var time = ds1307.GetTime(); var formattedDate = StringUtility.Format( "Netduino Time in UTC: {0}", time.ToString("yyyy-MM-ddTHH:mm:ssZ") ); Debug.Print(formattedDate); //sleep for 5 seconds Thread.Sleep(5000); } }
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"); }
// Test the square wave frequencies supported by the clock (oscilloscope or interrupt handler useful here). public static void TestSquareWaves(ref DS1307 clock) { Debug.Print("1Hz frequency test"); clock.SetSquareWave(DS1307.SQWFreq.SQW_1Hz, DS1307.SQWDisabledOutputControl.One); Thread.Sleep(5 * 1000); Debug.Print("4kHz frequency test"); clock.SetSquareWave(DS1307.SQWFreq.SQW_4kHz, DS1307.SQWDisabledOutputControl.One); Thread.Sleep(5 * 1000); Debug.Print("8kHz frequency test"); clock.SetSquareWave(DS1307.SQWFreq.SQW_8kHz, DS1307.SQWDisabledOutputControl.One); Thread.Sleep(5 * 1000); Debug.Print("32kHz frequency test"); clock.SetSquareWave(DS1307.SQWFreq.SQW_32kHz, DS1307.SQWDisabledOutputControl.One); Thread.Sleep(5 * 1000); // Test the logic levels when the oscillator is off clock.Halt(true); // No frequency, square wave output pin pulled high Debug.Print("Square Wave disabled, square wave output pin pulled high"); clock.SetSquareWave(DS1307.SQWFreq.SQW_OFF, DS1307.SQWDisabledOutputControl.One); Thread.Sleep(5 * 1000); // No frequency, square wave output pin pulled low Debug.Print("Square Wave disabled, square wave output pin pulled low"); clock.SetSquareWave(DS1307.SQWFreq.SQW_OFF, DS1307.SQWDisabledOutputControl.Zero); Thread.Sleep(5 * 1000); // Resume the oscillator clock.Halt(false); }
public static void Main() { //set current date and time + 1 or 2 minutes var newDateTime = new DateTime(2012, 09, 04, 21, 30, 45); Debug.Print("Wait for " + newDateTime); using (var userButton = new InterruptPort(Stm32F4Discovery.ButtonPins.User, false, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeLow)) { var ds1307 = new DS1307(); var storeData = Reflection.Serialize(newDateTime, typeof(DateTime)); ds1307.WriteRam(storeData); //push userbutton when time comes userButton.OnInterrupt += (d1, d2, t) => { ds1307.SetDateTime(newDateTime); Debug.Print("Initialized"); }; Thread.Sleep(Timeout.Infinite); } }
public void Load() { var rtc = DS1307.GetSingleton(); var s = rtc.GetStrings(); if (s.Length == 11) { try { //DualAxis = s[0] == "Y"; DualAxis = false; EastAzimuth = Convert.ToInt32(s[1]); WestAzimuth = Convert.ToInt32(s[2]); Latitude = (float)Convert.ToDouble(s[3]); Longitude = (float)Convert.ToDouble(s[4]); MaximumElevation = Convert.ToInt32(s[5]); MinimumElevation = Convert.ToInt32(s[6]); VerticalLength = Convert.ToInt32(s[7]); HorizontalLength = Convert.ToInt32(s[8]); VerticalSpeed = Convert.ToInt32(s[9]); HorizontalSpeed = Convert.ToInt32(s[10]); } catch (Exception ex) { DefaultSettings(); } } else // default { DefaultSettings(); } }
public void Initialize() { SetupLCD(); if (SetupLCD()) { if (SetupRTC()) { //45.421389, 75.691667 //_latitude = DegreesMinuteSecondDecimalDegrees("45.24.40"); //_longitude = DegreesMinuteSecondDecimalDegrees("75.41.53"); _latitude = 45.421389; _longitude = -75.691667; while (true) { var x1 = CalcSun(_latitude, _longitude, DateTime.Now, -4); #if DEBUG Debug.Print("azimuth: " + x1.azimuth.ToString("f2")); Debug.Print("elevation: " + x1.elevation.ToString("f2")); Debug.Print("eqTime: " + x1.eqTime.ToString("f2")); Debug.Print("solarDec: " + x1.solarDec.ToString("f2")); Debug.Print("coszen: " + x1.coszen.ToString("f4")); Debug.Print(x1.dark ? "night" : "day"); #else if (x1.dark) { _lcd.Clear(); _lcd.SetCursorPosition(0, 0); _lcd.Write(Resources.GetString(Resources.StringResources.Dark)); } else { //_lcd.Clear(); //_lcd.SetCursorPosition(0, 0); //_lcd.Write(Resources.GetString(Resources.StringResources.Azimuth)); //_lcd.Write(x1.azimuth.ToString("f2")); //_lcd.SetCursorPosition(0, 1); //_lcd.Write(Resources.GetString(Resources.StringResources.Elevation)); //_lcd.Write(x1.elevation.ToString("f2")); _lcd.Clear(); _lcd.SetCursorPosition(0, 0); var rtc = DS1307.GetSingleton(); var time = rtc.Get(); _lcd.Write(time.ToString(Resources.GetString(Resources.StringResources.DateFormat))); _lcd.SetCursorPosition(0, 1); _lcd.Write("A:"); _lcd.Write(x1.azimuth.ToString("f2")); _lcd.Write(" E:"); _lcd.Write(x1.elevation.ToString("f2")); } #endif Thread.Sleep(10000); } } } }
public MeadowApp() { Console.WriteLine("Initializing..."); rtc = new DS1307(Device.CreateI2cBus()); DS1307Test(); }
private void DS1307Test(II2cBus i2c) { Console.WriteLine("+DS1307 Test"); var rtc = new DS1307(i2c); Console.Write(" Checking IsRunning..."); var running = rtc.IsRunning; Console.WriteLine($"{(running ? "is running" : "is not running")}"); if (!running) { Console.WriteLine(" Starting RTC..."); rtc.IsRunning = true; } DateTime now = new DateTime(); while (true) { for (int i = 0; i < 3; i++) { now = rtc.GetTime(); Console.WriteLine($" RTC current time is: {now.ToString("MM/dd/yy HH:mm:ss")}"); Thread.Sleep(1000); } var rand = new Random(); if (now.Year < 2019) { now = DateTime.Now; } else { now = now.AddSeconds(rand.Next(1, 30)); } Console.WriteLine($" Setting RTC to : {now.ToString("MM/dd/yy HH:mm:ss")}"); var data = new byte[56]; for (int i = 0; i < 56; i++) { data[i] = (byte)rand.Next(256); } Console.WriteLine($" Writing to RTC RAM : {BitConverter.ToString(data)}"); rtc.WriteRAM(0, data); Console.Write($" Reading from RTC RAM : "); data = rtc.ReadRAM(0, 56); Console.WriteLine(BitConverter.ToString(data)); Thread.Sleep(rand.Next(1, 5)); } }
public RTC_Controller(string I2Cbus, int SQW_pin_n, IPinFunctionPolicy i2c_pin_policy) { i2c_pin_policy.SetPinAlternate(); clock = DS1307.CreateDevice(I2Cbus); SQW_pin = GpioController.GetDefault().OpenPin(SQW_pin_n); SQW_pin.SetDriveMode(GpioPinDriveMode.Input); Sync_clocks(); Enable_SQW(); }
public static void InitializePeripherals() { LedGreen.Write(true); Clock = new DS1307(); ThermoCouple = new Max6675(); InitializeStorage(true); InitializeClock(new DateTime(2012, 06, 06, 16, 22, 00)); ThermoCouple.Initialize(ThermoCoupleChipSelect); TemperatureSampler = new Timer(new TimerCallback(LogTemperature), null, 250, TemperatureLoggerPeriod); LedGreen.Write(false); }
public static void Main() { DS1307 RTC = new DS1307(); // Comment this line out to set the time for the first time //RTC.SetTime(Year: 2012, Month: 5, Day: 27, Hour: 22, Minute: 52, Second: 0); // Synchronises the Netduino with the DS1307 RTC module RTC.Synchronize(); while (true) { Debug.Print(DateTime.Now.ToString()); Thread.Sleep(1000); } }
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); } }
public void Save() { // verify limits if (MaximumElevation > 90) { MaximumElevation = 90; } if (MaximumElevation < 45) { MaximumElevation = 45; } if (MinimumElevation > 45) { MinimumElevation = 45; } if (MinimumElevation < 0) { MinimumElevation = 0; } if (EastAzimuth < 45) { EastAzimuth = 45; } if (EastAzimuth > 135) { EastAzimuth = 135; } if (WestAzimuth < 225) { WestAzimuth = 225; } if (WestAzimuth > 315) { WestAzimuth = 315; } var rtc = DS1307.GetSingleton(); string[] sa = new[] { DualAxis ? "Y" : "N", EastAzimuth.ToString(), WestAzimuth.ToString(), Latitude.ToString("f6"), Longitude.ToString("f6"), MaximumElevation.ToString(), MinimumElevation.ToString(), VerticalLength.ToString(), HorizontalLength.ToString(), VerticalSpeed.ToString(), HorizontalSpeed.ToString() }; rtc.SetRam(sa); }
private bool SetupRTC() { bool rVal = true; try { //var browserTime = new DateTime(2015, 09, 04, 14, 55, 30); //DS1307.SetRTCTime(browserTime); var rtc = DS1307.GetSingleton(); DS1307.SetNetduinoTimeFromRTC(); TrackerState = State.ClockSet; Thread.Sleep(2000); } catch (Exception) { rVal = false; } return(rVal); }
private static bool SetupRTC() { bool rVal = true; try { var rtc = DS1307.GetSingleton(); //DS1307.SetRTCTime(new DateTime(2014, 11, 17, 15, 14, 01)); var time = rtc.Get(); _lcd.SetCursorPosition(0, 0); _lcd.Write(time.ToString(Resources.GetString(Resources.StringResources.DateFormat))); DS1307.SetNetduinoTimeFromRTC(); Thread.Sleep(2000); } catch (Exception) { rVal = false; } return(rVal); }
/* * Commands from android app; * "Track" * "Cycle" * "Stop" * "GetConfiguration" * "GetDateTime" * "BroadcastPosition" * "StopBroadcast" * "SetC" * "SetL" * "SetO" * "SetDateTime" * "MoveTo" */ private void DoCommand(String command, String data) { var json = new JsonFormatter(); var enc = new UTF8Encoding(); StringBuilder sb; byte[] bytes; switch (command) { case "Track": Track(); break; case "Cycle": Test(); break; case "Stop": StopTracking(); break; case "GetConfiguration": var txfr = Configuration.GetConfigTransfer(); var sconfig = json.ToJson(txfr); sb = new StringBuilder(); sb.Append("\n"); sb.Append("Cf|"); sb.Append(sconfig); sb.Append("\r"); bytes = enc.GetBytes(sb.ToString()); _netduinoSerialPort.Write(bytes, 0, bytes.Length); _netduinoSerialPort.Flush(); break; case "GetDateTime": sb = new StringBuilder(); sb.Append("\n"); sb.Append("Dt|{"); sb.Append("\"sT\":"); // seconds in unixtime sb.Append(GetEpochTime().ToString()); sb.Append("}"); bytes = enc.GetBytes(sb.ToString()); _netduinoSerialPort.Write(bytes, 0, bytes.Length); _netduinoSerialPort.Flush(); break; case "BroadcastPosition": if (_broadcastTimer == null) { _broadcastTimer = new Timer(BroadcastTimerFunction, null, 2000, 2000); } break; case "StopBroadcast": if (_broadcastTimer != null) { _broadcastTimer.Dispose(); _broadcastTimer = null; } break; case "SetA": var ca = (Actuator)json.FromJson(enc.GetBytes(data), typeof(Actuator)); Configuration.SetActuator(ca); break; case "SetC": var cf = (Location)json.FromJson(enc.GetBytes(data), typeof(Location)); Configuration.SetLocation(cf); break; case "SetL": var limits = (Limits)json.FromJson(enc.GetBytes(data), typeof(Limits)); Configuration.SetLimits(limits); break; case "SetO": var options = (Options)json.FromJson(enc.GetBytes(data), typeof(Options)); Configuration.SetOptions(options); break; case "SetDateTime": var dt = UnixTimeStampToDateTime(double.Parse(data)); Utility.SetLocalTime(dt); DS1307.SetRTCTime(dt); break; case "MoveTo": MoveTo(data); break; } }
/// <summary> /// Constructor /// </summary> private Rtc() { #if !EMULATOR this.ds1307 = new DS1307(); #endif }
static void Main(string[] args) { try { /* #region lora * string SerialPortName = ConfigurationManager.AppSettings["Port"]; * UART = new SimpleSerial(SerialPortName, 57600); * UART.ReadTimeout = 0; * * //UART.ReadBufferSize = 1024; * //UART.WriteBufferSize = 1024; * //UART.BaudRate = 38400; * //UART.DataBits = 8; * //UART.Parity = Parity.None; * //UART.StopBits = StopBits.One; * * UART.DataReceived += UART_DataReceived; * Console.WriteLine("57600"); * Console.WriteLine("RN2483 Test"); * * var reset = Pi.Gpio[BcmPin.Gpio06]; //pin 6 * var reset2 = Pi.Gpio[BcmPin.Gpio06]; //pin 3 #endregion */ Pi.Init <BootstrapWiringPi>(); Console.WriteLine(">> Init mqtt"); MqttService mqtt = new MqttService(); DS1307 jam = new DS1307(); TempSensor tempSensor = new TempSensor(); var nowDate = DateTime.Now; Console.WriteLine("Device Date :" + nowDate); jam.SetDateAsync(nowDate).GetAwaiter().GetResult(); Console.WriteLine("TGL RTC:" + jam.GetDateAsync().GetAwaiter().GetResult().ToString()); ADS1115_PY analog = new ADS1115_PY(); Relay1 = Pi.Gpio[BcmPin.Gpio06]; //Pi.Gpio.Pin06; Relay2 = Pi.Gpio[BcmPin.Gpio13]; //Pi.Gpio.Pin13; Limit1 = Pi.Gpio[BcmPin.Gpio19]; //Pi.Gpio.Pin19; Limit2 = Pi.Gpio[BcmPin.Gpio26]; //Pi.Gpio.Pin26; Relay1Status = true; Relay2Status = true; WriteDigital(Relay1, Relay1Status); WriteDigital(Relay2, Relay2Status); mqtt.CommandReceived += (string Message) => { Task.Run(async() => { await DoAction(Message); }); }; var INTERVAL = int.Parse(ConfigurationManager.AppSettings["Interval"]); //analog.Start(); /* * analog.ChannelChanged += (object sender, ChannelReadingDone e) => * { * Console.WriteLine($">> channel {e.Channel} : {e.RawValue}"); * };*/ /* #region lora * reset.Write(true); * reset2.Write(true); * * Thread.Sleep(100); * reset.Write(false); * reset2.Write(false); * * Thread.Sleep(100); * reset.Write(true); * reset2.Write(true); * * Thread.Sleep(100); * * waitForResponse(); * * sendCmd("sys factoryRESET"); * sendCmd("sys get hweui"); * sendCmd("mac get deveui"); * * // For TTN * sendCmd("mac set devaddr AAABBBEE"); // Set own address * Thread.Sleep(1000); * sendCmd("mac set appskey 2B7E151628AED2A6ABF7158809CF4F3D"); * Thread.Sleep(1000); * * sendCmd("mac set nwkskey 2B7E151628AED2A6ABF7158809CF4F3D"); * Thread.Sleep(1000); * * sendCmd("mac set adr off"); * Thread.Sleep(1000); * * sendCmd("mac set rx2 3 868400000");//869525000 * Thread.Sleep(1000); * * sendCmd("mac join abp"); * sendCmd("mac get status"); * sendCmd("mac get devaddr"); * Thread.Sleep(1000); #endregion */ while (true) { /* * for (int i = 0; i < 4; i++) * { * Console.WriteLine($"A{i} = {analog.read_adc(i)}"); * }*/ var sensor = new DeviceData() { LimitSwitch1 = ReadDigital(Limit1), LimitSwitch2 = ReadDigital(Limit2), Relay1 = Relay1Status, Relay2 = Relay2Status, TDS1 = analog.read_adc(0), TDS2 = analog.read_adc(1), Temp = tempSensor.Read() }; Console.WriteLine(">>------------------>>"); Console.WriteLine($"TDS 1: {sensor.TDS1}"); Console.WriteLine($"TDS 2: {sensor.TDS2}"); Console.WriteLine($"Relay 1: {sensor.Relay1}"); Console.WriteLine($"Relay 2: {sensor.Relay2}"); Console.WriteLine($"Limit 1: {sensor.LimitSwitch1}"); Console.WriteLine($"Limit 2: {sensor.LimitSwitch2}"); Console.WriteLine($"Temp: {sensor.Temp}"); mqtt.PublishMessage(JsonConvert.SerializeObject(sensor)); /* #region lora * var jsonStr = JsonConvert.SerializeObject(sensor); * Debug.Print("kirim :" + jsonStr); * //PrintToLcd("send count: " + counter); * sendData(jsonStr); * Thread.Sleep(INTERVAL); * byte[] rx_data = new byte[20]; * * if (UART.BytesToRead > 0) * { * var count = UART.Read(rx_data, 0, rx_data.Length); * if (count > 0) * { * Debug.Print("count:" + count); * var hasil = new string(System.Text.Encoding.UTF8.GetChars(rx_data)); * Debug.Print("read:" + hasil); * * //mac_rx 2 AABBCC * } * } #endregion */ Thread.Sleep(INTERVAL); } } catch (Exception ex) { Console.WriteLine(ex); } }
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); }
public static void Main() { #if SD_ENABLED // If your Netduino can't execute the next line of code, make sure you got at least firmware 4.1.1 beta 1 // See also: http://forums.netduino.com/index.php?/topic/1592-netduino-firmware-v411-beta-1/ StorageDevice.MountSD("SD", SPI_Devices.SPI1, Pins.GPIO_PIN_D10); // Determines the filename string filename = ""; int index = 0; do { filename = @"\SD\LOGGER" + Tools.ZeroFill(index, 2) + ".CSV"; ++index; }while (File.Exists(filename)); // Starts writing to the file FileStream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter writer = new StreamWriter(stream); // Writes file headers writer.WriteLine("ticks,datetime,light,temp"); #endif // LEDs OutputPort red = new OutputPort(Pins.GPIO_PIN_D2, false); OutputPort green = new OutputPort(Pins.GPIO_PIN_D3, false); // An analog light sensor IADCPort light = new Netduino.ADC(Pins.GPIO_PIN_A0); light.RangeSet(0, 1024); // An analog temperature sensor Tmp36 temperature = new Tmp36(new Netduino.ADC(Pins.GPIO_PIN_A1)); // Time module (comment out SetTime once, to set the clock) DS1307 time = new DS1307(); /*time.SetTime( * Day: 11, * Month: 8, * Year: 2012, * Hour: 12, * Minute: 0, * Second: 0 * );*/ time.Synchronize(); while (true) { // Green status LED ON green.Write(true); // Builds the output string output = ""; output += DateTime.Now.Ticks.ToString() + ", "; output += DateTime.Now.ToString() + ", "; output += light.RangeRead().ToString() + ", "; output += temperature.Temperature.ToString(); // Prints the output to the debugger Debug.Print(output); #if SD_ENABLED // Writes the output to the SD buffer writer.WriteLine(output); #endif // Green status LED OFF, Red status LED ON green.Write(false); red.Write(true); #if SD_ENABLED // Flushes the buffers to the SD card writer.Flush(); stream.Flush(); #endif // Red status LED OFF red.Write(false); // Sleeps for a second Thread.Sleep(1000); } }