Advertisement
Victoryus82

rc_tank_udp_wemos_d1r2

Jul 29th, 2019
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <Adafruit_MotorShield.h>
  4.  
  5. int UdpCsomag;
  6.  
  7.  
  8. //--wifi config
  9.   #ifndef APSSID
  10.   #define APSSID "D1R2"
  11.   #define APPSK  "nincsnincs"
  12.   #endif
  13.   const char *ssid = APSSID;
  14.   const char *password = APPSK;
  15.   IPAddress ip(192, 168, 1, 10); // Wemos ip címe
  16.   IPAddress netmask(255, 255, 255, 0); //netmask
  17.  
  18. //--udp config
  19.   WiFiUDP UdpServer;
  20.   unsigned int UDPPort = 4000; //udp szerver portja
  21.   const int packetSize = 20; // csomag mérete
  22.   char packetBuffer[packetSize];
  23.  
  24.    
  25. //--MOTOR shield object
  26.    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  27.    Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
  28.    Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
  29.  
  30. void setup()
  31. {
  32.   Serial.begin(115200);  //start Serial
  33.  
  34.  
  35.   //--WIFI
  36.   WiFi.softAPConfig(ip, ip, netmask);
  37.   WiFi.softAP(ssid, password, 8);
  38.   Serial.println("Indul...");
  39.   Serial.println("Wemos D1R2 UDP teszt 2019.06.24");
  40.   Serial.println((String)"SSID: " + ssid + "  PASS: " + password);
  41.   Serial.println((String)"RoboRemo app udp adatok " + ip.toString() + ":" + UDPPort);
  42.  
  43.   //--UDP start
  44.   UdpServer.begin(UDPPort);
  45.  
  46.   //--MOTOR
  47.   AFMS.begin();  // create with the default frequency 1.6KHz  AFMS.begin(1000); OR with a different frequency, say 1KHz
  48.   myMotor->setSpeed(0); // Set the speed to start, from 0 (off) to 255 (max speed)
  49.   myMotor2->setSpeed(0);
  50.   //myMotor->run(FORWARD);  
  51.   myMotor->run(RELEASE); // turn on motor
  52.   myMotor2->run(RELEASE);
  53.  
  54.  
  55. }
  56.  
  57. void loop()
  58.  
  59. {
  60.  
  61.   int cb = UdpServer.parsePacket();
  62.   if (cb) {
  63.  
  64.     int len = UdpServer.read(packetBuffer, packetSize); //len változó a csomag hossza
  65.     if (len > 0) {
  66.  
  67.       packetBuffer[len - 1] = '\0'; //betesz az utolsó karakternek egy lezárást
  68.  
  69.       UdpCsomag = atoi(packetBuffer + 3);  //integert csinálok a packetbuffer tartalmából
  70.       Serial.println("Van udp kapcsolat!");
  71.       Serial.println(UdpCsomag);
  72.  
  73.   //motor 1
  74.   if (UdpCsomag >= -200 && UdpCsomag <= 200)
  75.         {
  76.            if (UdpCsomag >= -200 && UdpCsomag <0)
  77.             {  
  78.                myMotor->run(BACKWARD);
  79.                myMotor->setSpeed(UdpCsomag*-1);
  80.             }
  81.            else if (UdpCsomag == 0)
  82.             {
  83.               myMotor->setSpeed(UdpCsomag);
  84.             }
  85.  
  86.            else if (UdpCsomag > 0 && UdpCsomag <200)
  87.  
  88.             {              
  89.               myMotor->run(FORWARD);
  90.               myMotor->setSpeed(UdpCsomag);              
  91.             }
  92.        
  93.         }
  94.  
  95.     //motor 2
  96.   if (UdpCsomag >= 400 && UdpCsomag <= 800)
  97.         {
  98.            if (UdpCsomag >= 400 && UdpCsomag <600)
  99.             {  
  100.                myMotor2->run(FORWARD);
  101.                myMotor2->setSpeed((UdpCsomag-600)*-1);
  102.             }
  103.            else if (UdpCsomag == 600)
  104.             {
  105.               myMotor2->setSpeed(0);
  106.             }
  107.  
  108.            else if (UdpCsomag > 600 && UdpCsomag <800)
  109.  
  110.             {              
  111.               myMotor2->run(BACKWARD);
  112.               myMotor2->setSpeed(UdpCsomag-600);              
  113.             }
  114.        
  115.         }
  116.  
  117.   }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement