Created
November 29, 2018 13:06
-
-
Save hussanhijazi/36668147e81ddbf88de6f75cc52db8bc to your computer and use it in GitHub Desktop.
Kotlin tempertature/Humidity sensor activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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