Frederick Vandenbosch
Published

Spark Core IoT Controller

MQTT messages at the press of a button

Full instructions provided1,893
Spark Core IoT Controller

Things used in this project

Hardware components

Spark Core
Particle Spark Core
×1
pushbutton
×14
prototyping board
×1
1x12 female header
×2
battery pack (4xAA)
×1

Story

Read more

Code

code.txt

Plain text
// SKETCH

int BTN_A0 = A0;
int BTN_A1 = A1;
int BTN_A2 = A2;
int BTN_A3 = A3;
int BTN_A4 = A4;
int BTN_A5 = A5;
int BTN_A6 = A6;
int BTN_A7 = A7;

int BTN_D0 = D0;
int BTN_D1 = D1;
int BTN_D2 = D2;
int BTN_D3 = D3;
int BTN_D4 = D4;
int BTN_D5 = D5;
int BTN_D6 = D6;
int BTN_D7 = D7;

long lastRetry = 0;

TCPClient tcpClient;
PubSubClient client("iot.eclipse.org", 1883, 0, tcpClient);

void setup() {
    // onboard RGB LED will be used
    RGB.control(true);
    
    // use internal pull-ups, all pins HIGH by default
    pinMode(BTN_A0, INPUT_PULLUP);
    pinMode(BTN_A1, INPUT_PULLUP);
    pinMode(BTN_A2, INPUT_PULLUP);
    pinMode(BTN_A3, INPUT_PULLUP);
    pinMode(BTN_A4, INPUT_PULLUP);
    pinMode(BTN_A5, INPUT_PULLUP);
    pinMode(BTN_A6, INPUT_PULLUP);
    pinMode(BTN_A7, INPUT_PULLUP);

    pinMode(BTN_D0, INPUT_PULLUP);
    pinMode(BTN_D1, INPUT_PULLUP);
    pinMode(BTN_D2, INPUT_PULLUP);
    pinMode(BTN_D3, INPUT_PULLUP);
    pinMode(BTN_D4, INPUT_PULLUP);
    pinMode(BTN_D5, INPUT_PULLUP);
    pinMode(BTN_D6, INPUT_PULLUP);
    pinMode(BTN_D7, INPUT_PULLUP);

    // wait for wifi to be ready
    while( !WiFi.ready() ){
        // do nothing
        delay(250);
    }
}

void loop() {
    // check if connected to MQTT broker
    // only retry every 5 seconds, don't bash the broker
    if(!client.connected() && (millis() - lastRetry) > 5000) {
        // set last retry to now
        lastRetry = millis();
        // not connected, turn LED red
        RGB.color(255, 0, 0);
        // connect to MQTT broker
        if (client.connect("SparkCoreController")) {
            // connected, turn LED green
            RGB.color(0, 255, 0);
            // send notification about connection
            client.publish("SparkCoreController/Init", "Connected to broker.");
        }
    } else {
        client.loop();
        // check which buttons have been pressed
        buttonPressed(BTN_A0, "SparkCoreController/Command", "DOWN");
        buttonPressed(BTN_A1, "SparkCoreController/Command", "LEFT");
        buttonPressed(BTN_A2, "SparkCoreController/Command", "UP");
        buttonPressed(BTN_A3, "SparkCoreController/Command", "STOP");
        buttonPressed(BTN_A4, "SparkCoreController/Command", "RIGHT");
        buttonPressed(BTN_A5, "SparkCoreController/Command", "F9");
        buttonPressed(BTN_A6, "SparkCoreController/Command", "F8");
        buttonPressed(BTN_A7, "SparkCoreController/Command", "F7");
        buttonPressed(BTN_D0, "SparkCoreController/Command", "0");
        buttonPressed(BTN_D1, "SparkCoreController/Command", "F6");
        buttonPressed(BTN_D2, "SparkCoreController/Command", "F5");
        buttonPressed(BTN_D3, "SparkCoreController/Command", "0");
        buttonPressed(BTN_D4, "SparkCoreController/Command", "F4");
        buttonPressed(BTN_D5, "SparkCoreController/Command", "F3");
        buttonPressed(BTN_D6, "SparkCoreController/Command", "F2");
        buttonPressed(BTN_D7, "SparkCoreController/Command", "F1");
    }
}

void buttonPressed(int button, char topic[], char command[]) {
    // if button has been pressed
    if(digitalRead(button) == LOW) {
        // button pressed, turn LED blue
        RGB.color(0, 0, 255);
        // publish the command to the provided topic
        client.publish(topic, command);
        // wait a little
        delay(250);
        // turn the LED back to green
        RGB.color(0, 255, 0);
    }
}

Credits

Frederick Vandenbosch

Frederick Vandenbosch

8 projects • 68 followers
Building electronic things

Comments