Skip to content

Instantly share code, notes, and snippets.

@hussanhijazi
Created November 29, 2018 13:06
Show Gist options
  • Save hussanhijazi/36668147e81ddbf88de6f75cc52db8bc to your computer and use it in GitHub Desktop.
Save hussanhijazi/36668147e81ddbf88de6f75cc52db8bc to your computer and use it in GitHub Desktop.
Kotlin tempertature/Humidity sensor activity
class SensorActivity : AppCompatActivity() {
companion object {
const val TAG = "SensorActivity"
const val TEMPERATURE_TOPIC = "t0th/temperature"
const val HUMIDITY_TOPIC = "t0th/humidity"
}
val mqttClient by lazy {
MqttClient(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sensor)
mqttClient.connect(arrayOf(TEMPERATURE_TOPIC, HUMIDITY_TOPIC), ::setData)
}
private fun setData(topic: String, msg: MqttMessage) {
when (topic) {
TEMPERATURE_TOPIC -> {
txtTemperature.text = " ${String(msg.payload)} ° c"
}
else -> {
txtHumidity.text = " ${String(msg.payload)}"
}
}
}
override fun onDestroy() {
super.onDestroy()
mqttClient.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment