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

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

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

Gunosy.go#12にて

y_matsuwitter

April 13, 2015
Tweet

More Decks by y_matsuwitter

Other Decks in Programming

Transcript

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

    Memory: 256 ~ 1GB • USB microBͰڅి • GPIOϐϯ͕༻ҙ͞Ε͍ͯΔ
  2. About Gobot • GolangͰϋʔυ΢ΣΞϓϩάϥϛϯάશൠΛߦ͏ͨΊͷϑϨ ʔϜϫʔΫ • RaspberryPi, Arduino, AR.DroneͳͲͷαϙʔτ •

    MQTT·ΘΓͷΞμϓλΛॳظ͔Βαϙʔτ • ͍͔ͭ͘ͷηϯαྨ͸͙͢ʹ࢖͑ΔΑ͏υϥΠό༻ҙ͞Εͯ ͍Δ
  3. 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() }
  4. 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) } }
  5. embdͰSHT21͔ΒԹ౓Λऔಘ • ࠓճ͸EmbdͷI2CBusΛ௚઀ར༻ // Busʢܦ࿏ʣͷ࡞੒ bus := embd.NewI2CBus(1) // ॳظԽ

    bus.WriteByte(addr, byte(softreset)) // ॳظԽ׬ྃ଴ͪ time.Sleep(500 * time.Millisecond)
  6. ଓ͖ // Թ౓Λૹͬͯ΋Β͏Α͏৴߸ૹ৴ 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