Even though i am mentioning everything in the beginning then too m getting out of scope error. Code Reference :- https://noisycarlos.com/project/how-to-turn-your-computer-on-and-off-remotely/ I dont know what wwrong here. I am using ESP8266 module. Please help.
#include <ESP8266WiFi.h>
const char* ssid = <YOUR WIFI NAME>;
const char* password = <YOUR WIFI PASSWORD>;
const char ON = '+',
OFF = '-',
SLEEP = '/',
AUTO = 'A';
const int pinPowerLightOut = D1,
pinPowerLightIn = D5,
pinPowerButton = D6,
pinResetButton = D7,
buttonPressTime = 250;
char currentStatus = OFF;
char lightStatus = AUTO;
unsigned long autoDisconnectTime = 0,
powerButtonReleaseTime = 0;
WiFiServer wifiServer(80);
WiFiClient client;
unsigned long lastOn = 0,
lastOff = 0;
void changeStatusTo(char newStatus)
{
if(newStatus != currentStatus)
{
Serial.print("Status changed to ");
Serial.println(newStatus);
}
currentStatus = newStatus;
}
void recordLightStatusChange(bool lightIn)
{
if(lightIn)
lastOn = millis();
else
lastOff = millis();
if(lightStatus == AUTO);
digitalWrite(pinPowerLightOut, lightIn);
}
void statusChange()
{
bool currentLightStatus = !digitalRead(pinPowerLightIn);
if(lastOn != 0 && lastOff != 0)
{
if((millis() < lastOn + 4000) && (millis() < lastOff + 4000))
{
changeStatusTo(SLEEP);
recordLightStatusChange(currentLightStatus);
return;
}
}
if(currentLightStatus)
changeStatusTo(ON);
else
changeStatusTo(OFF);
recordLightStatusChange(currentLightStatus);
}
void Print(String toPrint)
{
client.print(toPrint);
Serial.print(toPrint);
}
void Println(String toPrint)
{
client.println(toPrint);
Serial.println(toPrint);
}
void processCommand(String command)
{
Serial.print(F("Received command "));
Serial.println(command);
if(command.equalsIgnoreCase(F("status")))
{
Print("Status: ");
if(currentStatus == ON)
Println("On");
else if(currentStatus == OFF)
Println("Off");
else if(currentStatus == SLEEP)
Println("Sleeping");
}
else if(command.equalsIgnoreCase(F("on")))
{
if(currentStatus != ON)
{
Println("Power Button Pressed (powering on)");
pressPowerButton(buttonPressTime);
}
else
Println("Machine is already on (button not pressed)");
}
else if(command.equalsIgnoreCase(F("off")))
{
if(currentStatus == ON)
{
Println("Power Button Pressed (powering off)");
pressPowerButton(buttonPressTime);
}
else
Println("Machine is already off (button not pressed)");
}
else if(command.equalsIgnoreCase(F("reset")))
{
unsigned long releaseTime = millis() + 500;
digitalWrite(pinResetButton, HIGH);
while(millis() < releaseTime)
{}
digitalWrite(pinResetButton, LOW);
Println("Reset Button Pressed");
}
else if(command.equalsIgnoreCase(F("force off")))
{
if(currentStatus != OFF)
{
Print("Holding Power Button for 10 seconds...");
pressPowerButton(10000);
}
}
else if(command.equalsIgnoreCase(F("pressPwr")))
{
digitalWrite(pinPowerButton, HIGH);
Println("Power Button Pressed");
}
else if(command.equalsIgnoreCase(F("releasePwr")))
{
digitalWrite(pinPowerButton, LOW);
Println("Power Button Released");
}
else if(command.equalsIgnoreCase(F("light on")))
{
setLightStatus(ON);
}
else if(command.equalsIgnoreCase(F("light off")))
{
setLightStatus(OFF);
}
else if(command.equalsIgnoreCase(F("light auto")))
{
setLightStatus(AUTO);
}
}
void setLightStatus(char newStatus)
{
if(newStatus == ON)
{
digitalWrite(pinPowerLightOut, HIGH);
Println("Light On");
return;
}
if(newStatus == OFF)
{
digitalWrite(pinPowerLightOut, LOW);
Println("Light Off");
return;
}
if(newStatus == AUTO)
{
Println("Light Set to Auto");
setLightStatus(currentStatus);
}
}
void pressPowerButton(int duration)
{
powerButtonReleaseTime = millis() + (duration);
digitalWrite(pinPowerButton, HIGH);
}
void runRoutineChecks()
{
checkIfPowerNeedsToRelease();
if((millis() > lastOn + 4000) && (millis() > lastOff + 4000))
statusChange();
}
void checkIfPowerNeedsToRelease()
{
if(powerButtonReleaseTime != 0)
{
if(millis() >= powerButtonReleaseTime)
{
digitalWrite(pinPowerButton, LOW);
powerButtonReleaseTime = 0;
Println("Power Button Released");
}
}
}
/////////////----------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
pinMode(pinPowerLightIn, INPUT_PULLUP);
pinMode(pinPowerLightOut, OUTPUT);
pinMode(pinPowerButton, OUTPUT);
pinMode(pinResetButton, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pinPowerLightIn), statusChange, CHANGE);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting..");
}
Serial.print("Connected to WiFi. IP:");
Serial.println(WiFi.localIP());
wifiServer.begin();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
runRoutineChecks();
client = wifiServer.available();
if (client)
{
while (client.connected())
{
String comm = "";
while (client.available()>0)
{
char c = client.read();
autoDisconnectTime = millis() + 60000;
if(c == ';')
{
processCommand(comm);
comm = "";
}
else
{
comm.concat(c);
}
}
runRoutineChecks();
if(autoDisconnectTime > 1000 && millis() > autoDisconnectTime)
{
Serial.println("Client timeout");
client.stop();
}
}
client.stop();
Serial.println("Client disconnected");
autoDisconnectTime = 0;
}
}
The ERROR :-
NODEMCU:2:30: error: 'D1' was not declared in this scope; did you mean 'y1'?
2 | const int pinPowerLightOut = D1,
| ^~
| y1
C:\Users\wahid\Documents\Arduino\NODEMCU\NODEMCU.ino: In function 'void statusChange()':
NODEMCU:61:42: error: 'pinPowerLightIn' was not declared in this scope; did you mean 'pinPowerLightOut'?
61 | bool currentLightStatus = !digitalRead(pinPowerLightIn);
| ^~~~~~~
| pinPowerLightOut
C:\Users\wahid\Documents\Arduino\NODEMCU\NODEMCU.ino: In function 'void processCommand(String)':
NODEMCU:118:24: error: 'buttonPressTime' was not declared in this scope
118 | pressPowerButton(buttonPressTime);
| ^~~~~~~
NODEMCU:130:24: error: 'buttonPressTime' was not declared in this scope
130 | pressPowerButton(buttonPressTime);
| ^~~~~~~
NODEMCU:140:18: error: 'pinResetButton' was not declared in this scope
140 | digitalWrite(pinResetButton, HIGH);
| ^~~~~~
NODEMCU:158:18: error: 'pinPowerButton' was not declared in this scope; did you mean 'pressPowerButton'?
158 | digitalWrite(pinPowerButton, HIGH);
| ^~~~~~
| pressPowerButton
NODEMCU:163:18: error: 'pinPowerButton' was not declared in this scope; did you mean 'pressPowerButton'?
163 | digitalWrite(pinPowerButton, LOW);
| ^~~~~~
| pressPowerButton
C:\Users\wahid\Documents\Arduino\NODEMCU\NODEMCU.ino: In function 'void pressPowerButton(int)':
NODEMCU:212:16: error: 'pinPowerButton' was not declared in this scope; did you mean 'pressPowerButton'?
212 | digitalWrite(pinPowerButton, HIGH);
| ^~~~~~
| pressPowerButton
C:\Users\wahid\Documents\Arduino\NODEMCU\NODEMCU.ino: In function 'void checkIfPowerNeedsToRelease()':
NODEMCU:232:20: error: 'pinPowerButton' was not declared in this scope; did you mean 'pressPowerButton'?
232 | digitalWrite(pinPowerButton, LOW);
| ^~~~~~
| pressPowerButton
C:\Users\wahid\Documents\Arduino\NODEMCU\NODEMCU.ino: In function 'void setup()':
NODEMCU:246:11: error: 'pinPowerLightIn' was not declared in this scope; did you mean 'pinPowerLightOut'?
246 | pinMode(pinPowerLightIn, INPUT_PULLUP);
| ^~~~~~~
| pinPowerLightOut
NODEMCU:248:11: error: 'pinPowerButton' was not declared in this scope; did you mean 'pressPowerButton'?
248 | pinMode(pinPowerButton, OUTPUT);
| ^~~~~~
| pressPowerButton
NODEMCU:249:11: error: 'pinResetButton' was not declared in this scope
249 | pinMode(pinResetButton, OUTPUT);
| ^~~~~~
exit status 1
'D1' was not declared in this scope; did you mean 'y1'?