IoT Tracking Device - PHP Server & OpenStreetMap (Part 2)
A Tutorial on Writing Coordination to Server Using PHP and Display On the Map
Want some music while reading? 🎵📻
Introduction
This tutorial continues the work from the previous part where we set up the ESP32 to write data to the server.
In this part, we should display the coordination data on the map that we post to the server in the previous post and show the markers that indicate our locations as well. Our group of students quite struggled with the product’s requirements. The teacher expects that we shouldn’t use any map API that requires an API key and the programming language should be PHP for consistency (our server is created with PHP). Therefore, Google Maps shouldn’t be used for this project. This part is another team’s responsibility but I was really curious and I also want to learn PHP. Afterwards, I decided to do this part just for fun but I succeeded 😁. I’m going to show you how to do this.
Requirements
- XAMPP
- PhPStorm / VSCode
We need XAMPP for this project and PHP in general because it’s super easy to run PHP code on localhost with XAMPP. After installing XAMPP, navigate to the installation folder and search for “htdocs” folder. You should place this project folder inside, let’s call this folder “madrid-iot”. After that, start Apache server from XAMPP and you could access the project from
http://localhost/madrid-iot/
If you need further information for the setup, please check this video out.
Extract Data
This is the code for extracting the data from the server.
|
|
First of all, we have 2 global variables: last_lines and data. The last_lines variable is for displaying and checking purposes and the data variable contains all of the data that we want to get, specifically all of latitudes and longitudes.
We we an array of base_urls because our group consists of 5 teams and each team will carry a device to track their GPS. Therefore we need to have 5 endpoints to get the data from all of them.
On line 13, we start looping through each base url to:
- Get the file from that endpoint
- Get the number of lines of that file and extract the last line from the file
- Extract latitude and longitude from that last line
- Add the data to the data array
From line 14 - 18, we basically just set up the file name and the saving directory for the files. The files will be saved in the current folder in this case.
Get Files From Server
The first step is getting the files from the server so that we could extract to use after that. Here is the code for that
|
|
We simply curl a request using the base url and save the file at the defined saving location.
Extract The Last Line From File
|
|
Why do we need to get the number of lines? The file will definitely contains many lines because ESP32 will write data to the file every 1 second. So we need to know how many lines there are to get the last line. Then we could extract the last line from all the previous lines, which is the last known location. This line of code seems a little bit strange at first but it’s not
$lines[$no_of_lines - 2]
We know that, for example, in Javascript, if we want to get the last item from an array, it should be [items.length - 1], but in here it’s - 2 because when we download the file, there’s a trailing empty line at the end. Therefore, the code is - 2 not - 1.
Extract Coordination From The Last Line
|
|
The data line from the server’s file looks like this
|
|
It starts with the timestamp when the line is written into the file, latitude, longitude, temperate, pressure, altitude and number of satellites.
We’re only interested the later part of the string, therefore, we need to clean up the string first. The first line of the code removes all of the empty space. The second line separates the string into an array by the "," delimiter. And then we save each item of that array into an array that we’re going to use at the end.
Map
So after we have extracted the data from the files, we could now take a look into how to set up the map to display the data. We use OpenStreetMap here because it doesn’t require an API key to manipulate the markers.
The OpenStreetMap API uses Javascript, but we’re using PHP, how can we integrate it into our code? It’s super simple, let’s take a look into the code Below
|
|
The code looks a bit messy, but it’s basically HTML and PHP combined with Javascript.
There is a button called “Fetch Data”, every time we click it, it will execute the extractData() function and run all the code mentioned above. The code below is responsible for this
|
|
|
|
We know that we have the global variable data that contains an array of the information that we need. For Javascript to read PHP variable, we add this line of code
|
|
After this, we can just loop through the data array, use the information to set latitude and longitude for the markers and they will be rendered on the map.
Conclusion
It was a really fun to learn PHP and at the same time solve a challenge like this. Before this project, I’ve always thought that PHP was outdated somehow but that’s actually wrong. PHP is still used by nearly 80% of all websites and that could be because of the ease of usage and the its versatility.
I was really happy when the teacher called be “guru” after seeing me be able to solve this issue. That was the best compliment I have received so far 😄. Still, I feel that I need to improve my skills a lot more!