Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andreareginato
Forked from bugduino/WorkingTinyLedBtn.ino
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreareginato/db87f7ec6f799824308a to your computer and use it in GitHub Desktop.
Save andreareginato/db87f7ec6f799824308a 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 2
#define ADAFRUIT_CC3000_VBAT A3
#define ADAFRUIT_CC3000_CS 8
// 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);
// 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;
/* ---------------- */
/* WIFI CREDENTIALS */
/* ---------------- */
#define WLAN_SSID "<WLAN_SSID>" // * WiFi network name (cannot be longer than 32 characters)
#define WLAN_PASS "<WLAN_PASS>" // * WiFi password
/* ------------------ */
/* DEVICE CREDENTIALS */
/* ------------------ */
char* deviceId = "<DEVICE-ID>"; // * set your device id (will be the MQTT client username)
char* deviceSecret = "<SECRET-ID>"; // * set your device secret (will be the MQTT client password)
char* outTopic = "devices/<DEVICE-ID>/set"; // * MQTT channel where physical updates are published
char* inTopic = "devices/<DEVICE-ID>/get"; // * MQTT channel where lelylan updates are received
char* clientId = "<DEVICE-ID>"; // * set your device id (will be the MQTT client id)
/* ------------ */
/* SKETCH LOGIC */
/* ------------ */
/* Server 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 = { 47, 108, 62, 178 }; // reverted ip so 178.62.108.47 is used
/* Sample payload published to lelylan */
/* The id is the status property id of the basic light /*
/* http://lelylan.github.io/types-dashboard-ng/#/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 = 4; // 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("[CC3000] Hi there"));
delay(500);
Serial.println(F("[CC3000] Init the WiFi connection"));
if (!cc3000.begin()) {
Serial.println(F("[CC3000] Fail init CC3000"));
for(;;);
}
Serial.println(F("[CC3000] Deleting old profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("[CC3000] Fail deleting old profiles"));
while(1);
}
char *ssid = WLAN_SSID;
Serial.print(F("[CC3000] Connecting to "));
Serial.println(ssid);
Serial.print(F("\n"));
// (note: secure connections are not available in 'Tiny' mode)
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("[CC300] Connection failed"));
while(1);
}
Serial.println(F("[CC3000] Connection OK"));
/* Wait for DHCP to complete */
Serial.println(F("[CC3000] Setting 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("[PHYSICAL] Led turned on");
lelylanPublish("on");
state = HIGH;
} else {
Serial.println("[PHYSICAL] Led turned 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()) {
if (mqttclient.connect(clientId, deviceId, deviceSecret)) {
Serial.println("[PHYSICAL] Successfully connected with MQTT");
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("[LELYLAN] Led turned on");
lelylanPublish("on");
state = HIGH;
} else {
Serial.println("[LELYLAN] Led turned 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