47
Hint and Tips to Bring your Raspberry Pi Project into “Real World” Operation June 4, 2015 @ LinuxCon Tokyo 2015 Masahiro Furutera

Hint and Tips to Bring your Raspberry Pi Project …...Hint and Tips to Bring your Raspberry Pi Project into “Real World” Operation June 4, 2015 @ LinuxCon Tokyo 2015 Masahiro

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

  • Hint and Tips to Bring your Raspberry Pi Project into “Real World” Operation

    June 4, 2015 @ LinuxCon Tokyo 2015 Masahiro Furutera

  • DisclaimerThis document is provided for LinuxCon Tokyo 2015 as a public documentation. Raspberry Pi is a trademark of the Raspberry Pi Foundation. Raspbian is a trade mark of the Raspberry Pi Foundation. Debian is a trade mark of the Debian Project. Other Product and Service names may be trademarks or registered trademarks of their respective companies or organizations. Some topics are assuming specific Japanese law and regulations, may be different situation for other countries. Information includes author’s own opinion for specific S/W, H/W, and operating circumstance. Information includes sample program/script are “as is”, “no guaranty”, “without warranty”. Usage and treatment of information will become your own risk. Information may be changed by future updates of S/W, H/W, Service, and local law/regulations. The copyright of this documentation belongs to the author. You can refer the contents freely in your presentation, document, or web content. However you need to include reference info. If you translate contents, you need also specify it is translated from the original. For further questions, please contact to the author.

    Copyright 2015 Masahiro Furutera, All rights reserved.

  • 35+ years experience of IT industry 1980-1986 Host based CAD/CAM (developer/maintainer) UNIVAC Airline Reservation System (developer/system programmer) 1986-1988 Microcode of PC intelligent communication card (developer) IBM 1988-2008 Everything about RS/6000, pSeries, and AIX IBM Technical Disclosure, Beta programs, Internationalization, 2nd level pre-sales support, HPC benchmarking, Y2K, 3rd party H/W and S/W porting, OEM Support Brand Manager (launch, marketing, BID pricing, and contract) 2008-2012 Linux Alliance Manager IBM after IBM retirement from 2012 Landlord/President, Le Eclat (Personal business, High-Tech Apartment) http://www.eclat-east1408.com * contents are Japanese Technology Strategist, Time Machine K.K. (nonexclusive, H/W, S/W, and Cloud Solutions for Wireless Sensor Network) http://tmcn.jp * contents are Japanese       

    Who am I ?

    http://www.eclat-east1408.comhttp://tmcn.jp

  • Raspberry Pi H/W Basics

    Raspberry Pi Operating System Basics

    Tips for DYI H/W development

    Tips for DYI S/W (application) development

    Considerations for Law and Regulations

    Tips for Operation and Maintenance

    Example: My Project

    Demo

    Today’s topics

  • Raspberry Pi is an ARM architecture based low cost one board micro computer. (foot print is very similar to Credit Card size for Type A/B/B+/2 B. A+ is smaller.)

    Comparison to micro servers and traditional Linux platform : - no Power Switch, no Real Time Clock (RTC) - boot from SD/micro SD card only - native GPIO ports - most model has 100BT Ethernet - on board camera and other special I/Fs - no rich BIOS, simple boot mechanism

    Comparison to other micro controllers : - on board audio, video, and HDMI outputs - except model A/A+, 100BT Ethernet is on board - rich memory, rich storage, near 1GHz 32bit CPU, could run Linux and other OS - no analog IO

    note: imagine UNIX/Linux server/work station of early 2000’s, or old Cray 2 Super Computer, Raspberry Pi is much powerful and with almost full AV functions.

    Raspberry Pi H/W Basics

    Raspberry Pi Type B+ Photo Source: https://www.raspberrypi.org

    https://www.raspberrypi.org

  • Raspberry Pi H/W Basics (con’t)

    Type A Type A+ Type B Type B+ 2 Type B

    SoC*BCM2835

    1 core 700MHzBCM2835

    1 core 700MHzBCM2835

    1 core 700MHzBCM2835

    1 core 700MHzBCM2836

    4 core 900MHz

    memory 256MB 256MB 512MB 512MB 1GB

    power 300 mA (1.5W) 200 mA (1.0W) 700 mA (2.1W) 600 mA (1.8W) 900 mA (4.5-5.5W)

    pin header

    26 40 26 40 40

    USB2 x 1 x 1 x 2 x 4 x 4100BT X X O O OSD SD micro SD SD micro SD micro SD

    Video RCA combined with audio mini

    RCA combined with audio mini

    combined with audio mini

    foot print**

    85.6mm x 56.5mm 65mm x 56.5mm 85.6mm x 56.5mm 85.6mm x 56.5mm 85.6mm x 56.5mm

    delivary End of Life available on demand ? *** available availableprice $25 $20 $35 $35 → $25 $35

    Raspberry Pi Model differences as of May 2015

    * CPU clock is without over clock option enabled ** excluding SD/micro SD/connector *** may be on demand production

  • Several operating systems supports Raspberry Pi. Debian base Linux : Raspbian, Ubuntu mate, Snappy Ubuntu Core, OpenELEC, OSMC, Pidra (Fedora) , Arch Linux (in NOBOS only) RISC OS : Windows 10 : will be supported soon As of May, 2015 those operating systems are 32bit. Operating systems are supplied by SD card image. NOOBOS is multiple operating system install image, similar to Windows recovery area. The partition will be hidden after OS installation. User can reinstall OS from the partition when OS is corrupted. NOOBOS image includes Raspbian, Pidra, OpenELEC, OSMC, RISC OS, and Arch Linux NOOBOS LITE include same OS, but requires to internet connection during installation. Raspbian is Debian based Linux for Raspberry Pi, distributed by Raspberry Pi Foundation. Today’s topics are for Raspbian, may be applicable to other Debian based Linux.

    Operating Systems can be downloaded via ; https://www.raspberrypi.org/downloads/

    Raspberry Pi Operating System Basics

    https://www.raspberrypi.org/downloads/

  • Circuit Design and Power supply

    LED and Relay output

    Raspberry Pi power supply spec and issues

    Solutions for Raspberry Pi power supply issues

    Switch and signal input from GPIO

    Tips for DIY H/W development

  • Start study from to blink LEDs.

    Use Bread Board. You can repeat try and test, after test works well, soldering if required. Recommend to use hard jumper on board and soft jumper/wire between board and Raspberry Pi or between board and board/module.

    LCR circuit is difficult, not suitable for Bread Board. Consumer modules available for specific functions, i.e. servo control, censor control, RF, and etc. Consider to use those modules.

    If you want to sell your designed circuit, do not soldering by yourself. Better to have professional’s review. RoHS compliant is mandatory, consider to use circuit board production service. To prevent PL and RoHS issues, some supplier sells circuit as a parts kit. Don’t consider to integrate AC-DC converter in the BOX and plug to wall socket directly. PSE certification (in Japan) and Safety design is required. PL issues also exist. Use certified AC-DC converters. Most of consumer products uses AC-DC converter. Another alternative is to use DIY PC BOX and PC power supply, but may be over spec.

    Circuit Design and Power supply

  • GPIO output of Raspberry Pi is 3.3V

    Use 5V LED and appropriate resistor. LED is current drive, you need to follow specification in data sheet, Calculation is required to choose appropriate resistor. You have a great alternative, Resistor LED, for example OptoSupply products. Resistor LED makes circuit design easier and reduce foot print. LED use in circuit results around 0.2V voltage drop. It is difficult to find 5V water proof LED. 12V water proof tape LEDs are typical.

    There are a few 3.3V spec relay and expensive. 5.5V relay works well but you need to test it will work with your designed circuit. Also you need to consider GPIO state transition of Raspberry Pi’s power on boot. You can buy consumer negative logic relay module, for example, SainSmart. 3.3V operation is guaranteed. Fail-safe design, power on protection power on : high impedance = NC → HIGH : NC → LOW : NO note: you need to test your purchased relay module. Picture is “SainSmart 4 Channel Relay Module for Arduino DSP AVR PIC ARM”, TTL logic. You can also find newer Photo or Solid State Relay products.  

    LED and Relay output

    Photo Source: http://www.sainsmart.com

    http://www.sainsmart.com

  • Raspberry Pi power supply spec and issues

    Raspberry Pi Type B+ Photo Source: https://www.raspberrypi.org

    USB ports are directly connected to 5V5. Power supply is not disconnected. during shutdown and reboot. USB devices are continuing to get power supply and remaining data on intelligent chip sometime cause device driver initialize problem.

    Main power supply is micro USB. Raspberry Pi doesn’t have power switch.

    5V5 port can be used for an alternative power supply. However it bypasses current protection circuit.

    Note: Temporary supply shortage of power, short circuit by USB loose connecting operation will cause trap and reboot. Sometimes cause root file System corruption.

    Power supply of LAN port (100BT) is also not disconnected during shutdown and reboot. Similar issue exist.

    HDMI output requires it connected to display at boot time while video output is available anytime.

    https://www.raspberrypi.org

  • Minimum Requirement is to disconnect Power during a few seconds before reboot. Some power management board for Raspberry Pi may resolve the issue, but expensive. Some industrial system using Raspberry Pi resolves this problem by watchdog H/W.

    DIY solution with relay and timer IC could be a cheap solution. Here is my DIY example.

    Solutions for Raspberry Pi power supply issues

    Possible enhancement of DIY solution. external watchdog by Arduino or other micro controller to check heat beat via GPIO. 2 GPIO coupled Raspberry PI to check heat beat each other (2 Cluster HA).

    Timer IC x2

    Small Relay

    Connected to Raspberry Pi 5V5/GND

    Raspberry Pi Power is supplied by 5V5 pin. GPIO HIGH triggers timer IC /Relay (NC=on, NO=off). Timer IC1 (555) keeps a few second for shutdown and Timer IC2 keeps a few seconds for power off. Movie is a test of single timer prototype.

  • Raspberry Pi GPIO input is also 3.3V. High voltage input yo GPIO damages Raspberry Pi. In another side, better to handle 5V or higher for switch (especially for mechanical push switch) signal to prevent malfunction. (except switches on board)

    Raspberry Pi has pull down/pull up (resister) function, but for GPIO input, adding 1K-100K input resister is highly recommended to protect Raspberry Pi.

    Shifting 5V to 3.3V : Level shifter → module is consumer available, it requires 3.3V source. Passive → use resistors only. calculation is required to couple resisters.

    Switch and signal input from GPIO

    5V to 3.3V passive level sifter by author.

  • Installation and Backup

    Useful Raspbian option and Tools

    Understanding Raspbian GPIO handling - gpioreadreg - GPIO status set at boot/shutdown time

    RTC vs ntp

    Java and Web Server

    Traditional Unix/Linux approach is still effective

    Tips for DIY S/W (application) development

  • You need to write downloaded image to SD card or buy NOOBS SD for installation. for Windows, SD image write tool “win32 Disk Imager” is recommended. for project development (not hobby) install from single Raspbian image is recommended.

    Number of Sector of SD/micro SD varies even though among same supplier’s products. Writing SD images to SD which has smaller numbers of sector will fail. Numbers of sector of micro SD is smaller than SD typically. starting 8G micro SD for development is recommended. use 4GB if applicable. smaller image has more flexibility for installation and reducing disk space of backup images.

    Basic backup method is SD image backup. Raspbian with Raspbian allows cloning. Image can be used for another system. (take care about conflicts of hostname, etc.) In case for /root file system corruption, you had better to have 2nd Raspberry Pi, removing SD and connect it to 2nd system via USB SD adapter. Full fsck will fix with high possibility. (can be done by single system, but your USB slots may be full)

    Installation and Backup

  • For your DYI development, you may want to access your Raspberry Pi from non Linux OS in you local network. Your Raspberry Pi may be without display/keyboard/mouse. If you are using Windows for your DYI development ; telnet : Windows doesn’t have vendor supplied ssh client. telnet is a standard option of Windows. You may have security issue, but it is useful for development. Keyboard mapping is not required for several languages including Japanese. xrdp : Windows standard support for remote desktop is by rdp protocol. Note that it requires keyboard mapping.

    For numbers of sector problem of SD cards, file system copy, shrink, and move ; gparted : partition management tool like Windows partition management Note that it run graphic environment only, workable with xrdp connection

    There are several free and charged ssh client and remote desk clients for Windows, Mac OS, iOS, and Android as you may well know in your business.

    Useful Raspbian options and Tools

  • Raspbian support GPIO by low level device driver. Raspbian has two device driver I/F. command line (limited function only) echo out > /sys/class/gpio/gpioXX/direction /sys/class/gpio/gpioXX/value

  • gpioreadreg

    gpioreadreg output current GPIO pin status. -r option change output to MSB, default output is LSB. note: the program doesn’t have status change function Compile: copy and paste next 2 page program to make gpioreadreg.c file, then ; #make gpioreadreg #chmod 755 ./gpioreadreg Here is an example of Raspberry Pi 2 Model B #./gpioreadreg --------------------------------------------------------------- |3|3|3|3|3|3|3|3|4|4|4|4|4|4|4|4|4|4|5|5|5|5| | | | | | | | | | | |2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3| | | | | | | | | | | |/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/| | | | | | | | | | | |0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|2|2|2|2|2|2|2|2|2|2|3|3| |0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1| ---------------------------------------------------------------------------| | GPFSEL0 |1|1|0|0|1|0|0|0|1|0|1|0|1|0|1|0|0|0|1|0|0|0|1|0|1|0|1|0|1|0|1|0| | GPFSEL1 |1|0|1|0|1|0|1|0|1|0|1|0|1|0|0|1|1|0|1|0|0|0|1|0|0|0|1|0|1|0|1|0| | GPFSEL2 |1|0|1|0|1|0|1|0|1|0|1|0|1|1|1|1|0|0|1|0|1|0|1|0|1|0|0|0|1|0|1|0| | GPFSEL3 |1|0|1|0|0|0|1|0|1|1|1|0|1|0|1|0|1|0|1|0|1|0|0|0|1|0|1|0|1|0|1|1| | GPFSEL4 |1|0|1|0|1|1|1|0|1|0|1|0|1|1|1|0|1|0|0|0|1|1|1|0|1|0|1|0|1|0|1|0| | GPFSEL5 |1|1|1|0|1|1|1|1|1|0|1|0|1|0|1|0|1|0|0|0|1|0|1|0|1|0|1|0|1|0|1|0| | Reserved |1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|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|1|1|1|1|0|0|1|1|1|0|1|0|1|0|1|0|1|0|0|0|1|0|1|0|1|0| | GPPDUCLK1 |1|0|1|0|1|0|1|0|1|0|1|0|1|1|1|0|1|0|1|0|1|0|1|0|1|1|0|1|1|0|1|0| | Reserved |1|0|0|1|1|0|1|0|1|1|1|0|1|0|0|0|1|1|1|1|1|0|1|0|1|0|1|0|1|0|0|0| | Test |1|0|1|0|1|0|1|0|1|1|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0|1|0| ---------------------------------------------------------------------------| |0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|2|2|2|2|2|2|2|2|2|2|3|3| |0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1| |/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/| | | | | | | | | | | |3|3|3|3|3|3|3|3|4|4|4|4|4|4|4|4|4|4|5|5|5|5| | | | | | | | | | | |2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3| | | | | | | | | | | ---------------------------------------------------------------

  • gpioreadreg.c - 1/2/* */ #define GPIO_BASE (0x20200000) #define BLOCK_SIZE (4*1024) #include #include #include #include #include #include #include static char *reg_names [42] = { "GPFSEL0 ", "GPFSEL1 ", "GPFSEL2 ", "GPFSEL3 ", "GPFSEL4 ", /* 0 - 4 */ "GPFSEL5 ", "Reserved ", "GPSET0 ", "GPSET1 ", "Reserved ", /* 5 - 9 */ "GPCLR0 ", "GPCLR1 ", "reserved ", "GPLEV0 ", "GPLEV1 ", /* 10 - 14 */ "reserved ", "GPEDS0 ", "GPEDS1 ", "reserved ", "GPREN0 ", /* 15 - 19 */ "GPREN1 ", "Reserved ", "GPFEN0 ", "GPFEN1 ", "Reserved ", /* 20 - 24 */ "GPHEN0 ", "GPHEN1 ", "Reserved ", "GPLEN0 ", "GPLEN1 ", /* 25 - 29 */ "reserved ", "GPAREN0 ", "GPAREN1 ", "Reserved ", "GPAFEN0 ", /* 30 - 34 */ "GPAFEN1 ", "Reserved ", "GPPUD ", "GPPUDCLK0 ", "GPPDUCLK1 ", /* 35 - 39 */ "Reserved ", "Test " /* 40 - 41 */ } ; static char *gpio_numbers [12] = { " |3|3|3|3|3|3|3|3|4|4|4|4|4|4|4|4|4|4|5|5|5|5| | | | | | | | | | |\n", /* 0 */ " |2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3| | | | | | | | | | |\n", /* 1 */ " |/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/| | | | | | | | | | |\n", /* 2 */ " |0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|2|2|2|2|2|2|2|2|2|2|3|3|\n", /* 3 */ " |0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|2|3|4|5|6|7|8|9|0|1|\n", /* 4 */ " ---------------------------------------------------------------------------|\n", /* 5 */ " --------------------------------------------------------------- \n", /* 6 */ " | | | | | | | | | | |5|5|5|5|4|4|4|4|4|4|4|4|4|4|3|3|3|3|3|3|3|3|\n", /* 7 */ " | | | | | | | | | | |3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|\n", /* 8 */ " | | | | | | | | | | |/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|/|\n", /* 9 */ " |3|3|2|2|2|2|2|2|2|2|2|2|1|1|1|1|1|1|1|1|1|1|0|0|0|0|0|0|0|0|0|0|\n", /* 10 */ " |1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|\n", /* 11 */ } ; static unsigned int mask [32] = { 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000 } ;

  • gpioreadreg.c - 2/2void print_usage() { printf("Usage: gpioreadreg [-r] (-r for MSB ordering)\n" " assuming w/o argument and LSB ordering\n\n") ; }

    int main(int argc, char *argv[]) { int lsb = 1 ; if (argc > 2) { print_usage() ; } else if (arc == 2) { if (strcmp (argv[1], "-r") != 0) { print_usage() ; } else { lsb = 0 ; } } int fd = open("/dev/mem", O_RDWR|O_SYNC) ; if (fd < 0) { fprintf(stderr, "can't open /dev/mem (%s)\n", strerror(errno)) ; exit(EXIT_FAILURE) ; }

    void *gpio_map = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ; close(fd) ; if (gpio_map == MAP_FAILED) { fprintf(stderr, "mmap error (%s)\n", strerror(errno)) ; exit(EXIT_FAILURE) ; }

    volatile unsigned *gpio = (volatile unsigned *)gpio_map ; int value [42] ; int bit = 0 ; int count = 0 ;

    int offset = 0 ; while (offset < 42) { value [offset] = gpio[offset] ; offset = offset + 1 ; }

    if (lsb) { printf("%s%s%s%s%s%s%s", gpio_numbers[6], gpio_numbers[0], gpio_numbers[1], gpio_numbers[2], gpio_numbers[3], gpio_numbers[4], gpio_numbers[5]) ; } else { printf("%s%s%s%s%s%s%s", gpio_numbers[6], gpio_numbers[7], gpio_numbers[8], gpio_numbers[9], gpio_numbers[10], gpio_numbers[11], gpio_numbers[5]) ; } offset = 0 ; while (offset

  • You need to set GPIO to your desired state at boot time as soon as possible. You also need to reset GPIO to your desired initial state at before shutdown of OS. GPIO status keeps last change until power off or next boot time. Sample program gpioreadreg is useful to check GPIO status.

    There are two methods to set your desired GPIO state at boot time. script in rc.local, remaining some timing issues. script from cron, recommended in general. → add following entry to cron @reboot your-python-script-path to change GPIO status

    There is no built in mechanism of Raspbian to invoke your script at start of shutdown. modifying shutdown is possible, but not recommended, OS updates may effect adding your rc.shutdown to inittab may be better solution. → copy /etc/init.d/rc.local to /etc/init.d/rc.shutdown change template, #provides rc.shutdown, #Default-Start: to null, #Default-Stop: 0 1 6 and change /etc/rc.local to /etc/rc.shutdown the first entry of /etc/rc.shutdown should be your-python-script-path to reset GPIO status

    GPIO status set at boot/shutdown time

  • RTC (Real Time Clock) Raspberry Pi doesn’t have RTC, it looks like not only by parts cost, but also by shipping, stock, and maintenance cost considerations. Several 3rd Power Management (daughter) board for Raspberry Pi has RTC feature, but price is typically double or more to Raspberry Pi board. RTC requires 4 GPIO pins (SDA, SCL, Power, and GND) occupying first I2C interface. RTC timepiece error is bigger than you are expecting, It requires adjustment for every 3-6 months, otherwise difference will be minutes,

    ntp Requires internet connection. GPS based stand alone ntp server is considerable, but expensive.

    Solution: RTC with ntp will be the best, but expensive, If your project will have internet connection, consider ntp with UPS. Even though entry level UPS could keep power 30-60 minutes for your Raspberry Pi, Router, and Fiber Optical Modem. note: above assumes Japanese environment. Long time outage of Power is less than 1 / year.

    RTC vs ntp

  • Java : Java is headache, too many security updates. If you want to freeze code for long time, you need to consider use Java, or not to use Java. Traditional LAMP environment is still powerful for DIY projects.

    https : https requires Server Certificate, Issueing Self Certificate is possible, but not recommended in general. You need to consider trade off of using https or http.

    Web Server: Apache is commonly used, powerful, with rich functions. However security patch looks like increasing. nginx has similar situation. If you will use web server for administrative purpose only, you may use light httpd. - lighthttpd good but source code distribution - Mongoose may be most simple one, source code distribution, small updates You must familiar with several Apache alternatives. note : Some application includes own small web server, for example MJPG-Streamer. You can add your own scripts to do your own administrative purpose.

    Java and Web Server

  • DB vs File System : There are several easy to use RDB for Linux. However, you had better to raise self question “Do my project really need RDB ?”. If you use RDB, you need - to back up DB periodically and keep backup - to make DB recovery procedure CPU and File system are faster than UNIX servers of early 2000s with enhanced functions. File system is recommended for simple DYI project. You can use ; - simple file lock - memory map - advanced data recovery functions (for example, zfs) - CIFS client function to access Windows remote file systems , etc.

    daemon vs Back Ground Shell Script : Writing daemon program is tough work as you may know. Recommend to use traditional back ground shell program for your DYI project. However don’t forget to log by your scripts.

    Traditional Unix/Linux approach is still effective

  • Product Liability (PL)

    Power cable safety / Environmental requirements - Power Cable certification - Law and regulations for Electric work - RoHS and soldering - External usage and water proof

    RF certification

    Lows and Guide lines of local government - Building Standards Act - Fire Laws - Guide Lines of local government

    Considerations for Law and Regulations

  • Product Liability will apply ; if you will sell your DYI product if you will operate(service) something by your DIY product

    You need to consider ; physical damage by your equipment electric shock by your equipment and power wiring locked in(consigned)/out by your equipment etc. , etc. ..

    You need to consider insurance for your operation ; (in Japan, may be different in other countries) Personal liability insurance: if you operate in/out door of your house you are living Premises liability Insurance: if you operate your equipment in your facility beyond above : you need assistance from insurance professionals note: you need to verify T’s & C’s of insurance before signing

    Product Liability

  • Power Cable certification (in Japan) You need to use PSE labeled Power Code for AC connection. Most of AC/DC adaptor sold in Japan certifies PSE. but need to take care for imported ones.

    Law and regulations for Electric work Only certified engineer can work for direct power cable connection. You are not allowed to staple power cables directly to the wall, sealing, or floor. use cable clip.

    RoHS and soldering Sn - Pb soldering is not RoHS compliant. RoHS compliant soldering requires temperature controlled tool with skill. Alternatives, for example conductive adhesive (Ag paste) is expensive solution. For DYI, still Sn - Pb soldering is suitable. If you want to sell your circuit, consult with professional and consider circuit production service.

    External usage and water proof A few water proof switch/connector are consumer available, need to buy from supplier for professionals.

    Power cable safety / Environmental requirements

  • You can only use Japan certified RF equipments, which have certification mark.

    RF band varies among countries, most of RF sensor soled in U.S. are not allowed to use in Japan.

    There are a lot of RF equipment soled in Japan which doesn’t have foreign countries’ RF certification. Restriction is stated or labeled with equipment or BOX.

    5GB WiFi is not allowed for outdoor usage in Japan (as of May 2015).

    Most of 3G/LTE modem sold in US, EU or China are not RF certified in Japan. RF certified 3G connection board or USB dongle are expensive in Japan. 3G connection board : $200-300 3G USB dongle: $100+

    RF certification

  • Lows and Guide lines of local government

    Building Standards Act and Electric Power related Law regulations If you considers some construction for your project, you need to follow Law and local government guideline. For example, there are n x KW solar power DYI kit, but construction, set up, and operation requires to meet regulations with paper work. Several engineering work requires skill certifications.

    Fire Laws You need to follow Fire Laws. For example, your equipment in your MDF requires fire preventing design and MDF itself must be fire preventing. Short circuit protection is mandatory. Even though AC/DC converter has over current protective function, fuse or other over current prevention is required for your project. Guide Lines of local government Your project need to follow guide lines of local government. For example, you can only drive your DIY vehicle within your property. Using public spaces have several restrictions, you can not test your DYI pitching machine or Toy rocket in a park without permission.

  • Broad band and 3G/LTE connection issues - Connection is disconnected from Server - You may want to hide your global dress - 3G/LTE private address

    H/W maintenance considerations

    S/W maintenance considerations

    Tips for Operation and Maintenance

  • PPPoE connection disconnected by several reasons and consumer router sometimes fails to recover - Provider problem → 1- 2 times / year - Carrier problem → for optical connection, very few - Provider/Carrier construction → for CATV construction, several times / year - PPPoE server time out (router hart beat)→ recovery may fail for consumer router - Intensive server side disconnect (no packet for time out duration) → see solution

    3G/LTE connection will be disconnected (following example is for some specific MVNO carrier in Japan) - no packet during one hour → wvdial/pppd recovery doesn’t work - every continuous connection for (about) 23 hours → wvdial/pppd recovery does work Solution: To keep continuous connection, recommend to send/receive every 1 minutes. for example, run following command from cron (getting time form NICT server in Japan) wget -q -O - https://not-a1.nict.go.jp/cgi-bin/time

    Connection is disconnected from server

    https://not-a1.nict.go.jp/cgi-bin/time

  • Do you like to open your global address of security camera or gate controller ? Port mapping to specific http port via router will have security risk. Domain registration will result to open location (not specific, but could be inferred). VPN connection to global fixed address without domain may be the best but expensive.

    However you want to access your server from remote location by several reasons.

    Solution: VPN access may be better ;

    1. DDNS alternatives to know global address of your router/server you can notify your (DHCP assigned) global address by several way with cron mail to specific user and analyze header → you need to specify user/password getting global address and write to cloud → you need to specify user/password getting global address to BuffaloNAS → may be good, but port mapping consideration remains http access and client address analysis → may be best if you have your own public web server

    2. VPN and local accessing even though consumer router, it supports pptp, working well. pptp connection will require additional user authentication.

    You may want to hide your global address

  • Low cost plan for MVNO 3G/LTE SIM is moving(moved) to private address, global address will no longer provided.

    You can not login to your server/router from internet. If your administration requires login or http server access, etc, it is a big problem. Free running with client access only operation is hard to implement.

    Solution: (information as of May 2015)

    VPN alternative : P2PTunnel for consumer, CloudPi by PLANEX is available in Japan, very good product, but some limitation exist - not so expensive, but requires additional cost (35-40$/year) - SLA is not meet to enterprise use - Server code is Linux only, no Linux client - need consideration for client security, intruder can access any port → server may need to close ports except P2PTunnel ports for security

    3G/LTE private address

  • Once starting operation, you have narrow maintenance time slot. You need to take effort for H/W - S/W integration test before in production. You need to replacement units for hardware trouble. You had better to keep integration test equipment, it can be replacement units.

    Connection line change or wire replacement are tough task. Even though your project will be placed in a Master Distribution Facility (MDF) BOX, narrow space and requires careful wire replacement operation. Use connecter as much as possible. Use independent power supply or tap for your project. Power tap with switch is useful.

    Temporary power outage will damage your equipment. Even though Japanese power quality is the best, but you need consider other situation, i.e. Lightning and shut off by circuit breaker. Use Surge current protected (by Lightning) power tap. Using UPS is highly recommended.

    H/W maintenance considerations

  • Once starting operation, updating S/W is difficult.

    S/W freeze issues are almost same for commercial systems you are familiar with. Don’t update system S/W after integration test as much as possible. Leave a way to update your application remotely, establish VPN connection is highly recommended ← assuming remote location, if you will install your system to your house, don’t care, but you may want to control something while you are traveling.

    There are several way for Source code maintenance. If your project is your only, or replicate small numbers (less than 10), everything on it and save SD image will be the best. If you want to install (may sell) 10s of your project, you had better to consider GitHub, source code maintenance and making installation procedure are mandatory. Making, NOBOS image is good idea, but you need to consider numbers of installation for each NOBOS image, making NOBOS image for a few installation is not a good idea. note: NOBOS image has Windows like recovery area. Target OS(system image) is installed from this area and recoverable.

    S/W maintenance considerations

  • Example: My Project - Gate Door timer controller

    Controller is building inside. All power is inside,

    Under ground line connection.

    Water proof Magnet Lock door / Operator panel.

    Internet connection sharing with security camera, etc.

    Raspberry Pi based DYI controller

    Timer controlling open/lock of gate door of garbage area.

  • Garbage collectors belong to local government. (hired and contracted) Local government requests garbages should only be put on garbage area on early morning of operation day. Garbage collectors may open door of garbage area and open garbage box, however door and box should not be locked. Local government doesn’t keeps key of building owner supplied and not allowed collectors to use key. → policy of local government, policy varies local governments in Japan.

    Idea is following operation senario may be practical and operation cost effective. - Timer controller unlock the door at very early morning of garbage collection day. - Residents bring garbage before garbage collection time. - Schedule cleaning staff (contract with owner) after the garbage collection. - After cleaning, cleaning staff locks door manually. - Door is locked until next timer schedule.

    Looking for outdoor control functionality with building main door facilities, but, Existing controller is over spec, too much expensive, and requires system development. Rough cost estimation exceeds $30,000. except garbage area construction with door.

    Back ground and Why DIY ?

    OK DIY !

  • Reliable operations. → ntp, simple control program design, UPS backup.

    Safety, especially for electric shock. → concealed wiring, no outdoor AC power supply, waterproof panel and switches.

    Prevent someone is confined. → manual lock operation checked by cleaner. emergency open / lock functions.

    10 - 15 years system life with minimum maintenance → Linux, without Java and DB, order made panel/switch box, order made MDF remote connection via VPN. test and demo system can be replacement parts

    PL and protection for accident → Premises liability insurance.

    What is going on ? →

    Basic Requirements and solutions

    DIY controller is running over 11 months w/o problem !

  • Photo Source: http://www.sainsmart.com

    Switched circuit only with weekly timer → Could not control state for manual operation. Switched circuit with TTL logic → Arduino like micro controller based programming is easier. Arduino with weekly timer → Possible, but following issues ; Any S/W update requires USB connection to PC (typically Windows) BOX design is limited by weekly timer, display of weekly timer is too small need to adjust time of weely timer every 3-6 months

    Studied alternatives

    Not Cool !

    100V →5V

    100V →12V

    100V →5V

    AC 100V

    Photo Source: http://www.arduino.cc

    Weekly Timer (100V)

    http://www.sainsmart.comhttp://www.arduino.cc

  • H/W configuration: - Raspberry Pi Model B - SainSmart 4 Channel Relay Module for Arduino DSP AVR PIC ARM - 12V and 5V AC/DC convertor - Magnalock(Magnet lokking system, Walter Proof, 12V DC) - AC Tap with separate switch, surge current protection) - UPS (OMRON) - DYI DC Power Circuit with fuze protection and passive 5V-3.3V level shifter - DYI controller BOX - Order made Switch BOX with DYI switch circuit and LED indicator

    S/W configuration: - Raspbian, cron, ntp, rc.shutdown (configured to inittab by author) - telnet, xrdp, CIFS client (for remote login and BuffaloNAS access) - Apache2 - pptp (on router for VPN access) - Application by author (language; bash, perl, python, html)

    H/W and S/W configuration

  • My work, Professional work, and Water ProofOrder made panel BOX, Water Proof. Water Proof Switches. Water Proof LEDs. DIY circuit and lines are connected by professional.

    Jumper Cables To Raspberry Pi

    DYI Controller is in a order made MDF which is fire preventive.

    Magnet Lock “Magnalock” is water proof and is set by professional.

    Lines are connected by plugs and wired under ground in a water proof pipe. by professional work.

    Concealed wiring and no outdoor AC 100V connection prevents risk of electric shock for operator (cleaner) and residents.

  • DYI Controller H/W building block

    Fuses for Safety

    Exhaust duct with bug intrusion protector.

    (bottom view)

    Raspberry Pi is on top and connected with jumpers

    DYI Controller is in a BOX and AC power to AC/DC converter is supplied via surge current protected

    tap and connected to UPS.

    Cable connections are according to thin to fat rule. Connectors are used for

    connection with long cable.note: cable are heavier than your expectation.

  • Administrative interfaces of DIY controller

    Administrative interface of timer is Web based, Language is English because no user except author. Neither D/B nor Java. Simple one line control files are used for recent status and daily schedule.

    Minimum security protection by passcode in case residents access http port and scripts occasionally from local network.

    Emergency unlock/lock functions are also Web based.

    Note: Those administrative functions can be operated remotely via VPN.

  • Demo: Live system vs Test / Demo system

    Controller

    Operator Pannel

    Door and Lock

    Magnalock emulator is here (just emulate NO/NC by relay)

    Switch is not water proof LED 12V → 5V

    Simulate Door open/close by magnetic sensor

    5V/12V to 5V single no fuse protection additional drug LEDs flat arrangement

    in side of MDF

    debug LEDs

    Relay circuit

    Magnalock is top behind

    Raspberry Pi Type B

  • Demo: Operation

    Door Operation: see debug LED also 1. Door is locked by timer → LED LOCK 2. Turn off key switch → all LED off 3. Turn on key switch → LED OPEN 4. Push push switch (1-2 second) → LED LOCK 5. Push push switch again → no change 6. open/close door, if open → LED DOOR CHECK

    Remote Door Open/Close : passcode 9999 is required. 1. Door is locked 2. Touch OPEN 


    → You will have message and LED OPEN 3. return and touch CLOSE


    → You will have message and LED CLOSE note: this function is prepared for emergency.
 real system requires 3G/LTE with VPN access. demo environment uses local Wifi.

    You can see timer schedule change web I/F and operation log via Windows telnet console if time allows after the presentation.

  • You will have hurdles to make your project into “Real World ”Operation.

    You need to consider Law and Regulations for your project. Sometimes they hit to H/W and S/W design. You need to consider characteristics of Raspberry Pi H/W & S/W, operating environment, and maintenance work. If your project will be beyond DIY, you need professional assistance.

    Anytime, simple design is the best solution.

    Anyway you can find solutions for your project.

    Trade off considerations are required, but don’t hesitate to go ahead. Good Luck !

    Conclusion

  • For further questions,

    mailto: [email protected]

    Any Question ?

    mailto:[email protected]