Exemple #1
0
func mqttWorker(c *client.Client) {

	// Subscribe to topics.
	err := c.Subscribe(&client.SubscribeOptions{
		SubReqs: []*client.SubReq{
			&client.SubReq{
				TopicFilter: []byte("fgdata"),
				QoS:         mqtt.QoS0,
				// Define the processing of the message handler.
				Handler: func(topicName, message []byte) {
					wmux.Lock()
					fgdata = string(message)
					lastseen = time.Now().String()
					wmux.Unlock()
				},
			},

			&client.SubReq{
				TopicFilter: []byte("light1"),
				QoS:         mqtt.QoS0,
				// Define the processing of the message handler.
				Handler: func(topicName, message []byte) {
					wmux.Lock()
					light1 = string(message)
					light1ls = time.Now().String()
					wmux.Unlock()
				},
			},
		},
	})
	if err != nil {
		fmt.Println("Error", err)
	}
}