Hey guys,
I'm new to the Arduino world and I'm trying to replicate this project I found on YouTube. However, when I try to compile the code, I get the message "WifiServer does not name a type" and a multitude of other errors. The library that is needed to be downloaded is downloaded and included within the code. My guess is there's some additional things I need to change in the code to make it work but it's not exactly covered in the video. Thanks again for the help.
This is the video I'm trying to follow:
These are the errors:
Arduino: 1.8.11 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"
Test:7:1: error: 'WifiServer' does not name a type
WifiServer server(8181);
^
C:\Users\scott\OneDrive\Documents\Arduino\Test\Test.ino: In function 'void setup()':
Test:12:3: error: 'Wifi' was not declared in this scope
Wifi.begin(myssid, mypassword);
^
Test:12:14: error: 'myssid' was not declared in this scope
Wifi.begin(myssid, mypassword);
^
Test:12:22: error: 'mypassword' was not declared in this scope
Wifi.begin(myssid, mypassword);
^
C:\Users\scott\OneDrive\Documents\Arduino\Test\Test.ino: In function 'void loop()':
Test:28:3: error: 'WifiClient' was not declared in this scope
WifiClient client = server.available();
^
Test:28:14: error: expected ';' before 'client'
WifiClient client = server.available();
^
Test:30:10: error: 'client' was not declared in this scope
while (client.connected()) {
^
exit status 1
'WifiServer' does not name a type
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
This is the code I have:
#include <ESP8266WiFi.h>
#define RELAY_PIN D1
const char* password = "mypassword";
const char* ssid = "myssid";
const int port = 8181;
WifiServer server(8181);
void setup() {
Serial.begin(9600);
Wifi.begin(myssid, mypassword);
while (Wifi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP Address:");
Serial.println(Wifi.localIP());
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
WifiClient client = server.available();
while (client.connected()) {
if (client.available()) {
uint8_t buf; //The data that is sent, just one byte
size_t length = 1;
client.read(&buf, length);
client.write(handleCmd(buf));
client.stop();
}
}
}
char *handleCmd(uint8_t cmd) {
Serial.print(cmd);
switch(cmd) {
case 49:
//49 is the ASCII value for 1, turn on lights
digitalWrite(RELAY_PIN, HIGH);
return "> Relay pin set to high \n";
case 48:
//48 is the ASCII value for 0, turn off lights
digitalWrite(RELAY_PIN, LOW);
return "> Relay pin set to low \n";
default:
return "> Send 1 for on, and 0 for off \n";
}
}