Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WiFiUdp.h>
- //-------------config-----------------
- const char *ssid = "DoItEsp32_WIFI"; // Wifi AP neve
- const char *pw = "Wifi12345678"; // WifiAP jelszava
- int UdpCsomag;
- IPAddress ip(192, 168, 1, 20); // ip címe
- IPAddress netmask(255, 255, 255, 0);
- WiFiUDP UDPServer;
- unsigned int UDPPort = 3000; //udp szerver portja
- const int packetSize = 128; // csomag mérete, 4 byte
- char packetBuffer[packetSize];
- // Motor A
- int motor1Pin1 = 13; //sárga
- int motor1Pin2 = 12; //narancs
- int motor1En = 26; //zöld
- // Motor B
- int motor2Pin1 = 14; //
- int motor2Pin2 = 27;
- int motor2En = 25; //kék
- // Setting PWM properties
- const int freq = 30000;
- const int pwmChannel = 0;
- const int resolution = 8;
- int dutyCycle = 200;
- void setup() {
- Serial.begin(115200);
- Serial.println();
- WiFi.softAPConfig(ip, ip, netmask); // WifiAp ip címének, netmaskjának beállítása
- WiFi.softAP(ssid, pw); // WifiAp SSID és jelszó beállítása
- Serial.println("DoIt_Esp32 UDP teszt"); // mégis mire jó...
- Serial.println((String)"SSID: " + ssid + " PASS: " + pw);
- Serial.println((String)"RoboRemo app must connect to " + ip.toString() + ":" + UDPPort);
- UDPServer.begin(UDPPort);
- // sets the motors pins as outputs:
- pinMode(motor1Pin1, OUTPUT);
- pinMode(motor1Pin2, OUTPUT);
- pinMode(motor2Pin1, OUTPUT);
- pinMode(motor2Pin2, OUTPUT);
- // configure PWM functionalitites
- ledcSetup(pwmChannel, freq, resolution);
- // attach the channel to the GPIO to be controlled
- ledcAttachPin(motor1En, pwmChannel);
- ledcAttachPin(motor2En, pwmChannel);
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- ledcWrite(pwmChannel, 0);
- }
- void loop() {
- handleUDPServer();
- //delay(1);
- }
- void handleUDPServer() {
- int cb = UDPServer.parsePacket();
- if (cb) {
- int len = UDPServer.read(packetBuffer, packetSize); //len változó a csomag hossza
- if (len > 0) {
- packetBuffer[len - 1] = '\0'; //betesz az utolsó karakternek egy lezárást
- UdpCsomag = atoi(packetBuffer + 3); //integert csinálok a packetbuffer tartalmából
- Serial.println(UdpCsomag);
- // motor1
- if (UdpCsomag > -200 && UdpCsomag <=200)
- {
- if (UdpCsomag > -200 && UdpCsomag <0)
- {
- //motor1 hátra
- digitalWrite(motor1Pin1, HIGH);
- digitalWrite(motor1Pin2, LOW);
- ledcWrite(pwmChannel, UdpCsomag*-1);
- }
- else if (UdpCsomag == 0)
- {
- // motor1 kikapcs
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, LOW);
- }
- else if (UdpCsomag > 0 && UdpCsomag <200)
- {
- //motor1 előre
- digitalWrite(motor1Pin1, LOW);
- digitalWrite(motor1Pin2, HIGH);
- ledcWrite(pwmChannel, UdpCsomag);
- }
- }
- // motor2
- if (UdpCsomag > 400 && UdpCsomag <800)
- {
- if (UdpCsomag > 400 && UdpCsomag <600)
- {
- //motor2 hátra
- digitalWrite(motor2Pin1, HIGH);
- digitalWrite(motor2Pin2, LOW);
- ledcWrite(pwmChannel, (UdpCsomag-600)*-1);
- }
- else if (UdpCsomag == 600)
- {
- // motor2 kikapcs
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, LOW);
- }
- else if (UdpCsomag > 600 && UdpCsomag <800)
- {
- //motor2 előre
- digitalWrite(motor2Pin1, LOW);
- digitalWrite(motor2Pin2, HIGH);
- ledcWrite(pwmChannel, UdpCsomag-600);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement