1

I have a raspberry pi that is connected to the internet and writing data to my mysql database server. I wanted to write a php script that would echo the status. So if the raspberry pi for some reason stops writing data to my mysql device it will echo $status = 0 or something. I wanted to ping the Raspberry pi from my webpage but my raspberry pi IP address won't be the same once I move it somewhere. I thought about maybe just check the last timestamp of the data it wrote to the mysql server with the current time and if it was greater than 5 minutes it will echo status 0. Is there a better way to do this?

Alan
  • 25
  • 1
  • 5

1 Answers1

2

If you want to heartbeat something, which is what you're talking about, give the device a consistent ID, even a UUID, which identifies it, and update some table with a check-in DATETIME field:

UPDATE checkins SET checked_in_at=UTC_TIMESTAMP() where device_id=?

When the device stops checking in you know it's offline for some reason.

You could also add an ip_address column so you know where your device is.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • How would I get it to compare the current time and last checked in time so that I can display that it is offline? – Alan May 02 '16 at 02:08
  • You'll have to determine what time constitutes offline, like three missed checkins or something. In PHP you can compute the time difference using [standard date functions](http://stackoverflow.com/questions/2920335/how-to-calculate-time-difference-in-php) or [`DATEDIFF()`](http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff) in MySQL. – tadman May 02 '16 at 02:14