public MeadowClient() { Led = new Led(Device, Device.Pins.OnboardLedGreen); try { sensor = new Sht31D(Device.CreateI2cBus()); ISpiBus spiBus = Device.CreateSpiBus(500); rfm9XDevice = new Rfm9XDevice(Device, spiBus, Device.Pins.D09, Device.Pins.D10, Device.Pins.D12); rfm9XDevice.Initialise(Frequency, paBoost: true, rxPayloadCrcOn: true); #if DEBUG rfm9XDevice.RegisterDump(); #endif rfm9XDevice.OnReceive += Rfm9XDevice_OnReceive; rfm9XDevice.Receive(deviceAddress); rfm9XDevice.OnTransmit += Rfm9XDevice_OnTransmit; } 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}%"); string payload = $"t {sensor.Temperature:0.0},h {sensor.Humidity:0}"; Led.IsOn = true; rfm9XDevice.Send(fieldGatewayAddress, Encoding.UTF8.GetBytes(payload)); Thread.Sleep(periodTime); } }
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); } }