Advertisement
Victoryus82

DoIt_udp_l293d_Alfa_GOOD

Jul 10th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. //-------------config-----------------
  5.  
  6. const char *ssid = "DoItEsp32_WIFI";  // Wifi AP neve
  7. const char *pw = "Wifi12345678"; // WifiAP jelszava
  8. int UdpCsomag;
  9.  
  10. IPAddress ip(192, 168, 1, 20); // ip címe
  11. IPAddress netmask(255, 255, 255, 0);
  12. WiFiUDP UDPServer;
  13. unsigned int UDPPort = 3000; //udp szerver portja
  14. const int packetSize = 128; // csomag mérete, 4 byte
  15. char packetBuffer[packetSize];
  16.  
  17. // Motor A
  18. int motor1Pin1 = 13; //sárga
  19. int motor1Pin2 = 12; //narancs
  20. int motor1En = 26; //zöld
  21.  
  22. // Motor B
  23. int motor2Pin1 = 14; //
  24. int motor2Pin2 = 27;
  25. int motor2En = 25; //kék
  26.  
  27. // Setting PWM properties
  28. const int freq = 30000;
  29. const int pwmChannel = 0;
  30. const int resolution = 8;
  31. int dutyCycle = 200;
  32.  
  33. void setup() {
  34.  
  35.   Serial.begin(115200);
  36.   Serial.println();
  37.   WiFi.softAPConfig(ip, ip, netmask); // WifiAp ip címének, netmaskjának beállítása
  38.   WiFi.softAP(ssid, pw); // WifiAp SSID és jelszó beállítása
  39.  
  40.   Serial.println("DoIt_Esp32 UDP teszt"); // mégis mire jó...
  41.   Serial.println((String)"SSID: " + ssid + "  PASS: " + pw);
  42.   Serial.println((String)"RoboRemo app must connect to " + ip.toString() + ":" + UDPPort);
  43.   UDPServer.begin(UDPPort);
  44.  
  45.   // sets the motors pins as outputs:
  46.   pinMode(motor1Pin1, OUTPUT);
  47.   pinMode(motor1Pin2, OUTPUT);
  48.   pinMode(motor2Pin1, OUTPUT);
  49.   pinMode(motor2Pin2, OUTPUT);
  50.  
  51.   // configure PWM functionalitites
  52.   ledcSetup(pwmChannel, freq, resolution);
  53.  
  54.   // attach the channel to the GPIO to be controlled
  55.     ledcAttachPin(motor1En, pwmChannel);
  56.     ledcAttachPin(motor2En, pwmChannel);
  57.  
  58.     digitalWrite(motor1Pin1, LOW);
  59.     digitalWrite(motor1Pin2, LOW);
  60.     digitalWrite(motor2Pin1, LOW);
  61.     digitalWrite(motor2Pin2, LOW);
  62.     ledcWrite(pwmChannel, 0);
  63.    
  64. }
  65.  
  66.  
  67. void loop() {  
  68.    handleUDPServer();
  69.    //delay(1);
  70. }
  71.  
  72. void handleUDPServer() {
  73.  int cb = UDPServer.parsePacket();
  74.   if (cb) {
  75.  
  76.     int len = UDPServer.read(packetBuffer, packetSize); //len változó a csomag hossza
  77.     if (len > 0) {
  78.       packetBuffer[len - 1] = '\0'; //betesz az utolsó karakternek egy lezárást
  79.       UdpCsomag = atoi(packetBuffer + 3);  //integert csinálok a packetbuffer tartalmából
  80.       Serial.println(UdpCsomag);
  81.  
  82.       // motor1
  83.       if (UdpCsomag > -200 && UdpCsomag <=200)
  84.       {
  85.         if (UdpCsomag > -200 && UdpCsomag <0)
  86.         {
  87.           //motor1 hátra
  88.           digitalWrite(motor1Pin1, HIGH);
  89.           digitalWrite(motor1Pin2, LOW);
  90.           ledcWrite(pwmChannel, UdpCsomag*-1);
  91.         }
  92.         else if (UdpCsomag == 0)
  93.         {
  94.           // motor1 kikapcs
  95.           digitalWrite(motor1Pin1, LOW);
  96.           digitalWrite(motor1Pin2, LOW);
  97.         }
  98.         else if (UdpCsomag > 0 && UdpCsomag <200)
  99.         {
  100.           //motor1 előre
  101.           digitalWrite(motor1Pin1, LOW);
  102.           digitalWrite(motor1Pin2, HIGH);
  103.           ledcWrite(pwmChannel, UdpCsomag);
  104.         }
  105.       }
  106.  
  107.       // motor2
  108.       if (UdpCsomag > 400 && UdpCsomag <800)
  109.       {
  110.         if (UdpCsomag > 400 && UdpCsomag <600)
  111.         {
  112.           //motor2 hátra
  113.           digitalWrite(motor2Pin1, HIGH);
  114.           digitalWrite(motor2Pin2, LOW);
  115.           ledcWrite(pwmChannel, (UdpCsomag-600)*-1);
  116.         }
  117.         else if (UdpCsomag == 600)
  118.         {
  119.           // motor2 kikapcs
  120.           digitalWrite(motor2Pin1, LOW);
  121.           digitalWrite(motor2Pin2, LOW);
  122.         }
  123.         else if (UdpCsomag > 600 && UdpCsomag <800)
  124.         {
  125.           //motor2 előre
  126.           digitalWrite(motor2Pin1, LOW);
  127.           digitalWrite(motor2Pin2, HIGH);
  128.           ledcWrite(pwmChannel, UdpCsomag-600);
  129.         }
  130.       }
  131.  
  132.  
  133.   }
  134.  }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement