Esp8266 Declare pins

I am using an ESP8266 and do not know why I can't compile. I wanted to save pin D1 into a variable and use it to declare pinmode and when using pin. but with the ESP8266 there is no pin 1, 2, 3 ... etc and I can't find a conversion chart anywhere so I attempted the following.

char servoPin = D1;

to save pin # and

myservo.attach(servoPin);

but received an error at the first code location saying D1' was not declared in this scope

I also tried

myservo.attach(D1);

and received same error here.

So my answer lies in questions,

  1. is there a pin map from chars to ints
  2. Did I declare servoPin correctly or
  3. Am I not even close

here's my total code. It's a slave esp8266 using esp-now communication was working fine using embedded LED prior to needing pins.

/****************************************************************************************************************************************************
 *  TITLE: ESP-NOW Getting Started Examples
 *
 *  By Frenoy Osburn
 *  YouTube Video: https://youtu.be/_cNAsTB5JpM
 ****************************************************************************************************************************************************/

 /********************************************************************************************************************
  * Please make sure that you install the board support package for the ESP8266 boards.
  * You will need to add the following URL to your Arduino preferences.
  * Boards Manager URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
 ********************************************************************************************************************/
 
 /********************************************************************************************************************
 *  Board Settings:
 *  Board: "WeMos D1 R1 or Mini"
 *  Upload Speed: "921600"
 *  CPU Frequency: "80MHz"
 *  Flash Size: "4MB (FS:@MB OTA:~1019KB)"
 *  Debug Port: "Disabled"
 *  Debug Level: "None"
 *  VTables: "Flash"
 *  IwIP Variant: "v2 Lower Memory"
 *  Exception: "Legacy (new can return nullptr)"
 *  Erase Flash: "Only Sketch"
 *  SSL Support: "All SSL ciphers (most compatible)"
 *  COM Port: Depends *On Your System*
 *********************************************************************************************************************/
#include<ESP8266WiFi.h>
#include<espnow.h>
#include<Servo.h>

#define MY_NAME   "SLAVE_BED_WINDOW"

//define global variables 
bool openWindow;
bool closeWindow;
char servoPin = D1;
char stopOpen = D2;
char stopClose = D4;
Servo myservo;

//define structures 
struct __attribute__((packed)) dataPacket {
  bool structOpenWindow;
  bool structCloseWindow;
};

//Define methods
void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
  char macStr[18];
  dataPacket packet;  

  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);
  digitalWrite(D2, HIGH);
  
  Serial.println();
  Serial.print("Received data from: ");
  Serial.println(macStr);

  memcpy(&packet, data, sizeof(packet));

  openWindow = packet.structOpenWindow;
  closeWindow = packet.structCloseWindow;

  
  Serial.print("Sent Open Window: ");
  Serial.println(packet.structOpenWindow);
  Serial.print("Sent Close Window: ");
  Serial.println(packet.structCloseWindow);
  Serial.print("Open Window: ");
  Serial.println(openWindow);
  Serial.print("Close Window: ");
  Serial.println(closeWindow);
}

//Setup program 
void setup() {
  myservo.attach(servoPin);
  pinMode(stopOpen, INPUT_PULLUP);
  pinMode(stopClose, INPUT_PULLUP);
  //pinMode(2, OUTPUT);
  //pinMode(16, OUTPUT);
  //digitalWrite(2, LOW);
  Serial.begin(9600);     // initialize serial port

  openWindow = 0;
  closeWindow = 0;
  
  Serial.println();
  Serial.println();
  Serial.println();
  Serial.print("Initializing...");
  Serial.println(MY_NAME);
  Serial.print("My MAC address is: ");
  Serial.println(WiFi.macAddress());

  WiFi.mode(WIFI_STA);
  WiFi.disconnect();        // we do not want to connect to a WiFi network

  if(esp_now_init() != 0) {
    Serial.println("ESP-NOW initialization failed");
    return;
  }

  esp_now_register_recv_cb(dataReceived);   // this function will get called once all data is sent

  Serial.println("Initialized.");
}

//define program 
void loop() {
 delay(1000);
  Serial.print("Open Window: ");
  Serial.println(openWindow);
  Serial.print("Close Window: ");
  Serial.println(closeWindow);
 //Serial.println(openWindow);
 if(openWindow == 1) {
    myservo.write(180); 
    digitalWrite(2, HIGH);
    digitalWrite(16, LOW);
    digitalWrite(D2, HIGH);
    Serial.println("opening window");
    if(!digitalRead(stopOpen))  {
      openWindow = 0;
      Serial.println("Window open");
    }
  } else if(closeWindow == 1) {
    digitalWrite(16, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(D2, LOW);
    Serial.println("closing window");
    if(!digitalRead(stopClose))  {
      closeWindow = 0;
      Serial.println("Window closed");
    }
  } else {
    myservo.write(180); 
    Serial.println("no change ");
  } 
}

Page 7?

#define servopin D1

Not all ESP8266 boards use "D" numbers. The ones that do, don't agree on what "D" number goes with what GPIO number. If your chosen model of ESP8266 doesn't define "D" numbers, you would have to define them yourself.

Try using the number/name printed on the ESP8266 board. That will generally work.

And how is that substantially different to the OP's approach?

char servoPin =  D1;

The above line is compiled for my ESP8266 based NodeMCU Board.

Humm - compiles fine in my basement. Board is the same as your picture.
IDE 1.18.13
ESP8266 community version 3.0.2

1 Like

so am I correct that

char servoPin = D1;

should be the same as

int servoPin = 5;

well that's frustrating. It just compiled for me. I have found arduino not the most consistent

Yes!

char data type has the range: -128 to 127.
int data type has the range: -32768 to 32767.

The ESP8266 is not an Arduino so how well it works in the Arduino environment depends on whoever wrote the third-party 'core' that adds support for the processor.

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