Skip to content

Instantly share code, notes, and snippets.

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");
/* 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)) {
lelylanConnection(); // MQTT server connection
pinMode(inPin, INPUT); // button pin setup
pinMode(outPin, OUTPUT); // led pin setup
Serial.println(F("[CC3000]Init the WiFi connection\n"));
if (!cc3000.begin()) {
Serial.println(F("[CC3000] Fail init CC3000"));
for(;;);
}
Serial.println(F("[CC3000] Deleting old profiles\n"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("[CC3000] Fail deleting old profiles"));
while(1);
int inPin = 5; // button
int outPin = 7; // led
#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
private void _mqttClient_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) {
var messageString = new String(Encoding.UTF8.GetChars(e.Message));
this._ledOnBoard.Write(messageString.IndexOf("on") > 0);
LelylanPublish(messageString.IndexOf("on") > 0 ? "on" : "off");
}
if (_mqttClient.IsConnected) return;
// register to message publish/subscribe
_mqttClient.MqttMsgSubscribed += _mqttClient_MqttMsgSubscribed;
_mqttClient.MqttMsgUnsubscribed += _mqttClient_MqttMsgUnsubscribed;
_mqttClient.MqttMsgPublishReceived += _mqttClient_MqttMsgPublishReceived;
_mqttClient.MqttMsgPublished += _mqttClient_MqttMsgPublished;
_mqttClient.MqttMsgDisconnected += _mqttClient_MqttMsgDisconnected;
// connection to lelylan MQTT server
public void RunLightMonitor() {
// Get onboard led reference
this._ledOnBoard = new OutputPort(Pins.ONBOARD_LED, false);
while (true) {
LelylanConnection();
// read onboard led status
bool ledStatus = this._ledOnBoard.Read();
public Controller() {
// Init Client ID
_clientId = Guid.NewGuid().ToString();
if (_clientId.Length > 20)
_clientId = _clientId.Substring(_clientId.Length - 20, 20);
// create client instance
_mqttClient = new MqttClient(IPAddress.Parse(LelylanCore.MqttBrokerAddress));
}