'D1' was not declared in this scope; did you mean 'y1'?

Even though mentioning the inputs after the library i am getting out of scope error.This is the code . Reference :- How to turn your computer on and off remotely | noisycarlos

Code :-

#include <ESP8266WiFi.h>
const int pinPowerLightOut = D1,
pinPowerLightIn = D5,
pinPowerButton = D6,
pinResetButton = D7,
buttonPressTime = 250;
const char* ssid = "duddu_CORE";
const char* password = "hamlet@2018";
const char ON = '+',
OFF = '-',
SLEEP = '/',
AUTO = 'A';

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'?

If you get an error message, please post it.
All of it.

What board are you compiling for?

Please remember to use code tags when posting code

So the board you are using hasn’t D1 defined, do it yourself

#define D1 1

You use plenty of <,> instead of <;> in your code.

Is the correct usage in this case

I have posted full error. Sry i am using ESP8266 Nodemcu

what do u mean sir?

I have posted full error plz see.

@frankruss
Installation and Troubleshooting is for Problems with the Arduino itself NOT your project. It says so in the description of the section. Therefore I have moved your post here.

You may want to read this before you proceed:-
how to get the best out of this forum

Hey Frankruss,
e.g. taking this part code,

#include <ESP8266WiFi.h>
const int pinPowerLightOut = D1,

pinPowerButton = D6,
pinResetButton = D7,
buttonPressTime = 250;
const char* ssid = "duddu_CORE";
const char* password = "hamlet@2018";
const char ON = '+',
OFF = '-',
SLEEP = '/',
AUTO = 'A';

When you end a line of code one should write
const int pinPowerLightOut = D1; // Mind the <;> delimitter and not a <,>

This kind of error happens a couple of times in your code.
const int pinPowerLightIn = D5;
etc...

I can't see an error - can you explain please?

Probably depending on what kind of compiler you are using,

e.g.


#include <ESP8266WiFi.h>
const int pinPowerLightOut = D1,

pinPowerButton = D6,
pinResetButton = D7,
buttonPressTime = 250;
const char* ssid = "duddu_CORE";
const char* password = "hamlet@2018";
const char ON = '+',
OFF = '-',
SLEEP = '/',
AUTO = 'A';

good practice would be,

const int pinPowerLightOut, pinPowerButton, pinResetButton, buttonPressTime;
pinPowerLightOut = D1;
pinPowerButton = D6;
pinResetButton = D7;
buttonPressTime = 250;

Or just like that,

const int pinPowerLightOut = D1;
const int pinPowerButton = D6;
const int pinResetButton = D7;
const int buttonPressTime = 250;

No.
Please be serious

I really would hope not.

I have already pointed out to you that OP’s use was legit, why fixate on non error?

...with another (very obvious) error!

Noooooooooo! Aaaaaa, I missed that, what a facepalm!

@killzone_kid ,
Indeed at former Ide versions I had to take these into account,

Wemos D1 FriendlyPinOut
/*
  Wemos d1 mini pin out

  Pin  Function                       ESP-8266 Pin
  TX  TXD                             TXD
  RX  RXD                             RXD
  A0  Analog input, max 3.3V input    A0
  D0  IO                              GPIO16
  D1  IO, SCL                         GPIO5
  D2  IO, SDA                         GPIO4
  D3  IO, 10k Pull-up                 GPIO0
  D4  IO, 10k Pull-up, BUILTIN_LED    GPIO2
  D5  IO, SCK                         GPIO14
  D6  IO, MISO                        GPIO12
  D7  IO, MOSI                        GPIO13
  D8  IO, 10k Pull-down, SS           GPIO15
  G   Ground                          GND
  5V  5V                              -
  3V3 3.3V                            3.3V
  RST Reset                           RST

  GPIO0: pull low during boot for flash mode (connects to a push button).
  Other than that, usable as GPIO but beware of waht you connect to it.
  GPIO1, 3: TX and RX (Serial). Also usable as GPIO if not using serial.
  GPIO2: is pulled up during boot & internal LED (active LOW).
  GPIO15: fixed external pull-down (for boot).
  GPIO4, 5, 12-14, 16: nothing special.
  Conclusion
  Stick to A0, D0, D5, D6 and D7.
*/
static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
type or paste code here

When I select the board "NodeMCU 1.0 (ESP-12E Module)" the sketch compiles with no errors and only one warning. That is with ESP8266 (3.0.2). What version of the core do you have installed and what EXACT board did you select?

Warning:

/Users/john/Documents/Arduino/sketch_jan06b/
sketch_jan06b.ino: 
In function 'void recordLightStatusChange(bool)':
/Users/john/Documents/Arduino/sketch_jan06b/
sketch_jan06b.ino:45:27: 
warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   45 |   if (lightStatus == AUTO);
      |         

Are you SURE you want to put that ';' there?!?