We are using the S2 Lolin mini with circuitpython. It was our hope to build a way to get data off of the device, in a reliable(ish) way. While you can use STDIO and print to the output window (command line) you can’t save to a file without modifications to the device that I wasn’t willing to make. I spent some time on this today and it wasn’t easy. (This is actually a good reason to use a regular raspberry pi with memory.) These are my notes on how I saved data from my device into a CSV file on my computer.

Things that didn’t work

  • Adafruit has some built in IoT functionality; however, none of it seemed to work with S2 Lolin mini. However there were many options for the ESP32-S2 which the Lolin chip is based off. We couldn’t make it work quickly enough. So it could also be our fault.
  • I considered building an external database and sending messages over the internet. I would normally have gone this way. However, we have a fairly closed internet system on campus and setting up databases and the like isn’t easy to do locally. I spent a little time setting up a database online but got side tracked.
  • I screwed around with a couple of other ideas.

I came up with a VERY brittle solution which will work for what we are doing in the short term. Here it is.

Step 1: You can log the shell in thonny. To do this, go to Thonny settings, click over to the general tab, click the “log program usage…” option.

Step 2: Restart Thonny

Step 3: Design a program in Thonny that outputs a regular data output, with a key in front.

For example here we used the key !!!!. So you get a regular output with a string of data. See the shell below:

Notice in the shell with the leading key. This string will be logged in the log file on the host computer. Unfortunately, there is also a bunch of other crap that also gets logged.

counter       = 0
StopCounting  = 100
kHalf         = "!!"

while counter < StopCounting:
    
    Temperature  = counter * 3
    Laser        = Temperature - 7
    
    myStr = kHalf + kHalf + "," + str(counter) + ", " + str(Temperature) + ", " + str(Laser) + ", 0"
    print (myStr)
    
    counter = counter + 1

Unfortunately, you can’t use the key in the code directly, unless you want to do more programming – because that also shows up in the log. And we are just going to search for the key. Again, I was going for the sloppiest most brittle solution. I JUST WANT MY DATA! So I built my key from two peices !! and !!.

Step 4: Parse and save the Log

Based on the recommendation in the following thread, https://forums.raspberrypi.com/viewtopic.php?t=310502 I developed a short python script that is ran on the host computer (I ran it in Sypder because I had it opened.) It goes through the log and looks for the key and then parses the data. Finally the data is stored to a CSV file.

myFile   = 'C:\\Users\\mwright\\AppData\\Roaming\\Thonny\\user_logs\\2023-04-27_16-47-41_0'
f = open(myFile + '.txt','r')


MyKey      = '!!!!'
MyLines    = str('')

try:
    while True:
        line = f.readline();
        if not line:
            break
        if line.find(MyKey)>=0:
            text_value = line.split(',')
            val1 = float(text_value[1])
            val2 = float(text_value[2])
            val3 = float(text_value[3])
            OutToFile = str(val1) + ',' + str(val2) + ',' + str(val3) + '\n'
            print (OutToFile)
            MyLines = MyLines + OutToFile
except:
     pass
            
 
f.close()

g = open(myFile + '.csv','w')
g.write(MyLines)
g.close()

If you run this script it nicely parses the data into a csv file.


OMG! There has to be a better way! But it works and it does what I need it to do.

Posted in

Leave a Reply


Cosmic Pathways, Lab for Kids, and many of the other research activities discussed on this website is supported by the National Science Foundation and the Physics Teacher Education Coalition (PhysTEC) under grant no. 2325980. Any opinions, findings, and conclusions, or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.

Discover more from Cosmic Pathways

Subscribe now to keep reading and get access to the full archive.

Continue reading