MQTT Arduino UNO with ESP8266 as shield


#1

Hello,

Could somebdy review my code? I just conect my arduino to MQTT but temporary widgets not appear and if I add by custom widgets data is empty.
What I am doing wrong?
I can see comunicatin by serial.
Here my code

#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include “DHT.h”

// WiFi network info.
char ssid[] = “xequebo”;
char wifiPassword[] = “++++”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “8****++1”;
char password[] = “******+”;
char clientID[] = “98090b10-14d1-11e8-aa1d-++++++”;

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial

ESP8266 wifi(&EspSerial);

#define DHTPIN 7
#define DHTTYPE DHT11 // DHT 11
#define SENSOR_PIN 1 // fotoresistor
#define Azul 9 //PWM LED
#define Rojo 10 //PWM LED
#define Verde 11 //PWM LED
#define Amarillo 12
#define Blanco 13
DHT dht(DHTPIN, DHTTYPE);
#define VIRTUAL_CHANNEL 1 // Temperatura
#define VIRTUAL_CHANNEL2 2// Humedad
#define VIRTUAL_CHANNEL3 3// Luminosidad
#define VIRTUAL_CHANNEL4 4// Led
#define VIRTUAL_CHANNEL5 5// Led
#define VIRTUAL_CHANNEL6 6// Led
#define VIRTUAL_CHANNEL7 7// Led
#define VIRTUAL_CHANNEL8 8// Led

void setup()
{
dht.begin();
Serial.begin(9600);
delay(10);

// Set ESP8266 baud rate
EspSerial.begin(9600);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

}

void loop()
{
Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// 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);
}

CAYENNE_OUT(VIRTUAL_CHANNEL)
{
float t = dht.readTemperature();
Cayenne.virtualWrite(VIRTUAL_CHANNEL,t,TYPE_TEMPERATURE,UNIT_CELSIUS);
}

CAYENNE_OUT(VIRTUAL_CHANNEL2)
{

float h = dht.readHumidity();
Cayenne.virtualWrite(VIRTUAL_CHANNEL2,h,TYPE_RELATIVE_HUMIDITY,UNIT_PERCENT);
}

CAYENNE_OUT(VIRTUAL_CHANNEL3)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL3, analogRead(SENSOR_PIN),TYPE_LUMINOSITY,UNIT_LUX);
}

CAYENNE_IN(VIRTUAL_CHANNEL4)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL4, Azul, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(Azul, value);
}

CAYENNE_IN(VIRTUAL_CHANNEL5)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL5, Rojo, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(Rojo, value);
}

CAYENNE_IN(VIRTUAL_CHANNEL6)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL6, Verde, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(Verde, value);
}

CAYENNE_IN(VIRTUAL_CHANNEL7)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL7, Amarillo, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
digitalWrite(Amarillo, value);
}
CAYENNE_IN(VIRTUAL_CHANNEL8)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL8, Blanco, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
digitalWrite(Blanco, value);
}

If I monitor the serial port I can see comunication:
T+CIPSEND=1,40
[9903872] Publish: topic 1, channel 3, value 411, subkey lux, key lum
AT+CIPSEND=1,40
[9908895] Publish: topic 1, channel 0, value 9908895, subkey , key
AT+CIPSEND=1,40
[9913917] Connection ok
[9914233] Publish: topic 1, channel 1, value 26.000, subkey c, key temp
AT+CIPSEND=1,40
[9919531] Publish: topic 1, channel 2, value 53.000, subkey p, key rel_hum
AT+CIPSEND=1,40
[9924560] Publish: topic 1, channel 3, value 421, subkey lux, key lum
AT+CIPSEND=1,40
[9929584] Publish: topic 1, channel 0, value 9929584, subkey , key
AT+CIPSEND=1,40

But dashboard not works:

Advanced thanks fr yur support¡¡¡


#2

first try this simple code and see if the board gets connected to cayenne and you see a value widget on the dashboard

#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>

// WiFi network info.
char ssid[] = “xequebo”;
char wifiPassword[] = “++++”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “8****++1”;
char password[] = “******+”;
char clientID[] = “98090b10-14d1-11e8-aa1d-++++++”;

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial

unsigned long lastMillis = 0;

ESP8266 wifi(&EspSerial);

void setup()
{
Serial.begin(9600);

// Set ESP8266 baud rate
EspSerial.begin(9600);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
  if (millis() - lastMillis > 60000)
  {
    lastMillis = millis();
Cayenne.virtualWrite(2, millis());
}
}

#3

Thanks fr the suggestion, the widget still not appear even if I delete the device and try to add a new one, same behavior: device is detected but widget no appears…


#4

Serial comunnications still send messages but nothing reflects on dashboard:

AT+CIPSEND=1,40
[93933] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
AT+CIPSEND=1,40
[98960] Publish: topic 7, channel 65534, value 16000000, subkey , key
AT+CIPSEND=1,40
1kav1/8ca111e0-e1f6-11e7-9768-2143f8645AT+CIPSEND=1,40
011/things/2ff16c80-159d-11e8-822e-bbf38AT+CIPSEND=1,29
[104699] Publish: topic 5, channel 65534, value 1.1.0, subkey , key
AT+CIPSEND=1,40
[109722] Publish: topic 8, channel 65534, value ESP8266, subkey , key
[109759] Connection ok


#5

I think you have some minor mis types which is stopping the data from reaching Cayenne, even though the device connects.

Looks like you are trying to add DHT11 sensor, use this post as a guide to see how the text is formatted :slight_smile:

~Benny


#6

Thanks for the info, in addition I guess that other mistakes are present.
due I’m using old comands like:

CAYENNE_IN(VIRTUAL_CHANNEL8)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL8, Blanco, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
digitalWrite(Blanco, value);
}

instead the new ones, rigth? as well the #define comands used wronly…


#7

HEY¡¡
Now is starting to work, I re-plan and read carefully the docs and It succes¡¡:

This is the code That I use:

//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
#include “DHT.h”
// WiFi network info.
char ssid[] = “xequebo”;
char wifiPassword[] = “0”;

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

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
#define DHTPIN 7
#define DHTTYPE DHT11
ESP8266 wifi(&EspSerial);
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
delay(10);
dht.begin();
// Set ESP8266 baud rate
EspSerial.begin(9600);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
Cayenne.luxWrite(2, analogRead(1));
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
Cayenne.virtualWrite(3, t, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(4, h, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);
}

// 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(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}

I will continious with actuators, But NOW I AM ON THE RIGTH WAY¡¡