Golang+Raspiで趣味的IoT入門的な話

 Golang+Raspiで趣味的IoT入門的な話

Gunosy.go#12にて

05e8adce66be4e80390a29ace0075161?s=128

y_matsuwitter

April 13, 2015
Tweet

Transcript

  1. Golang+RaspiͰझຯత IoTೖ໳తͳ࿩ @y_matsuwitter

  2. ࣗݾ঺հ • ։ൃຊ෦ࣥߦ໾һࡶ༻໾ • iOS, Android, Infrastructure, Web • Golang෍ڭ໾

    • ਖ਼࠲ • ࠷ۙؾʹͳΔ΋ͷ: MQTT
  3. ࠓ೔ͷ࿩ • GolangͰݸਓझຯతʹIoTʹ৮Εͨ࿩ w/RaspberryPi • Gobotͱembdͷ঺հ

  4. ஫: ిࢠ޻࡞ૉਓͷ࿩Ͱ͢

  5. IoT(Internet of Things) • ༷ʑͳϞϊ͕ΠϯλʔωοτΛհͯ͠ܨ͕Δٕज़ͷ૯শ • ࠷᷿ۙͰେਓؾ

  6. RaspberryPiͱ͸

  7. Spec • খܕɾ҆Ձͳڭҭ޲͚LinuxϚγϯ • RaspberryPi Foundation͕։ൃ • CPU: ARM •

    Memory: 256 ~ 1GB • USB microBͰڅి • GPIOϐϯ͕༻ҙ͞Ε͍ͯΔ
  8. ARMͳͷͰ Golang͕ಈ͘ʂʂ GOARCH=arm go build

  9. GPIO(General Purpose Input/ Output)ͱ͸ʁ • ଞσόΠεɺճ࿏౳ͱೖग़ྗΛߦ͏ͨΊͷΠϯλʔϑΣʔε • ιϑτ΢ΣΞଆͷࢦఆͰಈ࡞Λ੾Γସ͑Δࣄ͕Ͱ͖Δ

  10. ࠓճ࢖ͬͨ΋ͷ • SHT21 ... Թ౓ɾ࣪౓ηϯα • I2Cͱ͍͏ํࣜͰԹ౓ɺ࣪౓Λ΍Γͱ Γ͢Δ

  11. I2Cͱ͸ • ૊ΈࠐΈػث౳Ͱར༻͞ΕΔγϦΞϧ௨৴ن֨ͷҰͭ • ௿଎ͳ௨৴͕ڐ͞ΕΔ༻్޲͚ • Ұͭͷܦ࿏্Ͱෳ਺ͷػثͱ΍ΓͱΓͰ͖Δ

  12. GolangͰI2C • Gobot΍embdͷར༻ਪ঑

  13. Gobot

  14. About Gobot • GolangͰϋʔυ΢ΣΞϓϩάϥϛϯάશൠΛߦ͏ͨΊͷϑϨ ʔϜϫʔΫ • RaspberryPi, Arduino, AR.DroneͳͲͷαϙʔτ •

    MQTT·ΘΓͷΞμϓλΛॳظ͔Βαϙʔτ • ͍͔ͭ͘ͷηϯαྨ͸͙͢ʹ࢖͑ΔΑ͏υϥΠό༻ҙ͞Εͯ ͍Δ
  15. Sample: ArduinoͰLEDͷ఺໓ package main import ( "time" "github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot/platforms/firmata" "github.com/hybridgroup/gobot/platforms/gpio"

    ) func main() { gbot := gobot.NewGobot() firmataAdaptor := firmata.NewFirmataAdaptor("arduino", "/dev/ttyACM0") led := gpio.NewLedDriver(firmataAdaptor, "led", "13") work := func() { gobot.Every(1*time.Second, func() { led.Toggle() }) } gbot.AddRobot(gobot.NewRobot("bot", []gobot.Connection{firmataAdaptor}, []gobot.Device{led}, work, )) gbot.Start() }
  16. kidoman/embd

  17. About embd • ૊ΈࠐΈϓϩάϥϛϯά޲͚ϑϨʔϜϫʔΫ • Gobotͱൺ΂Δͱ΋͏গ͠௿ϨΠϠ? • ରԠ୺຤͸গͳ͍ • ֤छBusΛ௚઀৮Γ΍͍͢

    • Ұ෦ηϯα౳ͷυϥΠό΋༻ҙ͞Ε͍ͯΔ
  18. Sample: RasPiͰLED఺໓ package main import ( "time" "github.com/kidoman/embd" _ "github.com/kidoman/embd/host/rpi"

    // This loads the RPi driver ) func main() { for { embd.LEDToggle("LED0") time.Sleep(250 * time.Millisecond) } }
  19. embdͰSHT21͔ΒԹ౓Λऔಘ • ࠓճ͸EmbdͷI2CBusΛ௚઀ར༻ // Busʢܦ࿏ʣͷ࡞੒ bus := embd.NewI2CBus(1) // ॳظԽ

    bus.WriteByte(addr, byte(softreset)) // ॳظԽ׬ྃ଴ͪ time.Sleep(500 * time.Millisecond)
  20. ଓ͖ // Թ౓Λૹͬͯ΋Β͏Α͏৴߸ૹ৴ bus.WriteByte(addr, triggerTemperatureNoHold) // ϋʔυ΢ΣΞଆͷ৴߸଴ͪ time.Sleep(temperatureWait * time.Millisecond)

    // 1όΠτ໨ಡΈग़͠ first, _ := bus.ReadByte(addr) // 2όΠτ໨ಡΈग़͠ second, _ := bus.ReadByte(addr) // ̎ͭͷ஋Λݩʹηϧγ΢ε౓ʹม׵ temp := (175.72*float64((int64(first)<<8+int64(second))&statusBitMusk))/float64(1<<16) - 46.85
  21. ੒Ռ෺ ఆظతʹࣨԹΛChatʹ౤͛ͯ͘ΕΔBot

  22. ײ૝ • Golangࣗମ͕ܰྔͳͷͰݸਓ޻࡞Ͱͷ૊ΈࠐΈʹ͸࢖͍΍͢ ͦ͏ • ίʔσΟϯάΑΓ΋֤छηϯαͷ࢓༷ॻͱͷ֨ಆ • ֤छϑϨʔϜϫʔΫ͕༻ҙ͍ͯ͠ΔυϥΠό࢖͑ΔηϯαΛ બͿํָ͕

  23. ࢀߟ • RaspberryPi https://www.raspberrypi.org/ • Gobot http://gobot.io/ • embd http://embd.kidoman.io/