Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Joy of Connecting Elixir to the Physical World

Frank Hunleth
September 02, 2016

The Joy of Connecting Elixir to the Physical World

Frank Hunleth

September 02, 2016
Tweet

Other Decks in Programming

Transcript

  1. Agenda ▸ Three routes to hardware with Elixir ▸ Nerves

    ▸ Desktop Linux single board computers ▸ Your laptop ▸ Examples ▸ Monitoring your garden via SBC ▸ Finding out where you are via GPS on your laptop ▸ Comparing the routes
  2. Key Nerves attributes ▸ Build tooling and some basic infrastructure

    for running Elixir on embedded Linux ▸ Start small and build up vs. trim down ▸ Cross-compiled ▸ Elixir and Erlang/OTP libraries and applications over embedded Linux ones (when they exist)
  3. Nerves Your Elixir project and dependencies Linux, C libraries, Erlang

    runtime Nerves System Image OTP release Firmware image
  4. Desktop Linux Single Board Computers ▸ Usually low cost ARM

    boards with hardware-friendly I/O ▸ Raspberry Pi, Beaglebone Black, probably 100 others ▸ Most distributed with Debian or a Debian-derived distributions like Raspbian ▸ Work like real computers What I learned the hard way: Don’t choose a board just based on hardware specs
  5. The great part about Debian Pretty much everyone else uses

    it so it usually works well, has tutorials, and large user communities But, you probably will need to build Erlang from scratch. 
  6. Agenda ▸ Three routes to hardware with Elixir ▸ Nerves

    ▸ Desktop Linux single board computers ▸ Your laptop ▸ Examples ▸ Monitoring your garden via SBC ▸ Finding out where you are via GPS on your laptop ▸ Comparing the routes
  7. Example ▸ What do you do if you forget to

    water your plants, but have a Raspberry Pi handy? ▸ Answer: Connect a moisture sensor to the Pi to monitor it and push notifications to your phone, of course.
  8. Hooking it up ▸ MCP3008 converts analog measurements to digital

    ▸ Use the SPI pins on the Raspberry Pi for input ▸ Read values using elixir_ale Analog Digital SPI
  9. Figure out what to transfer 3 input address bits 1

    mode bit 1 Start bit 10 bits in the result
  10. Figure out what to transfer 3 input address bits 1

    mode bit 1 Start bit 1 bit between request & response 1 bit that is zero 10 bits in the result
  11. Figure out what to transfer 3 input address bits 1

    mode bit 1 Start bit 1 bit between request & response 1 bit that is zero 10 bits in the result 1+1+3+1+1+10 = 17 bits????
  12. The code mode = 0 # Single-ended mode sensor =

    2 # Try this sensor <<_::size(14), value::size(10)>> = Spi.transfer(pid, <<0x01, mode::size(1), sensor::size(3), 0::size(12)>>) IO.puts "Sensor #{sensor} measured #{value}”
  13. Agenda ▸ Three routes to hardware with Elixir ▸ Nerves

    ▸ Desktop Linux single board computers ▸ Your laptop ▸ Examples ▸ Monitoring your garden via SBC ▸ Finding out where you are via GPS on your laptop ▸ Comparing the routes
  14. Physical computing from your laptop ▸ No need to change

    your development routine ▸ Many sensors and actuators supported ▸ The catch ▸ Your laptop is connected ▸ Interface can be slow
  15. The UART ▸ Universal Asynchronous Receiver/Transmitter ▸ Three main wires:

    receive, transmit, and ground ▸ Provides a byte stream connection like TCP except: ▸ You or someone else wires up the connection ▸ Both sides need to agree on the connection speed and parameters ▸ Bytes get dropped and bits get corrupted sometimes ▸ Sometimes you get flow control
  16. The UART ▸ Dirt simple technology that’s still ubiquitous in

    embedded ▸ Inexpensive and well-supported ▸ Easy to debug ▸ Wired, but wireless options that look nearly identical to software
  17. USB to serial cable ▸ Key attributes ▸ Logic voltage

    level - 5V or 3V ▸ 6 pin header vs. wires ▸ 5 V power supply? ▸ FTDI, Prolific, or clone chip ▸ Sold by Adafruit, Digikey, Microcenter, Mouser, Sparkfun, etc. ▸ Except for the clone chip versions, all work on Linux, Mac, and Windows
  18. Where am I? ▸ Use GPS! ▸ Most modules provide

    a UART interface (GND, RX, TX) ▸ If the modules accepts 5 V, power it from the USB to serial cable ▸ Position data (and more) comes across as NMEA-0183 strings
  19. A taste of nerves_uart iex(1)> Nerves.UART.enumerate %{"ttyUSB0" => %{description: "USB-Serial

    Controller", manufacturer: "Prolific Technology Inc.", product_id: 8963, vendor_id: 1659}}
  20. A taste of nerves_uart iex> {:ok, uart}= Nerves.UART.start_link {:ok, #PID<0.186.0>}

    iex> Nerves.UART.open(uart, "ttyUSB0", speed: 9600, active: false) :ok
  21. A taste of nerves_uart iex> Nerves.UART.configure(uart, active: true) :ok iex>

    flush {:nerves_uart, "ttyUSB0", "$"} {:nerves_uart, "ttyUSB0", "P"} {:nerves_uart, "ttyUSB0", "G"} {:nerves_uart, "ttyUSB0", "T"} {:nerves_uart, "ttyUSB0", "O"} {:nerves_uart, "ttyUSB0", "P"} {:nerves_uart, "ttyUSB0", ","} {:nerves_uart, "ttyUSB0", "1"} {:nerves_uart, "ttyUSB0", "1"} {:nerves_uart, "ttyUSB0", ","} {:nerves_uart, "ttyUSB0", "2"} {:nerves_uart, "ttyUSB0", "*"} {:nerves_uart, "ttyUSB0", "6"} {:nerves_uart, "ttyUSB0", "E"} {:nerves_uart, "ttyUSB0", "\r"} {:nerves_uart, "ttyUSB0", "\n"}
  22. A taste of nerves_uart iex> flush {:nerves_uart, "ttyUSB0", "$PGTOP,11,2*6E"} {:nerves_uart,

    "ttyUSB0", "$GPGGA,000417.800,,,,,0,0,,,M,,M,,*42"} {:nerves_uart, "ttyUSB0", "$GPGSA,A,1,,,,,,,,,,,,,,,*1E"} {:nerves_uart, "ttyUSB0", "$GPRMC,000417.800,V,,,,,0.00,0.00,060180,,,N*48"} {:nerves_uart, "ttyUSB0", "$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32"} :ok
  23. The Hayes AT command set lives! ▸ Bluetooth - Talk

    to your phone ▸ Serial port extenders++ like the RN-42 ▸ BLE modules like Adafruit’s Bluefruit ▸ Cellular modems - Talk to the nearest cell tower ▸ SparqEE, LinkSprite, Adafruit ▸ Satellite modems - Talk to a nearby Iridium satellite ▸ RockBLOCK
  24. nerves_uart to an Arduino ▸ Install the firmata firmware on

    the Arduino ▸ UART protocol for controlling pins ▸ https://github.com/firmata/protocol ▸ Elixir even has a firmata library ▸ https://github.com/kfatehi/firmata ▸ Looking for a maintainer
  25. Common embedded system architecture Hard real-time Microprocessor (ARM Cortex, x86,

    etc.) Microcontroller (ARM M3, ATmega) Soft real-time Elixir C/C++ UART
  26. Agenda ▸ Three routes to hardware with Elixir ▸ Nerves

    ▸ Desktop Linux single board computers ▸ Your laptop ▸ Examples ▸ Monitoring your garden via SBC ▸ Finding out where you are via GPS on your laptop ▸ Comparing the routes
  27. Comparing approaches Laptop + nerves_uart Desktop Linux SBC Nerves Edit/build/test

    cycle Host Target Edit/build on host, run on target System specs Modern laptop ~700 MHz+ w/ GB of storage 300-400 MHz+ 32 MB+ Supported devices Limited to UART- based HW, firmata Lots out of the box Lots, but may need to be added Tools You have them already Manually build Erlang Nerves provides compilers Deployment Share code Disk image and .deb files Firmware image file When I use it Full featured dev environment; Easiest to unit test Quick debugging of low level hw Small, focused, built for making products
  28. Where to find out more ▸ Project pages: ▸ https://github.com/nerves-project/nerves_uart

    ▸ https://github.com/fhunleth/elixir_ale ▸ Twitter: @NervesProject and @fhunleth ▸ #nerves channel on the elixir-lang slack ▸ Nerves and embedded Linux consulting/development [email protected]