Advertisement
Victoryus82

udp_roboremo_dc_servo_0516

May 16th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3. #include <Adafruit_MotorShield.h>
  4. #include <Servo.h>
  5.  
  6. // --------------------------------[ config ]-------------------------------------
  7.   //--LED
  8.     //const int ledPin = 14; //teszt jelleggel, sck led
  9.  
  10.   //---SERVO motor megadása
  11.     Servo myservo;
  12.  
  13.   //--wifi config
  14.     #ifndef APSSID
  15.     #define APSSID "WEMOS"
  16.     #define APPSK  "nincs"
  17.     #endif
  18.     const char *ssid = APSSID;
  19.     const char *password = APPSK;
  20.     IPAddress ip(192, 168, 1, 10); // Wemos ip címe
  21.     IPAddress netmask(255, 255, 255, 0); //netmask
  22.  
  23.   //--udp config
  24.     WiFiUDP UDPTestServer;
  25.     unsigned int UDPPort = 4000; //udp szerver portja
  26.     const int packetSize = 20; // csomag mérete
  27.     char packetBuffer[packetSize];
  28.  
  29.   //--MOTOR shield object
  30.     Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  31.     Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // Select which 'port' M1, M2, M3 or M4. In this case, M1 Sima DC motor
  32.     //Adafruit_DCMotor *myMotor = AFMS.getMotor(2); // Select which 'port' M1, M2, M3 or M4. In this case, M2
  33.    
  34.  
  35. //----------------------------------[ SETUP ]-----------------------------------
  36. void setup() {
  37.    
  38.     Serial.begin(115200);
  39. //--MOTOR
  40.     AFMS.begin();  // create with the default frequency 1.6KHz  AFMS.begin(1000); OR with a different frequency, say 1KHz
  41.     myMotor->setSpeed(150); // Set the speed to start, from 0 (off) to 255 (max speed)
  42.     //myMotor->run(FORWARD);  
  43.     myMotor->run(RELEASE); // turn on motor
  44.  
  45.  
  46.   //--Servo motor GPIO
  47.     myservo.attach(13,600,2300);  //GPIO 5, meg min max, ezt nem értem, de ennyi volt a példában
  48.  
  49.   //--WIFI
  50.     WiFi.softAPConfig(ip, ip, netmask);
  51.     WiFi.softAP(ssid, password, 8); // WifiAp SSID és jelszó beállítás, csatorna, qurvára nem csinál semmit, a 20-al ezelőttit használja
  52.     Serial.println("Indul...");  //ezt sose írja ki, csak időhúzás
  53.     Serial.println("Wemos D1R2 UDP teszt 2019.05.15");
  54.     Serial.println((String)"SSID: " + ssid + "  PASS: " + password);  //ennek sincs sok értelme, úgy se ez a valős
  55.     Serial.println((String)"RoboRemo app udp adatok " + ip.toString() + ":" + UDPPort);
  56.   //--UDP start
  57.     UDPTestServer.begin(UDPPort);
  58. }
  59. //---------------------------------[ A lényeg :) ]-------------------------------
  60. void loop() {  
  61.    handleUDPServer();
  62.    delay(1);
  63. }
  64. //---------------------------------[ A program... ]------------------------------
  65. void handleUDPServer() {
  66.   int cb = UDPTestServer.parsePacket(); //A cb az üzenet mérete. Ha van üzenet, akkor megy tovább....
  67.     if (cb) {
  68.        int len = UDPTestServer.read(packetBuffer, packetSize); //len változó a csomag hossza
  69.           if (len > 0) {
  70.             packetBuffer[len-1] = '\0'; //betesz az utolsó karakternek egy lezárást
  71.               // Serial.println(len); len változó hossza
  72.               // Serial.println(packetBuffer);  pocketBuffer a csomag tartalma
  73.             int iValue = atoi(packetBuffer + 3);
  74.             Serial.println(iValue);  
  75.          
  76.            
  77.              if (iValue <= 2000 && iValue >= 1000)
  78.             {
  79.               myservo.write(iValue);
  80.  
  81.             } else
  82.               {
  83.          
  84.                 if (iValue >= 0 && iValue < 200)
  85.                   {
  86.                     myMotor->run(FORWARD);
  87.                     myMotor->setSpeed(iValue);
  88.                   }
  89.                 else if (iValue < 0 && iValue > -200)
  90.                   {
  91.                     myMotor->run(BACKWARD);
  92.                     myMotor->setSpeed(iValue*-1);
  93.                   }
  94.        
  95.               }        
  96.           }        
  97.            
  98.         }
  99.      
  100.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement