@PaulS Here's the whole sketch rather than just snippets. Please don't laugh at my code, coding is not what I do best but I'm trying to learn!
You'll see the callback right at the end of the sketch (I've amended it to account for your previous comments, thank you).
It's still not working, I'm obviously doing something silly but I can't spot it.
/*
Sainsmart ethernet shield and Uno
*/
#include <SPI.h>
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK);
#include <Ethernet.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <Encoder.h>
#include <ClickButton.h>
const int buttonPin1 = 14;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
byte mac[] = {
0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = {
192, 168, 0, 2 };
byte ip[] = {
192, 168, 0, 100 };
Encoder myEnc(2, 3);
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
char* msg = "";
char* writetitle = "INITIALISING ... ";
char* writevol = "?";
int titlelength = 20;
int scrollcounter = 100;
void setup()
{
// Setup button params
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 200; // Time limit for multi clicks
button1.longClickTime = 250; // time until "held-down clicks" register
Ethernet.begin(mac, ip);
delay(720);
// MQTT Startup
client.connect("arduinoClient");
delay(50);
client.publish("audio/home/bedroom/arduinovector", "trackplease");
delay(50);
client.subscribe("audio/home/bedroom/#");
delay(50);
Serial.begin(9600);
// Some display params
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
int x_pos = 3; // global variable
int oldPosition = 0;
int trackupdatecycle = 50;
void draw(void) {
// Draw 3 lines of text on the display. Top 2 are the important ones with volume and track title
u8g.setFont(u8g_font_6x10);
// u8g.drawStr( 2, 10, "Vol __|__________ 18%");
u8g.drawStr( 2, 10, writevol);
// u8g.setFont(u8g_font_helvB12);
u8g.drawStr( x_pos, 39, writetitle);
// u8g.setFont(u8g_font_6x12);
u8g.drawStr( 3, 62, "Jazz R4 Sleep");
}
void loop()
{
u8g.firstPage();
do {
draw();
// update track every 1000 cycles
trackupdatecycle--;
if (trackupdatecycle == 0) {
// Request track over MQTT
client.publish("audio/home/bedroom/arduinovector", "trackplease");
trackupdatecycle = 1500;
}
// Send volume command
int newPosition = myEnc.read();
if (newPosition > oldPosition) {
oldPosition = newPosition;
Serial.println("Volume UP");
client.publish("audio/home/bedroom/arduinovector", "up");
}
else if (newPosition < oldPosition) {
oldPosition = newPosition;
Serial.println("Volume DOWN");
client.publish("audio/home/bedroom/arduinovector", "down");
}
// Send playpause next prev command
button1.Update();
if (button1.clicks != 0) {
if (button1.clicks == -1) {
client.publish("audio/home/bedroom/arduinovector", "playpause");
Serial.println("PlayPause");
}
if (button1.clicks == -2) {
client.publish("audio/home/bedroom/arduinovector", "next");
Serial.println("Next");
}
if (button1.clicks == -3) {
client.publish("audio/home/bedroom/arduinovector", "prev");
Serial.println("Prev");
}
}
[b] // Debug my values - this is not showing what I want it to show!
[/b]
Serial.print(writetitle);
Serial.print(" ");
Serial.println(writevol);
// Scroll title if required. Scrollpause used to pause it scrolling when left character is aligned with left of display
if (titlelength > 14) { // title too long for the page?
if (scrollcounter < 0) { // scroll pause countdown finished?
x_pos--; // scroll
scrollcounter = 0;
}
else{
scrollcounter--;
}
}
if (x_pos < -256) {
scrollcounter = 100;
x_pos = 0;
}
// delay(5);
client.loop();
}
while( u8g.nextPage() );
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
// String strPayload = String((char*)payload);
String strTopic = String((char*)topic);
if(strcmp(topic, "audio/home/bedroom/title") == 0)
{
// found a title
msg = (char*)payload;
writetitle = msg;
titlelength = length; // not yet used this
scrollcounter = 100; // this resets the scroll counter so it pauses before scrolling
x_pos = 3; // set scroll position to left
Serial.println("Found a title update");
}
if(strcmp(topic, "audio/home/bedroom/vol") == 0)
{
// found a volume
msg = (char*)payload;
writevol = msg;
Serial.println("Found a volume command");
}
}