Giter Site home page Giter Site logo

iothub's Introduction

iothub

Azure IoT Hub SDK for Golang, provides both device-to-cloud (iotdevice) and cloud-to-device (iotservice) packages for end-to-end communication.

API is subject to change until v1.0.0. Bumping minor version indicates breaking changes.

See TODO section to see what's missing in the library.

Installation

To install the library as a dependency:

go get -u github.com/midnightrun/iothub

To install CLI applications:

GO111MODULE=on go get -u github.com/midnightrun/iothub/cmd/{iothub-service,iothub-device}

Usage Example

Receive and print messages from IoT devices in a backend application:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/midnightrun/iothub/iotservice"
)

func main() {
	c, err := iotservice.NewFromConnectionString(
		os.Getenv("IOTHUB_SERVICE_CONNECTION_STRING"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// subscribe to device-to-cloud events
	log.Fatal(c.SubscribeEvents(context.Background(), func(msg *iotservice.Event) error {
		fmt.Printf("%q sends %q", msg.ConnectionDeviceID, msg.Payload)
		return nil
	}))
}

Send a message from an IoT device:

package main

import (
	"context"
	"log"
	"os"

	"github.com/midnightrun/iothub/iotdevice"
	iotmqtt "github.com/midnightrun/iothub/iotdevice/transport/mqtt"
)

func main() {
	c, err := iotdevice.NewFromConnectionString(
		iotmqtt.New(), os.Getenv("IOTHUB_DEVICE_CONNECTION_STRING"),
	)
	if err != nil {
		log.Fatal(err)
	}

	// connect to the iothub
	if err = c.Connect(context.Background()); err != nil {
		log.Fatal(err)
	}

	// send a device-to-cloud message
	if err = c.SendEvent(context.Background(), []byte(`hello`)); err != nil {
		log.Fatal(err)
	}
}

cmd/iothub-service and cmd/iothub-device are reference implementations of almost all available features.

CLI

The project provides two command line utilities: iothub-device and iothub-sevice. First is for using it on IoT devices and the second manages and interacts with them.

You can perform operations like publishing, subscribing to events and feedback, registering and invoking direct methods and so on straight from the command line.

iothub-service is a iothub-explorer replacement that can be distributed as a single binary opposed to a typical nodejs app.

See -help for more details.

Testing

TEST_IOTHUB_SERVICE_CONNECTION_STRING is required for end-to-end testing, which is a shared access policy connection string with all permissions.

TEST_EVENTHUB_CONNECTION_STRING is required for eventhub package testing.

TODO

iotservice

  1. Stabilize API
  2. Fix TODOs

iotdevice

  1. Device modules support.
  2. HTTP transport (files uploading).
  3. AMQP transport (batch sending, WS).

Contributing

All contributions are welcome.

iothub's People

Contributors

amenzhinsky avatar whywaita avatar sleeping-barber avatar camal-cakar-gcx avatar brocaar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.