#include SoftwareSerial Ard(2,3); #include Uduino_Wifi uduino("uduinoBoard"); // Declare and name your object // Servo #if defined(ESP32) // Install esp32servo library => Sketch>Include Library>Manager Libraries>Esp32Servo #include #else #include #endif #define MAXSERVOS 8 void setup() { Serial.begin(115200); Ard.begin(9600); #if defined (__AVR_ATmega32U4__) // Leonardo while (!Serial) {} #elif defined(__PIC32MX__) delay(1000); #endif // Optional functions, to add BEFORE connectWifi(...) uduino.setPort(4222); // default 4222 uduino.connectWifi("SSID", "pswd"); uduino.addCommand("s", SetMode); uduino.addCommand("d", WritePinDigital); uduino.addCommand("a", WritePinAnalog); uduino.addCommand("rd", ReadDigitalPin); uduino.addCommand("r", ReadAnalogPin); uduino.addCommand("br", BundleReadPin); uduino.addCommand("b", ReadBundle); uduino.addCommand("Control", Control); uduino.addInitFunction(InitializeServos); uduino.addDisconnectedFunction(DisconnectAllServos); } void ReadBundle() { char *arg = NULL; char *number = NULL; number = uduino.next(); int len = 0; if (number != NULL) len = atoi(number); for (int i = 0; i < len; i++) { uduino.launchCommand(arg); } } void SetMode() { int pinToMap = 100; //100 is never reached char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToMap = atoi(arg); } int type; arg = uduino.next(); if (arg != NULL) { type = atoi(arg); PinSetMode(pinToMap, type); } } void PinSetMode(int pin, int type) { //TODO : vérifier que ça, ça fonctionne if (type != 4) DisconnectServo(pin); switch (type) { case 0: // Output pinMode(pin, OUTPUT); break; case 1: // PWM pinMode(pin, OUTPUT); break; case 2: // Analog pinMode(pin, INPUT); break; case 3: // Input_Pullup pinMode(pin, INPUT_PULLUP); break; case 4: // Servo SetupServo(pin); break; } } void WritePinAnalog() { int pinToMap = 100; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToMap = atoi(arg); } int valueToWrite; arg = uduino.next(); if (arg != NULL) { valueToWrite = atoi(arg); if (ServoConnectedPin(pinToMap)) { UpdateServo(pinToMap, valueToWrite); } else { analogWrite(pinToMap, valueToWrite); } } } void WritePinDigital() { int pinToMap = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) pinToMap = atoi(arg); int writeValue; arg = uduino.next(); if (arg != NULL && pinToMap != -1) { writeValue = atoi(arg); digitalWrite(pinToMap, writeValue); } } void ReadAnalogPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); if (pinToRead != -1) printValue(pinToRead, analogRead(pinToRead)); } } void ReadDigitalPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); } if (pinToRead != -1) printValue(pinToRead, digitalRead(pinToRead)); } void BundleReadPin() { int pinToRead = -1; char *arg = NULL; arg = uduino.next(); if (arg != NULL) { pinToRead = atoi(arg); if (pinToRead != -1) printValue(pinToRead, analogRead(pinToRead)); } } void loop() { uduino.update(); //delay(10); } void printValue(int pin, int targetValue) { uduino.print(pin); uduino.print(" "); //<- Todo : Change that with Uduino delimiter uduino.println(targetValue); } /* SERVO CODE */ Servo servos[MAXSERVOS]; int servoPinMap[MAXSERVOS]; void InitializeServos() { #if defined(ESP32) ESP32PWM::allocateTimer(0); ESP32PWM::allocateTimer(1); ESP32PWM::allocateTimer(2); ESP32PWM::allocateTimer(3); #else #endif for (int i = 0; i < MAXSERVOS - 1; i++ ) { servoPinMap[i] = -1; servos[i].detach(); } } void SetupServo(int pin) { if (ServoConnectedPin(pin)) return; int nextIndex = GetAvailableIndexByPin(-1); if (nextIndex == -1) nextIndex = 0; #if defined(ESP32) servos[nextIndex].setPeriodHertz(50); #endif servos[nextIndex].attach(pin); servoPinMap[nextIndex] = pin; } void DisconnectServo(int pin) { servos[GetAvailableIndexByPin(pin)].detach(); servoPinMap[GetAvailableIndexByPin(pin)] = 0; } bool ServoConnectedPin(int pin) { if (GetAvailableIndexByPin(pin) == -1) return false; else return true; } int GetAvailableIndexByPin(int pin) { for (int i = 0; i < MAXSERVOS - 1; i++ ) { if (servoPinMap[i] == pin) { return i; } else if (pin == -1 && servoPinMap[i] < 0) { return i; // return the first available index } } return -1; } void UpdateServo(int pin, int targetValue) { int index = GetAvailableIndexByPin(pin); servos[index].write(targetValue); delay(10); } void DisconnectAllServos() { for (int i = 0; i < MAXSERVOS; i++) { servos[i].detach(); digitalWrite(servoPinMap[i], LOW); servoPinMap[i] = -1; } } void Control(){ String angles = String(uduino.getParameter(0))+","+String(uduino.getParameter(1))+","+String(uduino.getParameter(2))+","+String(uduino.getParameter(3))+","+String(uduino.getParameter(4))+","+String(uduino.getParameter(5)); Ard.print(angles); Ard.println(); delay(100); }