Wednesday, 15 August 2018

Make It Log

Overview

sensors_davinci_notebook.jpg
Picture by Todd Dailey via Flickr CC BY-SA 2.0) https://www.flickr.com/photos/twid/3013680713

The Past

Before the advent  of computers, all data was written in notebooks. One of Leonardo da Vinci's notebooks, above, is an example, as are the notebooks by the great scientists of the past.

The Present

While observation by eye is still used today, more often than not we automate readings by electronics. In any given day, billions of sensors worldwide take readings of heat, light, wind, etc. But where does that data go?

The Spreadsheet

To record and analyze readings, it is easiest for most folks to use a spreadsheet. Available on all computer platforms, a spreadsheet is one of the first programs people learn. And, the analytical capabilities (re. plotting/graphing) are excellent.
This guide will quickly allow you to learn about placing data readings directly into the spreadsheet of your choice, A Circuit Playground Express and it's multitude of sensors, will log data to a spreadsheet and an Android phone.

Parts List

For the plant monitoring example, add:

For monitoring to an Android phone, add:

Or a standard USB OTG cable adapter

Logging Into a Spreadsheet

There are several ways to record data into a spreadsheet. The following examples will have Circuit Playground Express poll its sensors and then output the data over USB in such a way that it will "type" the numbers into a on-screen computers spreadsheet.
This method uses the USB feature known as human interface device protocol (HID). Plug in a keyboard or mouse into modern computers via USB, and they "just work". Computers can have multiple keyboards, it doesn't confuse them. So we'll use this property to have the Circuit Playground Express output data like a keyboard would, "typing" characters out.
For spreadsheets, to go "to the next line" the keystrokes are typically the down arrow followed by the left arrow to get back to column 1. We'll emulate those keystrokes also to align the data into nice, neat columns.
The first examples shows logging data via CircuitPython. The second example demonstrates the same concept in Microsoft MakeCode.

CircuitPython

CircuitPython

This program is written in CircuitPython, the easy to use language for microcontrollers. If you'd like to learn more about CircuitPython and how to easily get started, please read this guide.
Adafruit recommends the Mu editor. Available for PC, Mac, and Linux for free, it provides a clean, easy to use interface and code upload directly to Adafruit boards. You can learn about Mu here.

How it Works

The following program will take three readings:
  1. time relative to the power-on of the board, not time of day - this is what we get when we request time.monotonic()
  2. light intensity - this is not in lux but it does have higher numbers when theres more light. This is what we get when we request light.value
  3. temperature in Celsius (unless you uncomment one line to get Fahrenheit). This is what we get when we request thermistor.temperature
Once we've collected those three data points, the values are written out via Keyboard emulation to your computer.

Enabling/Disabling the Keyboard


makecode_switch.png
When the slide switch is closed, moved toward the black speaker, it does not log data. When the switch is moved towards the microphone, data will be "typed" out by the board and populate your spreadsheet. Moving the switch back stops the logging so you can use your regular keyboard without numbers being injected into your work.

Start with the switch closed (to the left)

The Code

Copy the following code into Mu. Then save the code to the Circuit Playground Express as code.py. The program runs immediately, so be sure to switch the closed as explained above!
  1. # Circuit Playground Express Data Time/Light Intensity/Temp
  2. # Log data to a spreadsheet on-screen
  3. # Open Spreadsheet beforehand and position to start (A,1)
  4. # Use slide switch to start and stop sensor readings
  5. # Time values are seconds since board powered on (relative time)
  6.  
  7. import time
  8. from digitalio import DigitalInOut, Direction, Pull
  9. import analogio
  10. import board
  11. from adafruit_hid.keyboard import Keyboard
  12. from adafruit_hid.keycode import Keycode
  13. from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
  14. import adafruit_thermistor
  15.  
  16. # Switch to quickly enable/disable
  17. switch = DigitalInOut(board.SLIDE_SWITCH)
  18. switch.pull = Pull.UP
  19.  
  20. # light level
  21. light = analogio.AnalogIn(board.LIGHT)
  22. # temperature
  23. thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000,
  24. 10000, 25, 3950)
  25.  
  26. # Set the keyboard object!
  27. # Sleep for a bit to avoid a race condition on some systems
  28. time.sleep(1)
  29. kbd = Keyboard()
  30. layout = KeyboardLayoutUS(kbd) # US is only current option...
  31.  
  32. led = DigitalInOut(board.D13) # Set up red LED "D13"
  33. led.direction = Direction.OUTPUT
  34.  
  35. print("Time\tLight\tTemperature") # Print column headers
  36.  
  37. def slow_write(string): # Typing should not be too fast for
  38. for c in string: # the computer to be able to accept
  39. layout.write(c)
  40. time.sleep(0.2) # use 1/5 second pause between characters
  41.  
  42. while True:
  43. if switch.value: # If the slide switch is on, don't log
  44. continue
  45.  
  46. # Turn on the LED to show we're logging
  47. led.value = True
  48. temp = thermistor.temperature # In Celsius
  49. # if you want Fahrenheit, uncomment the line below
  50. # temp = temp * 9 / 5 + 32
  51. # Format data into value 'output'
  52. output = "%0.1f\t%d\t%0.1f" % (time.monotonic(), light.value, temp)
  53. print(output) # Print to serial monitor
  54. slow_write(output) # Print to spreadsheet
  55.  
  56. kbd.press(Keycode.DOWN_ARROW) # Code to go to next row
  57. time.sleep(0.01)
  58. kbd.release_all()
  59. for _ in range(3):
  60. kbd.press(Keycode.LEFT_ARROW)
  61. time.sleep(0.015)
  62. kbd.release_all()
  63. time.sleep(0.025) # Wait a bit more for Google Sheets
  64.  
  65. led.value = False
  66. # Change 0.1 to whatever time you need between readings
  67. time.sleep(0.1)

The Spreadsheet

You should have your computer open to a spreadsheet where you want the data (often cell A1). You can use Excel, Google sheets, OpenOffice Calc...anything you like!

Use

When the slide switch is to the right, the data outputs the characters for the time, light intensity and temperature to the screen. Tabs are used to go from cell to cell and emulated cursor keys to get to the first cell of the next row.
When you are done logging, move the slide switch left towards the square black speaker.
If you want more delay between readings, see the bottom of the code to place a call to time.sleep with the parameter afterwards the number of seconds between each reading, such as time.sleep(2.0) to wait 2 seconds between readings.
If you find that the data is getting into the wrong column, adjust the delay between arrow keystrokes a tiny bit - we are "faking" typing, and the Circuit Playground Express can type faster than we can.

Graphing

Each spreadsheet is a bit different on how it graphs so you should read the guides for your spreadsheet on how to do this. Generally you highlight the data (three columns by the number of rows you want graphed and then tell the spreadsheet to plot the data.
The first value will usually be chosen as the x-axis which is good - the time value will give you a basis for consistent reading times for the plot. The light and temperature readings should be plotted as two y values with separate scales.
The following is a plot on Google Sheets:
Plotting both on the same graph is a bit limiting as the scales are very different for light and temperature. I used a logarithmic scale to adjust, but you might wish to plot each separately to see variations.

MakeCode

The MakeCode program acts 100% like the CircuitPython code. You will need to take a couple of steps to set up MakeCode to use Keyboard extension blocks to have the days go into individual spreadsheet cells.

Setup

As of this guide (August 14, 2018) you will need to use the beta web version of MakeCode.
https://makecode.adafruit.com/beta
As this uses the Keyboard extension which has some very new edits. They should be in the main MakeCode builds later in the month.

makecode_ext.png
To install the Keyboard extension, click on the ADVANCED block then the EXTENSIONS.

makecode_sensors_keyboard-extension.png
Select the Keyboard extension and a new block, group colored black, named KEYBOARD will be available. You can use the blocks in this group to move the arrow keys in the spreadsheet.

How it Works

The following program will take three readings:
  1. time relative to the power-on of the board, not time of day - this is the millis (ms) block
  2. light intensity - this is not in lux but it does have higher numbers when theres more light. This is the light level block
  3. temperature in Celsius or Fahrenheit, this is the temperature block
The values are written out via Keyboard emulation to your computer.
Here's the full MakeCode block set:

The Spreadsheet

You should have your computer open to a spreadsheet where you want the data (often cell A1). You can use Excel, Google sheets, OpenOffice Calc...anything you like!
Note that this code will not work with a word processor as-is as it assumes using arrow keys and tabs to change columns. The character 9 in the data writes is the standard code for the tab key so the numbers are written and the code will output a tab afterwards going to the next cell in the spreadsheet.
When the slide switch is closed, moved toward the black speaker, it does not log data. When the switch is moved towards the microphone, data will be "typed" out by the board and populate your spreadsheet. Moving the switch back stops the logging so you can use your regular keyboard without  numbers being injected into your work.

Changes

Feel free to make changes to read other sensors. Also you can use the Pause block to slow down readings in case you believe the readings are being taken too quickly.

Additional use of Keyboard Commands

See the Adafruit tutorial Make it a Keyboard for additional keyboard examples.

Plant Monitoring

Adafruit's basic tutorial for monitoring soil moisture is in this guide.
This page expands that project to log plant data to a spreadsheet. It includes the soil moisture level as measured in the soil moisture tutorial via a capacitive touch pad connected to a piece of metal in the soil of the plant. Measuring the temperature and light levels gives a complete picture of the plant's progress.

The Code

The CircuitPython code is the same as the previous example with these changes:
  • The soil moisture is read on a capacitive touch pad connected to a nail in the soil
  • The moisture data is a fourth column in the spreadsheet.
Here is the code:

Plotting

Here I used Microsoft Excel to hold the data this time. The time units were from 613 to 698 seconds after the board was powered. That forms the x-axis values. The plots are the light (varies the most), temperature and soil moisture (which usually don't vary much over a few minutes).

Use

This code provides a complete plant monitoring solution. You can monitor the light and temperature and how moist the soil is over time.
The code could be expanded to possibly self-water. You would need a soil moisture value at which you consider the soil to be dry. We have found that 1500 is a good baseline that you can use based on the type of plant and it's requirements.
You would check if the moisture was < 1500, then do something that releases a determined amount of water (which again varies by the plant type, pot, etc.) to water the plant appropriately.

Logging Via Android Phone

In the tutorial Android GBoard Morse Code Control with Circuit Playground Express, Adafruit demonstrates using the HID Keyboard functions to enter data into an Android phone and then use a feature of the Google GBoard keyboard to turn Morse Code into text.
But you don't have to use Morse Code, you can plug the CPX right into your Android phone or tablet and have it type just the same way it does on your computer with a spreadsheet above. This time you can go mobile. All you need is a special USB cable called OTG (on the go).

Connecting Your Phone to the Board


circuitpython_1200px-USB_OTG_Log-sm.svg
source: Wikipedia, marked "Fair Use"
Android phones typically come with a micro-B USB connection. This USB connection follows the USB on-the-go (OTG) specification so peripherals can be plugged in to the port and be used by the phone.
This project is going to use the capabilities of Circuit Playground Express to act as a human interface device (HID), namely a keyboard. When the user wants the Express to send a key, they'll use a button, the board will translate this to some keyboard character, and the phone will believe a keyboard was used to enter the character.
This type of behavior is useful for a great many projects. Control of devices using alternative interfaces is the most popular. This can be in manufacturing, at home, or in assistive technology (AT) situations where traditional keyboards cannot be conveniently used.

Making the Connection


circuitpython_3610-01.jpg
For this project you will need a micro-B male to micro-B male connection that conforms to the OTG specification. Both the phone and the Circuit Playground Express have a micro-B female connector.

The cable at left is the most direct connection. Some electronics stores carry these - Adafruit sells them as product #3610.

There are also a number of other OTG to regular USB connector devices including Adafruit Tiny OTG Adapter and the short USB OTG Host Cable - MicroB OTG male to A female.

The Final Phone Setup


circuitpython_P1000013.jpg
Here is a picture of the connections with a USB OTG adapter rather than the micro-micro cable as I did not have the cable at hand. The connections would be identical - connect the phone micro-B USB to the Circuit Playground Express micro-USB connector.

Logging to the Phone

With your USB connection from the Circuit Playground to your phone set, the Circuit Playground Express data can be logged to the phone.
Depending on your phone, you may need to download a power sharing app to ensure the phone can provide power to the Circuit Playground Express. For Samsung phone power over USB, go to the Samsung App Store and download the app "Power Sharing"

Spreadsheet or other Data Capture Program

You should also use the Google Play Store to install a spreadsheet (or other program of your choice) to capture data from the Circuit Playground Express acting as a HID keyboard.
As Android is Google-centric, the best option is to go to the Google App store and load Google Sheets if it is not already available. I note that Microsoft Excel is also available in the Google App Store, but we didn't try it!

Code

The code can be either of the programs earlier in this tutorial. The demonstration will use the second example which includes plant soil moisture data in the fourth column of the spreadsheet. You can change the program to add or subtract data as desired.

Usage

Load your spreadsheet app and select that you want to create a new spreadsheet. Position the focus on cell A1. Switch the slide switch and watch the data flow.
This type of data capture is great for gathering data "in the field" or "on the go".

No comments:

Post a Comment