Skip to content

Instantly share code, notes, and snippets.

@jt-nti
Forked from anonymous/mqttjogglermashup.c
Created October 5, 2015 14:24
Show Gist options
  • Save jt-nti/b79b99d590f0818ea9b7 to your computer and use it in GitHub Desktop.
Save jt-nti/b79b99d590f0818ea9b7 to your computer and use it in GitHub Desktop.
Simply publishes the Joggler light level reading to MQTT.
/*
* Basic mash up of Andrew de Quincey's Joggler light sensor code
* and sample MQTT client code to publish light sensor reading
* messages.
* For more info. see:
* http://code.google.com/p/adqmisc/source/browse/trunk/joggler/readlightsensor.c
* http://www.eclipse.org/paho/files/mqttdoc/Cclient/
* http://jtlog.wordpress.com/2013/02/07/mqtt-joggler/
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "MQTTClient.h"
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define ADDRESS "tcp://m2m.eclipse.org:1883"
#define CLIENTID "ExampleJogglerPub"
#define TOPIC "joggler"
#define PAYLOAD "Hello MQTT!"
#define QOS 1
#define TIMEOUT 10000L
volatile MQTTClient_deliveryToken deliveredtoken;
void delivered(void *context, MQTTClient_deliveryToken dt)
{
printf("Message with token value %d delivery confirmed\n", dt);
deliveredtoken = dt;
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
int i;
char* payloadptr;
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: ");
payloadptr = message->payload;
for(i=0; i<message->payloadlen; i++)
{
putchar(*payloadptr++);
}
putchar('\n');
MQTTClient_freeMessage(&message);
MQTTClient_free(topicName);
return 1;
}
void connlost(void *context, char *cause)
{
printf("\nConnection lost\n");
printf(" cause: %s\n", cause);
}
int main(int argc, char* argv[])
{
/* mqtt bits */
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
/* message buffer */
char msg[50];
/* light sensor stuff */
unsigned char buf[5];
int fd;
union i2c_smbus_data smbus_data;
struct i2c_smbus_ioctl_data smbus_ioctl_data;
fd = open("/dev/i2c-0", O_RDWR);
if (fd < 0) {
perror("Open i2c bus\n");
exit(1);
}
// set slave device to 8051 microprocessor
if (ioctl(fd, I2C_SLAVE, 0x34)) {
perror("Set slave i2c address\n");
exit(1);
}
// send the initial request
smbus_data.block[0] = 2; // count of bytes
smbus_data.block[1] = 0;
smbus_data.block[2] = 0;
smbus_ioctl_data.read_write = 0;
smbus_ioctl_data.command = 0x27;
smbus_ioctl_data.size = I2C_SMBUS_BLOCK_DATA;
smbus_ioctl_data.data = &smbus_data;
if (ioctl(fd, I2C_SMBUS, &smbus_ioctl_data)) {
perror("Send initial request\n");
exit(1);
}
// wait for response
smbus_data.block[0] = 4;
smbus_ioctl_data.read_write = 1;
while(1) {
if (ioctl(fd, I2C_SMBUS, &smbus_ioctl_data)) {
perror("Read result\n");
exit(1);
}
if (smbus_data.block[1] == 0x87)
break;
usleep(1000);
}
if (smbus_data.block[1] != 0x87) {
fprintf(stderr, "Failed to get response\n");
exit(1);
}
/*
printf("%u\n", (smbus_data.block[3] << 8) | smbus_data.block[2]);
*/
sprintf(msg, "Light sensor: %u", (smbus_data.block[3] << 8) | smbus_data.block[2]);
close(fd);
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(-1);
}
/*
pubmsg.payload = PAYLOAD;
pubmsg.payloadlen = strlen(PAYLOAD);
*/
pubmsg.payload = msg;
pubmsg.payloadlen = strlen(msg);
pubmsg.qos = QOS;
pubmsg.retained = 1;
deliveredtoken = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
PAYLOAD, TOPIC, CLIENTID);
while(deliveredtoken != token);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment