Skip to content

Commit

Permalink
updated project url, #10 handle exception thrown from disconnected lcd
Browse files Browse the repository at this point in the history
  • Loading branch information
almostengr committed May 27, 2023
1 parent 7a87f9f commit 2fb5596
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
81 changes: 46 additions & 35 deletions Almostengr.ThermometerPi.Api/Workers/LcdDisplayWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,75 @@
using System.Device.I2c;
using Iot.Device.Pcx857x;
using Iot.Device.CharacterLcd;
using Microsoft.Extensions.Logging;

namespace Almostengr.ThermometerPi.Api.Workers
{
public class LcdDisplayWorker : BackgroundService
{
private readonly ITemperatureReadingService _temperatureReadingService;
private readonly ILogger<LcdDisplayWorker> _logger;
private const int DelaySeconds = 5;
private Lcd1602 lcd;

public LcdDisplayWorker(IServiceScopeFactory factory)
public LcdDisplayWorker(IServiceScopeFactory factory,
ILogger<LcdDisplayWorker> logger)
{
_temperatureReadingService = factory.CreateScope().ServiceProvider.GetRequiredService<ITemperatureReadingService>();
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
using Pcf8574 driver = new Pcf8574(i2c);
try
{
using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
using Pcf8574 driver = new Pcf8574(i2c);

lcd = new Lcd1602(registerSelectPin: 0,
enablePin: 2,
dataPins: new int[] { 4, 5, 6, 7 },
backlightPin: 3,
backlightBrightness: 1f,
readWritePin: 1,
controller: new GpioController(PinNumberingScheme.Logical, driver));
lcd = new Lcd1602(registerSelectPin: 0,
enablePin: 2,
dataPins: new int[] { 4, 5, 6, 7 },
backlightPin: 3,
backlightBrightness: 1f,
readWritePin: 1,
controller: new GpioController(PinNumberingScheme.Logical, driver));

while (!stoppingToken.IsCancellationRequested)
{
TemperatureDto interiorTemp = await _temperatureReadingService.GetLatestInteriorReadingAsync();
while (!stoppingToken.IsCancellationRequested)
{
TemperatureDto interiorTemp = await _temperatureReadingService.GetLatestInteriorReadingAsync();

lcd.Clear();
string output = string.Empty;
lcd.Clear();
string output = string.Empty;

DisplayLcdText(
interiorTemp != null ?
$"In: {interiorTemp.Fahrenheit.ToString()}F {interiorTemp.Celsius.ToString()}C" :
"No Data",
DateTime.Now.ToString("ddd MM/dd HH:mm")
);
DisplayLcdText(
interiorTemp != null ?
$"In: {interiorTemp.Fahrenheit.ToString()}F {interiorTemp.Celsius.ToString()}C" :
"No Data",
DateTime.Now.ToString("ddd MM/dd HH:mm")
);

await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);

TemperatureDto minInteriorTemp = await _temperatureReadingService.GetMinInteriorReadingAsync();
TemperatureDto maxInteriorTemp = await _temperatureReadingService.GetMaxInteriorReadingAsync();
TemperatureDto minInteriorTemp = await _temperatureReadingService.GetMinInteriorReadingAsync();
TemperatureDto maxInteriorTemp = await _temperatureReadingService.GetMaxInteriorReadingAsync();

output = string.Empty;
output = string.Empty;

DisplayLcdText(
minInteriorTemp != null ?
$"Min: {minInteriorTemp.Fahrenheit.ToString()}F {minInteriorTemp.Celsius.ToString()}C" :
string.Empty,
maxInteriorTemp != null ?
$"Max: {maxInteriorTemp.Fahrenheit.ToString()}F {maxInteriorTemp.Celsius.ToString()}C" :
string.Empty
);
DisplayLcdText(
minInteriorTemp != null ?
$"Min: {minInteriorTemp.Fahrenheit.ToString()}F {minInteriorTemp.Celsius.ToString()}C" :
string.Empty,
maxInteriorTemp != null ?
$"Max: {maxInteriorTemp.Fahrenheit.ToString()}F {maxInteriorTemp.Celsius.ToString()}C" :
string.Empty
);

await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}

Expand All @@ -79,5 +90,5 @@ private void DisplayLcdText(string line1 = "No data", string line2 = "")
lcd.Write(line2);
}

} // end class
} // end class
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Thermometer Pi

This project uses a Raspberry Pi to make data available via an API from a temperature sensor.
Thermometer that reads the current temperature for a DS18S20.
This project uses a Raspberry Pi to make data available via an API from a temperature sensor.
Thermometer that reads the current temperature for a DS18S20.
This is an application created on .NET 5.0 with C#.

In addition, the application gets weather information from the National Weather Service via
API. Both internal and external temperature are then displayed on an LCD that is connected to the
Raspberry Pi via I2C.
In addition, the application gets weather information from the National Weather Service via
API. Both internal and external temperature are then displayed on an LCD that is connected to the
Raspberry Pi via I2C.

My Home Assistant instance calls the API every 5 minutes. Based on the temperature, a Wemo Smart Outlet
is either switched on or off by Home Assistant. The window air conditioners (AC) are connected to the Wemo
outlets.

## Resources

Visit the project page at https://thealmostengineer.com/thermometerpi
Visit the project page at https://thealmostengineer.com/projects/thermometer-pi/

Business inquiries https://rhtservices.net

0 comments on commit 2fb5596

Please sign in to comment.