Read the status of the digital ports with nodemcu ESP8266

Read the status of the digital ports with nodemcu ESP8266

I have a project to know the state of 5 “HIGH / LOW” motors, in each engine I have an NC contactor from where I get the signal and take it to my Nodemcu:

GPIO4-D2
GPIO5-D3
GPIO12-D6
GPIO13-D7
GPIO14-D5

I just need to know the state of each one to make a trigger!

The problem is in communication stability:
3739251] MQTT connect failed, error -1
[3747531] Connected
[3778614] Disconnected
[3778614] Connecting to mqtt.mydevices.com:1883
[3797301] Connected
[3852472] Disconnected
[3852473] Connecting to mqtt.mydevices.com:1883
[3869833] Connected
[5026533] Disconnected
[5026533] Connecting to mqtt.mydevices.com:1883
[5056995] MQTT connect failed, error -1
[5088232] MQTT connect failed, error -1
[5119648] MQTT connect failed, error -1
[5150880] MQTT connect failed, error -1
[5182289] MQTT connect failed, error -1

My wireless network is normal and the code I’m using is:

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

//defines - mapeamento de pinos do NodeMCU
#define D0    16
#define D1    5
#define D2    4
#define D3    0
#define D4    2
#define D5    14
#define D6    12
#define D7    13
#define D8    15
#define D9    3
#define D10   1

int x;
int bomba1 ;
int bomba2 ;
int bomba3 ;
int bomba4 ;
int bomba5 ;

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

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

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(D1, INPUT);  //INPUT_PULLUP
  pinMode(D2, INPUT);
  pinMode(D5, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);
  //delay(1000);
}

void loop() {
  Cayenne.loop();
  
  //Status da bomba1
  int valorD2 = digitalRead(D2);
  if (valorD2 == LOW){
    bomba2 = 1;
    //Serial.println("Bomba1:Liga");
  }
  else{
    bomba2 = 0;
    //Serial.println("Bomba1:Desliga");
  }
    
  //Status da bomba2
  int valorD1 = digitalRead(D1);
  if (valorD1 == LOW){
    bomba1 = 1;
    //Serial.println("Bomba2:Liga");
  }
  else{
    bomba1 = 0;
    //Serial.println("Bomba2:Desliga");
  }
  
//Status da bomba3
  int valorD5 = digitalRead(D5);
  if (valorD5 == LOW){
    bomba3 = 1;
    //Serial.println("Bomba3:Liga");
  }
  else{
    bomba3 = 0;
    //Serial.println("Bomba3:Desliga");
  }
  
//Status da bomba4
  int valorD6 = digitalRead(D6);
  if (valorD6 == LOW){
    bomba4 = 1;
    //Serial.println("Bomba4:Liga");
  }
  else{
    bomba4 = 0;
    //Serial.println("Bomba4:Desliga");
  }
  
//Status da bomba5
  int valorD7 = digitalRead(D7);
  if (valorD7 == LOW){
    bomba5 = 1;
    //Serial.println("Bomba5:Liga");
  }
  else{
    bomba5 = 0;
    //Serial.println("Bomba5:Desliga");
  }
}

// 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);
}

// 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");
}
CAYENNE_OUT(1)
{
  Cayenne.virtualWrite(1, bomba1, "digital_sensor", "d");
}
CAYENNE_OUT(2)
{
  Cayenne.virtualWrite(2, bomba2, "digital_sensor", "d");
}
CAYENNE_OUT(3)
{
  Cayenne.virtualWrite(3, bomba3, "digital_sensor", "d");
}
CAYENNE_OUT(4)
{
  Cayenne.virtualWrite(4, bomba4, "digital_sensor", "d");
}
CAYENNE_OUT(5)
{
  Cayenne.virtualWrite(5, bomba5, "digital_sensor", "d");
}

My protoboard:

There is currently a problem that is being worked on.

Thanks for the feedback.

It is stable today!

I took advantage of and made some changes in the code to send the state of the ports only when they are changed:

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

//defines - mapeamento de pinos do NodeMCU
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
#define D9 3
#define D10 1

int previousState = -1;
int currentState = -1;
int previousState2 = -1;
int currentState2 = -1;
int previousState3 = -1;
int currentState3 = -1;
int previousState4 = -1;
int currentState4 = -1;
int previousState5 = -1;
int currentState5 = -1;

// WiFi network info.
char ssid = “xxxx”;
char wifiPassword = “xxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxx”;
char password = “xxxxxxx”;
char clientID = “xxxxxx”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D5, INPUT);
pinMode(D6, INPUT);
pinMode(D7, INPUT);
//delay(1000);
}

void loop() {
Cayenne.loop();

currentState = digitalRead(D2);
if (currentState != previousState) {
Cayenne.virtualWrite(1, currentState, “digital_sensor”, “d”);
previousState = currentState;
}
currentState2 = digitalRead(D1);
if (currentState2 != previousState2) {
Cayenne.virtualWrite(2, currentState2, “digital_sensor”, “d”);
previousState2 = currentState2;
}
currentState3 = digitalRead(D5);
if (currentState3 != previousState3) {
Cayenne.virtualWrite(3, currentState3, “digital_sensor”, “d”);
previousState3 = currentState3;
}
currentState4 = digitalRead(D6);
if (currentState4 != previousState4) {
Cayenne.virtualWrite(4, currentState4, “digital_sensor”, “d”);
previousState4 = currentState4;
}
currentState5 = digitalRead(D7);
if (currentState5 != previousState5) {
Cayenne.virtualWrite(5, currentState5, “digital_sensor”, “d”);
previousState5 = currentState5;
}
}

1 Like