使用ESP8266向客户端发送消息

时间:2020-05-16 20:55:03

标签: c# c++ unity3d arduino arduino-esp8266

因此,我将某处设置为两个esp,一个作为访问点,另一个作为客户端,并且您的计算机应该加入esp网络。其目的是将两个esp的信息都发送到计算机。由于无法弄清楚如何进行客户端到客户端的通信,因此我决定将客户端esp的信息发送到接入点esp,并代表另一个esp将信息发送到计算机。我希望将信息发送到计算机,但是由于我不知道如何挑选计算机,并且这样做可能会花费太多时间,因此我决定将消息发送给所有客户端。我在由I2cdev库制作的脚本中使用FIFO,我的代码本质上不能花费太长时间,否则它将被卡住并溢出。我在计算机上统一使用c#中的代码来提取数据。我这样做的方法是列出所有客户端,并使用def add_cyl_csys(part): ################################### ## ADD CSYS AND MATERIAL ORIENTATIONS ################################### csys_cyl = part.DatumCsysByThreePoints( coordSysType=CYLINDRICAL, name='csys_cyl', origin=(0.0, 0.0, 0.0), point1=( 0.0, -1.0, 0.0), point2=(1.0, 0.0, 0.0)) part.MaterialOrientation( additionalRotationField='', additionalRotationType=ROTATION_NONE, angle=0.0, axis=AXIS_3, fieldName='', localCsys=part.datums[csys_cyl.id], orientationType=SYSTEM, region=Region(cells=part.cells), stackDirection=STACK_3) 发送给每个客户端,因为我的c#代码可以识别此客户端。但是,当我添加了发送其他esp信息的功能时,它花费的时间太长并且溢出。罪魁祸首是消息传递系统。我在文档中找到了一种足够快速的解决方案,可以在其中执行client.println()发送给所有客户端,但是我的C#代码无法解决此问题。因此,我需要找到一种方法使c#识别server.println(),以便我的代码可以通过。这是我的接入点esp的代码,其中函数server.println()是溢出的罪魁祸首:

msgAllClients()

这是c#部分的统一脚本,我认为不需要正确的控制器,但是如果需要,我可以添加,因为我的一个假设可能是错误的,例如可能如何进行客户端到客户端的通信。无论如何,这里是C#代码:

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2012 Jeff Rowberg
==============================

  GY-521  NodeMCU
  MPU6050 devkit 1.0
  board   Lolin         Description
  ======= ==========    ====================================================
  VCC     VU (5V USB)   Not available on all boards so use 3.3V if needed.
  GND     G             Ground
  SCL     D1 (GPIO05)   I2C clock
  SDA     D2 (GPIO04)   I2C data
  XDA     not connected
  XCL     not connected
  AD0     not connected
  INT     D8 (GPIO15)   Interrupt pin
*/
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"

#include <ESP8266WiFi.h>

#include "MPU6050_6Axis_MotionApps20.h" //might need (0x03)
//#include "MPU6050.h" // not necessary if using MotionApps include file

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 mpu;
//MPU6050 mpu(0x69); // <-- use for AD0 high


// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer, can be set to 1024 instead

// uncomment "OUTPUT_READABLE_QUATERNION" if you want to see the actual
// quaternion components in a [w, x, y, z] format (not best for parsing
// on a remote host such as Processing or something though)
#define OUTPUT_READABLE_QUATERNION

// uncomment "OUTPUT_READABLE_EULER" if you want to see Euler angles
// (in degrees) calculated from the quaternions coming from the FIFO.
// Note that Euler angles suffer from gimbal lock (for more info, see
// http://en.wikipedia.org/wiki/Gimbal_lock)
//#define OUTPUT_READABLE_EULER

// uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
// pitch/roll angles (in degrees) calculated from the quaternions coming
// from the FIFO. Note this also requires gravity vector calculations.
// Also note that yaw/pitch/roll angles suffer from gimbal lock (for
// more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
//#define OUTPUT_READABLE_YAWPITCHROLL

// uncomment "OUTPUT_READABLE_REALACCEL" if you want to see acceleration
// components with gravity removed. This acceleration reference frame is
// not compensated for orientation, so +X is always +X according to the
// sensor, just without the effects of gravity. If you want acceleration
// compensated for orientation, us OUTPUT_READABLE_WORLDACCEL instead.
//#define OUTPUT_READABLE_REALACCEL

// uncomment "OUTPUT_READABLE_WORLDACCEL" if you want to see acceleration
// components with gravity removed and adjusted for the world frame of
// reference (yaw is relative to initial orientation, since no magnetometer
// is present in this case). Could be quite handy in some cases.
#define OUTPUT_READABLE_WORLDACCEL


// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector

#ifdef OUTPUT_READABLE_EULER
float euler[3];         // [psi, theta, phi]    Euler angle container
#endif
#ifdef OUTPUT_READABLE_YAWPITCHROLL
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
#endif

#define INTERRUPT_PIN 15 // use pin 15 on ESP8266
#define CLIENT_COUNT 5 // amount of clients allowed on server

const char DEVICE_NAME[] = "mpu6050";

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void ICACHE_RAM_ATTR dmpDataReady() {
    mpuInterrupt = true;
}

void mpu_setup()
{
  // join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  Wire.begin();
  Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  Fastwire::setup(400, true);
#endif

  // initialize device
  Serial.println(F("Initializing I2C devices..."));
  mpu.initialize();
  pinMode(INTERRUPT_PIN, INPUT);

  // verify connection
  Serial.println(F("Testing device connections..."));
  Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  // load and configure the DMP
  Serial.println(F("Initializing DMP..."));
  devStatus = mpu.dmpInitialize();

  // supply your own gyro offsets here, scaled for min sensitivity
  mpu.setXGyroOffset(220);
  mpu.setYGyroOffset(76);
  mpu.setZGyroOffset(-85);
  mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

  // make sure it worked (returns 0 if so)
  if (devStatus == 0) {
    // turn on the DMP, now that it's ready
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);

    // enable Arduino interrupt detection
    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
    attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();

    // set our DMP Ready flag so the main loop() function knows it's okay to use it
    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    dmpReady = true;

    // get expected DMP packet size for later comparison
    packetSize = mpu.dmpGetFIFOPacketSize();
  } else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
  }
}

// push buttons
int aButtonPin = 12; //D6
int bButtonPin = 13; //D7
int aVal = 0; 
int bVal = 0;

String message = "L";

//wifi config
WiFiServer server(80);
IPAddress IP(192,168,4,1);
IPAddress mask = (255, 255, 255, 255);
WiFiClient clients[CLIENT_COUNT];
WiFiClient newClient;

const char* ssid = "VRControllers";
const char* password = "VRControllers";
String line;
int openIndex;


void setup()
{
  Serial.begin(9600);
  delay(10);

  //setup the access point
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  bool result = WiFi.softAPConfig(IP, IP, mask);
  server.begin();
  server.setNoDelay(true);
  Serial.println(result ? "Server started." : "Server failed to start");

  //set up buttons
  pinMode(aButtonPin, INPUT);
  pinMode(bButtonPin, INPUT); 
  //set up accelerometer 
  mpu_setup();
}

