G6EJD / ESP32-e-Paper-Weather-Display

An ESP32 and 2.9", 4.2" or 7.5" ePaper Display reads Weather Underground data via their API and then displays the weather
Other
946 stars 206 forks source link

Fix UTC to local time conversion error in DisplayForecastWeather for … #168

Closed skiphansen closed 2 years ago

skiphansen commented 2 years ago

…some examples.

Noticed in Waveshare_4_2.ino, copied fix from Waveshare_2_7.ino.

G6EJD commented 2 years ago

I need to check if OWM have corrected the time code, it used to be in UTC and the timezone field had to be added to get local time/forecast data. Did you confirm they change it please?

skiphansen commented 2 years ago

No I didn't.

I don't think it has anything to do with OWM changes, it's a double correction. ConvertUnixTime uses localtime() to convert the time_t to a struct tm so there's no need to add and additional correction. This assumes that TZ is set correctly and it is since the current time is correct.

In any case 6 of t he 11 samples add WxConditions[0].Timezone and 5 do not so something is up.

73's Skip

G6EJD commented 2 years ago

Thanks Skip, I'll pull the full json forecast and check the ‘dt’ field for the forecast at 3-hours from now, well they used to do from time now, then changed it to set hours with a 3-hour interval, so for UTC they occur at 00:00 03:00 06:00, but I know for EST they occur from a different baseline and with a 5-hour offset so that’s using the timezone. The problem is OWM don’t state their reference but they were definitely UTC that’s why I added the timezone offset. They were publishing the main weather at the correct Ordinal time (local time for the city) then the forecast was in UTC, yes the irregularities need to be fixed, I’ll spend some time going through it.

G6EJD commented 2 years ago

Here's a current example of Washington, DC dt_txt":"2021-12-07 18:00:00"},{"dt":1638910800,"main"} So the forecast time text is at 18:00 but the dt field is for 2021-12-07 21:00:00 which is 3-hours later than the text field! So which is correct? This corresponds to what I've seen that US forecasts are often 5-hours ahead of UTC and not 5-hours behind UTC, The time here now is 15:46 which is in UTC so I'd expect to see data for the US in EST to be 5-hours earlier and I should see a first forecast for either 12:00 EST or 15:00 EST, but not so...

G6EJD commented 2 years ago

I found this on the OWM support forum:

The OpenWeatherMap 5 day forecast gives a response with 3 hour reference timestamps reported in UTC for the next 5 days.

Each 3 hour reference timestamp has to be converted to local time

And ConvertUnixTime takes an argument and converts it to local time, So apart from the missing offsets in some codes, there is still the issue that OWM are publishing two times for each forecast the one in text and the one in ordinal time.

For now the timezone should be added.

skiphansen commented 2 years ago

I haven't worked with OWM before so I can't help there.

Last night at 17:42 local my log showed

WiFi connected at: 192.168.66.148

Creating object...and  Decoding weather data
 Lon: -118.24
 Lat: 34.05
Main: Haze
For0: haze

Icon: 50n
Temp: 59.20
Pres: 1016.00
Humi: 82.00
TLow: 53.65
THig: 65.21
WSpd: 0.00
WDir: 0.00
CCov: 75
Visi: 9656
Rain: 0.00
Snow: 0.00
Ctry: US
SRis: 1638801903
SSet: 1638837820
TZon: -28800

Creating object...and  Decoding forecast data

Receiving Forecast period - 
Period-0--------------
DTim: 1638846000
Temp: 59.16
TLow: 59.16
THig: 61.20
Pres: 1016.00
Humi: 82.00
For0: Clouds

Icon: 04n
Desc: broken clouds
CCov: 75
WSpd: 5.44
WDir: 151.00
Rain: 0.00
Snow: 0.00
Pop:  0.00
Peri: 2021-12-07 03:00:00

Period-1--------------
DTim: 1638856800
Temp: 59.23
TLow: 59.23
THig: 59.36
Pres: 1016.00
Humi: 82.00
For0: Clouds

