Example #1
0
func main() {
	gbot := gobot.NewGobot()

	a := api.NewAPI(gbot)
	a.Start()

	// digital
	board := edison.NewEdisonAdaptor("edison")
	button = gpio.NewGroveButtonDriver(board, "button", "2")
	blue = gpio.NewGroveLedDriver(board, "blue", "3")
	green = gpio.NewGroveLedDriver(board, "green", "4")
	red = gpio.NewGroveLedDriver(board, "red", "5")
	buzzer = gpio.NewGroveBuzzerDriver(board, "buzzer", "7")
	touch = gpio.NewGroveTouchDriver(board, "touch", "8")

	// analog
	rotary = gpio.NewGroveRotaryDriver(board, "rotary", "0")
	sensor = gpio.NewGroveTemperatureSensorDriver(board, "sensor", "1")
	sound = gpio.NewGroveSoundSensorDriver(board, "sound", "2")

	work := func() {
		Reset()

		gobot.On(button.Event(gpio.Push), func(data interface{}) {
			TurnOff()
			fmt.Println("On!")
			blue.On()
		})

		gobot.On(button.Event(gpio.Release), func(data interface{}) {
			Reset()
		})

		gobot.On(touch.Event(gpio.Push), func(data interface{}) {
			Doorbell()
		})

		gobot.On(rotary.Event("data"), func(data interface{}) {
			fmt.Println("rotary", data)
		})

		gobot.On(sound.Event("data"), func(data interface{}) {
			DetectSound(data.(int))
		})

		gobot.Every(1*time.Second, func() {
			CheckFireAlarm()
		})
	}

	robot := gobot.NewRobot("airlock",
		[]gobot.Connection{board},
		[]gobot.Device{button, blue, green, red, buzzer, touch, rotary, sensor, sound},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
func main() {
	flag.Parse()

	// Initialize Intel Edison
	edisonAdaptor := edison.NewEdisonAdaptor("edison")
	edisonAdaptor.Connect()

	lightSensor := gpio.NewGroveLightSensorDriver(edisonAdaptor, "light", *sensorLightPin, *sensorPollingInterval)
	lightSensor.Start()
	gobot.On(lightSensor.Event("data"), func(data interface{}) {
		raw := float64(data.(int))
		// convert to lux
		resistance := (1023.0 - raw) * 10.0 / raw * 15.0
		light = 10000.0 / math.Pow(resistance, 4.0/3.0)
		lightUpdated = time.Now()
		log.Debugln("illuminance: ", light)
	})

	soundSensor := gpio.NewGroveSoundSensorDriver(edisonAdaptor, "sound", *sensorSoundPin, *sensorPollingInterval)
	soundSensor.Start()
	gobot.On(soundSensor.Event("data"), func(data interface{}) {
		sound = float64(data.(int))
		soundUpdated = time.Now()
		log.Debugln("sound level: ", sound)
	})

	tempSensor := gpio.NewGroveTemperatureSensorDriver(edisonAdaptor, "temp", *sensorTempPin, *sensorPollingInterval)
	tempSensor.Start()
	gobot.On(tempSensor.Event("data"), func(data interface{}) {
		celsius = data.(float64)
		fahrenheit = celsius*1.8 + 32
		tempUpdated = time.Now()
		log.Debugln("temperature: ", celsius)
	})

	// Initialize prometheus exporter
	exporter := NewExporter()
	prometheus.MustRegister(exporter)

	log.Infof("Listening on: %s", *listenAddress)
	http.Handle(*metricPath, prometheus.Handler())
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`
			<html>
			<head>
				<title>IoT Edison exporter</title>
			</head>
			<body>
				<h1>Prometheus exporter for sensor metrics from Intel Edison</h1>
				<p><a href='` + *metricPath + `'>Metrics</a></p>
			</body>
			</html>
		`))
	})
	log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
