-
Couldn't load subscription status.
- Fork 13.3k
Description
Basic Infos
Hardware
Hardware: ESP-12
Core Version: ?2.1.0-rc2?
Description
I'm using this code to read yaw pitch and roll from an IMU module and send them to a TCP socket over WiFi, but client.println() is too slow, transmitting at around 15 values/second. However, when I try the WiFiClientBasic example, client.println() functions normally and the data rate is much higher, even for very long strings. I measured the time difference(using micros()) in both the sketches and the one I'm running takes around 127311 ticks per call whereas WiFiClientBasic takes around 500 ticks per call. I've tried printing single characters, it's still very slow. Can this be solved if I use UDP sockets instead of TCP?
Settings in IDE
Module: NodeMCU v1.0
Flash Size: 4MB
CPU Frequency: 80Mhz
Flash Mode: qio?
Flash Frequency: 40Mhz
Upload Using: SERIAL
Reset Method: nodemcu
Sketch
#include <Arduino.h>
void setup() {
#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
if(SerialDebug)
Serial.begin(115200);
if(SocketDebug) {
WiFiMulti.addAP(SSID, Password);
if(SerialDebug)
Serial.print("Wait for WiFi... ");
while(WiFiMulti.run() != WL_CONNECTED) {
//Serial.print(".");
delay(500);
}
if(SerialDebug) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("connecting to ");
Serial.println(host);
}
while(!client.connect(host, port)) {
if(SerialDebug) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
}
delay(5000);
}
//client.setNoDelay(true);
}
// Set up the interrupt pin, its set as active high, push-pull
pinMode(intPin, INPUT);
digitalWrite(intPin, LOW);
if (accelGyroMag.testConnection()) {
if(SerialDebug)
Serial.println("MPU9150 is online...");
if(SocketDebug)
client.println("MPU9150 is online...");
getAres();
getGres();
//Calibrate gyro and accelerometers, load biases in bias registers
//calibrateMPU9150(gyroBias, accelBias);
//Calibrated values for my MPU9150, run calibrateMPU9150 for other sensors
accelGyroMag.initialize();
//accelGyroMag.setRate(7);
//accelGyroMag.setDLPFMode(4);
accelGyroMag.setFullScaleAccelRange(0);
accelGyroMag.setFullScaleGyroRange(0);
accelGyroMag.setXAccelOffset(-1320);
accelGyroMag.setYAccelOffset(1836);
accelGyroMag.setZAccelOffset(830);
accelGyroMag.setXGyroOffset(-35);
accelGyroMag.setYGyroOffset(-24);
accelGyroMag.setZGyroOffset(28);
accelGyroMag.setI2CBypassEnabled(true);
// Initialize device for active mode read of acclerometer, gyroscope, and temperature
if(SerialDebug)
Serial.println("MPU9150 initialized for active data mode....");
if(SocketDebug)
client.println("MPU9150 initialized for active data mode....");
// Read the WHO_AM_I register of the magnetometer, this is a good test of communication
uint8_t c = readByte(AK8975A_ADDRESS, WHO_AM_I_AK8975A);
delay(1000);
// Get magnetometer calibration from AK8975A ROM
MagRate = 15;
initAK8975A(magCalibration);
//magcalMPU9150(magbias);
magbias[0] = 8.14*mRes*magCalibration[0]; // User environmental x-axis correction in milliGauss
magbias[1] = 63.39*mRes*magCalibration[1]; // User environmental y-axis correction in milliGauss
magbias[2] = 14.71*mRes*magCalibration[2];
}
else {
if(SerialDebug)
Serial.println("MPU9150 connection failed.");
if(SocketDebug)
client.println("MPU9150 connection failed.");
while(1);
}
}
void loop() {
// If intPin goes high or data ready status is TRUE, all data registers have new data
if (accelGyroMag.getIntDataReadyStatus()) {
getCorrectedMotion9(&acc, &gyr, &mg);
}
//set integration time by time elapsed since last filter update
Now = micros();
deltat = ((Now - lastUpdate)/1000000.0f);
lastUpdate = Now;
MadgwickQuaternionUpdate(
acc.x,
acc.y,
acc.z,
gyr.x*PI/180.0f,
gyr.y*PI/180.0f,
gyr.z*PI/180.0f,
mg.y,
mg.x,
mg.z
);
//getRealAccel();
//getRealWorldAccel();
if (!AHRS) {
int delt_t = millis() - count;
String output = String(1000*q[1], 2) +
',' + String(1000*q[2], 2) +
',' + String(1000*q[3], 2);
if(SerialDebug) {
Serial.println(output);
}
if(SocketDebug) {
while(!client.connected()) {
client.connect(host, port);
client.setNoDelay(true);
}
client.println(output);
}
}
else {
delt_t = millis() - count;
yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
pitch *= 180.0f / PI;
yaw *= 180.0f / PI;
roll *= 180.0f / PI;
outString = String(yaw, 2) + ',' + String(pitch, 2) + ',' + String(roll, 2);
if(delt_t > 2) {
long oldTime, newTime;
if(SocketDebug) {
while(!client.connected()) {
client.connect(host, port);
//client.setNoDelay(true);
}
oldTime = micros();
client.println(outString);
newTime = micros();
}
if(SerialDebug) {
//Serial.println(outString);
Serial.println(newTime - oldTime);
}
count = millis();
}
}
}