The WiFi shield from Adafruit allows me to control the TARDIS from a tablet or smartphone.
Code Snippet for triggering actions based on messages received via AdaFruit cloud
/*
* tardis_network.h
* Claude Felizardo 2018-10-06
*
* uses wifi board form Adafruit
*/
#ifndef _tardis_network_h_
#define _tardis_network_h_
#define TARDIS_NETWORK_VERSION "$Id$"
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi101.h>
#include "network_config.h" // wifi and adafruit io config info
int network_status = WL_NO_SHIELD;
//Set up the wifi client
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// Setup various feeds to control the tardis
Adafruit_MQTT_Subscribe mqtt_power_off = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.power-off");
Adafruit_MQTT_Subscribe mqtt_door_switch = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.door-switch");
Adafruit_MQTT_Subscribe mqtt_stop_sound = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.stop-sound");
Adafruit_MQTT_Subscribe mqtt_play_sfx = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.play-sfx");
Adafruit_MQTT_Subscribe mqtt_play_theme = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.play-theme");
Adafruit_MQTT_Subscribe mqtt_lights_off = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.lights-off");
Adafruit_MQTT_Subscribe mqtt_roof_light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.roof-light");
Adafruit_MQTT_Subscribe mqtt_sign_color = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.sign-color");
Adafruit_MQTT_Subscribe mqtt_sound_bank = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/tardis.sound-bank");
void tardis_network_setup()
{
Serial.println("Setting up WiFi...");
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
// Initialise the Client
Serial.print(F("\nInit the WiFi module..."));
// check for the presence of the breakout
if ((network_status = WiFi.status()) == WL_NO_SHIELD) {
Serial.println("WINC1500 not present");
// don't continue:
// while (true);
} else {
Serial.println("ATWINC OK!");
}
mqtt.subscribe(&mqtt_power_off);
mqtt.subscribe(&mqtt_stop_sound);
mqtt.subscribe(&mqtt_play_sfx);
mqtt.subscribe(&mqtt_play_theme);
mqtt.subscribe(&mqtt_lights_off);
mqtt.subscribe(&mqtt_roof_light);
mqtt.subscribe(&mqtt_sign_color);
mqtt.subscribe(&mqtt_door_switch);
mqtt.subscribe(&mqtt_sound_bank);
Serial.println("WiFi setup done.");
} // tardis_network_setup
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care of connecting.
void MQTT_connect() {
// attempt to connect to Wifi network:
while ((network_status = WiFi.status()) != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
network_status = WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("network_status="); Serial.println(network_status);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && ((network_status = WiFi.status()) != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
//if (WiFi.status() == WL_CONNECTED) {
// Serial.print("Connected to SSID");
//}
// return if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
int8_t ret;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
} // MQTT_connect()
void tardis_network_check()
{
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
if (network_status != WL_NO_SHIELD)
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(50/*00*/))) {
if (subscription == &mqtt_power_off) {
Serial.print(F("Got power_off: "));
Serial.println((char *)mqtt_power_off.lastread);
if (0 == strcmp((char *)mqtt_power_off.lastread, "OFF")) {
TardisAudio::stopAll();
atx_shield.powerOff();
sign_color.disable();
roof_light.disable();
}
} else if (subscription == &mqtt_stop_sound) {
Serial.print(F("Got stop_sound: "));
Serial.println((char *)mqtt_stop_sound.lastread);
if (0 == strcmp((char *)mqtt_stop_sound.lastread, "STOP-SOUND")) {
TardisAudio::stopAll();
}
} else if (subscription == &mqtt_play_sfx) {
Serial.print(F("Got play_sfx: "));
Serial.println((char *)mqtt_play_sfx.lastread);
if (0 == strcmp((char *)mqtt_play_sfx.lastread, "PLAY-SFX")) {
if (TardisAudio::sound_bank == 0)
sound_track = TARDIS_list.getNext();
else
sound_track = Alt_list.getNext();
sprintf(buffer, "Playing sound_track %d", sound_track);
Serial.println(buffer);
TardisAudio::playTrack(sound_track);
}
} else if (subscription == &mqtt_play_theme) {
Serial.print(F("Got play_theme: "));
Serial.println((char *)mqtt_play_theme.lastread);
if (0 == strcmp((char *)mqtt_play_theme.lastread, "PLAY-THEME")) {
if (TardisAudio::sound_bank == 0)
sound_track = Theme_list.getNext();
else
sound_track = Alt_list.getNext();
sprintf(buffer, "Playing sound_track %d", sound_track);
Serial.println(buffer);
TardisAudio::playTrack(sound_track);
}
} else if (subscription == &mqtt_sound_bank) {
Serial.print(F("Got sound_bank: "));
Serial.println((char *)mqtt_sound_bank.lastread);
if (0 == strcmp((char *)mqtt_sound_bank.lastread, "SOUND-BANK")) {
TardisAudio::change_sound_bank();
}
} else if (subscription == &mqtt_lights_off) {
Serial.print(F("Got lights_off: "));
Serial.println((char *)mqtt_lights_off.lastread);
if (0 == strcmp((char *)mqtt_lights_off.lastread, "LIGHTS-OFF")) {
roof_override = 0;
sign_color_override = 0xffffff;
}
} else if (subscription == &mqtt_door_switch) {
Serial.print(F("Got door_switch: "));
Serial.println((char *)mqtt_door_switch.lastread);
if (0 == strcmp((char *)mqtt_door_switch.lastread, "OPEN")) {
door_override = 1;
}
if (0 == strcmp((char *)mqtt_door_switch.lastread, "CLOSED")) {
door_override = 0;
}
} else if (subscription == &mqtt_roof_light) {
Serial.print(F("Got roof_light: "));
Serial.print((char *)mqtt_roof_light.lastread);
roof_override = atoi((char *)mqtt_roof_light.lastread);
Serial.print(F(" => "));
Serial.println(roof_override);
} else if (subscription == &mqtt_sign_color) {
Serial.print(F("Got sign_color: "));
Serial.print((char *)mqtt_sign_color.lastread);
Serial.print(F(" -> "));
Serial.print((char *)mqtt_sign_color.lastread+1);
sign_color_override = strtol((char *)mqtt_sign_color.lastread+1, 0, 16) ^ 0xffffff;
Serial.print(F(" => "));
Serial.println(sign_color_override);
}
}
if (door_override != -1) door_position = door_override;
} // tardis_network_check
#endif // _tardis_network_h_
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.