public void Run(IBackgroundTaskInstance taskInstance)
        {
            Radio.OnDataReceived    += Radio_OnDataReceived;
            Radio.OnTransmitFailed  += Radio_OnTransmitFailed;
            Radio.OnTransmitSuccess += Radio_OnTransmitSuccess;

            Radio.Initialize(ChipEnablePin, ChipSelectPin, InterruptPin);
            Radio.Address    = Encoding.UTF8.GetBytes(BaseStationAddress);
            Radio.Channel    = nRF24Channel;
            Radio.PowerLevel = PowerLevel.High;
            Radio.DataRate   = DataRate.DR250Kbps;
            Radio.IsEnabled  = true;

            Radio.IsAutoAcknowledge    = true;
            Radio.IsDyanmicAcknowledge = false;
            Radio.IsDynamicPayload     = true;

            Debug.WriteLine("Address: " + Encoding.UTF8.GetString(Radio.Address));
            Debug.WriteLine("PA: " + Radio.PowerLevel);
            Debug.WriteLine("IsAutoAcknowledge: " + Radio.IsAutoAcknowledge);
            Debug.WriteLine("Channel: " + Radio.Channel);
            Debug.WriteLine("DataRate: " + Radio.DataRate);
            Debug.WriteLine("IsDynamicAcknowledge: " + Radio.IsDyanmicAcknowledge);
            Debug.WriteLine("IsDynamicPayload: " + Radio.IsDynamicPayload);
            Debug.WriteLine("IsEnabled: " + Radio.IsEnabled);
            Debug.WriteLine("Frequency: " + Radio.Frequency);
            Debug.WriteLine("IsInitialized: " + Radio.IsInitialized);
            Debug.WriteLine("IsPowered: " + Radio.IsPowered);

            deferral = taskInstance.GetDeferral();

            timer = ThreadPoolTimer.CreatePeriodicTimer(SendMessageTimer, TimeSpan.FromSeconds(20));

            Debug.WriteLine("Start completed");
        }
Esempio n. 2
0
        static void Main()
        {
            RF24 radio        = new RF24();
            byte messageCount = System.Byte.MaxValue;

            try
            {
                radio.OnDataReceived    += Radio_OnDataReceived;
                radio.OnTransmitFailed  += Radio_OnTransmitFailed;
                radio.OnTransmitSuccess += Radio_OnTransmitSuccess;

#if NETDUINO3_WIFI
                radio.Initialize("SPI2", PinNumber('A', 2), PinNumber('A', 1), PinNumber('A', 3));
#endif
                radio.Address = Encoding.UTF8.GetBytes(DeviceAddress);

                radio.Channel = 15;
                //radio.PowerLevel = PowerLevel.Max;
                //radio.PowerLevel = PowerLevel.High;
                //radio.PowerLevel = PowerLevel.Low;
                radio.PowerLevel = PowerLevel.Minimum;
                radio.DataRate   = DataRate.DR250Kbps;
                //radio.DataRate = DataRate.DR1Mbps;
                //radio.DataRate = DataRate.DR2Mbps;
                radio.IsEnabled = true;

                radio.IsAutoAcknowledge    = true;
                radio.IsDyanmicAcknowledge = false;
                radio.IsDynamicPayload     = true;

                Debug.WriteLine($"Address: {Encoding.UTF8.GetString(radio.Address, 0, radio.Address.Length)}");
                Debug.WriteLine($"PowerLevel: {radio.PowerLevel}");
                Debug.WriteLine($"IsAutoAcknowledge: {radio.IsAutoAcknowledge}");
                Debug.WriteLine($"Channel: {radio.Channel}");
                Debug.WriteLine($"DataRate: {radio.DataRate}");
                Debug.WriteLine($"IsDynamicAcknowledge: {radio.IsDyanmicAcknowledge}");
                Debug.WriteLine($"IsDynamicPayload: {radio.IsDynamicPayload}");
                Debug.WriteLine($"IsEnabled: {radio.IsEnabled}");
                Debug.WriteLine($"Frequency: {radio.Frequency}");
                Debug.WriteLine($"IsInitialized: {radio.IsInitialized}");
                Debug.WriteLine($"IsPowered: {radio.IsPowered}");

                while (true)
                {
                    string payload = $"hello {messageCount}";
                    messageCount -= 1;

                    Debug.WriteLine($"{DateTime.UtcNow:HH:mm:ss}-TX {payload.Length} byte message {payload}");
                    radio.SendTo(Encoding.UTF8.GetBytes(BaseStationAddress), Encoding.UTF8.GetBytes(payload));

                    Thread.Sleep(30000);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
Esempio n. 3
0
        public MeadowApp()
        {
            try
            {
                var config = new Meadow.Hardware.SpiClockConfiguration(2000, SpiClockConfiguration.Mode.Mode0);

                ISpiBus spiBus = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.MOSI, Device.Pins.MISO, config);

                Radio.OnDataReceived    += Radio_OnDataReceived;
                Radio.OnTransmitFailed  += Radio_OnTransmitFailed;
                Radio.OnTransmitSuccess += Radio_OnTransmitSuccess;

                Radio.Initialize(Device, spiBus, Device.Pins.D09, Device.Pins.D10, Device.Pins.D11);

                Radio.Address              = Encoding.UTF8.GetBytes(DeviceAddress);
                Radio.Channel              = nRF24Channel;
                Radio.PowerLevel           = PowerLevel.High;
                Radio.DataRate             = DataRate.DR250Kbps;
                Radio.IsEnabled            = true;
                Radio.IsAutoAcknowledge    = true;
                Radio.IsDyanmicAcknowledge = false;
                Radio.IsDynamicPayload     = true;

                Console.WriteLine($"Address: {Encoding.UTF8.GetString(Radio.Address)}");
                Console.WriteLine($"Channel: {Radio.Channel}");
                Console.WriteLine($"Frequency: {Radio.Frequency}");
                Console.WriteLine($"PowerLevel: {Radio.PowerLevel}");
                Console.WriteLine($"DataRate: {Radio.DataRate}");
                Console.WriteLine($"IsEnabled: {Radio.IsEnabled}");
                Console.WriteLine($"IsAutoAcknowledge: {Radio.IsAutoAcknowledge}");
                Console.WriteLine($"IsDynamicAcknowledge: {Radio.IsDyanmicAcknowledge}");
                Console.WriteLine($"IsDynamicPayload: {Radio.IsDynamicPayload}");
                Console.WriteLine($"IsInitialized: {Radio.IsInitialized}");
                Console.WriteLine($"IsPowered: {Radio.IsPowered}");

                while (true)
                {
                    string payload = "hello " + DateTime.Now.ToShortTimeString();
                    Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss}-TX {payload.Length} byte message {payload}");

                    Radio.SendTo(Encoding.UTF8.GetBytes(BaseStationAddress), Encoding.UTF8.GetBytes(payload));

                    Task.Delay(10000).Wait();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return;
            }
        }
Esempio n. 4
0
        public MeadowClient()
        {
            Led = new Led(Device, Device.Pins.OnboardLedGreen);

            try
            {
                sensor = new Sht31D(Device.CreateI2cBus());

                var config = new Meadow.Hardware.SpiClockConfiguration(
                    2000,
                    SpiClockConfiguration.Mode.Mode0);

                ISpiBus spiBus = Device.CreateSpiBus(
                    Device.Pins.SCK,
                    Device.Pins.MOSI,
                    Device.Pins.MISO, config);

                Radio.OnDataReceived    += Radio_OnDataReceived;
                Radio.OnTransmitFailed  += Radio_OnTransmitFailed;
                Radio.OnTransmitSuccess += Radio_OnTransmitSuccess;

                Radio.Initialize(Device, spiBus, Device.Pins.D09, Device.Pins.D10, Device.Pins.D11);
                Radio.Address = Encoding.UTF8.GetBytes(DeviceAddress);

                Radio.Channel    = nRF24Channel;
                Radio.PowerLevel = PowerLevel.Low;
                Radio.DataRate   = DataRate.DR250Kbps;
                Radio.IsEnabled  = true;

                Radio.IsAutoAcknowledge    = true;
                Radio.IsDyanmicAcknowledge = false;
                Radio.IsDynamicPayload     = true;

                Console.WriteLine($"Address: {Encoding.UTF8.GetString(Radio.Address)}");
                Console.WriteLine($"PowerLevel: {Radio.PowerLevel}");
                Console.WriteLine($"IsAutoAcknowledge: {Radio.IsAutoAcknowledge}");
                Console.WriteLine($"Channel: {Radio.Channel}");
                Console.WriteLine($"DataRate: {Radio.DataRate}");
                Console.WriteLine($"IsDynamicAcknowledge: {Radio.IsDyanmicAcknowledge}");
                Console.WriteLine($"IsDynamicPayload: {Radio.IsDynamicPayload}");
                Console.WriteLine($"IsEnabled: {Radio.IsEnabled}");
                Console.WriteLine($"Frequency: {Radio.Frequency}");
                Console.WriteLine($"IsInitialized: {Radio.IsInitialized}");
                Console.WriteLine($"IsPowered: {Radio.IsPowered}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            while (true)
            {
                sensor.Update();

                Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss}-TX T:{sensor.Temperature:0.0}C H:{sensor.Humidity:0}%");

                Led.IsOn = true;

                string values = "T " + sensor.Temperature.ToString("F1") + ",H " + sensor.Humidity.ToString("F0");

                // Stuff the 2 byte header ( payload type & deviceIdentifierLength ) + deviceIdentifier into payload
                byte[] payload = new byte[1 + Radio.Address.Length + values.Length];
                payload[0] = (byte)((1 << 4) | Radio.Address.Length);
                Array.Copy(Radio.Address, 0, payload, 1, Radio.Address.Length);
                Encoding.UTF8.GetBytes(values, 0, values.Length, payload, Radio.Address.Length + 1);

                Radio.SendTo(Encoding.UTF8.GetBytes(BaseStationAddress), payload);

                Thread.Sleep(periodTime);
            }
        }