ESP8266 (LinkNode D1) can't read digital switch state


#1

I have a PIR sensor working on this board with Cayenne but for some reason I just can’t get the state of my push button to be read. I would like for someone to have a look at the code below and let me know if this is a mistake in the code. If not I will keep on checking hardware.
Thanks!

#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "*********";
// Your network name and password.
char ssid[] = "******";
char password[] = "********";

// Virtual Pin of the Digital Motion Sensor widget.
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN1 V2

// Digital pin the motion sensor is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int motionSensorPin = 4;
const int frontDoor = 3;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
  //pinMode(frontDoor, INPUT);
  //digitalWrite(frontDoor, LOW);
}

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

int previousState = -1;
int currentState = -1;
int fdState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 250) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(motionSensorPin);
    fdState = digitalRead(frontDoor);
    //Serial.println("PIR State: ");
    //Serial.print(currentState);
    if (currentState != previousState) {
      Cayenne.virtualWrite(VIRTUAL_PIN1, fdState);
      Cayenne.virtualWrite(VIRTUAL_PIN, currentState);
      
      //Serial.println("PIR State: ");
      //Serial.print(currentState);
      previousState = currentState;
    }
        previousMillis = currentMillis;
  }
}

#2

I would never assume that your Arduino is going to start as all input pins, although they probably are. It’s just good programming to explicitly assign them with pinMode.

I think the issue you are facing is pin remapping.

Check this out - http://linksprite.com/wiki/index.php5?title=LinkNode_D1

#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"

// Virtual Pin of the Digital Motion Sensor widget.
#define VIRTUAL_PIN V1
#define VIRTUAL_PIN1 V2

#define MOTION_PIN D4 //GPIO4
#define DOOR_PIN D0 //GPIO3


// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "*********";
// Your network name and password.
char ssid[] = "******";
char password[] = "********";

int previousState = -1;
int currentState = -1;
int fdState = -1;
unsigned long previousMillis = 0;


void setup()
{
  pinMode(DOOR_PIN, INPUT);
  pinMode(MOTION_PIN, INPUT);

  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
}

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


void checkSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 250) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(MOTION_PIN);
    fdState = digitalRead(DOOR_PIN);
    //Serial.println("PIR State: ");
    //Serial.print(currentState);
    if (currentState != previousState) {
      Cayenne.virtualWrite(VIRTUAL_PIN1, fdState);
      Cayenne.virtualWrite(VIRTUAL_PIN, currentState);
      
      //Serial.println("PIR State: ");
      //Serial.print(currentState);
      previousState = currentState;
    }
        previousMillis = currentMillis;
  }
}

Cheers,

Craig


#3

I finally got it straight. I needed to set thepin mode to pinMode(MOTION_PIN, INPUT_PULLUP);


#4

Ahh… That’s great. Personally I try not to rely on integral pull-ups. You may want to add an external resistor instead.

Cheers,

Craig