Cayenne noob needs help with NodeMCU code

I’m looking to replace an ageing IO-204 device from IOBridge as IOS11 no longer supports the app. It is being used to monitor the status of two doors (digital 1/0) and control two relays; each door.

I’m no code guy as hardware is my speciality. I’m very familiar with the ESP8266 boards and flashing them with Arduino, ESPEasy, Tasmota etc.

When it comes to writing code though, this 53yr old brain struggles!

Could anyone share some code to allow me to control two digital pins with a 1s pulse on the NodeMCU and read the status of two other pins so I can configure the dashboard to show whether the door is opened or closed. As far as hooking it up, that part I’m very comfortable with. The key criteria is that the output pulses for 1s on then off when activated and that the outputs don’t pulse when the NodeMCU is rebooted (opening or closing the door).

I’ve flashed the demo ESP8266 code to my NodeMCU and can see it in the Cayenne dashboard but merging code from multiple blog posts (1 and 2) I’ve read is a bit beyond my capabilities.

Any assistance would be appreciated.

Happy to add the project to my YouTube and Blog channels on completion.

welcome to cayenne community sir.

add a custom button widget with channel 1 and add this code.

  CAYENNE_IN(1)
{
  int x = getValue.asInt();
  if ( x == 1)
 {
  digitalWrite(4,  HIGH);
   delay(1000);
   digitalWrite(4,low);
 }

using below code it will add a two state widget to your cayenne dashboard:

if (millis() - lastMillis1 > 10000)
  {
    lastMillis1 = millis();
   int y = digitalRead(5);
   Cayenne.virtualWrite(2, y, "digital_sensor", "d");
   }

@shramik_salgaonkar Thank you so much for your prompt reply, much appreciated. Sorry, code is not my friend :frowning:

Where would I insert your suggestion into my sample code?

// 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[] = "wifiPassword";

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

unsigned long lastMillis = 0;

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

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();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, lastMillis);
		//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");
}
// 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 &lt;CayenneMQTTESP8266.h&gt;

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

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

unsigned long lastMillis = 0;

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

void loop() {
	Cayenne.loop();
   int y = digitalRead(5);

 if (millis() - lastMillis1 > 10000)
  {
    lastMillis1 = millis();
   Cayenne.virtualWrite(2, y, "digital_sensor", "d");
   }
    }
   CAYENNE_IN(1)
{
  int x = getValue.asInt();
  if ( x == 1)
 {
  digitalWrite(4,  HIGH);
   delay(1000);
   digitalWrite(4,low);
 }

Thank you! I’ll give that a try when I get back from holidays.

1 Like

Sorry, moved house and been off-grid buried in boxes for a while. Tried compiling for my NodeMCU this evening but get the following error using the above code.

exit status 1
'lastMillis1' was not declared in this scope

I suck at this. What does that mean?

sorry my mistake.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include &lt;CayenneMQTTESP8266.h&gt;

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

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

unsigned long lastMillis1 = 0;

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

void loop() {
	Cayenne.loop();
   int y = digitalRead(5);

 if (millis() - lastMillis1 > 10000)
  {
    lastMillis1 = millis();
   Cayenne.virtualWrite(2, y, "digital_sensor", "d");
   }
    }
   CAYENNE_IN(1)
{
  int x = getValue.asInt();
  if ( x == 1)
 {
  digitalWrite(4,  HIGH);
   delay(1000);
   digitalWrite(4,low);
 }

@shramik_salgaonkar Thanks for the continued assistance, much appreciated. Looks like one last error.

   digitalWrite(4,  HIGH);
   delay(1000);
   digitalWrite(4,low);
 }

"exit status 1
'low' was not declared in this scope"

Change low to LOW

2 Likes