Example #3
0
/*
This function is used if only the rotary port is in play.
func mapValueToColor(i int) (int, int, int) {
	// Masking to account for very sensitive rotary
	red := ((i & (1 << 1)) << 2) | ((i & (1 << 4)) << 0) | ((i & (1 << 7)) >> 2)
	green := ((i & (1 << 2)) << 1) | ((i & (1 << 5)) >> 1) | ((i & (1 << 8)) >> 3)
	blue := ((i & (1 << 3)) << 0) | ((i & (1 << 6)) >> 2) | ((i & (1 << 9)) >> 4)

	return red, green, blue
}
*/
func main() {
	gbot := gobot.NewGobot()

	board := edison.NewEdisonAdaptor("edison")
	screen := i2c.NewGroveLcdDriver(board, "screen")
	sensorRotary := gpio.NewGroveRotaryDriver(board, "sensor", "0")
	sensorLight := gpio.NewGroveLightSensorDriver(board, "light", "1")
	sensorTemp := gpio.NewGroveTemperatureSensorDriver(board, "temp", "2")

	work := func() {
		var r, g, b int

		gobot.On(sensorRotary.Event("data"), func(data interface{}) {
			r = data.(int) >> 2
		})

		gobot.On(sensorLight.Event("data"), func(data interface{}) {
			fmt.Printf("%d\n", data)
			g = data.(int) * 255 / 790
		})

		gobot.Every(time.Millisecond*500, func() {
			b = int(gobot.ToScale(gobot.FromScale(sensorTemp.Temperature(), 25, 35), 0, 255))
			screen.Clear()
			screen.Home()
			screen.SetRGB(r, g, b)
			screen.Write(fmt.Sprintf("#%02X%02X%02X", r, g, b))
		})

	}

	robot := gobot.NewRobot("screenBot",
		[]gobot.Connection{board},
		[]gobot.Device{screen, sensorRotary, sensorLight, sensorTemp},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
func main() {
	gbot := gobot.NewGobot()

	board := edison.NewEdisonAdaptor("board")
	sensor := gpio.NewGroveTemperatureSensorDriver(board, "sensor", "0")

	work := func() {
		gobot.Every(500*time.Millisecond, func() {
			fmt.Println("current temp (c): ", sensor.Temperature())
		})
	}

	robot := gobot.NewRobot("sensorBot",
		[]gobot.Connection{board},
		[]gobot.Device{sensor},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
Example #5
0
func main() {
	gbot := gobot.NewGobot()

	board := edison.NewEdisonAdaptor("board")
	screen := i2c.NewGroveLcdDriver(board, "screen")
	buzzer := gpio.NewBuzzerDriver(board, "buzzer", "3")
	sensor := gpio.NewGroveTemperatureSensorDriver(board, "sensor", "1")

	//the hot song is
	work := func() {
		//initial calibration in fahrenheit
		min = 60.0
		max = 80.0
		diff = max - min
		type note struct {
			tone     float64
			duration float64
		}
		//the cold song is "do you want to build a snowman"
		coldSong := []note{
			{gpio.C3, gpio.Quarter},
			{gpio.C3, gpio.Quarter},
			{gpio.C3, gpio.Quarter},
			{gpio.G3, gpio.Quarter},
			{gpio.C3, gpio.Quarter},
			{gpio.E4, gpio.Quarter},
			{gpio.D4, gpio.Half},
			{gpio.E4, gpio.Half},
		}
		//the hot song is "let's get it started in here"
		hotSong := []note{
			{gpio.E4, gpio.Quarter},
			{gpio.E4, gpio.Quarter},
			{gpio.E4, gpio.Quarter},
			{gpio.E4, gpio.Quarter},
			{gpio.C3, gpio.Quarter},
			{gpio.A3, gpio.Quarter},
			{gpio.C3, gpio.Quarter},
		}
		gobot.Every(time.Second, func() {
			screen.Clear()
			time.Sleep(5 * time.Millisecond)
			screen.Home()
			time.Sleep(5 * time.Millisecond)

			//get temps
			celsius := sensor.Temperature()
			fahrenheit := (celsius * 1.8) + 32
			//recalibrate
			min = math.Min(fahrenheit, min)
			max = math.Max(fahrenheit, max)
			diff = max - min

			//check
			if fahrenheit > 80 {
				for _, val := range hotSong {
					buzzer.Tone(val.tone, val.duration)
					<-time.After(10 * time.Millisecond)
				}
				screen.Write(fmt.Sprintf("TOO HOT (%.2gF)", fahrenheit))
			} else if fahrenheit < 60 {
				for _, val := range coldSong {
					buzzer.Tone(val.tone, val.duration)
					<-time.After(10 * time.Millisecond)
				}

				screen.Write(fmt.Sprintf("TOO COLD (%.2gF)", fahrenheit))
			} else {
				screen.Write(fmt.Sprintf("%.2gF, %.2gC", fahrenheit, celsius))
			}
			time.Sleep(5 * time.Millisecond)
			fmt.Printf("%.2gF, %.2gC\n", fahrenheit, celsius)

			//set to params
			screen.SetRGB(getRed(fahrenheit), 0, getBlue(fahrenheit))
		})
	}

	robot := gobot.NewRobot("sensorBot",
		[]gobot.Connection{board},
		[]gobot.Device{screen, buzzer, sensor},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()
}
func main() {
	//AWS MQTT Broker SSL Configuration
	tlsconfig := NewTLSConfig()
	fmt.Println("TLSConfig initiation Completed")
	opts := MQTT.NewClientOptions()
	opts.AddBroker("ssl://AEV5KR4BW3J9L.iot.us-east-1.amazonaws.com:8883")
	opts.SetClientID("iot-sample").SetTLSConfig(tlsconfig)
	fmt.Println("Invoking Publish Handler method ")
	opts.SetDefaultPublishHandler(f)

	// Start the connection
	c := MQTT.NewClient(opts)
	if token := c.Connect(); token.Wait() && token.Error() != nil {
		panic(token.Error())
	}
	// _ = "breakpoint"
	fmt.Println("MQTT Connection established")

	c.Subscribe("/go-mqtt/sample", 0, nil)

	// Gobot initiation
	gbot := gobot.NewGobot()
	board := edison.NewEdisonAdaptor("board")
	sensort := gpio.NewGroveTemperatureSensorDriver(board, "tempsensor", "0")
	screen := i2c.NewGroveLcdDriver(board, "screen")

	// Struct to hold sensor data
	type Sensord struct {
		Temp float64 `json:"temp"`
	}

	work := func() {
		screen.Write("Thermostat is On !!")
		screen.SetRGB(255, 0, 0)

		gobot.Every(5*time.Second, func() {
			fmt.Println("current temp (c): ", math.Floor(sensort.Temperature()))

			// LCD showing the temperature
			screen.Clear()
			screen.Home()
			screen.SetRGB(0, 255, 0)
			//screen.SetCustomChar(0, i2c.CustomLCDChars["smiley"])

			//Update the struct with sensor data, Jsonify & Convert to string
			res1Z := Sensord{
				Temp: math.Floor(sensort.Temperature()),
			}

			jData, err := json.Marshal(res1Z)
			if err != nil {
				fmt.Println(err)
				return
			}

			s := string(jData)

			// Writes to the LCD Screen & publish to AWS MQTT Broker.

			screen.Write(s)
			gobot.Every(2*time.Second, func() {
				screen.Scroll(false)
			})

			fmt.Println("The json data to be published in IOT topic is", s)
			c.Publish("/go-mqtt/sample", 0, false, s)
			//c.Disconnect(250)

			//screen.Home()
			//<-time.After(2 * time.Second)
			//screen.SetRGB(0, 0, 255)
		})
	}

	robot := gobot.NewRobot("sensorBot",
		[]gobot.Connection{board},
		[]gobot.Device{sensort, screen},
		work,
	)

	gbot.AddRobot(robot)

	gbot.Start()

}