Apple HomeKit on iOS with Arduino (ESP8266) through Raspberry Pi – Part 2

Apple HomeKit on iOS with Arduino (ESP8266) through Raspberry Pi

Part 1 of this article explained how to install a HomeKit server on a Raspberry Pi. This will allow you to add new, DIY accessories that can receive commands from Siri.

Part 2 (this article) explains how to install an MQTT server that can be a “bridge” between Siri (through you HomeKit server) and you hardware.

IoT Part 2

MQTT is a communication protocol that will let you send and receive simple messages from one computer to another. You need one server a a number of clients. We will setup the server on our Raspberry Pi, but there are servers available publicly.

Install the MQTT server: Mosquitto

There are other MQTT servers available but this one works well and is easy to install.

sudo wget
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget
sudo apt-get update
sudo apt-get install mosquitto

This will add the mosquitto repo to your list, then the last instruction actually uses the standard apt-get install.

Next,  install the Mosquitto clients to test your server (if you want). They are the actual commands to use your Raspberry Pi as an MQTT client.

sudo apt-get install mosquitto-clients

This link has many good pointers to configure the Mosquitto server to start and stop as a service, but this simple installation will be ok for testing.

Let’s test our mosquitto server. Open a new terminal window on your Raspberry Pi (or an SSH session if you are accesing it remotely) and type

mosquitto_sub -d -t hello/world

This will “subscribe” to the “hello” topic (-t), “world” sub-topic on your server (localhost). This can be ANYTHING you want. There is no real format and the / or /’s are optional.  The terminal will be executing MQTT in “subscribe” (think if it as “receive” or “listen”) mode and will just wait for the proper incoming message, meaning with the proper topic.

Now we need to publish a message (-m) on the same topic (-t), but in a new terminal window (or SSH session):

mosquitto_pub -d -t hello/world -m "Hello from Terminal window 2!"

The first window should show the message

Client mosqsub/12234-raspberry received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (29 bytes))
Hello from Terminal window 2!

In my environment, I have one MQTT server on a Raspberry Pi and messages coming and going from other Raspberry Pis and sensor equipped Arduinos.

A particular device can send or receive MQTT messages and can also do both at the same time.

EPS8266 Fifi Micro-controller

For this article, I’m using an Arduino type of micro-controller that has a Wifi stack. Someone has written an Arduino library that handles MQTT communications, and being Wifi, this Arduino also has a Wifi library taking care of communications.

I’m using an Adafruit Huzzah ESP8266 micro controller. It has Wifi on board and is compatible with the Arduino environment. It’s not strictly an arduino, and the processor is not the typical Atmel 328, but Adafruit has written everything you need to program it from the Arduino IDE. Any wifi-enabled Arduino could be used, but the the ESP8266 is dirt cheap (10$) and is very compact. Please use the link above to learn how to dowload the appropriate Arduino library and run some tests for Wifi connections.

Now, let’s test the ESP8266 with a simple sketch that will send a message on the topic “/hello”. The message is “world” and is sent every minute. The sketch will also listen to the topic “hello/world” and print incoming messages. So if everything goes well, it will print the message it just sent to the MQTT server!

  Basic ESP8266 MQTT example

  This sketch demonstrates the capabilities of the pubsub library in combination
  with the ESP8266 board/library.

  It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

  It will reconnect to the server if the connection is lost using a blocking
  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  achieve the same result without blocking the main loop.

  To install the ESP8266 board, (using Arduino 1.6.4+):
  - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
  - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  - Select your ESP8266 in "Tools -> Board"


#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.

const char* ssid = "default";
const char* password = "leborddulac";
const char* mqtt_server = "";
const char* host = "hello/world"; //used to identify topic. This is the string of
//  characcters used in the *_accessory.js file to identify the topic

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  digitalWrite(BUILTIN_LED, HIGH); // ...and turn if off (reversed logic for the esp8266)
  client.setServer(mqtt_server, 1883); //connect to the mqtt server at address above

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
 Serial.print("Message arrived [");
 Serial.print("] ");
 String mypayload = " ";
 for (int i = 0; i < length; i++) {
 mypayload[i] = (char)payload[i];
 Serial.print(" Payload: ");
 String mytopic = String(topic);
 if (mytopic == host) { // Is this the right Topic?
 // Switch on the LED if the payload in "on"
 //if ((char)payload[1] == 'n') { if (mypayload == statusOn) {
 if (mypayload == statusOn) {
 digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
 // but actually the LED is on; this is because
 // it is acive low on the ESP-01)
 //client.publish(host, "Powered");
 } else {
 digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
 //client.publish(host, "NOTPowered");

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect("ESP8266Client")) {
 // Once connected, publish an announcement...
 client.publish("outTopic", "hello world");
 // ... and resubscribe
 } else {
 Serial.print("failed, rc=");
 Serial.println(" try again in 5 seconds");
 // Wait 5 seconds before retrying

void loop() {

 if (!client.connected()) {

 long now = millis();
 if (now - lastMsg > 60000) {
 lastMsg = now;
 snprintf (msg, 75, "hello world #%ld", value);
 Serial.print("Publish message: ");
 client.publish("outTopic", msg);

Once you ESP8266 is communicating with your MQTT server, most of the hard work is done. You now have an Arduino type micro-controller linked to your MQTT network.

You could decide to go no further and not link this to HomeKit and Siri. In fact, most of my sensors use MQTT to communicate together on my Wifi network, and these sensors are connected to Arduino, Raspberry Pi and other computers. In a future article, I will talk about HomeKit in more details, and eventually I will explain my node-red setup and interface.

Posted in Arduino, Electronics | 7 Comments

Apple HomeKit on iOS with Arduino (ESP8266) through Raspberry Pi – Part 1

The Internet of Thinks (IoT), using and iPhone, Siri, HomeBridge, MQTT and an Arduino – Part 1

The global project is shown in this drawing:


Part 1 (this article) is in yellow. Part 2 (next article) will cover the Mosquitto server (just below) and the DIY HomeKit Device. The rest will be detailed in future articles, over the next few weeks. Continue reading

Posted in Uncategorized | 1 Comment

Review: Zivix PUC+ for Bluetooth Midi Wireless

I’ve been a fan off wireless MIDI for a while. I built a few prototypes and I still use some of the devices I made. I might have found a replacement: Zivix just came out (fall 2015) with a wireless Midi transmitter, called the puc+ (puc plus), that is using Bluetooth LE (BLE) to exchange Midi information between any Midi device and a computer (Mac) or iPhone/iPad. This review is for the puc+. Zivix made a previous version called “puc” a couple of years ago (after a successful Indiegogo campaign). It was using a Wifi dedicated network connection. The new device is definitely more useful.

(By the way, you might have found this page doing a google search on puc+, puc plus or puc. Product names with special characters (+) are not search engine friendly…)

 PUC+ top Continue reading

Posted in Electronics, Music equipment, Uncategorized | Tagged , , | 4 Comments

M4L: Max for live patch for better guitar solo

Max for Live using Pitch Bend

When using a keyboard to play guitar, you have to use Midi Pitch Bend (Pitchbend or PB) quite often to emulate the effect of bending a string on a real guitar. The problem is that PB affects the sound synthesizer, not the Midi notes. Modifying the synth sound means that every note currently playing will be bent. On a real guitar, it’s generally the highest note that is bent, and sometimes the second highest if you keep pushing on the string, far enough to hit the second string.

So I programmed a small Max for Live (M4L) patch for Ableton Live to let me bend one note only, even when playing chords.

If you use Live, this will mean something to you. Keep reading.

The device is made of two patches. The first is a little program that will identify the highest note and pass it to the second patch. The second patch receives notes sent by the first one and passes them to the synth.

To install, you have to place the first patch in front of your synth in you guitar (or other instrument) track.

Sending patch in front of synth

Sending patch in front of synth

Continue reading

Posted in Ableton, M4L, Max for Live | Tagged , | Leave a comment

VC-1: Volume control for guitar using Arduino

As explained here, here and here, the VC-1 is a volume control for guitar (or bass) that can use a potentiometer, a variable analog signal or a Midi command to change the output volume.


Continue reading

Posted in Arduino, Electronics, Music equipment, VC-1 | 1 Comment

Akai MPK261: One more thing using Sysex…

In a previous post, I explained how to control preset changes on the mPK261 using SysEx commands. In this post, I explain how to get the MPK2 series to show some tricked pad colors.

Continue reading

Posted in MPK261, Music equipment | 7 Comments

MIDI Experiments: Arduino as master

Arduino as a Midi master

The previous two posts explained how standard MIDI can supply some current at 5 Volts if implemented according to the MIDI standards. In this post, I go a little further and explain how an Arduino, set up as a MIDI device, can power a second Arduino using a standard MIDI cable.

Continue reading

Posted in Arduino, Electronics | Leave a comment

MIDI experiments: Stealing power for an Arduino

In the previous post, I explained a simple circuit that will indicate if a MIDI OUT port can provide 5 Volts and a little bit of current between pin 2 (ground) and pin 4 (+5 Volts). Not all MIDI devices are wired that way. Some leave pin 2 disconnected (bad) while others connect it to chassis ground (often the same as circuit ground) and a few use a micro-controller pin as a false ground (!?).

What if 5 Volts is available?

Here’s an old Arduino Duemilanove hookup to to my Akai MPK261:


If you have sharp eyes, you will see the power LED lit right beside the Arduino word. Continue reading

Posted in Arduino, Electronics | Leave a comment

MIDI experiments: A simple MIDI tester

While doing some research to build a simple MIDI tester, I started to study the “real world” MIDI electrical specifications out there. I also experimented with various MIDI equipment and measured some voltages.

A simple MIDI tester

MIDI specifications imply that a MIDI OUT circuit always provides +5 Volts, on pin 4, in reference to circuit ground, present on pin 2. Pin 5 carries the actual MIDI signal and switches from +5 volts to zero Volts, referenced to the ground present on pin 2.

To test if a MIDI OUT circuit adheres to the specs, I use this simple circuit:

simple-midi-tester-1 Continue reading

Posted in Electronics, Music equipment | Tagged | 2 Comments

About MIDI connections: Identifying the Midi pin numbers

A short article to help identify MIDI pin numbers in all those circuit drawings  and photos on the internet. Continue reading

Posted in Electronics | Tagged | 8 Comments