Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <Adafruit_MotorShield.h>
- int UdpCsomag;
- //--wifi config
- #ifndef APSSID
- #define APSSID "D1R2"
- #define APPSK "nincsnincs"
- #endif
- const char *ssid = APSSID;
- const char *password = APPSK;
- IPAddress ip(192, 168, 1, 10); // Wemos ip címe
- IPAddress netmask(255, 255, 255, 0); //netmask
- //--udp config
- WiFiUDP UdpServer;
- unsigned int UDPPort = 4000; //udp szerver portja
- const int packetSize = 20; // csomag mérete
- char packetBuffer[packetSize];
- //--MOTOR shield object
- Adafruit_MotorShield AFMS = Adafruit_MotorShield();
- Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
- Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
- void setup()
- {
- Serial.begin(115200); //start Serial
- //--WIFI
- WiFi.softAPConfig(ip, ip, netmask);
- WiFi.softAP(ssid, password, 8);
- Serial.println("Indul...");
- Serial.println("Wemos D1R2 UDP teszt 2019.06.24");
- Serial.println((String)"SSID: " + ssid + " PASS: " + password);
- Serial.println((String)"RoboRemo app udp adatok " + ip.toString() + ":" + UDPPort);
- //--UDP start
- UdpServer.begin(UDPPort);
- //--MOTOR
- AFMS.begin(); // create with the default frequency 1.6KHz AFMS.begin(1000); OR with a different frequency, say 1KHz
- myMotor->setSpeed(0); // Set the speed to start, from 0 (off) to 255 (max speed)
- myMotor2->setSpeed(0);
- //myMotor->run(FORWARD);
- myMotor->run(RELEASE); // turn on motor
- myMotor2->run(RELEASE);
- }
- void loop()
- {
- 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("Van udp kapcsolat!");
- Serial.println(UdpCsomag);
- //motor 1
- if (UdpCsomag >= -200 && UdpCsomag <= 200)
- {
- if (UdpCsomag >= -200 && UdpCsomag <0)
- {
- myMotor->run(BACKWARD);
- myMotor->setSpeed(UdpCsomag*-1);
- }
- else if (UdpCsomag == 0)
- {
- myMotor->setSpeed(UdpCsomag);
- }
- else if (UdpCsomag > 0 && UdpCsomag <200)
- {
- myMotor->run(FORWARD);
- myMotor->setSpeed(UdpCsomag);
- }
- }
- //motor 2
- if (UdpCsomag >= 400 && UdpCsomag <= 800)
- {
- if (UdpCsomag >= 400 && UdpCsomag <600)
- {
- myMotor2->run(FORWARD);
- myMotor2->setSpeed((UdpCsomag-600)*-1);
- }
- else if (UdpCsomag == 600)
- {
- myMotor2->setSpeed(0);
- }
- else if (UdpCsomag > 600 && UdpCsomag <800)
- {
- myMotor2->run(BACKWARD);
- myMotor2->setSpeed(UdpCsomag-600);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement