Project updated to beta 6.2 (released on March 5th, 2022)
This project uses a LM35 temperature sensor and a ST7789 SPI display to make a simple room temperature monitor. Everything you need to build this project is included in the Wilderness Labs Meadow F7 w/Hack Kit Pro. We'll see how easy is to program these peripherals using Meadow.Foundation.
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.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LED project to properly set up your development environment.
Step 1 - Assemble the circuitWire your project like this:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it TemperatureMonitor.
Step 3 - Add the required NuGet packagesFor this project, search and install the following NuGet packages:
Step 4 - Write the code for TemperatureMonitorCopy the following code below:
// public class MeadowApp : App<F7Micro, MeadowApp> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
Color[] colors = new Color[4]
{
Color.FromHex("#67E667"),
Color.FromHex("#00CC00"),
Color.FromHex("#269926"),
Color.FromHex("#008500")
};
MicroGraphics graphics;
AnalogTemperature analogTemperature;
public MeadowApp()
{
Initialize();
LoadScreen();
analogTemperature.StartUpdating(TimeSpan.FromSeconds(5));
}
void Initialize()
{
var onboardLed = new RgbPwmLed(
device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
analogTemperature = new AnalogTemperature(
device: Device,
analogPin: Device.Pins.A00,
sensorType: AnalogTemperature.KnownSensorType.LM35
);
analogTemperature.TemperatureUpdated += AnalogTemperatureUpdated;
var config = new SpiClockConfiguration(
speed: new Frequency(48000, Frequency.UnitType.Kilohertz),
mode: SpiClockConfiguration.Mode.Mode3);
var spiBus = Device.CreateSpiBus(
clock: Device.Pins.SCK,
copi: Device.Pins.MOSI,
cipo: Device.Pins.MISO,
config: config);
var st7789 = new St7789(
device: Device,
spiBus: spiBus,
chipSelectPin: Device.Pins.D02,
dcPin: Device.Pins.D01,
resetPin: Device.Pins.D00,
width: 240, height: 240)
{
IgnoreOutOfBoundsPixels = true
};
graphics = new MicroGraphics(st7789);
graphics.Rotation = RotationType._270Degrees;
onboardLed.SetColor(Color.Green);
}
void AnalogTemperatureUpdated(object sender, IChangeResult<Meadow.Units.Temperature> e)
{
graphics.DrawRectangle(
x: 48, y: 160,
width: 144,
height: 40,
color: colors[colors.Length - 1],
filled: true);
graphics.DrawText(
x: 48, y: 160,
text: $"{e.New.Celsius:00.0}°C",
color: Color.White,
scaleFactor: ScaleFactor.X2);
graphics.Show();
}
void LoadScreen()
{
graphics.Clear(true);
int radius = 225;
int originX = graphics.Width / 2;
int originY = graphics.Height / 2 + 130;
graphics.Stroke = 3;
for (int i = 1; i < 5; i++)
{
graphics.DrawCircle
(
centerX: originX,
centerY: originY,
radius: radius,
color: colors[i - 1],
filled: true
);
radius -= 20;
}
graphics.DrawLine(0, 220, 239, 220, Color.White);
graphics.DrawLine(0, 230, 239, 230, Color.White);
graphics.CurrentFont = new Font12x20();
graphics.DrawText(54, 130, "TEMPERATURE", Color.White);
graphics.Show();
}
}
In
the MeadowApp's constructor, its all about initializing the peripherals on our project, which in this case is the AnalogTemperature object, in which we make sure we're specifying its a LM35 sensor type. To make the sensor be active to read the temperature, we'll use the events and IObservable pattern found in Meadow.Core to read, poll and filter input from sensors automatically. In this case, we're registering the LM35 to an Updated
event to constantly check the old and new values once we call the StartUpdating method.
We also initialize the ST7789 display, specifying which Pins are we connected and passing a SPI Configuration object to make sure we're connecting to the display in the right SPI mode. We're also using Meadow.Foundation's GraphicsLibrary to easily draw basic shapes and text for our UI.
In the LoadScreen method it will load all the UI to display the temperature, it starts by drawing four concentric circles, and a TEMPERATURE text as a title and two horizontal lines in the bottom of the screen.
Finally, in the AnalogTemperatureUpdated
event handler, we'll simply draw the previous temperature value in the same color of the background to "erase" it, and we draw the new value right after, so we won't need to redraw the entire screen each time the event is raised.
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