
Timezone: Europe/London
As my project requires the units in the field to be running un-interupted for months, I started uploading system logs every time they connected to the web but the increased overhead in data usage and power consumption made this less than ideal. I rely on these units drawing as little power as possible in order for the solar panel to keep the battery charged even after several dull days in mid-winter.
The answer to this was to keep the logs on and SD card.
The advantage here is the card can be removed to read the logs without the need to upload the logs, or to stop the units and connect them up to a computer to read them. I have tried 3 different SD breakout boards, two from Adafruit and one from Pololu and they all work without the need for any external libraries if only basic functionality is required, ie Create, Delete, List, Read and Write. All these are available from the native uPython module "os".
There is a driver, SDCard, in the machine module, but I have found it problematic. I have found this library works well.
sdcard.py from micropython-lib on github
You need to set up the pins you are going to use for the SPI interface, and power. Ground needs to be connected to the ESP GND pin. You can power the card reader either by permanent connection to the ESP 3v pin, or any GPIO output pin. EDIT. I've since found that the current from a GPIO is not enought to power some breakouts. An option is to use an NPN transistor as a switch. Connect Vin on your breakout to +3v, GND to the emitter of the transistor. Connect the collector to GND and base via a 220R resistor to the GPIO pin.
SD breakout boards label their input/output as either DO or SO, and DI or SI. These need to go to whatever you choose for mosi and miso, in my case I chose pin 32 for mosi, wired to DI on my breakout, and pin 14 for mosi, to DO on the breakout.
The breakouts have a clock pin, SCK or SCK and I chose pin 5 on the ESP, which is actually the SCK pin. The CS pin I chose to connect to use pin 27 on the ESP but you can use another pin if you need to. The CD pin is "Card Detect", and on most (not all though) breakouts this is shorted to ground when a card is not inserted.
This line initialises the SPI bus (use SoftSPI and not SPI) "spi=SoftSPI(1,sck=Pin(5),mosi=Pin(32),miso=Pin(14))"
After that we can create an SDCard object called "sd" and use it with the uPython inbuilt functions for file manipulation.
Creating this object will not throw an error if there is not a card inserted, so it is wise to check for this each time we access any of the filesystem functions.
That way, if there is a card error we can bypass that code without interupting the rest of the programme.
If the files written to the SD card are mission critical, you could always write code to write to the internal file system if the SD card is not available.
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
OTA Updating running software without interuption
Future articles
Communication between ESPs
Using an Adafruit GPS module
Hassle free web communication with urequests.py.