Skip to content

Instantly share code, notes, and snippets.

@bugduino
Created December 6, 2014 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bugduino/f30ff3604590f16170af to your computer and use it in GitHub Desktop.
Save bugduino/f30ff3604590f16170af to your computer and use it in GitHub Desktop.
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <cc3000_PubSubClient.h>
// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 9
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
#define WLAN_SSID "Telecom-46036665" // cannot be longer than 32 characters!
#define WLAN_PASS "xxx"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
Adafruit_CC3000_Client client;
/* Lelylan setup ------- */
/* Device credentials */
char* deviceId = "532b02edb9a8a7555d000001"; // set your device id (will be the MQTT client username)
char* deviceSecret = "bar"; // set your device secret (will be the MQTT client password)
/* Device topics */
char* outTopic = "devices/532b02edb9a8a7555d000001/set"; // where physical updates are published
char* inTopic = "devices/532b02edb9a8a7555d000001/get"; // where lelylan updates are received
/* Access settings */
// We're going to set our broker IP and union it to something we can use
union ArrayToIp {
byte array[4];
uint32_t ip;
};
// ArrayToIp server = { 24, 44, 194, 173 }; //google
ArrayToIp server = { 170, 109, 126, 96 }; //reverted ip so 96.12.109.170
char* clientId = "bugman"; // MQTT client id (random, max 23 bytes)
/* Sample payload published to lelylan */
/* Status-id is the basic light status id (http://types.lelylan.com/types/518be107ef539711af000001) */
char* payloadOn = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"on\"}]}";
char* payloadOff = "{\"properties\":[{\"id\":\"518be5a700045e1521000001\",\"value\":\"off\"}]}";
void callback(char* topic, byte* payload, unsigned int length); // subscription callback
cc3000_PubSubClient mqttclient(server.ip, 1883, callback, client, cc3000);
/* Pins configuration */
int inPin = 5; // button
int outPin = 3; // led
/* Button and led logics */
int state = HIGH; // current state of the output pin
int reading; // current reading from the input pin
int previous = LOW; // previous reading from the input pin
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
/* arduino setup */
void setup() {
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
delay(500);
Serial.println(F("\nInit the CC3000..."));
if (!cc3000.begin()) {
Serial.println(F("fail init CC3000"));
for(;;);
}
Serial.println(F("\nDeleting old profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Attempt to connect to an access point */
char *ssid = WLAN_SSID; /* Max 32 chars */
Serial.print(F("\nConnecting to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("ConnOK!"));
/* Wait for DHCP to complete */
Serial.println(F("DHCP?"));
while (!cc3000.checkDHCP()) {
delay(100); // ToDo: Insert a DHCP timeout!
}
// connect to the broker
lelylanConnection(); // MQTT server connection
pinMode(inPin, INPUT); // button pin setup
pinMode(outPin, OUTPUT); // led pin setup
}
/* arduino loop */
void loop() {
mqttclient.loop();
char* value;
reading = digitalRead(inPin); // read the button state
// if the input just went from LOW and HIGH and we've waited long enough to ignore
// any noise on the circuit, toggle the output pin and remember the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == LOW) {
Serial.println("[P]Led-on");
lelylanPublish("on");
state = HIGH;
} else {
Serial.println("[P]Led-off");
lelylanPublish("off");
state = LOW;
}
time = millis();
}
// effectively update the light status
digitalWrite(outPin, state);
previous = reading;
}
/* MQTT server connection */
void lelylanConnection() {
// add reconnection logics
if (!client.connected()) {
client = cc3000.connectTCP(server.ip, 1883);
}
// did that last thing work? sweet, let's do something
if(client.connected()) {
Serial.println("here");
if (mqttclient.connect(clientId, deviceId, deviceSecret)) {
Serial.println("[P]MQTT-OK!!");
lelylanSubscribe();
}
}
mqttclient.loop();
}
/* MQTT publish */
void lelylanPublish(char* value) {
if (value == "on")
mqttclient.publish(outTopic, payloadOn); // light on
else
mqttclient.publish(outTopic, payloadOff); // light off
}
/* MQTT subscribe */
void lelylanSubscribe() {
mqttclient.subscribe(inTopic);
}
/* Receive Lelylan message and confirm the physical change */
void callback(char* topic, byte* payload, unsigned int length) {
// copu the payload content into a char*
char* json;
json = (char*) malloc(length + 1);
memcpy(json, payload, length);
json[length] = '\0';
// update the physical status and confirm the executed update
if (String(payloadOn) == String(json)) {
Serial.println("[L] Led on");
lelylanPublish("on");
state = HIGH;
} else {
Serial.println("[LY] Led off");
lelylanPublish("off");
state = LOW;
}
digitalWrite(outPin, state);
free(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment