Sample code for EZControl and Lelylan - it works!

138 views
Skip to first unread message

oto...@gmail.com

unread,
Jul 21, 2015, 5:51:35 AM7/21/15
to lel...@googlegroups.com
Guys,
This code works for me.
I connect through a home WiFi router with no problems.
Any changes I make with the EZControl are relected on Lelylan and any Lelylan changes come back to the EZControl.
I had to use Arduino IDE 1.0.6 to get it to recognize the MQTT library.
Note the changes in the interrupt and enable constants for the CC3000.
I use a simple read from the serial monitor either n or f - oN or ofF - gettit!

I did not actually test the relay was turning on and off a light but there is no reason it will not work once you get your wiring correct.
Remember to be careful working with mains voltages.

-otoolo

---- code----

#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  8
#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);

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_UNSEC // < - using an unsecured testing network
Adafruit_CC3000_Client client;

/* ---------------- */
/* WIFI CREDENTIALS */
/* ---------------- */

#define WLAN_SSID "My Unsecure network"  // * WiFi network name (cannot be longer than 32 characters)
#define WLAN_PASS ""  // * WiFi password - none needed on open test network

/* ------------------ */
/* DEVICE CREDENTIALS */
/* ------------------ */

char* deviceId     = "xxxxxxxxxxxxxxxxxxxxxxxx";              // * set your device id (will be the MQTT client username)
char* deviceSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";              // * set your device secret (will be the MQTT client password)
char* outTopic     = "devices/xxxxxxxxxxxxxxxxxxxxxxxx/set";  // * MQTT channel where physical updates are published
char* inTopic      = "devices/xxxxxxxxxxxxxxxxxxxxxxxx/get";  // * MQTT channel where lelylan updates are received
char* clientId     = "xxxxxxxxxxxxxxxxxxxxxxxx";              // * 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 = 7; // relay on EZControl board

int inChar; // Where to store the character read

/* 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();

 if (Serial.available() 0) {
               // read the incoming byte:
     inChar = Serial.read();
     if (inChar == 'n') {
       Serial.println("[PHYSICAL] Led turned on");
       lelylanPublish("on");
        digitalWrite(outPin, HIGH);
     } // end inChar = 'n'
     if (inChar == 'f') {
       Serial.println("[PHYSICAL] Led turned off");
       lelylanPublish("off");
        digitalWrite(outPin, LOW);
     }// end inChar = 'f'
 } // end if Serial.available

} // end loop

/* 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) {

 // copy 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);
}

oto...@gmail.com

unread,
Jul 21, 2015, 6:00:37 AM7/21/15
to lel...@googlegroups.com, oto...@gmail.com
It seems we lost a ">" in the process of posting this!
It should be if (Serial.available() > 0)

marti...@gmail.com

unread,
Jul 21, 2015, 8:23:15 PM7/21/15
to Lelylan, oto...@gmail.com

I can confirm that this sketch works.   We need to make this available for people to use.

=-M

Lelylan Touch

unread,
Jul 22, 2015, 8:19:13 AM7/22/15
to lel...@googlegroups.com, Mick O'Toole
We're closing some meetings and then we'll focus on creating the best documentation.
It should be ready for tomorrow.

--
You received this message because you are subscribed to the Google Groups "Lelylan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lelylan+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Andrea Reginato
Lelylan, Building the Connected Home
http://lelylan.com

Lelylan Support

unread,
Jul 23, 2015, 4:36:40 AM7/23/15
to Lelylan, to...@lelylan.com, oto...@gmail.com, to...@lelylan.com
Hi everyone, 

we've been working on making the final documentation as complete as we could to make your EZControl work with Lelylan.

A special thanks to Mick (aka otoolo) which was great enough to take the time to play with the EZ and solve the main issues. Thanks.

Everything is not working. This means you can control your EZ from Lelylan and you can reflect the physical changes to the dashboard. We also added info about the PINS: Relay on port 7 and temperature sensor on port A3. The Arduino IDE board must be the version >~ 1.0.6.

I you guys can test it out, issue new problems, we can improve even more the documentation. 
The documentation is on Github, so feel free to send us a pull request for any improvement.

All the best

Lelylan Support

unread,
Jul 23, 2015, 4:38:15 AM7/23/15
to Lelylan, to...@lelylan.com, oto...@gmail.com, to...@lelylan.com
Sorry :%s/not/now (Everything is now working)


Il giorno giovedì 23 luglio 2015 10:36:40 UTC+2, Lelylan Support ha scritto:
Hi everyone, 

we've been working on making the final documentation as complete as we could to make your EZControl work with Lelylan.

A special thanks to Mick (aka otoolo) which was great enough to take the time to play with the EZ and solve the main issues. Thanks.

Everything is now working. This means you can control your EZ from Lelylan and you can reflect the physical changes to the dashboard. We also added info about the PINS: Relay on port 7 and temperature sensor on port A3. The Arduino IDE board must be the version >~ 1.0.6.

Coda24

unread,
Sep 9, 2015, 3:34:48 PM9/9/15
to Lelylan
I can not get past the:

"[CC3000] Setting DHCP"

It just hangs there and nothing happens.  Any suggestions on what could be causing this?  (using the EZControl.it board).

Some additional info:
  • My router recognizes that a new device has connected, and an IP has been assigned.  I just don't know what the line:   while (!cc3000.checkDHCP())  is waiting to receive, and how I can check the status of the request.
  • Not always, but most times, after about 1 minute the LED will light up, but I can not control it from the Lelylan app.  I can only control it from the physical push-button.  So obviously there doesn't seem to be a connection between the board and the app.

Note:  If I switch and use an Ethernet Shield (with the appropriate code) I have no issues, but I really want the EZControl.IT board to work.

Thank you,
Rich

Andrea Reginato

unread,
Nov 11, 2015, 3:04:05 AM11/11/15
to Lelylan, richard...@gmail.com
Hi Rich, 

were you able to find a solution?
Reply all
Reply to author
Forward
0 new messages