I'm currently working on a wireless audio control surface using an ESP8266.
The ESP handles all the connection stuff, like WiFi, a webserver, UDP, OSC ... and has a couple of shift registers connected to drive all the LEDs. To get input from potentiometers, faders, buttons, rotary encoders etc, I'm thinking of using an Arduino (or a Teensy).
My question is: what is the best approach to get the two to communicate?
I'll need three types of messages:
From the ESP to the Arduino, to setup the pins (pin mapping can be changed in a web interface), for example: 'button on pin 5' (which tells the Arduino to turn on the internal pull-up resistor on pin 5, and report changes of state back to the ESP), or 'rotary encoder on pins 2 and 3' (initialize rotary encoder, and report relative position when moved)
Readings from the Arduino back to the ESP: when a value for an input is different than the previous reading, the Arduino should send that new value back to the ESP.
Values from the ESP to the Arduino: I'd like to support motorized faders as well. This means that the ESP should be able to change the position of the fader, so it should be able to send position values to the Arduino. The Arduino will then control the motor depending on the desired position, and the current analog value.
First question: What interface is the most suitable in this case? UART? SPI? Something else?
Second question: How does the ESP know that a value has changed? In the case of UART, is checking for Serial.available in the loop of the ESP sufficient? Should I use a hardware interrupt (the Arduino driving a pin high when it's going to send a value)? In the case of SPI, who takes the role as master? Does that matter?
And finally: how do I know when a message is finished? I read these articles by Nick Gammon, but he either uses only 1-byte messages, or a datatype/structure with a fixed length (here). My initialization messages have different lengths, for example, a 'button on pin 12' command has only 1 or 2 bytes, while a 'motorized fader with its motors on pins 4, 5 and 6, knob touch sensor on pin 6, analog input A9' message is much longer.
Should the first byte include the packet length? Should I use start and stop bytes? Should I make all packets of equal length, just sending 0x00 for bytes I don't need for a shorter message? Can they get out of sync in this case? (If it expects a new packet every 6 bytes, for example, and it skips one byte, then the first byte of the next packet will be interpreted as the second byte of the packet, with possibly disastrous consequences).
Any help, comments or thoughts are appreciated.
Thanks in advance, Pieter