Setting pin modes in a custom library

Hello, I am currently trying to set two input pins for a wireless project I am working on and everything works except the read switch function no matter what pin I set it to read on the server ESP8266 NodeMCU it always reads off GPIO 0. I have tried everything I can think of but I'm assuming I messed something up in the library I created. Any help would be very much appreciated!

CPP File

#include "Arduino.h"

#include "ReadSensors.h"

ReadSensors::ReadSensors(int pin, int pin1){

_pin = pin;

_pin1 = pin1;

}

  void ReadSensors::pinSetup(){

    pinMode(_pin, INPUT);

    pinMode(_pin1, INPUT_PULLUP);

  }

  int ReadSensors::readSwitch() {

    int _switchState = digitalRead(_switchPin);

    return _switchState;

    }

  float ReadSensors::readSensor(){

    float _sensorValue = analogRead(_pin);

    return _sensorValue;

  }

.h file

#ifndef ReadSensors_h

#define ReadSensors_h

#include "Arduino.h"

class ReadSensors

{

public:

ReadSensors(int pin, int pin1);

    void pinSetup();

    int readSwitch(void);

    float readSensor(void);

private:

int _switchPin;

int _pin;

int _pin1;

float _sensorValue;

int _switchState;

};

#endif

Server code

#include <ReadSensors.h>

#include <ESP8266WiFi.h>

#include "ESPAsyncWebServer.h"

// Set your access point network credentials

const char* ssid = "ESP8266-Access-Point";

const char* password = "123456789";

ReadSensors ReadSensors(A0, 9);

const int Relay1 = 5;

const int Relay2 = 4;

// Create AsyncWebServer object on port 80

AsyncWebServer server(80);

String readTemp() {

return String(ReadSensors.readSwitch());

//return String(1.8 * bme.readTemperature() + 32);

}

String readHumi() {

return String(ReadSensors.readSensor());

}

//String readPres() {

//return String(bme.readPressure() / 100.0F);

//}

void setup(){

// Serial port for debugging purposes

Serial.begin(115200);

Serial.println();

pinMode(Relay1, OUTPUT);

pinMode(Relay2, OUTPUT);

// Setting the ESP as an access point

Serial.print("Setting AP (Access Point)…");

// Remove the password parameter, if you want the AP (Access Point) to be open

WiFi.softAP(ssid, password);

IPAddress IP = WiFi.softAPIP();

Serial.print("AP IP address: ");

Serial.println(IP);

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){

request->send_P(200, "text/plain", readTemp().c_str());});

server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){

request->send_P(200, "text/plain", readHumi().c_str());});

//server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){

//request->send_P(200, "text/plain", readPres().c_str());

//});

// Start server

server.begin();

}

void loop(){

int switchState = ReadSensors.readSwitch();

float sensorValue = ReadSensors.readSensor();

Serial.print("Switch State: ");

Serial.println(switchState);

Serial.print("Sensor Value: ");

Serial.println(sensorValue);

if(switchState == 1 || sensorValue <= 300){

digitalWrite(Relay1, HIGH);

digitalWrite(Relay2, HIGH);

}

else{

digitalWrite(Relay1, LOW);

digitalWrite(Relay2, LOW);

}

delay(200);

}

Please edit your code and add in code tags.

In general, you can't use Arduino functions like pinMode() in constructors, because constructors are executed at the very start of a program, before other important "peripheral initialization" stuff has happened.

I can't tell for sure whether that's what has happened in your code, or not, due to the formatting.

The general "fix" is to have a separate "begin" member function that must be explicitly called from Arduino-level code (eg in setup())

DON'T use pinMode() on a pin used only for analogRead().

You have THREE pin variables but you only initialize two of them. Then you use the third.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.