It now became highly necessary to not just display the data from the chip in a sensible manner, using PHP calls from a Raspberry Pi running Apache 3 and PHP, but also I thought it would be nice to have a bit more granular data, not just knowing total distances travelled and maximum speeds, but the ability to scroll back through time and see how the data has changed. Thus, Python was invoked, to regularly poll data from the ESP32 and store the variables below into a .csv file:
- distance
- motionCount
Python (a surprisingly pleasant programming language to use following the data type-pernickety C++), was able to simply loop through its polling code,
import requests
import time
import os
import argparse
def retrieveandsave(i):
distance = str(requests.get("http://" + esp32IP + "/d/distance").content)
distance = distance.replace("b","")
distance = distance.replace("'","")
distance = str(float(distance))
motioncount = str(requests.get("http://" + esp32IP + "/d/motioncount").content)
motioncount = motioncount.replace("b","")
motioncount = motioncount.replace("'","")
motioncount = str(int(round(float(motioncount))))
outstring = (str(time.time()) +","+ distance +","+ motioncount)
with open(outfile, 'a') as f:
print(outstring, end="\n", file=f)
print("Saved line " + str(i))
time.sleep(delay)
either indefinitely or for a certain number of repetitions, as defined by input arguments to the program. It wasn't necessary to compile the script to run stand-alone as it doesn't need to be real-time or processor-intensive, simply pinging the ESP32 every 30 seconds or so is enough to generate a fairly large .CSV file.