MQTT for the C64
We are lucky!
Thanks to the work of Stephen Robinson there is already an implementation of MQTT for Contiki Link
So the integration is quite straight forward.
We have to include the new mqtt-msg.c and mqtt-sevrice.c to our Makefile by adding:
override webserver_src = webserver-nogui.c httpd.c http-strings.c psock.c memb.c \ httpd-fs.c httpd-cgi.c uip.c mqtt-msg.c mqtt-service.c
to our Makefile.
And of course we have to add some code to our webserver-example.c
First we have to include the mqtt-service.h
#include "mqtt-service.h"
Then we have to extend our PROCESS_THREAD with some mqtt "glue":
PROCESS_THREAD(get_temp_process, ev, data) { static struct etimer timer; static uip_ipaddr_t server_address; // Allocate buffer space for the MQTT client static uint8_t in_buffer[64]; static uint8_t out_buffer[64]; static uint8_t message[64]; // Setup an mqtt_connect_info_t structure to describe // how the connection should behave static mqtt_connect_info_t connect_info; connect_info.client_id = "c64contiki"; connect_info.username =NULL; connect_info.password = NULL; connect_info.will_topic = NULL; connect_info.will_message = NULL; connect_info.keepalive = 60; connect_info.will_qos = 0; connect_info.will_retain = 0; connect_info.clean_session = 1; PROCESS_BEGIN(); // Set the server address uip_ipaddr(&server_address, 192,168,1,43); // Initialise the MQTT client mqtt_init(in_buffer, sizeof(in_buffer), out_buffer, sizeof(out_buffer)); // Ask the client to connect to the server // and wait for it to complete. mqtt_connect(&server_address, UIP_HTONS(1883), 1, &connect_info); PROCESS_WAIT_EVENT_UNTIL(ev == mqtt_event); httpd_cgi_add(&MyTemp); // Set the server address etimer_set(&timer, 5*CLOCK_SECOND); while(1) { PROCESS_WAIT_EVENT(); if (ev == PROCESS_EVENT_TIMER) { //printf("Timer Event\n"); etimer_reset(&timer); gettemp(); // Send some MQTT stuff if (mqtt_ready()){ sprintf(message,"%d.%d",nMyTemperatur,nMyTemperaturDecimal); mqtt_publish("c64temp", message, 0, 1); } } } mqtt_disconnect(); PROCESS_END(); }
The command uip_ipaddr sets the IP address of our MQTT broker (e.g. a mosquitto). With mqtt_init(..) we initialize the MQTT process and with mqtt_connect(..) we connect to the broker.
If we now check our temperature every 5 seconds or so we can send out the value with mqtt_publish(...).
The mqtt topic is set in this example to "c64temp". You will receive this as "C64TEMP" in your broker.
One important thing is left. We have to tell Contiki to start the new MQTT process:
AUTOSTART_PROCESSES(&webserver_nogui_process,&get_temp_process, &mqtt_process);
We have to change to little things in the code of Stephen Robinson before we can compile this all:
We have to move the statement "PROCESS_NAME(mqtt_process);" from the mqtt-service.c to the mqtt-service.h file.
Now after a recompile of everything we have a web server and a MQTT client running on a Commodore 64.














