Arduino MQTT KAKU 3 years later…

After 3 years of bridging the gap between my KAKU devices and MQTT broker it is time for an update of my Arduino based device.

My MQTT broker is being updated to a Pi 3B+ and gets an new IP address, as this was hardcoded I needed to change my firmware. The Adafruit MQTT library used in the original post has been moved forward and changed. The 433MHz library for the KAKU devices has not changed but needed to be contained within the project. I have moved from the Arduino IDE to PlatformIO for all my new projects so I decided to do a slight rewrite to make my firmware compatible with the latest MQTT library and PlatformIO.

I have added the full source code to GitHub for future reference, since I have slowly moved away from KAKU devices I do not expect to work on this unless something breaks.

Much of the software stayed the same but some changes were needed as PlatformIO uses C++ instead of C, also the compiler and libraries have changed so my original code would not even build anymore using the latest tools. WordPress now also has a nice option to past in source code so why not use that to explain the different parts of the code.

Lets go trough the code step by step.

#include Arduino.h
#include SPI.h
#include Ethernet.h
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include EthernetClient.h
#include Dns.h
#include Dhcp.h

#include "RemoteReceiver.h"
#include "RemoteTransmitter.h"
#include "NewRemoteReceiver.h"
#include "NewRemoteTransmitter.h"
#include "InterruptChain.h"
#include avr/wdt.h 

Arduino.h is added so PlatformIO knows it is dealing with an Arduino sketch.
The sketch name is also changed to main.cppDns.h and Dhcp.h are added so a hostname can be resolved instead of using a fixed IP, this so I do not need to update the sketch when my IP changes in the future.

#define AIO_SERVER "[SERVER]"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "[USER]"
#define AIO_KEY "[PASSWORD]"

All PROGMEM definitions have been removed from code as they were not needed and this makes adjusting the codes for other platforms simpler.

void showOldCode(unsigned long receivedCode, unsigned int period);
void showNewCode(NewRemoteCode receivedCode);
void MQTT_connect();

Because we are now dealing with C++ we need to declare our voids before our actual code.

wdt_enable(WDTO_8S);

The watchdog timer has been changed from 2 seconds to 8 because sometime 2 seconds is not enough….

while ((ret = mqtt.connect()) !=0)
{ // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
wdt_reset();
mqtt.disconnect();
delay(5000); // wait 5 seconds
void (*resetFunc)(void) =0; //declare reset function at address 0
if (retry >=2)
resetFunc(); //call reset
retry++;
}

Do not forget to call wdt_reset(); throughout your entire code as it will restart your device if you do this to late. (but prevents code from waiting indefinitely)

The rest of the code is explained in the original article
Source code on GitHub
PlatformIO

 

Advertisements

One thought on “Arduino MQTT KAKU 3 years later…

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: