Earthquake Monitor for ESP32 with choice of displays.

Place your projects here
Post Reply
Posts: 111
Joined: Mon Feb 15, 2021 1:44 pm
Has thanked: 41 times
Been thanked: 81 times

Earthquake Monitor for ESP32 with choice of displays.

Post by AndyGadget »

This is an updated version which was originally developed for ESP8266 and comes in 2 versions.
Download if using ILI9341 (240x320) display.
Download if using ILI9487 or ILI9488 (320x480) display.
You will need Annex32 V1.43.5 or higher for ILI9488.

The zip file contains the program file and the map JPG to go with it.
I'm listing just the larger screen version here for neatness.
This won a runners-up prize in the recent Instructables Maps Challenge :-)
Full build details are [External Link Removed for Guests].

Code: [Local Link Removed for Guests]

' Quakes32 V1.1 for ESP32 and ILI9487/9488 (320x480)
' Uses map image map-l.jpg.
' AndyGadget 2021
' Tidied up code, added overnight dimming of display 
' added more comments and improved event location dot accuracy.

wlog can.stop ' Kill CanBus as not required

OPTION.NTPSYNC   ' Sync to internet time (Mage sure Timezone is correct on Config screen
TFT.INIT 1       ' May need TFT.INIT 3, depending on screen orientation.

' Various variable definitions.
FName$ = "/history.txt"
OldRef$ = ""
dim Hist(100,3)
PLat = 0 : PLon = 0 : PMag = 0
EOL$ = chr$(10)

tft.text.align 4
tft.text.col Yellow
tft.text.font 4

' Display bare map on screen.
tft.jpg "/map-l.jpg"

if file.exists(FName$) = 1 then
  ' Read event history from file into array.
  for FCnt = 1 to 100
    FLine$ =$(FName$,FCnt)
    Hist(FCnt,0) = val(word$(FLine$,1,"|"))
    Hist(FCnt,1) = val(word$(FLine$,2,"|"))
    Hist(FCnt,2) = val(word$(FLine$,3,"|"))
  next FCnt
end if

'Sets routine to call when data received from seismic portal API.
gosub QueryServer 

' Set timer to run Blink routine every 500mS
timer0 500, Blink

' Set timer to run data request routine every 30 seconds.
timer1 30000, QueryServer

'  Do nothing and wait for the events above to occur.

' ***********

' Request data from SeismicPortal API - No key needed.
' JSON is an option but chose simple delimited text string output.
' Requesting only most recent event.

' Green dot in top centre of screen - Clears when data received.
TFT.CIRCLE 224, 10, 3,green,1

' Turn TFT brightness down overnight
Hour = val(left$(time$,2))
if (Hour >=23) or (Hour <=7) then TFT.BRIGHTNESS 20 else TFT.BRIGHTNESS 255


' Seismic portal API has replied ot data request. 

TFT.CIRCLE 224, 10, 3,&H3475,1
Line$ = WORD$( WGETRESULT$, 2, chr$(10))
Ref$ = word$(Line$,1,"|")

' Only process result if it is a new event.
if (Ref$ <> OldRef$) and (val(Ref$) <> 0)  then
  wlog line$
  OldRef$ = Ref$

' Extract the data items we want from returned data line.
  TD$ = word$(Line$,2,"|")
  EDate$ = left$(TD$,10)
  ETime$ = left$(time$,5)  
  EMag$ = word$(Line$,11,"|")
  ELoc$ = word$(Line$,13,"|")
  Lat = val(word$(Line$,3,"|"))
  Lon = val(word$(Line$,4,"|"))
  ' Load map to clear display, then print text info about event.
  TFT.JPG "/map-l.jpg"
  tft.text.draw EMag$,40,30
  tft.text.draw ETime$,420,30
  tft.text.draw ELoc$, 240,300
  ' Shift array.
  For Cnt = 0 to 99
    Hist(Cnt,0) = Hist(Cnt+1,0)
    Hist(Cnt,1) = Hist(Cnt+1,1)
    Hist(Cnt,2) = Hist(Cnt+1,2)
  next Cnt
  ' Convert longitude and latitude of event to screen co-ordinates.
  ' Size of plotted circle is proportional to quake magnitude.
  PLon = (225 + (1.334 * Lon)) mod 480
  PLat = 225 - 1.333*(((0.86 * Lat) + (Lat * 0.0475)^3))

PMag = cint(val(EMag$) * 1.0)
  if PMag <= 1 then PMag = 1
  ' Write new event to array.
  Hist(100,0) = PLon
  Hist(100,1) = PLat
  Hist(100,2) = PMag
  ' Plot event history from array.
  for HCnt = 0 to 100
    RedVal = 155 + HCnt
    GrnVal = 55 + (HCnt * 2)
    BluVal = 0
    ' Colour of plotted point fades from bright yellow through orange to dull red with age.
    TFT.CIRCLE Hist(HCnt,0),Hist(HCnt,1),Hist(HCnt,2),TFT.RGB(RedVal,GrnVal,BluVal),1
  next HCnt
  ' Plot most recent event.
  TFT.CIRCLE PLon,Plat,PMag,&HF800,1
  ' Store array to file.
  TFT.CIRCLE 224, 10, 3,red,1
  x =  file.delete(FName$)
  for FCnt = 1 to 100
    FLine$ = str$(Hist(FCnt,0)) + "|" + str$(Hist(FCnt,1)) + "|" + str$(Hist(FCnt,2)) + EOL$
    file.append FName$,FLine$
  next FCnt
  TFT.CIRCLE 224, 10, 3,&H3475,1

' Flashing red / yellow dot to mark most recent event.
Flsh = (Flsh + 1) mod 2
if Flsh = 0 then
  TFT.CIRCLE PLon,PLat,PMag,Yellow,1
  TFT.CIRCLE PLon,PLat,PMag,Red,1
You do not have the required permissions to view the files attached to this post.
Posts: 2
Joined: Tue Mar 09, 2021 6:17 pm
Been thanked: 1 time

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by lew247 »

Annex32 WiFi BLE CAN 1.43.7
I am having problems with this and an ILI9488 display

It works on a 2.8" ILI9341 using
It only works if I comment out the line wlog can.stop ' Kill CanBus as not required
but there are random square artifacts on the screen in random places

When I try and use a 3.5" ILI9488 display using
All I get is a blank screen (I can see the LED light up)
If I step through the program I get the same problem with the line above and if I comment out the line it will then "seem to run"
but nothing on the display
it's a new display arrived today, I've checked the J1 link and tried the screen on both 3.3v and 5v power and the same happens

When I power up the smaller screen I get the Annex boot up screen
but I don't get anything other than a backlight when I use the larger one

I'm using a TTGO T8 v1.7.1 ESP32
I've triple checked the pinouts and I'm certain they are correct

I've commented out the lines that control brightness at night so it's always 100%
This is the board


This is the wiring diagram I followed


Has anyone got any suggestions why it isn't working? or could I have a duff ESP32 board
User avatar
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by Electroguard »

Try to get minimum hardware working with minimal software, cos if that doesn't work then nothing will.
Use a simple TFT example script with the smaller screen. then if that works you can try it with the other display - bearing in mind that it's not just the driver requirements which are different, a bigger screen needs more current... so perhaps the ESP's onboard regulator may not be adequate.
Try powering the screen from a different 3.3v source (with a shared ground of course).
Once you get the hardware working correctly with minimal software, then you can try running something more complex with more confidence.
Posts: 4
Joined: Sat Feb 05, 2022 10:11 am

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by »

I want to show the biggest Earthquake since start and I want to show a clock
Can you give me some pointers on the clock code?

For the biggest earthquake I would say I would write some code like this:
(Pseudo code)

Code: [Local Link Removed for Guests]

if EMag$ (Newest earthquake magnitude number) is > PMag (Current earthquake magnitude number)

Hmag =  EMag$ 
HTime =  ETime$ 
HLoc =  ELoc$
Hmag =  PMag 
HTime =  Ptime
HLoc =  Ploc

 ' Show biggest event history from start.
 ' Will be displayed as small text on top

'tft.text.draw EMag$,20,240
tft.text.draw ETime$,20,250
 tft.text.draw ELoc$, 20,260
User avatar
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by Electroguard »

I don't actually have this project, but speaking generally, if you want to display the time regularly you would use one of the timers to update the displayed time at your chosen frequency, depending on whether you wish to display seconds or just hours and minutes.
Both timers are already being used here though, but it can still be made to work.

However, using HTime = ETime$ will not work... for 2 reasons
1. HTime without a $ at the end is a numeric variable, whereas ETime$ is a string variable - but a numeric var cannot equate to a string var without conversion, ie: number = val(string$), or HTime = val(ETime$).
2. ETime$ appears to be the time of an occurring event, but events are not clockwork, so Etime$ will update according to the frequency of events, not time$

If it was me, I would periodically create my own time variable such as mytime$ = time$, then display that.
Update frequency will depend on whether needing to display seconds or whether hours and minutes would suffice.

If needing seconds, timer0 branches to Blink every half a second, where it displays a particular colour of ring every second ... so adding myttime$=time$ and displaying it with either of the yellow or red flsh circle statements would give a 1 second updated time display.

If minutes are sufficient, timer1 30000, QueryServer branches every 30 seconds, so adding mytime$=time$ and displaying it from within QueryServer would update the displayed hours and minutes twice a minute.
Posts: 4
Joined: Sat Feb 05, 2022 10:11 am

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by »

Do you know how to accomplish the following?

I compare the current magnitude val(Emag$) with the previous magnitude MaxEmag to see if it's bigger.
With the example code below it says that 2.1 is lower than 0..
MaxEmag is declared 0 at start
I think the previous value part is not right because when declare MaxEmag as static 0 and comnent MaxEmag =val(Emag$)then the comparison works

Code: [Local Link Removed for Guests]

MaxEmag = 0
MaxEmag =val(Emag$)

if val(Emag$) > MaxEmag  then
  MaxEmag$= Emag$  
  MaxEloc$ =  ELoc$
  MaxETime$ =  ETime$
   ' wlog "Loop: Current Magnitude:" + " " + EMag + "is higher than previous" + " " + Pmag
    wlog "Loop Highest Magnitude so far is " +" "+MaxEmag$
    wlog PMag
  wlog "Loop: Current Magnitude is:" +" " +Emag$
  tft.text.align 4
  tft.text.col Red
  tft.text.font 3 
  tft.text.draw MaxEmag$,30,15
  tft.text.draw MaxETime$,430,15
  tft.text.draw MaxEloc$, 240,15
 wlog "Loop: Current Magnitude:" + " "+ Emag$ + " is lower  than previous" + " " + MaxEmag$
User avatar
Posts: 277
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 55 times
Been thanked: 129 times

Re: Earthquake Monitor for ESP32 with choice of displays.

Post by Electroguard »

You are mixing number vars with string vars again... declaring MaxEmag as a numeric var
MaxEmag = 0
MaxEmag =val(Emag$)
so MaxEmag is always going to be the numeric value of the CURRENT Emag$
But your comparison compares with the MaxEmag numeric var then immediately saves a greater value to an undeclared MaxEmag$ string var instead, which is never compared to again.
if val(Emag$) > MaxEmag then
MaxEmag$= Emag$

Listen, variables are not case sensitive, so to avoid mixing up numeric and string variables why not use upper case for numeric vars and lower case (ending with $) for string vars, then it should be immediately apparent what you will be doing with what, and easy to spot a wrong type of var trying to sneak in.
Post Reply