Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample updates #22

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Meadow;
using Meadow.Gateways.Bluetooth;
using MeadowBleLed.Controllers;

namespace MeadowBleLed.Connectivity;

public class BluetoothServer
{
readonly string ON = "73cfbc6f61fa4d80a92feec2a90f8a3e";
readonly string OFF = "6315119dd61949bba21def9e99941948";
readonly string PULSING = "d755180131fc435da9941e7f15e17baf";
readonly string BLINKING = "3a6cc4f2a6ab4709a9bfc9611c6bf892";
readonly string RUNNING_COLORS = "30df1258f42b4788af2ea8ed9d0b932f";

private CommandController commandController;

ICharacteristic LedOn;
ICharacteristic LedOff;
ICharacteristic LedBlink;
ICharacteristic LedPulse;
ICharacteristic LedRunColors;

public BluetoothServer()
{
commandController = Resolver.Services.Get<CommandController>();
}

private void LedOnCharacteristicValueSet(ICharacteristic c, object data)
{
commandController.FireLedOn();
}

private void LedOffCharacteristicValueSet(ICharacteristic c, object data)
{
commandController.FireLedOff();
}

private void LedBlinkCharacteristicValueSet(ICharacteristic c, object data)
{
commandController.FireLedBlink();
}

private void LedPulseCharacteristicValueSet(ICharacteristic c, object data)
{
commandController.FireLedPulse();
}

private void LedRunColorsCharacteristicValueSet(ICharacteristic c, object data)
{
commandController.FireLedRunColors();
}

public Definition GetDefinition()
{
LedOn = new CharacteristicBool(
name: nameof(LedOn),
uuid: ON,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write);
LedOn.ValueSet += LedOnCharacteristicValueSet;

LedOff = new CharacteristicBool(
name: nameof(LedOff),
uuid: OFF,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write);
LedOff.ValueSet += LedOffCharacteristicValueSet;

LedBlink = new CharacteristicBool(
name: nameof(LedBlink),
uuid: BLINKING,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write);
LedBlink.ValueSet += LedBlinkCharacteristicValueSet;

LedPulse = new CharacteristicBool(
name: nameof(LedPulse),
uuid: PULSING,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write);
LedPulse.ValueSet += LedPulseCharacteristicValueSet;

LedRunColors = new CharacteristicBool(
name: nameof(LedRunColors),
uuid: RUNNING_COLORS,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write);
LedRunColors.ValueSet += LedRunColorsCharacteristicValueSet;

ICharacteristic[] characteristics =
{
LedOn,
LedOff,
LedBlink,
LedPulse,
LedRunColors
};

var service = new Service(
name: "Service",
uuid: 253,
characteristics
);

return new Definition("MeadowRGB", service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Meadow;
using System;

namespace MeadowBleLed.Controllers;

public class CommandController
{
public event EventHandler<bool> LedOnValueSet = default!;
public event EventHandler<bool> LedOffValueSet = default!;
public event EventHandler<bool> LedBlinkValueSet = default!;
public event EventHandler<bool> LedPulseValueSet = default!;
public event EventHandler<bool> LedRunColorsValueSet = default!;

public CommandController()
{
Resolver.Services.Add(this);
}

public void FireLedOn()
{
LedOnValueSet?.Invoke(this, true);
}

public void FireLedOff()
{
LedOffValueSet?.Invoke(this, true);
}

public void FireLedBlink()
{
LedBlinkValueSet?.Invoke(this, true);
}

public void FireLedPulse()
{
LedPulseValueSet?.Invoke(this, true);
}

public void FireLedRunColors()
{
LedRunColorsValueSet?.Invoke(this, true);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using Meadow;
using Meadow.Foundation.Leds;
using Meadow.Hardware;
using Meadow.Peripherals.Leds;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace MeadowBleLed.Controller;
namespace MeadowBleLed.Controllers;

public class LedController
{
RgbPwmLed rgbPwmLed;
IRgbPwmLed rgbPwmLed;

Task animationTask = null;
CancellationTokenSource cancellationTokenSource = null;

public LedController()
public LedController(IPin redPwmPin, IPin greenPwmPin, IPin bluePwmPin)
{
rgbPwmLed = new RgbPwmLed(
redPwmPin: MeadowApp.Device.Pins.D12,
greenPwmPin: MeadowApp.Device.Pins.D11,
bluePwmPin: MeadowApp.Device.Pins.D10);
rgbPwmLed = new RgbPwmLed(redPwmPin, greenPwmPin, bluePwmPin);
}

void Stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Meadow;
using Meadow.Devices;
using Meadow.Gateways;
using MeadowBleLed.Connectivity;

namespace MeadowBleLed.Controllers;

public class MainController
{
private IBluetoothAdapter bluetooth;

private LedController ledController;
private BluetoothServer bluetoothServer;
private CommandController commandController;

public MainController(F7FeatherV2 hardware, IBluetoothAdapter bluetooth)
{
this.bluetooth = bluetooth;

commandController = new CommandController();

ledController = new LedController(hardware.Pins.OnboardLedRed, hardware.Pins.OnboardLedGreen, hardware.Pins.OnboardLedBlue);
//ledController = new LedController(hardware.Pins.D12, hardware.Pins.D11, hardware.Pins.D12);

StartBluetoothServer();
}

private void StartBluetoothServer()
{
bluetoothServer = new BluetoothServer();

var definition = bluetoothServer.GetDefinition();
bluetooth.StartBluetoothServer(definition);

commandController.LedOnValueSet += (s, e) =>
{
Resolver.Log.Info("LedOnValueSet");
ledController.TurnOn();
};
commandController.LedOffValueSet += (s, e) =>
{
Resolver.Log.Info("LedOffValueSet");
ledController.TurnOff();
};
commandController.LedBlinkValueSet += (s, e) =>
{
Resolver.Log.Info("LedBlinkValueSet");
ledController.StartBlink();
};
commandController.LedPulseValueSet += (s, e) =>
{
Resolver.Log.Info("LedPulseValueSet");
ledController.StartPulse();
};
commandController.LedRunColorsValueSet += (s, e) =>
{
Resolver.Log.Info("LedRunColorsValueSet");
ledController.StartRunningColors();
};
}
}
69 changes: 4 additions & 65 deletions Source/Meadow F7 Feather/Bluetooth/MeadowBleLed/MeadowApp.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,21 @@
using Meadow;
using Meadow.Devices;
using Meadow.Gateways.Bluetooth;
using MeadowBleLed.Controller;
using MeadowBleLed.Controllers;
using System.Threading.Tasks;

namespace MeadowBleLed;

// public class MeadowApp : App<F7FeatherV1> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7FeatherV2>
{
LedController ledController;

readonly string ON = "73cfbc6f61fa4d80a92feec2a90f8a3e";
readonly string OFF = "6315119dd61949bba21def9e99941948";
readonly string PULSING = "d755180131fc435da9941e7f15e17baf";
readonly string BLINKING = "3a6cc4f2a6ab4709a9bfc9611c6bf892";
readonly string RUNNING_COLORS = "30df1258f42b4788af2ea8ed9d0b932f";

IDefinition bleTreeDefinition;

ICharacteristic On;
ICharacteristic Off;
ICharacteristic StartPulse;
ICharacteristic StartBlink;
ICharacteristic StartRunningColors;

public override Task Initialize()
{
ledController = new LedController();

ledController.SetColor(Color.Red);
Resolver.Log.Info("Initialize...");

bleTreeDefinition = GetDefinition();
Device.BluetoothAdapter.StartBluetoothServer(bleTreeDefinition);
var ble = Device.BluetoothAdapter;

On.ValueSet += (s, e) => { ledController.TurnOn(); };
Off.ValueSet += (s, e) => { ledController.TurnOff(); };
StartPulse.ValueSet += (s, e) => { ledController.StartPulse(); };
StartBlink.ValueSet += (s, e) => { ledController.StartBlink(); };
StartRunningColors.ValueSet += (s, e) => { ledController.StartRunningColors(); };

ledController.SetColor(Color.Green);
var mainController = new MainController(Device, ble);

return base.Initialize();
}

Definition GetDefinition()
{
var service = new Service(
name: "MeadowRGBService",
uuid: 253,
On = new CharacteristicBool(
name: nameof(On),
uuid: ON,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write),
Off = new CharacteristicBool(
name: nameof(Off),
uuid: OFF,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write),
StartPulse = new CharacteristicBool(
name: nameof(StartPulse),
uuid: PULSING,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write),
StartBlink = new CharacteristicBool(
name: nameof(StartBlink),
uuid: BLINKING,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write),
StartRunningColors = new CharacteristicBool(
name: nameof(StartRunningColors),
uuid: RUNNING_COLORS,
permissions: CharacteristicPermission.Read | CharacteristicPermission.Write,
properties: CharacteristicProperty.Read | CharacteristicProperty.Write)
);

return new Definition("MeadowRGB", service);
}
}
13 changes: 1 addition & 12 deletions Source/Meadow.Cloud/ProjectLab_ApiClient/DTOs/CommonDTOs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@

namespace ProjectLab_ApiClient.DTOs;

public class GreenhouseModel
{
public double TemperatureCelsius { get; set; }
public double HumidityPercentage { get; set; }
public double SoilMoisturePercentage { get; set; }
public bool IsLightOn { get; set; }
public bool IsHeaterOn { get; set; }
public bool IsSprinklerOn { get; set; }
public bool IsVentilationOn { get; set; }
}

public class MeasurementData
{
public string temperature { get; set; }
Expand Down Expand Up @@ -49,5 +38,5 @@ public class Root
{
public Data data { get; set; }
public bool isSuccessful { get; set; }
public object errorMessage { get; set; }
public string errorMessage { get; set; }
}
4 changes: 2 additions & 2 deletions Source/Meadow.Cloud/ProjectLab_ApiClient/app.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Logging:
Default: Trace

# Meadow.Cloud configuration.
MeadowCloud:
#MeadowCloud:

# Enable Logging, Events, Command + Control
Enabled: true
# Enabled: true

# Enable Over-the-air Updates
# EnableUpdates: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private void RecordSensor()
{ "humidity", $"{hardware.HumiditySensor.Humidity.Value.Percent:N2}" },
});

displayController.UpdateSyncStatus(false);
displayController.UpdateStatus("Data sent!");
Thread.Sleep(2000);
displayController.UpdateSyncStatus(false);
displayController.UpdateStatus(DateTime.Now.AddHours(TIMEZONE_OFFSET).ToString("hh:mm tt dd/MM/yy"));

displayController.UpdateLastUpdated(DateTime.Now.AddHours(TIMEZONE_OFFSET).ToString("hh:mm tt dd/MM/yy"));
Expand Down
2 changes: 1 addition & 1 deletion Source/Meadow.Cloud/ProjectLab_OTA/info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"v":"1.0","osVersion":"1.9.0.0"}
{"v":"1.0","osVersion":"1.10.0.2"}
Loading
Loading