Advertisement
andretafta

NodeMCU Amica RC Bluino App

Apr 24th, 2024 (edited)
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************** WiFi Robot Remote Control Mode ********************/
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ArduinoOTA.h>
  5.  
  6. // connections for drive Motors
  7. int PWM_A = D1;
  8. int PWM_B = D2;
  9. int DIR_A = D3;
  10. int DIR_B = D4;
  11.  
  12. const int buzPin = D5;      // set digital pin D5 as buzzer pin (use active buzzer)
  13. const int ledPin = D8;      // set digital pin D8 as LED pin (use super bright LED)
  14. const int wifiLedPin = D0;  // set digital pin D0 as indication, the LED turn on if NodeMCU connected to WiFi as STA mode
  15.  
  16. String command;          // String to store app command state.
  17. int SPEED = 1023;        // 330 - 1023.
  18. int speed_Coeff = 3;
  19.  
  20. ESP8266WebServer server(80);      // Create a webserver object that listens for HTTP request on port 80
  21.  
  22. unsigned long previousMillis = 0;
  23.  
  24. String sta_ssid = "";      // set Wifi networks you want to connect to
  25. String sta_password = "";  // set password for Wifi networks
  26.  
  27.  
  28. void setup(){
  29.   Serial.begin(115200);    // set up Serial library at 115200 bps
  30.   Serial.println();
  31.   Serial.println("*WiFi Robot Remote Control Mode*");
  32.   Serial.println("--------------------------------------");
  33.  
  34.   pinMode(buzPin, OUTPUT);      // sets the buzzer pin as an Output
  35.   pinMode(ledPin, OUTPUT);      // sets the LED pin as an Output
  36.   pinMode(wifiLedPin, OUTPUT);  // sets the Wifi LED pin as an Output
  37.   digitalWrite(buzPin, LOW);
  38.   digitalWrite(ledPin, LOW);
  39.   digitalWrite(wifiLedPin, HIGH);
  40.    
  41.   // Set all the motor control pins to outputs
  42.   pinMode(PWM_A, OUTPUT);
  43.   pinMode(PWM_B, OUTPUT);
  44.   pinMode(DIR_A, OUTPUT);
  45.   pinMode(DIR_B, OUTPUT);
  46.  
  47.   // Turn off motors - Initial state
  48.   digitalWrite(DIR_A, LOW);
  49.   digitalWrite(DIR_B, LOW);
  50.   analogWrite(PWM_A, 0);
  51.   analogWrite(PWM_B, 0);
  52.  
  53.   // set NodeMCU Wifi hostname based on chip mac address
  54.   String chip_id = String(ESP.getChipId(), HEX);
  55.   int i = chip_id.length()-4;
  56.   chip_id = chip_id.substring(i);
  57.   chip_id = "wificar-" + chip_id;
  58.   String hostname(chip_id);
  59.  
  60.   Serial.println();
  61.   Serial.println("Hostname: "+hostname);
  62.  
  63.   // first, set NodeMCU as STA mode to connect with a Wifi network
  64.   WiFi.mode(WIFI_STA);
  65.   WiFi.begin(sta_ssid.c_str(), sta_password.c_str());
  66.   Serial.println("");
  67.   Serial.print("Connecting to: ");
  68.   Serial.println(sta_ssid);
  69.   Serial.print("Password: ");
  70.   Serial.println(sta_password);
  71.  
  72.   // try to connect with Wifi network about 10 seconds
  73.   unsigned long currentMillis = millis();
  74.   previousMillis = currentMillis;
  75.   while (WiFi.status() != WL_CONNECTED && currentMillis - previousMillis <= 10000) {
  76.     delay(500);
  77.     Serial.print(".");
  78.     currentMillis = millis();
  79.   }
  80.  
  81.   // if failed to connect with Wifi network set NodeMCU as AP mode
  82.   if (WiFi.status() == WL_CONNECTED) {
  83.     Serial.println("");
  84.     Serial.println("*WiFi-STA-Mode*");
  85.     Serial.print("IP: ");
  86.     Serial.println(WiFi.localIP());
  87.     digitalWrite(wifiLedPin, LOW);    // Wifi LED on when connected to Wifi as STA mode
  88.     delay(3000);
  89.   } else {
  90.     WiFi.mode(WIFI_AP);
  91.     WiFi.softAP(hostname.c_str());
  92.     IPAddress myIP = WiFi.softAPIP();
  93.     Serial.println("");
  94.     Serial.println("WiFi failed connected to " + sta_ssid);
  95.     Serial.println("");
  96.     Serial.println("*WiFi-AP-Mode*");
  97.     Serial.print("AP IP address: ");
  98.     Serial.println(myIP);
  99.     digitalWrite(wifiLedPin, HIGH);   // Wifi LED off when status as AP mode
  100.     delay(3000);
  101.   }
  102.  
  103.  
  104.   server.on ( "/", HTTP_handleRoot );       // call the 'handleRoot' function when a client requests URI "/"
  105.   server.onNotFound ( HTTP_handleRoot );    // when a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
  106.   server.begin();                           // actually start the server
  107.  
  108.   ArduinoOTA.begin();                       // enable to receive update/uploade firmware via Wifi OTA
  109. }
  110.  
  111.  
  112. void loop() {
  113.     ArduinoOTA.handle();          // listen for update OTA request from clients
  114.     server.handleClient();        // listen for HTTP requests from clients
  115.    
  116.       command = server.arg("State");          // check HTPP request, if has arguments "State" then saved the value
  117.       if (command == "F") Forward();          // check string then call a function or set a value
  118.       else if (command == "B") Backward();
  119.       else if (command == "R") TurnRight();
  120.       else if (command == "L") TurnLeft();
  121.       else if (command == "G") ForwardLeft();
  122.       else if (command == "H") BackwardLeft();
  123.       else if (command == "I") ForwardRight();
  124.       else if (command == "J") BackwardRight();
  125.       else if (command == "S") Stop();
  126.       else if (command == "V") BeepHorn();
  127.       else if (command == "W") TurnLightOn();
  128.       else if (command == "w") TurnLightOff();
  129.       else if (command == "0") SPEED = 330;
  130.       else if (command == "1") SPEED = 400;
  131.       else if (command == "2") SPEED = 470;
  132.       else if (command == "3") SPEED = 540;
  133.       else if (command == "4") SPEED = 610;
  134.       else if (command == "5") SPEED = 680;
  135.       else if (command == "6") SPEED = 750;
  136.       else if (command == "7") SPEED = 820;
  137.       else if (command == "8") SPEED = 890;
  138.       else if (command == "9") SPEED = 960;
  139.       else if (command == "q") SPEED = 1023;
  140. }
  141.  
  142. // function prototypes for HTTP handlers
  143. void HTTP_handleRoot(void){
  144.   server.send ( 200, "text/html", "" );       // Send HTTP status 200 (Ok) and send some text to the browser/client
  145.  
  146.   if( server.hasArg("State") ){
  147.      Serial.println(server.arg("State"));
  148.   }
  149. }
  150.  
  151. void handleNotFound(){
  152.   server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
  153. }
  154.  
  155. // function to move forward
  156. void Forward(){
  157.   digitalWrite(DIR_A, HIGH);
  158.   digitalWrite(DIR_B, HIGH);
  159.   analogWrite(PWM_A, SPEED);
  160.   analogWrite(PWM_B, SPEED);
  161. }
  162.  
  163. // function to move backward
  164. void Backward(){
  165.   digitalWrite(DIR_A, LOW);
  166.   digitalWrite(DIR_B, LOW);
  167.   analogWrite(PWM_A, SPEED);
  168.   analogWrite(PWM_B, SPEED);
  169. }
  170.  
  171. // function to turn right
  172. void TurnRight(){
  173.   digitalWrite(DIR_A, LOW);
  174.   digitalWrite(DIR_B, HIGH);
  175.   analogWrite(PWM_A, SPEED);
  176.   analogWrite(PWM_B, SPEED);
  177. }
  178.  
  179. // function to turn left
  180. void TurnLeft(){
  181.   digitalWrite(DIR_A, HIGH);
  182.   digitalWrite(DIR_B, LOW);
  183.   analogWrite(PWM_A, SPEED);
  184.   analogWrite(PWM_B, SPEED);
  185. }
  186.  
  187. // function to move forward left
  188. void ForwardLeft(){
  189.   digitalWrite(DIR_A, HIGH);
  190.   digitalWrite(DIR_B, HIGH);
  191.   analogWrite(PWM_A, SPEED);
  192.   analogWrite(PWM_B, SPEED/speed_Coeff);
  193. }
  194.  
  195. // function to move backward left
  196. void BackwardLeft(){
  197.   digitalWrite(DIR_A, LOW);
  198.   digitalWrite(DIR_B, LOW);
  199.   analogWrite(PWM_A, SPEED);
  200.   analogWrite(PWM_B, SPEED/speed_Coeff);
  201. }
  202.  
  203. // function to move forward right
  204. void ForwardRight(){
  205.   digitalWrite(DIR_A, HIGH);
  206.   digitalWrite(DIR_B, HIGH);
  207.   analogWrite(PWM_A, SPEED/speed_Coeff);
  208.   analogWrite(PWM_B, SPEED);
  209. }
  210.  
  211. // function to move backward left
  212. void BackwardRight(){
  213.   digitalWrite(DIR_A, LOW);
  214.   digitalWrite(DIR_B, LOW);
  215.   analogWrite(PWM_A, SPEED/speed_Coeff);
  216.   analogWrite(PWM_B, SPEED);
  217. }
  218.  
  219. // function to stop motors
  220. void Stop(){
  221.   digitalWrite(DIR_A, LOW);
  222.   digitalWrite(DIR_B, LOW);
  223.   analogWrite(PWM_A, 0);
  224.   analogWrite(PWM_B, 0);
  225. }
  226.  
  227. // function to beep a buzzer
  228. void BeepHorn(){
  229.   digitalWrite(buzPin, HIGH);
  230.   delay(150);
  231.   digitalWrite(buzPin, LOW);
  232.   delay(80);
  233. }
  234.  
  235. // function to turn on LED
  236. void TurnLightOn(){
  237.   digitalWrite(ledPin, HIGH);
  238. }
  239.  
  240. // function to turn off LED
  241. void TurnLightOff(){
  242.   digitalWrite(ledPin, LOW);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement