Wireless gyro

Content

réseau

fixed IP 192.168.0.22

messages sortants :

/PIE/gyro [angleX] [angleY] [angleZ] [accelX] [accelY] [accelZ]

microprogramme

main

// pour LOLIN D1 mini pro ESP8266

//#define RFID
//#define TOF
//#define MPU6050


#include <Adafruit_NeoPixel.h>
const uint8_t PIN_LED_DATA = D5;
#define NUM_LEDS 1
Adafruit_NeoPixel pixels(NUM_LEDS, PIN_LED_DATA, NEO_GRBW + NEO_KHZ800);

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <TinyMPU6050.h>
MPU6050 mpu(Wire);

// WiFi stuff
const char* ssid = "***********"; // your network SSID (name)
const char* pwd = "***********"; // your network password
WiFiUDP Udp;
const IPAddress ip(192, 168, 0, 22);
const IPAddress gateway(192, 168, 0, 253);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress outIp(192, 168, 0, 255);  // remote IP of your computer
const unsigned int outPort = 6666;        // remote port to receive OSC
const unsigned int localPort = 1510;      // local port to listen for OSC packets (actually not used for sending)

/////////////////////////////
int sens = 0;
long int prevTimeSens = 0;
long int prevTimeGyro = 0;
int i = 0;
//////////////////////////////
byte BP0 = 0;
byte prevBP0 = 1;
#define BP0pin D6
#include <Bounce2.h>
// INSTANTIATE A Bounce OBJECT
Bounce button0 = Bounce();

void setup() {
  pixels.begin();                       // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.clear();                       // Set all pixel colors to 'off'
  pixels.show();                        // Send the updated pixel colors to the hardware.
  button0.attach(BP0pin, INPUT_PULLUP);  // USE INTERNAL PULL-UP
    // DEBOUNCE INTERVAL IN MILLISECONDS
  button0.interval(5);

  mpu.Initialize();

  Serial.begin(9600);
  //while (!Serial) {
  //  ;  // wait for serial port to connect. Needed for native USB port only
  //}
  // WiFi stuff
  WiFi.hostname("wireless_gyro1");
  WiFi.begin(ssid, pwd);
  WiFi.config(ip, gateway, subnet);
  // attempt to connect to WiFi network:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    pixels.setPixelColor(0, pixels.Color(100, 0, 0, 0));
    pixels.show();  // Send the updated pixel colors to the hardware.
    Serial.println(ssid);
    // wait 100ms before trying again to connect
    delay(100);
    pixels.clear();  // Set all pixel colors to 'off'
    pixels.show();
    delay(500);
    Serial.print(".");
    if (i > 50) {
      pixels.clear();  // Set all pixel colors to 'off'
      pixels.show();
      delay(100);
      Serial.print("resetting");
      ESP.reset();
      //break;
    }
    i++;
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  pixels.setPixelColor(0, pixels.Color(25, 12, 0, 0));
  pixels.show();  // Send the updated pixel colors to the hardware.
  delay(10);

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("HW MAC address: ");
  Serial.println(WiFi.macAddress());
  Serial.println("Starting UDP");

  Udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(Udp.localPort());

  Serial.println("Starting calibration...");
  mpu.Calibrate();
  Serial.println("Calibration complete!");

  // Shows offsets
  Serial.println("--- Offsets:");
  Serial.print("GyroX Offset = ");
  Serial.println(mpu.GetGyroXOffset());
  Serial.print("GyroY Offset = ");
  Serial.println(mpu.GetGyroYOffset());
  Serial.print("GyroZ Offset = ");
  Serial.println(mpu.GetGyroZOffset());
}


void loop() {

  while (WiFi.status() == WL_CONNECTED) {
    pixels.setPixelColor(0, pixels.Color(0, 1, 0, 0));
    pixels.show();  // Send the updated pixel colors to the hardware.
    sendSensing();
    mpu.Execute();
    sendGyro();
    button0.update();
    sendButton0();
    delay(1);
  }
  Serial.println("perte de co");
  pixels.clear();  // Set all pixel colors to 'off'
  pixels.show();
  ESP.reset();
}

osc

void sendGyro() {

  if (millis() - prevTimeGyro > 40) {
    pixels.setPixelColor(0, pixels.Color(20, 1, 0, 0));
    pixels.show();  // Send the updated pixel colors to the hardware.
    prevTimeGyro = millis();
    OSCMessage msg("/PIE/gyro");
    msg.add(mpu.GetAngX());
    msg.add(mpu.GetAngY());
    msg.add(180.0f + (mpu.GetAngZ()*180.0f/174.0f));
    msg.add(mpu.GetAccX());
    msg.add(mpu.GetAccY());
    msg.add(mpu.GetAccZ());
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp);
    Udp.endPacket();
    msg.empty();
  }
}

#ifdef TOF
void sendDistance() {
  if (prevDistance != distance) {  // || millis() - prevTimeDistance  > 1101) {
    prevDistance = distance;
    int val = 0;
    int presence = 0;
    if (distance != 255) {
      presence = 1;
    }

    //if(distance  == 255) val = 255; //enlever celui là poue un fonctionnement à 0 si la "pédale" est laissée ouverte
    /*else*/ if (distance > 127) {
      val = 0;
    } else {

      val = 127 - distance;
    }
    //Serial.println(val);
    OSCMessage msg("/PIE/distance");
    msg.add(val);
    msg.add(presence);
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp);
    Udp.endPacket();
    msg.empty();
  }
}
#endif

#ifdef RFID
void sendNFC() {
  if (presenceNfc == 1) {
    for (byte i = 0; i < uidLength; i++) {
      valToSend[i] = uid[i];
    }
  } else {
    valToSend[0] = 0;
    valToSend[1] = 0;
    valToSend[2] = 0;
    valToSend[3] = 0;
    valToSend[4] = 0;
    valToSend[5] = 0;
    valToSend[6] = 0;
  }
  controlVal = 0;
  for (byte i = 0; i < 7; i++) {
    controlVal += valToSend[i];
  }

  if (controlVal != prevControlVal) {
    prevControlVal = controlVal;
    OSCMessage msg("/PIE/nfc");
    msg.add(valToSend[0]);
    msg.add(valToSend[1]);
    msg.add(valToSend[2]);
    msg.add(valToSend[3]);
    msg.add(valToSend[4]);
    msg.add(valToSend[5]);
    msg.add(valToSend[6]);
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp);
    Udp.endPacket();
    msg.empty();
  }
}
#endif

void sendSensing() {
  if (millis() - prevTimeSens > 1000) {
    OSCMessage msg("/PIE/sens");
    msg.add(sens++);
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp);
    Udp.endPacket();
    msg.empty();
    prevTimeSens = millis();
  }
}

void sendButton0() {

 if ( button0.changed() ) {
    BP0 = !button0.read();
    //Serial.println(BP0);
        //Serial.println(val);
    OSCMessage msg("/PIE/BP00");
    msg.add(BP0);
    Udp.beginPacket(outIp, outPort);
    msg.send(Udp);
    Udp.endPacket();
    msg.empty();
  }
}