Slide 1

Slide 1 text

Reliable distribution system with Ruby x IoT Ruby Conf Taiwan 2019

Slide 2

Slide 2 text

whoami ࠓҪᏴհ Shinsuke IMAI Twitter: @imashin_ ! Japan Cookpad inc. / Shopping Platform Department Ruby x IoT

Slide 3

Slide 3 text

Topic • Our food delivery service with Ruby x IoT • Building a reliable IoT system

Slide 4

Slide 4 text

We do not use mruby

Slide 5

Slide 5 text

We use CRuby

Slide 6

Slide 6 text

Agenda • What is "cookpad mart" • Building two IoT systems • The way to develop stable IoT system

Slide 7

Slide 7 text

Fresh food EC platform released at last summer https://cookpad-mart.com/

Slide 8

Slide 8 text

How to use

Slide 9

Slide 9 text

Order Choose 
 a pick-up point

Slide 10

Slide 10 text

Order

Slide 11

Slide 11 text

Order Order items

Slide 12

Slide 12 text

Order

Slide 13

Slide 13 text

Pick up Next day, go to the pick-up point

Slide 14

Slide 14 text

Pick up Unlock fridge by QR code

Slide 15

Slide 15 text

Pick up Pick ordered items up

Slide 16

Slide 16 text

Distribution flow

Slide 17

Slide 17 text

Ordering to shops Rails batch sends orders to shops by FAX, Mail or Chat tool

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Preparing Shops attach labels to ordered items

Slide 20

Slide 20 text

Collecting A carrier collects items
 from shops

Slide 21

Slide 21 text

Delivering The carriers deliver items 
 to pick-up points

Slide 22

Slide 22 text

We don't have distribution centers

Slide 23

Slide 23 text

Deliver in one day Low cost

Slide 24

Slide 24 text

Fresh foods

Slide 25

Slide 25 text

IoT devices contribute to this distribution system

Slide 26

Slide 26 text

IoT devices Security lock on pick-up fridge Label printing system

Slide 27

Slide 27 text

Label printing system

Slide 28

Slide 28 text

Important point in this distribution Put labels on items Pickup labeled items Print labels

Slide 29

Slide 29 text

Prototype of label printer system

Slide 30

Slide 30 text

Hardwares iPad (Cellular model) Auto print app Always on Normal label printer Always on Ethernet connection Lightning to USB adapter USB to Ethernet adapter

Slide 31

Slide 31 text

1. get label data Workflow mart-server Rails iPad Label printer 2. print command 3. printer status 4. printer status

Slide 32

Slide 32 text

Failures • Too many steps to set up • Fail many times to print labels ‣ Unstable connection between iPad and label printer ‣ Label printer jam ‣ App crash ‣ App freeze • Unable to update easily

Slide 33

Slide 33 text

Label printing system as a product

Slide 34

Slide 34 text

Hardwares 1) Don't use iOS devices => Use controllable OS and device 2) Don't use normal label printer => Use businnes-use label printer

Slide 35

Slide 35 text

Hardwares STAR MICRONICS TSP700II Thermal label printer Sensors for monitoring SNMP Huawei M2372h-607 LTE USB dongle Raspberry Pi 3 Model B+ Edge controller Arch Linux ARM Router / SSH Server Network monitoring / Kitting

Slide 36

Slide 36 text

Network Can't access from server to edge => Use cellular network designed for IoT

Slide 37

Slide 37 text

https://www.soracom.io/products/

Slide 38

Slide 38 text

Network SORACOM print batch proxy gateway Canal Gate Air Shepherd mart server admin API API Order 6TFS Dongle Printer Raspi

Slide 39

Slide 39 text

Software Don't use uncontrollable printer controller => Develop controllable printer controller

Slide 40

Slide 40 text

https://rubygems.org/gems/star_ethernet

Slide 41

Slide 41 text

https://www.starmicronics.com/support/Mannualfolder/starline_cm_en.pdf

Slide 42

Slide 42 text

