Skip to content

Commit 2fb5596

Browse files
committed
updated project url, #10 handle exception thrown from disconnected lcd
1 parent 7a87f9f commit 2fb5596

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

Almostengr.ThermometerPi.Api/Workers/LcdDisplayWorker.cs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,75 @@
99
using System.Device.I2c;
1010
using Iot.Device.Pcx857x;
1111
using Iot.Device.CharacterLcd;
12+
using Microsoft.Extensions.Logging;
1213

1314
namespace Almostengr.ThermometerPi.Api.Workers
1415
{
1516
public class LcdDisplayWorker : BackgroundService
1617
{
1718
private readonly ITemperatureReadingService _temperatureReadingService;
19+
private readonly ILogger<LcdDisplayWorker> _logger;
1820
private const int DelaySeconds = 5;
1921
private Lcd1602 lcd;
2022

21-
public LcdDisplayWorker(IServiceScopeFactory factory)
23+
public LcdDisplayWorker(IServiceScopeFactory factory,
24+
ILogger<LcdDisplayWorker> logger)
2225
{
2326
_temperatureReadingService = factory.CreateScope().ServiceProvider.GetRequiredService<ITemperatureReadingService>();
27+
_logger = logger;
2428
}
2529

2630
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2731
{
28-
using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
29-
using Pcf8574 driver = new Pcf8574(i2c);
32+
try
33+
{
34+
using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
35+
using Pcf8574 driver = new Pcf8574(i2c);
3036

31-
lcd = new Lcd1602(registerSelectPin: 0,
32-
enablePin: 2,
33-
dataPins: new int[] { 4, 5, 6, 7 },
34-
backlightPin: 3,
35-
backlightBrightness: 1f,
36-
readWritePin: 1,
37-
controller: new GpioController(PinNumberingScheme.Logical, driver));
37+
lcd = new Lcd1602(registerSelectPin: 0,
38+
enablePin: 2,
39+
dataPins: new int[] { 4, 5, 6, 7 },
40+
backlightPin: 3,
41+
backlightBrightness: 1f,
42+
readWritePin: 1,
43+
controller: new GpioController(PinNumberingScheme.Logical, driver));
3844

39-
while (!stoppingToken.IsCancellationRequested)
40-
{
41-
TemperatureDto interiorTemp = await _temperatureReadingService.GetLatestInteriorReadingAsync();
45+
while (!stoppingToken.IsCancellationRequested)
46+
{
47+
TemperatureDto interiorTemp = await _temperatureReadingService.GetLatestInteriorReadingAsync();
4248

43-
lcd.Clear();
44-
string output = string.Empty;
49+
lcd.Clear();
50+
string output = string.Empty;
4551

46-
DisplayLcdText(
47-
interiorTemp != null ?
48-
$"In: {interiorTemp.Fahrenheit.ToString()}F {interiorTemp.Celsius.ToString()}C" :
49-
"No Data",
50-
DateTime.Now.ToString("ddd MM/dd HH:mm")
51-
);
52+
DisplayLcdText(
53+
interiorTemp != null ?
54+
$"In: {interiorTemp.Fahrenheit.ToString()}F {interiorTemp.Celsius.ToString()}C" :
55+
"No Data",
56+
DateTime.Now.ToString("ddd MM/dd HH:mm")
57+
);
5258

53-
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
59+
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
5460

55-
TemperatureDto minInteriorTemp = await _temperatureReadingService.GetMinInteriorReadingAsync();
56-
TemperatureDto maxInteriorTemp = await _temperatureReadingService.GetMaxInteriorReadingAsync();
61+
TemperatureDto minInteriorTemp = await _temperatureReadingService.GetMinInteriorReadingAsync();
62+
TemperatureDto maxInteriorTemp = await _temperatureReadingService.GetMaxInteriorReadingAsync();
5763

58-
output = string.Empty;
64+
output = string.Empty;
5965

60-
DisplayLcdText(
61-
minInteriorTemp != null ?
62-
$"Min: {minInteriorTemp.Fahrenheit.ToString()}F {minInteriorTemp.Celsius.ToString()}C" :
63-
string.Empty,
64-
maxInteriorTemp != null ?
65-
$"Max: {maxInteriorTemp.Fahrenheit.ToString()}F {maxInteriorTemp.Celsius.ToString()}C" :
66-
string.Empty
67-
);
66+
DisplayLcdText(
67+
minInteriorTemp != null ?
68+
$"Min: {minInteriorTemp.Fahrenheit.ToString()}F {minInteriorTemp.Celsius.ToString()}C" :
69+
string.Empty,
70+
maxInteriorTemp != null ?
71+
$"Max: {maxInteriorTemp.Fahrenheit.ToString()}F {maxInteriorTemp.Celsius.ToString()}C" :
72+
string.Empty
73+
);
6874

69-
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
75+
await Task.Delay(TimeSpan.FromSeconds(DelaySeconds), stoppingToken);
76+
}
77+
}
78+
catch (Exception ex)
79+
{
80+
_logger.LogError(ex.Message);
7081
}
7182
}
7283

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

82-
} // end class
93+
} // end class
8394
}

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Thermometer Pi
22

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

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

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

1515
## Resources
1616

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

1919
Business inquiries https://rhtservices.net

0 commit comments

Comments
 (0)