12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #include <cstdio>
- #include <cstring>
- #include "temperature_conversion.h"
- #include <mosquittopp.h>
- mqtt_tempconv::mqtt_tempconv(const char *id, const char *host, int port) : mosquittopp(id)
- {
- int keepalive = 60;
- /* Connect immediately. This could also be done by calling
- * mqtt_tempconv->connect(). */
- connect(host, port, keepalive);
- };
- mqtt_tempconv::~mqtt_tempconv()
- {
- }
- void mqtt_tempconv::on_connect(int rc)
- {
- printf("Connected with code %d.\n", rc);
- if(rc == 0){
- /* Only attempt to subscribe on a successful connect. */
- subscribe(NULL, "temperature/celsius");
- }
- }
- void mqtt_tempconv::on_message(const struct mosquitto_message *message)
- {
- double temp_celsius, temp_fahrenheit;
- char buf[51];
- if(!strcmp(message->topic, "temperature/celsius")){
- memset(buf, 0, 51*sizeof(char));
- /* Copy N-1 bytes to ensure always 0 terminated. */
- memcpy(buf, message->payload, 50*sizeof(char));
- temp_celsius = atof(buf);
- temp_fahrenheit = temp_celsius*9.0/5.0 + 32.0;
- snprintf(buf, 50, "%f", temp_fahrenheit);
- publish(NULL, "temperature/fahrenheit", strlen(buf), buf);
- }
- }
- void mqtt_tempconv::on_subscribe(int mid, int qos_count, const int *granted_qos)
- {
- printf("Subscription succeeded.\n");
- }
|