#include #include /* variables to enable communication by WiFi */ const char* ssid = "FritzBR 7530 2.4GHz"; const char* password = "N3wCyCl3&L1v3"; IPAddress wemosIP; //IP addres that wemos received //server to receive request from app WiFiServer httpServer(80); int counter = 0; /* variables to manage the servo*/ Servo servo; int pos_servo = 90; int min_servo = 0; int max_servo = 180; int pin_servo = D1; int speed_move = 3; int delay_t = 500; const int LEFT = 0; const int RIGHT = 1; void setup_servo(){ //define pinOut from microcontroller servo.attach(pin_servo); //move servo to initial position. servo.write(pos_servo); Serial.println("Servo ready."); } int move_servo(Servo s, int curr, int v_min, int v_max, int dir){ int new_pos = curr; if(dir == LEFT){ new_pos = curr - speed_move; if(new_pos >= v_min) s.write(new_pos); else Serial.println("Reach minimum..."); } else{ new_pos = curr + speed_move; if(new_pos <= v_max) s.write(new_pos); else Serial.println("Reach maximum..."); } return new_pos; } void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); wemosIP = WiFi.localIP(); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(wemosIP); Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(wemosIP); Serial.println("/"); } void setup_httpServer(){ /* Initialize the server */ /* Start the server */ httpServer.begin(); Serial.println("Server started..."); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void setup() { Serial.begin(9600); Serial.println(""); Serial.println("Initializing..."); setup_wifi(); setup_httpServer(); setup_servo(); Serial.println("MC ready."); } void loop() { // Check if a client has connected WiFiClient client = httpServer.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client..."); while(!client.available()){ delay(1000); } String request = client.readStringUntil('\r'); Serial.print("Value read from wifi: "); Serial.println(request); client.flush(); // Match the request String ret; if (request.indexOf("/servoA/left") != -1) { ret = "Move servo left..."; pos_servo = move_servo(servo, pos_servo, min_servo, max_servo, LEFT); } else if (request.indexOf("/servoA/right") != -1) { ret = "Move servo right..."; pos_servo = move_servo(servo, pos_servo, min_servo, max_servo, RIGHT); } else { ret = "Unknown request [" + request + "]"; } Serial.print("Last move: "); Serial.println(ret); counter++; // Return the response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println(""); // do not forget this one client.println(""); client.println(""); client.print("Counter: "); client.println(counter); client.print("Last move: "); client.println(ret); client.println(""); delay(1); Serial.println("Client disonnected"); Serial.println(""); }