Icon: 04n
Desc: broken clouds
CCov: 82
WSpd: 3.09
WDir: 122.00
Rain: 0.00
Snow: 0.00
Pop:  0.00
Peri: 2021-12-07 06:00:00

Converting DTim for period 0 yields:

skip@Dell-7040:~/esl/ESP32-e-Paper-Weather-Display/examples/epop_900$ date --date='@1638846000'
Mon 06 Dec 2021 07:00:00 PM PST
skip@Dell-7040:~/esl/ESP32-e-Paper-Weather-Display/examples/epop_900$

which seemed reasonable.

skiphansen commented 2 years ago

I agree that an adjustment needs to be added, but my point is that it was added TWICE before the change, once by the addition of WxConditions[0].Timezone and once by localtime().

If the time_t is adjusted "manually" then I think gmtime() should be used to convert from time_t to struct tm rather than localtime().

G6EJD commented 2 years ago

Yes agreed, gmtime() is the answer

skiphansen commented 2 years ago

That makes sense to me as well. Would you like me to revise the pull request or just close it?

G6EJD commented 2 years ago

If you can amend your pull request I’d appreciate that.

skiphansen commented 2 years ago

I've updated the PR to add the Timezone from OWM to the argument to ConvertUnixTime() and changed ConvertUnixTime() to use gmtime().

I noticed that WxForecast[index].Dt was adjusted by WxConditions[0].Timezone in a few places, I changed them to adjust by WxForecast[index].Timezone instead. It probably doesn't make any difference, but it looked "wrong".

I've successfully test compiled all examples, but didn't test any on hardware.

BTW I noticed just there are two Waveshare_2_9_v4.ino, one under examples/Waveshare_2_9/ and one in the root of the repo, I don't know if that was intentional or not but I've updated both.

Kobbe1 commented 2 years ago

Hi, I always thought the "next hours forecast" times were a bit off. For example when it updated the forecast at 13:30 "17:00" was displayed as the first one, and the next 2 offset with 3 hours - while it probably was using an earlier forecast actually. So when this commit came up I thought it might very well be related and tried it out. It seems to make more sense now, however the last time now is a bit weird?

Time of update: 15:26

Settings:

I don't use the example but have modified the code quite a bit over time, using coordinates instead of city name due to my location, I just thought that only the 3rd time being off might give you some quick clues as to what the issue might be. Without this commit things work well apart from the forecast time seeming a bit off as explained above.

In any case great project!

-T

image

`` ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:1216 ho 0 tail 12 room 4 load:0x40078000,len:10944 load:0x40080400,len:6388 entry 0x400806b4

Connecting to: xxx WiFi connected at: yyy

Creating object...and Decoding weather data Lon: zzz Lat: zzz Main: Clouds For0: Überwiegend bewölkt

Icon: 04d Temp: 0.19 Pres: 1016.00 Humi: 93.00 TLow: -1.05 THig: 1.16 WSpd: 3.09 WDir: 240.00 CCov: 75 Visi: 6000 Rain: 0.00 Snow: 0.00 Ctry: DE SRis: 1639206429 SSet: 1639234400 TZon: 3600 Temp_feelslike: -3.35 Wgust: 0.00

Creating object...and Decoding forecast data

Receiving Forecast period - Period-0-------------- DTim: 1639234800 Temp: 0.19 TLow: 0.19 THig: 0.99 Pres: 1016.00 Humi: 93.00 For0: Clouds

Icon: 04n Desc: Überwiegend bewölkt CCov: 75 WSpd: 2.38 WDir: 252.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-11 15:00:00 WGust: 3.57

Period-1-------------- DTim: 1639245600 Temp: -0.29 TLow: -1.24 THig: -0.29 Pres: 1017.00 Humi: 94.00 For0: Clouds

Icon: 04n Desc: Überwiegend bewölkt CCov: 74 WSpd: 2.48 WDir: 234.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-11 18:00:00 WGust: 7.02

Period-2-------------- DTim: 1639256400 Temp: -0.64 TLow: -1.05 THig: -0.64 Pres: 1019.00 Humi: 91.00 For0: Clouds

Icon: 03n Desc: Mäßig bewölkt CCov: 42 WSpd: 2.76 WDir: 219.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-11 21:00:00 WGust: 8.30

Period-3-------------- DTim: 1639267200 Temp: 0.54 TLow: 0.54 THig: 0.54 Pres: 1022.00 Humi: 86.00 For0: Clouds

Icon: 04n Desc: Überwiegend bewölkt CCov: 58 WSpd: 3.03 WDir: 225.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-12 00:00:00 WGust: 8.76

Period-4-------------- DTim: 1639278000 Temp: -0.11 TLow: -0.11 THig: -0.11 Pres: 1023.00 Humi: 90.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.72 WDir: 234.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-12 03:00:00 WGust: 7.37

Period-5-------------- DTim: 1639288800 Temp: -0.77 TLow: -0.77 THig: -0.77 Pres: 1024.00 Humi: 94.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.63 WDir: 228.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-12 06:00:00 WGust: 7.33

Period-6-------------- DTim: 1639299600 Temp: 1.05 TLow: 1.05 THig: 1.05 Pres: 1025.00 Humi: 87.00 For0: Clouds

Icon: 04d Desc: Bedeckt CCov: 100 WSpd: 2.51 WDir: 205.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-12 09:00:00 WGust: 6.43

Period-7-------------- DTim: 1639310400 Temp: 3.16 TLow: 3.16 THig: 3.16 Pres: 1024.00 Humi: 84.00 For0: Clouds

Icon: 04d Desc: Bedeckt CCov: 100 WSpd: 3.39 WDir: 217.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-12 12:00:00 WGust: 8.54

Period-8-------------- DTim: 1639321200 Temp: 2.53 TLow: 2.53 THig: 2.53 Pres: 1024.00 Humi: 90.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.80 WDir: 198.00 Rain: 0.00 Snow: 0.00 Pop: 0.05 Peri: 2021-12-12 15:00:00 WGust: 7.19

Period-9-------------- DTim: 1639332000 Temp: 2.64 TLow: 2.64 THig: 2.64 Pres: 1023.00 Humi: 92.00 For0: Rain

Icon: 10n Desc: Leichter Regen CCov: 100 WSpd: 3.24 WDir: 216.00 Rain: 0.10 Snow: 0.00 Pop: 0.28 Peri: 2021-12-12 18:00:00 WGust: 8.02

Period-10-------------- DTim: 1639342800 Temp: 2.65 TLow: 2.65 THig: 2.65 Pres: 1024.00 Humi: 96.00 For0: Rain

Icon: 10n Desc: Leichter Regen CCov: 100 WSpd: 2.63 WDir: 221.00 Rain: 0.39 Snow: 0.00 Pop: 0.48 Peri: 2021-12-12 21:00:00 WGust: 6.74

Period-11-------------- DTim: 1639353600 Temp: 2.95 TLow: 2.95 THig: 2.95 Pres: 1024.00 Humi: 98.00 For0: Rain

Icon: 10n Desc: Leichter Regen CCov: 100 WSpd: 1.96 WDir: 234.00 Rain: 0.14 Snow: 0.00 Pop: 0.41 Peri: 2021-12-13 00:00:00 WGust: 5.18

Period-12-------------- DTim: 1639364400 Temp: 3.19 TLow: 3.19 THig: 3.19 Pres: 1024.00 Humi: 99.00 For0: Rain

Icon: 10n Desc: Leichter Regen CCov: 100 WSpd: 1.76 WDir: 245.00 Rain: 0.41 Snow: 0.00 Pop: 0.21 Peri: 2021-12-13 03:00:00 WGust: 3.61

Period-13-------------- DTim: 1639375200 Temp: 3.60 TLow: 3.60 THig: 3.60 Pres: 1024.00 Humi: 99.00 For0: Rain

Icon: 10n Desc: Leichter Regen CCov: 100 WSpd: 1.90 WDir: 251.00 Rain: 0.49 Snow: 0.00 Pop: 0.52 Peri: 2021-12-13 06:00:00 WGust: 4.07

Period-14-------------- DTim: 1639386000 Temp: 4.38 TLow: 4.38 THig: 4.38 Pres: 1026.00 Humi: 99.00 For0: Rain

Icon: 10d Desc: Leichter Regen CCov: 100 WSpd: 1.97 WDir: 257.00 Rain: 0.47 Snow: 0.00 Pop: 0.57 Peri: 2021-12-13 09:00:00 WGust: 4.86

Period-15-------------- DTim: 1639396800 Temp: 5.52 TLow: 5.52 THig: 5.52 Pres: 1026.00 Humi: 99.00 For0: Clouds

Icon: 04d Desc: Bedeckt CCov: 100 WSpd: 1.93 WDir: 264.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-13 12:00:00 WGust: 4.82

Period-16-------------- DTim: 1639407600 Temp: 5.87 TLow: 5.87 THig: 5.87 Pres: 1026.00 Humi: 98.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.11 WDir: 271.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-13 15:00:00 WGust: 4.44

Period-17-------------- DTim: 1639418400 Temp: 6.09 TLow: 6.09 THig: 6.09 Pres: 1027.00 Humi: 97.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.13 WDir: 270.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-13 18:00:00 WGust: 3.54

Period-18-------------- DTim: 1639429200 Temp: 6.21 TLow: 6.21 THig: 6.21 Pres: 1027.00 Humi: 95.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.07 WDir: 248.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-13 21:00:00 WGust: 3.08

Period-19-------------- DTim: 1639440000 Temp: 6.16 TLow: 6.16 THig: 6.16 Pres: 1027.00 Humi: 93.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.16 WDir: 242.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-14 00:00:00 WGust: 3.90

Period-20-------------- DTim: 1639450800 Temp: 6.12 TLow: 6.12 THig: 6.12 Pres: 1027.00 Humi: 91.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.17 WDir: 247.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-14 03:00:00 WGust: 4.42

Period-21-------------- DTim: 1639461600 Temp: 5.98 TLow: 5.98 THig: 5.98 Pres: 1026.00 Humi: 92.00 For0: Clouds

Icon: 04n Desc: Bedeckt CCov: 100 WSpd: 2.36 WDir: 251.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-14 06:00:00 WGust: 5.51

Period-22-------------- DTim: 1639472400 Temp: 5.89 TLow: 5.89 THig: 5.89 Pres: 1026.00 Humi: 90.00 For0: Rain

Icon: 10d Desc: Leichter Regen CCov: 100 WSpd: 2.53 WDir: 237.00 Rain: 0.14 Snow: 0.00 Pop: 0.23 Peri: 2021-12-14 09:00:00 WGust: 5.89

Period-23-------------- DTim: 1639483200 Temp: 6.76 TLow: 6.76 THig: 6.76 Pres: 1026.00 Humi: 87.00 For0: Clouds

Icon: 04d Desc: Bedeckt CCov: 100 WSpd: 2.29 WDir: 251.00 Rain: 0.00 Snow: 0.00 Pop: 0.00 Peri: 2021-12-14 12:00:00 WGust: 6.63 Voltage = 4.25 04d 04n 04n 03n Entering 240-secs of sleep time Awake for : 8.105-secs Starting deep-sleep period... ``

skiphansen commented 2 years ago

I'll have to admit that I was lazy and didn't test the last change on actual hardware since I had my display connected to an Rpi at the time.

Please change

drawString(x + 31, y + 3, String(ConvertUnixTime(WxForecast[index].Dt + WxForecast[index].Timezone).substring(0,5)), CENTER);

back to

 drawString(x + 31, y + 3, String(ConvertUnixTime(WxForecast[index].Dt + WxConditions[0].Timezone).substring(0,5)), CENTER);

in DrawForecastWeather and see if that fixes the problem.

It doesn't look like Timezone is ever set in WxForecast.

So much for making changes to things that just "look wrong" ! Opps!

Kobbe1 commented 2 years ago

Haha, yes, that fixed it I would say.

So right now it is 22:00, and the 3 forecasts are 01:00, 04:00 and 07:00, so 3 hours from now, with 3 hours offset. I think that is intended behaviour.

Thanks!

skiphansen commented 2 years ago

Thanks for testing it... I'll update my PR and resend it to David.