/* NodeMCU remote servo controller * by Juho Kostet @ Mehackit * Made for the course "Robotiikka Tulevaisuudessa". */ // Servo library #include Servo servo; // WiFi library #include // WiFi credentials, change these to match your network const char* ssid = "YourWiFiName"; const char* password = "YourWiFiPassword"; WiFiServer server(80); void setup() { /* About pins: * NodeMCU has pins labeled different vs Arduino. * It's probably easier to use them directly vs * using analogRead(number). * * Google "NodeMCU pin map" for matching GPIO numbers. * digitalRead(D1) == digitalRead(5) for GPIO5. */ servo.attach(D1); /* Setup Serial connection for NodeMCU, * note the higher baud rate vs Arduino standard 9600. */ Serial.begin(115200); delay(10); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); server.begin(); // Print the IP address on the serial monitor Serial.print("Browse to this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); Serial.println(""); } void loop() { // 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()) { delay(1); } // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); int degree = 0; // Turn servo according to request /Req= if (request.indexOf("/Req") != -1) { String getReq = request.substring(request.indexOf("/Req")); degree = getReq.substring((getReq.indexOf('=') + 1)).toInt(); servo.write(degree); } // Return respose with html content (visible control page) client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // space between response header and content client.println(""); client.println(""); client.println("
"); client.println("

Remote light switch!



"); client.print("Servo is set to: "); client.print(degree); client.println("

"); /* Change these to set what the buttons say * and to which degree they turn the servo to. */ String btnText1 = " 0 degrees"; int btnDeg1 = 0; String btnText2 = " 90 degrees"; int btnDeg2 = 90; String btnText3 = "180 degrees"; int btnDeg3 = 180; client.println("
"); client.println("
"); client.println("
"); client.println("
"); client.println(""); delay(1); Serial.println("Client disonnected"); Serial.println(""); }