To add a relay in arduino MQTT


#1

adding a relay in the MQTT arduino is not easy for me.

I tried in some ways. including the cayenn example.

it is adding but not working. carries the writing but remains as unacceptable.

/*
Cayenne Generic Digital Output Example

This sketch shows how to set up a Generic Digital Output with Cayenne.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:

  1. In the Cayenne Dashboard add a new Generic Digital Output widget.
  2. Select a virtual channel number for the widget.
  3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
  4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
  5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
  6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  7. Compile and upload this sketch.
  8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
    */

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

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

#define VIRTUAL_CHANNEL 8
#define ACTUATOR_PIN 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

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

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

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}


#2

can you try adding a new device using Bring Your Own thing and then copying the MQTT credentials into your code.
next add a custom button widget with channel 1 and change the foolowing lines in your code.

// This function is called when data is sent from Cayenne.
CAYENNE_IN(1)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}

#3

does not become active


#4

first add a new device using Bring Your Own Thing.


#5

I can not understand your suggestion.
Can you explain me better?

I’m having some difficulties with the new library.

thank you


#6

Did you try with the cell phone to turn on or off the relay ?
For me it works with cell phone but not with web platform. It says that the relay is unreachable.


#7

on your dashboard add a new device.
go to : add new > devices/widget > “Bring Your Own Thing”. this will open a window with your MQTT credential.
now copy this credentials into your code and upload. this will bring your device online.
next go to: add new > device/widget > custom widget > button > and fill all the details selecting channel 1.
next add this code at the bottom of your code to read the status of the button:

// This function is called when data is sent from Cayenne.
CAYENNE_IN(1)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}

#8

I tried that way. it is disabled. I read somewhere that may be only the dashboard in the camputer. which on the cell phone can work. so it must be something that may be resolved in the future.
I could not do this either.

thank you


#9

its working fine for me. can you give it a try atleast. follow step by step.


#10

Yes. I tried with your step by step. You already spoke in another post about it. If you want, I can send you the pictures along with the texts.


#11

in fact with this final part nor is this carrying the writing anymore.