This project shows you how to use a Grove Soil Moisture sensor with a Project Lab, and show a virtual LED bar graph to display the moisture in percentage using MicroGraphics.
Project Lab is the most functional IoT prototyping platform on the planet. No more breadboards, complicated wiring, or soldering. Project Lab was built from the ground up using the industry's most powerful, capable, and reliable sensors, components, and connectors.
Meadow.Foundation a platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
Before you dive in this project, ensure that your Meadow board and development environment is fully up to date. Check the Release Notes section to double check.
Step 1 - Connect a Grove Moisture SensorConnect the Grove Soil Moisture sensor to the Project Lab in its Grove Analog port:
Create a new Meadow Application project in Visual Studio 2022 for Windows or macOS and name it MoistureMeter.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for MoistureMeterDisplayController
Copy the following code below:
public class DisplayController
{
private static readonly Lazy<DisplayController> instance =
new Lazy<DisplayController>(() => new DisplayController());
public static DisplayController Instance => instance.Value;
MicroGraphics graphics;
int padding = 12;
private DisplayController() { }
public void Initialize(IGraphicsDisplay display)
{
graphics = new MicroGraphics(display)
{
Stroke = 1,
CurrentFont = new Font12x20()
};
graphics.Clear();
}
public void UpdatePercentage(int percentage)
{
int barWidth = 100;
int barCompleteHeight = 218;
int barSingleHeight = 20;
int barSpacing = 2;
int x = (graphics.Width * 2) / 3 - barWidth / 2 + padding;
int y = 0;
graphics.Clear();
var barColor = Color.FromHex("004B6B");
graphics.DrawRectangle(x, y + padding, barWidth, barCompleteHeight, barColor, true);
int percentageGraph = (percentage == 100 ? 9 : percentage / 10);
for (int barIndex = percentageGraph; barIndex >= 0; barIndex--)
{
graphics.DrawRectangle(
x: x,
y: graphics.Height - padding - ((barSingleHeight + barSpacing) * barIndex) - 18,
width: barWidth,
height: barSingleHeight,
color: barIndex switch
{
>= 0 and <= 1 => Color.FromHex("FF3535"),
>= 2 and <= 4 => Color.FromHex("FF8251"),
>= 5 and <= 8 => Color.FromHex("35FF3D"),
>= 9 => Color.FromHex("475AFF"),
_ => throw new NotImplementedException(),
},
filled: true);
}
graphics.DrawText(graphics.Width / 3 - padding, (graphics.Height - graphics.CurrentFont.Height) / 2, $"{percentage}%", ScaleFactor.X2, alignmentH: HorizontalAlignment.Center);
graphics.Show();
}
}explain
- Initialize - We first create a MicroGraphics object when passing Project Lab's display, along with setting a Font and Stroke thickness when drawing shapes.
- UpdatePercentage - This method draws a virtual LED bar graph by drawing color filled rectangles and the percentage value on the left side.
MeadowAppclass
Copy the following code below:
public class MeadowApp : App<F7CoreComputeV2>
{
private RgbPwmLed onboardLed;
private MoistureSensor sensor;
private IProjectLabHardware projectLab;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
projectLab = ProjectLab.Create();
Resolver.Log.Info($"Running on ProjectLab Hardware {projectLab.RevisionString}");
onboardLed = projectLab.RgbLed;
onboardLed.SetColor(Color.Red);
DisplayController.Instance.Initialize(projectLab.Display);
sensor = new MoistureSensor(projectLab.GroveAnalog.Pins.D0);
sensor.Updated += (sender, result) =>
{
var percentage = (int)ExtensionMethods.Map(result.New.Millivolts, 0, 1750, 0, 100);
DisplayController.Instance.UpdatePercentage(Math.Clamp(percentage, 0, 100));
};
sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
onboardLed.SetColor(Color.Green);
return base.Initialize();
}
}
- Initialize - First we instanciate a ProjectLab object with
ProjectLab.Create()
factory method, which detects what version of the board is the app running. Next an onboard RGB LED object is created and it turns on red.After that theDisplayController
is initialized, and after the Grove soil moisture sensor is instantiated, registering theUpdated
event which is triggered every second, sending every percentage value to theDisplayController
to update the UI.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments
Please log in or sign up to comment.