My first MQTT project! Really CooL!

About This Project

This is a project with Arduino YUN and three resistors and three photoresistors. I’ve created this project with Cayenne classic edition, now I have modified this project with MQTT protocol. Each resistor have color filter three photoresistors, three filters: RED, GREEN, BLUE.
When the light hit the photoresistors, the resistence modify his value, and the analogRead of Arduino YUN read a different value like input. You can read the “quality of light” on your Dashboard.
The MQTT it’s a really revolution for me! You can use this protocol on all device that you want! It’ really cool!

What’s Connected

Arduino YUN three photoresistors in series with resistors. See the hardware project to this page:

Dashboard Screenshots

Photos of the Project

Code

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTYun.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "YOURUSER";
char password[] = "YOURPASS";
char clientID[] = "YOURCLIENT";

unsigned long lastMillis = 0;
float lux1 = 0;
float lux2 = 0;
float lux3 = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();

//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
	lastMillis = millis()/1000;
	//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
	Cayenne.virtualWrite(0, lastMillis);

    lux1 = analogRead(A0);

    lux2 = analogRead(A1);

    lux3 = analogRead(A2);

    Cayenne.luxWrite(1, lux1);

    Cayenne.luxWrite(2, lux2);

    Cayenne.luxWrite(3, lux3);
	}
}

CAYENNE_CONNECTED()
{
  CAYENNE_LOG("CAYENNE_CONNECTED");
}

CAYENNE_DISCONNECTED()
{
  CAYENNE_LOG("CAYENNE_DISCONNECTED");
}

CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  if (strcmp(getValue.asStr(), "error") == 0) {
    getValue.setError("There are an error");
  }
 }

CAYENNE_OUT_DEFAULT()
{
 CAYENNE_LOG("CAYENNE_OUT_DEFAULT(%u)", request.channel);
}

Hi @0lab,

Cool project! Thanks for posting your code too. It’s easy to follow along.

This is one of the first projects using new MQTT API, congrats!

-B

1 Like