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