Skip to content

Instantly share code, notes, and snippets.

@nathanchantrell
Last active August 29, 2015 14:11
Show Gist options
  • Save nathanchantrell/6275dcc61f572bd0ec7b to your computer and use it in GitHub Desktop.
Save nathanchantrell/6275dcc61f572bd0ec7b to your computer and use it in GitHub Desktop.
Basic Wicked Device WildFire MQTT example
/*
Basic Wicked Device WildFire MQTT example
- connects to an MQTT server
- publishes "hello world" to the topic defined in OUTTOPIC
- subscribes to the topic defined in INTOPIC and prints any messages received
*/
#include <WildFire_CC3000.h>
#include <WildFire.h>
#include <SPI.h>
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient
WildFire wildfire;
WildFire_CC3000 cc3000;
WildFire_CC3000_Client ethClient;
#define WLAN_SSID "SSID" // cannot be longer than 32 characters!
#define WLAN_PASS "password"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
// Update these with values suitable for your network.
byte server[] = { 21, 0, 168 , 192 }; // IP address of MQTT broker (in reverse order)
#define OUTTOPIC "test" // Topic to publish to
#define INTOPIC "test" // Topic to subscribe to
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
payload[length] = '\0';
String strPayload = String((char*)payload);
Serial.print(F("Message received on topic "));
Serial.print(topic);
Serial.print(F(" : "));
Serial.println(strPayload);
}
PubSubClient client(server, 1883, callback, ethClient);
void setup()
{
wildfire.begin();
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
/* Initialise the module */
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
if (client.connect("WildFireClient")) {
Serial.println(F("Connected to broker"));
client.publish(OUTTOPIC,"hello world");
Serial.println(F("Published"));
client.subscribe(INTOPIC);
Serial.println(F("Subscribed"));
}
}
void loop()
{
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment