MQTT digital input (Cayenne-OUT)


#1

Hello guys, im a newbie to Cayenne & MQTT but learning slowly, I can connect a no/nc garage door contact to analogue pin to get a reading,but i would like to connect it via a digital input pin to the cayenne dashboard but with no luck, I would be grateful for any help, thanks in advance.

//                                           GENERIC ESP8266-------- Garage Door & Light (Control & Status)
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>           
#define VIRTUAL_PIN 5                      // Virtual Pin of the garage door status
#define VIRTUAL_CHANNELa 7         //Garage door switch
#define ACTUATOR_PINa 4                //Garage door switch
#define VIRTUAL_CHANNELb 8         //Garage light switch
#define ACTUATOR_PINb 2                //Garage light switch

//                            log on info removed
int inPin = 5;                           // pushbutton connected to ESP8266 pin 5
int value = 0;                           // variable to store the read value

unsigned long lastMillis = 0;
int channel();
void setup() {
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode( inPin, INPUT_PULLUP);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);  //Cayenne.begin(token);
}
void loop()  {
  Cayenne.loop();                                 // Cayenne.run();

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

CAYENNE_OUT(V5)                                      // Garage door status switch
{
  int val = digitalRead(inPin);                      // read the input pin
  Cayenne.virtualWrite(V5, val);
}

CAYENNE_IN(VIRTUAL_CHANNELa)                        // Garage door
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNELa, ACTUATOR_PINa, value);
  digitalWrite(ACTUATOR_PINa, value);               // Write the value received to the digital pin.
}

CAYENNE_IN(VIRTUAL_CHANNELb)                        //Garage light
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNELb, ACTUATOR_PINb, value);
  digitalWrite(ACTUATOR_PINb, value);               // Write the value received to the digital pin.
}

#2

what is the error you are getting on the serial monitor?


#3

There is no error on the serial monitor channel 5 is showing 0 & 1 as expected, but when I go to set a trigger on channel 5 I get a slider from -500 to +500 instead of a 0 or 1 option and have to set it to 0.5, so the light on channel 5 operates from a switch 0 or 1.


#4

if you are using a button on channel 5 then why is it showing as a slider in trigger?


#5

That’s a very good question !


#6

Have you got the answer for this question?


#7

I have no idea that’s y I started this thread in the first place.


#8

can you share some screenshot of the same? and also the code used.


#9

Hi thanks for your patience, I think the problem is in lines 43 - 47 or 8/9/20-22
when I pull pin 5 low the door widget changes from 1 to 0 but the open close widget on the same channel stays the same !.

//                                           LoLin ESP8266-------- Garage Door & Light (Control & Status)
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>            // ESP 8266

#define VIRTUAL_CHANNEL 5                      // Virtual Pin of the garage door status
#define DIGITAL_PIN 5                      //
#define VIRTUAL_CHANNELa 7                 //Garage door switch
#define ACTUATOR_PINa 4                    //Garage door switch
#define VIRTUAL_CHANNELb 8                 //Garage light switch
#define ACTUATOR_PINb 0                    //Garage light switch

char ssid[] = "X";               // WiFi network info.
char wifiPassword[] = "X";
char username[] = "X";
char password[] = "X";
char clientID[] = "9X";          //  Generic ESP8266
int inPin = 5;                           // pushbutton connected to digital pin 5
int value = 0;                           // variable to store the read value
int val = 0;
unsigned long lastMillis = 0;
int channel();
void setup() {
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(5, INPUT_PULLUP);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);  //Cayenne.begin(token);
}
void loop()  {
  Cayenne.loop();                                 // Cayenne.run();

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

CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  int val = digitalRead(inPin);                      // read the input pin 
  Cayenne.virtualWrite(DIGITAL_PIN , val);
}

CAYENNE_IN(VIRTUAL_CHANNELa)                        // Garage door
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNELa, ACTUATOR_PINa, value);
  digitalWrite(ACTUATOR_PINa, value);               // Write the value received to the digital pin.
}


CAYENNE_IN(VIRTUAL_CHANNELb)                        //Garage light
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNELb, ACTUATOR_PINb, value);
  digitalWrite(ACTUATOR_PINb, value);               // Write the value received to the digital pin.
}

PS how do I post code in a box with line No,s


#10

Delete the channel 5 widget first.
then change this:

to:

CAYENNE_OUT(5)
{
int val = digitalRead(inPin); // read the input pin
Cayenne.virtualWrite(5 , val, "digital_sensor", "d");
}

#11

That seems to have done the trick :smiley:
Thanks a lot for all your help .


#12

you can have a look at the Data types for Cayenne MQTT API


#13

If you highlight your code then hit the </> button above the text window it will add 4 spaces to every line which turns it in to preformatted text.