-1

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'?
1
  • @Juraj m using Nodemcu ESP8266 which contains D0 to D8 pins ?
    – Frankruss
    Jan 13 at 7:39

1 Answer 1

0

Only for some esp8266 dev boards Dx pins are defined. The constants map esp module pin numbers to board's pin numbers (for example on most of them D1 is io 4).

If you have a dev board with Dx labels on the PCB, then select the right board in Tools menu.

For plain esp8266 module use pin numbers in code. If the author of the code had a Wemos D1 mini or a NodeMci dev board, use the Dx definitions in the header file to map the Dx pins to esp8266 pins.

Note that on esp8266 not all pins are general purpose IO. Some have limited usage, so use the same esp8266 pins as the author of the code.

5
  • I have used the same pins as the author. Can u explain the mapping part i dnt understand.THANKS.
    – Frankruss
    Jan 13 at 8:00
  • @Frankruss, if you have a NodeMcu, just select it in Tools menu. I wrote the answer before you revealed that you have a NodeMcu.
    – Juraj
    Jan 13 at 8:09
  • I have selected Generic ESP8266 module . should i select NodeMCU is yes then then which one?
    – Frankruss
    Jan 13 at 8:16
  • @Frankruss, yes select NodeMcu. the version you have or any which works
    – Juraj
    Jan 13 at 8:17
  • U r gr8 sir i can compile the code but while uploading i still get this error : esptool.py v3.0 Serial port COM8 the selected serial port _ does not exist or your board is not connected. @Juraj
    – Frankruss
    Jan 13 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.