Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By krishnapriya Neema
#75210 I am designing "Smart door lock system" using nodemcu and android application.
But During the compilation time my nodemcu returns an error that is:

Arduino: 1.8.5 (Windows 8), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (3M SPIFFS), v2 Lower Memory, Serial1, None, Only Sketch, 115200"

Smart_Door:20: error: 'servo' does not name a type

Multiple libraries were found for "Servo.h"
Used:
exit status 1
'servo' does not name a type

My Code :

#include <ESP8266WiFi.h>
#include <Servo.h>

const char* ssid = "ssid name";
const char* password = "password";

Servo servo;//create servo object to control a servo 12 servo
//object can be created on most boards

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
servo.attach(2); // attaches the servo on GPIO2 to the servo
object

//int ledPin = 13;

void setup() {
Serial.begin(115200);
delay(1000);
//connect to wifi network
Serial.println();
Serial.println();
Serial.print("Connecting to ..");
Serial.print(ssid);
WiFi.begin(ssid);
while(WiFi.status()!= WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" ");
Serial.println("Wifi Connected");
//Start Server
server.begin();
Serial.println("server started at..");
Serial.println(WiFi.localIP());
}

void loop() {
int pos; //to control servo motor
//check if a client has connected
WiFiClient client = server.available();
if(!client) {
return;
}
//wait until the client sends some data
Serial.println("new client");
while(!client.available()) {
client.setNoDelay(1);
}
//Read the first line of the request
String req = client.readStringUntil('/r');
Serial.println(req);
client.flush();
//match the request for the angle movement of the servo motor
//for door lock
if(req.indexOf("/lock/0") != -1) {
//goes from 0 degrees to 180 degrees
for(pos = 0; pos <= 180; pos += 1) {
//tell servo to go to positon in variable 'pos'
servo.write(pos);
//wait 15ms for the servo to reach the position
delay(15);
}
} else if(req.indexOf("/unlock/1") != -1) {
//for unlock
//goes from 180 degrees to 0 degrees
for(pos = 0; pos <= 180; pos -= 1) {
//tell servo to go to positon in variable 'pos'
servo.write(pos);
//wait 15ms for the servo to reach the position
delay(15);
}
} else {
Serial.println("invalid request");
client.stop();
return;
}
//return the response
client.println("HTTP/1.1 200 OK");
client.println("Content_Type: text/html");
client.println("");//do not forget this one
client.print("Door is");
if(pos == 180) {
client.print("Unlock");
} else {
client.print("lock");
}
Serial.println("client disconnected");
//the client will actually be disconnected
//when the function returns & 'client' object is destroyed
}


Please give me your suggestions.
User avatar
By lethe
#75244
krishnapriya Neema wrote:Smart_Door:20: error: 'servo' does not name a type

That line tells you where the error is: line 20
And here's your issue:
servo.attach(2); // attaches the servo on GPIO2 to the servo object

You cannot call methods outside of a function, you can only declare variables.
Move that line to setup().