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

The Joy of Connecting Elixir to the Physical World

C09145df06a9c8962699210b893b960c?s=47 Frank Hunleth
September 02, 2016

The Joy of Connecting Elixir to the Physical World

C09145df06a9c8962699210b893b960c?s=128

Frank Hunleth

September 02, 2016
Tweet

Transcript

  1. Frank Hunleth @fhunleth The Joy of Connecting Elixir to the

    Physical World
  2. Frank Hunleth @fhunleth How to avoid pain and agony with

    hardware…
  3. 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
  4. Nerves Craft and deploy bulletproof embedded software in Elixir

  5. 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)
  6. Nerves Your Elixir project and dependencies Linux, C libraries, Erlang

    runtime Nerves System Image OTP release Firmware image
  7. 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
  8. 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. 
  9. 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
  10. 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.
  11. 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
  12. What does elixir_ale want for SPI?

  13. Get the datasheet

  14. Figure out what to transfer

  15. Figure out what to transfer 1 Start bit

  16. Figure out what to transfer 1 mode bit 1 Start

    bit
  17. Figure out what to transfer 3 input address bits 1

    mode bit 1 Start bit
  18. Figure out what to transfer 3 input address bits 1

    mode bit 1 Start bit 10 bits in the result
  19. 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
  20. 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????
  21. Try #2 - Find app note

  22. Try #2 - Find app note

  23. Try #2 - Find app note 3 bytes!

  24. 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}”
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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}}
  32. 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
  33. A taste of nerves_uart iex> Nerves.UART.read(uart) {:ok, "$PGTOP,11,2*6E\r\n$GPGGA,000204.800,,,,,0,0,,,M,,M,,*46\r\n$GPGSA,A,1,,,,,,,,,,,,,,,*1E\r\n$GPRMC,0002 04.800,V,,,,,0.00,0.00,060180,,,N*4C\r\n$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32\r\n$PGTOP,11,2*6E\r\n$GPG GA,000205.800,,,,,0,0,,,M,,M,,*47\r\n$GPGSA,A,1,,,,,,,,,,,,,,,*1E\r\n$GPRMC,000205.800,V,,,,,0.00,0.00,06018

    0,,,N*4D\r\n$GPVTG,0.00,T,,M,0"}
  34. 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"}
  35. A taste of nerves_uart iex> Nerves.UART.configure(uart, framing: {Nerves.UART.Framing.Line, separator: "\r\n"})

    :ok
  36. 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
  37. What else can a USB->Serial cable do? ATDT5551212

  38. 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
  39. 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
  40. Common embedded system architecture Hard real-time Microprocessor (ARM Cortex, x86,

    etc.) Microcontroller (ARM M3, ATmega) Soft real-time Elixir C/C++ UART
  41. 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
  42. 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
  43. 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 fhunleth@troodon-software.com