Sample code require 'star_ethernet' data = [ "Hello\nRuby Conf\nTaiwan!!\n", # command for feed label StarEthernet::Command.form_feed, # command for cut label StarEthernet::Command.partial_cut_current_line, ].flatten.join socket = TCPSocket.new('192.168.11.2', 9100) socket.print(data) socket.close

Slide 43

Slide 43 text

Commands

Slide 44

Slide 44 text

def set_cancel_the_double_wide_high(n1, n2) [0x1b, 0x69, n1, n2].pack('C*') end def sextuple_font_size set_cancel_the_double_wide_high(0x05, 0x05) end https://www.starmicronics.com/support/Mannualfolder/starline_cm_en.pdf

Slide 45

Slide 45 text

require 'star_ethernet' data = [ StarEthernet::Command.quadruple_font_size, "Hello\nRuby Conf\nTaiwan!!\n", StarEthernet::Command.form_feed, StarEthernet::Command.partial_cut_current_line, ].flatten.join socket = TCPSocket.new('192.168.2.3', 9100) socket.print(data) socket.close

Slide 46

Slide 46 text

Next update of label printer system

Slide 47

Slide 47 text

Next update • LTE USB dongle's signal is not good in some places • AC adapters set is not cool

Slide 48

Slide 48 text

Next update Antenna 2ch (5V, 24V) power supply mini PCIe LTE module to USB

Slide 49

Slide 49 text

Pick-up point security

Slide 50

Slide 50 text

Important point in this distribution • Only user can open the fridge door • It works on unstable LTE network

Slide 51

Slide 51 text

Prototype of fridge lock system

Slide 52

Slide 52 text

Before

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

After At

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

It works.

Slide 59

Slide 59 text

Hardwares Controller box same with label printer system HF500-W Honeywell QR reader D4SL-N OMRON Lock door switch Key holding force of 1,300N OPEN/CLOSE monitoring LOCK/UNLOCK monitoring DHT11 Temperature sensor

Slide 60

Slide 60 text

Attached to fridge

Slide 61

Slide 61 text

Requirement User can unlock in any troubles

Slide 62

Slide 62 text

Unlocking workflow User Fridge system mart-server mart-edge Fridges JWT token QR Reader Signature verification working on offline status Authorized User Unauthorized user SORACOM Gate SORACOM Canal

Slide 63

Slide 63 text

Unlocking workflow User Fridge system mart-server mart-edge Fridges JWT token QR Reader Signature verification working on offline status Authorized User Unauthorized user SORACOM Gate SORACOM Canal

Slide 64

Slide 64 text

Unlocking workflow User Fridge system mart-server mart-edge Fridges JWT token QR Reader Signature verification working on offline status Authorized User Unauthorized user SORACOM Gate SORACOM Canal

Slide 65

Slide 65 text

Unlocking workflow User Fridge system mart-server mart-edge Fridges JWT token QR Reader Signature verification working on offline status Authorized User Unauthorized user SORACOM Gate SORACOM Canal

Slide 66

Slide 66 text

Edge services • QR reader ‣ serial port input mode 3BTQCFSSZ1J 4JOBUSB 23SFBEFS DPOUSPMMFS 23SFBEFS 0.30/MPDL -PDL DPOUSPMMFS User unlock Remote unlock

Slide 67

Slide 67 text

QR reader require 'net/http' require 'uri' require 'serialport' HOST = 'http://localhost:9292/qr_reader' SerialPort.open('/dev/ttyACM0', 38400, 8, 1, SerialPort::NONE) do |com| while true begin str = com.gets body = { input: str } Net::HTTP.post_form(URI.parse(HOST), body) rescue => e p e end end end

Slide 68

Slide 68 text

Edge services • Sinatra ‣ user / remote unlocking ‣ door status monitoring 3BTQCFSSZ1J 4JOBUSB 23SFBEFS DPOUSPMMFS 23SFBEFS 0.30/MPDL -PDL DPOUSPMMFS User unlock Remote unlock

Slide 69

Slide 69 text

Sinatra server class App < Sinatra::Base post '/qr_reader' do body = request.body.read decoded_token = JWT.decode body.input, nil, false if verify(decoded_token) fridges_controller.open_all_fridges end end end

Slide 70

Slide 70 text

Edge services • Lock controller ‣ door open/close ‣ door lock/unlock 3BTQCFSSZ1J 4JOBUSB 23SFBEFS DPOUSPMMFS 23SFBEFS 0.30/MPDL -PDL DPOUSPMMFS User unlock Remote unlock

Slide 71

Slide 71 text

Lock def door_auto_lock_on_open Thread.new do loop do lock! if opened? sleep 1 end end end def opened? RPi::GPIO.high? (@door_open_closed_detection_contact_pin_num) end def lock! Mutex.new.synchronize do RPi::GPIO.set_low(@solenoid_pin_num) end end

Slide 72

Slide 72 text

How to develop reliable IoT products

Slide 73

Slide 73 text

What I did • Find problems at first by prototyping • Understand what is acceptable and what is not • Make the system robust with designing

Slide 74

Slide 74 text

Ruby can do anything • Label printer system ‣ TCP socket programming ‣ Print commands ‣ Kitting codes • Fridge lock system • Control QR reader • Verification / Signature • Control fridge lock via GPIO

Slide 75

Slide 75 text

No content