void mpu_loop()
{
  // if programming failed, don't try to do anything
  if (!dmpReady) return;

  // wait for MPU interrupt or extra packet(s) available
  if (!mpuInterrupt && fifoCount < packetSize) return;

  // reset interrupt flag and get INT_STATUS byte
  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  // get current FIFO count
  fifoCount = mpu.getFIFOCount();

  // check for overflow (this should never happen unless our code is too inefficient)
  if ((mpuIntStatus & 0x10) || fifoCount == 1024) { // if fifoCount is 1024 change conditional to 16384
    // reset so we can continue cleanly
    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
  } else if (mpuIntStatus & 0x02) {
    // wait for correct available data length, should be a VERY short wait
    while (fifoCount < packetSize){
      fifoCount = mpu.getFIFOCount();
    }

    // read a packet from FIFO
    mpu.getFIFOBytes(fifoBuffer, packetSize);
    //mpu.resetFIFO();

    // track FIFO count here in case there is > 1 packet available
    // (this lets us immediately read more without waiting for an interrupt)
    fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
    // display Euler angles in degrees
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

    String x = String(ypr[0] * 180/M_PI);
    String y = String(ypr[1] * 180/M_PI);
    String z = String(ypr[2] * 180/M_PI);
    mpu.resetFIFO();
    message += ":" + x + "," + y + "," + z;
#endif

#ifdef OUTPUT_READABLE_QUATERNION
    // display quaternion values in easy matrix form: w x y z
    mpu.dmpGetQuaternion(&q, fifoBuffer);

    mpu.resetFIFO();
    message += ":" + String(q.w) + "," + String(q.x) + "," + String(q.y) + "," + String(q.z);
#endif

#ifdef OUTPUT_READABLE_EULER
    // display Euler angles in degrees
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetEuler(euler, &q);

    String x = String(euler[0] * 180/M_PI);
    String y = String(euler[1] * 180/M_PI);
    String z = String(euler[2] * 180/M_PI);
    mpu.resetFIFO();
    message += ":" + x + "," + y + "," + z;
#endif

#ifdef OUTPUT_READABLE_REALACCEL
    // display real acceleration, adjusted to remove gravity
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetAccel(&aa, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);

    mpu.resetFIFO();
    message += ":" + String(aaReal.x) + "," + String(aaReal.y) + "," + String(aaReal.z);
#endif

#ifdef OUTPUT_READABLE_WORLDACCEL
    // display initial world-frame acceleration, adjusted to remove gravity
    // and rotated based on known orientation from quaternion
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetAccel(&aa, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
    mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);

    mpu.resetFIFO();
    message += ":" + String(aaWorld.x) + "," + String(aaWorld.y) + "," + String(aaWorld.z);
#endif
  }
}

void updateClients(){
  newClient = server.available();
  openIndex = CLIENT_COUNT + 1;

  for(int i = 0; i < CLIENT_COUNT; i++){
    if(!clients[i] || clients[i].remoteIP() == newClient.remoteIP()){
      openIndex = i < openIndex ? i : openIndex;
    }
  }

  if(openIndex <= CLIENT_COUNT && newClient){
    clients[openIndex] = newClient;
    Serial.print("New connection"); 
  }
  /*
  else if(!present && openIndex > CLIENT_COUNT && newClient){
    newClient.stop();
    Serial.print("Server full, abondoning connection...");  
  }
  */

}

void getClientMsg(){
  for(int j = 0; j < CLIENT_COUNT; j++){
    if(clients[j].available()){
      line = clients[j].readStringUntil('\n');
        if(line[0] == 'R'){
          //clients[j].flush();
          //server.println(line);
          message += line + "\n";
      }
    }
  }
}

void msgAllClients(String message){
  for(int j = 0; j < CLIENT_COUNT; j++){
    if(clients[j]){
      clients[j].flush();
      clients[j].print(message);
    }
  }
}

void buttonLoop(){
  aVal = digitalRead(aButtonPin);
  bVal = digitalRead(bButtonPin);

  message += bVal == HIGH ? ":B_DOWN" : ":B_UP";
  message += aVal == HIGH ? ",A_DOWN\n" : ",A_UP\n";
}

void loop()
{
  mpu_loop();
  buttonLoop();
  updateClients();
  getClientMsg();
  Serial.print(message);
  //server.println(message);
  msgAllClients(message);
  message = "L";
}

我们非常感谢您的帮助,非常感谢你们! =)

0 个答案:

没有答案