Benny EstesHashem AbuBaker
Published © GPL3+

MQTT + ESP8266 + BMP180 + TSL2561 + Cayenne

Read data from two of the most popular sensors out there. Temperature, pressure, and luminosity.

IntermediateShowcase (no instructions)2 hours5,497
MQTT + ESP8266 + BMP180 + TSL2561 + Cayenne

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Cayenne
myDevices Cayenne
MQTT
MQTT

Story

Read more

Code

Cayenne-MQTT-ESP8266_BMP180_TSL2561

C/C++
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

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

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "password";

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

unsigned long lastMillis = 0;

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_TSL2561_U.h>

Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
const int address = TSL2561_ADDR_FLOAT;
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(address, 12345);


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  Wire.begin(0, 2); // SDA, SDL
  if (!bmp.begin())
  {
    CAYENNE_LOG("No BMP sensor detected");
    while (1);
  }
  if (!tsl.begin())
  {
    CAYENNE_LOG("No TSL2561 detected");
    while (1);
  }

  tsl.enableAutoRange(true);
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */
}

void loop() {
  Cayenne.loop();

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

    sensors_event_t event;
    tsl.getEvent(&event);
    float pressure;
    bmp.getPressure(&pressure);
    float temperature;
    bmp.getTemperature(&temperature);

    Cayenne.virtualWrite(3, event.light, TYPE_LUMINOSITY, UNIT_LUX);
    Cayenne.virtualWrite(1, pressure, TYPE_BAROMETRIC_PRESSURE, UNIT_PASCAL);
    Cayenne.virtualWrite(2, temperature, TYPE_TEMPERATURE, UNIT_CELSIUS);
    //Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    //Cayenne.luxWrite(2, 700);
    //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
  }
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Credits

Tad.Dvor

Posted by Benny Estes

Comments