I am writing a thesis project.
It is required to read the data from the GPS NEO-6M sensors and accelerometer GY-521 and transfer them to POST by a request to the database table via the esp-8266 module.
Data is not sent due to incorrect Content-Length counting.
Perhaps not enough Arduino Uno memory.
A small amount of data is transmitted well.
In my sketch, I commented on the wi-fi initialization and data transfer itself, since esp gives the busy s message ... and I try to deal with this by outputting the generated strings to the serial port.
Sketch and melon output below.
void setup()
{
//GPS
Serial.begin(9600);
ss.begin(GPSBaud); // скорость GPS
//Wi-Fi
inputString.reserve(20);
swSerial.begin(9600);
while (!Serial)
;
Serial.println("Starting wifi");
/*wifi.setTransportToTCP();
wifi.endSendWithNewline(false);
wifi.begin();
wifi.connectToAP("itqc", "An2YC4pr");
wifi.connectToServer("18.219.242.202", "80");*/
int error;
uint8_t c;
Wire.begin();
error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1);
error = MPU6050_read (MPU6050_PWR_MGMT_2, &c, 1);
MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
calibrate_sensors();
set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0);
}
void loop()
{
if (millis() - currentTime > 5)
{
currentTime = millis();
int error;
double dT;
accel_t_gyro_union accel_t_gyro;
error = read_gyro_accel_vals((uint8_t*) &accel_t_gyro);
unsigned long t_now = millis();
float FS_SEL = 131;
float gyro_x = (accel_t_gyro.value.x_gyro - base_x_gyro)/FS_SEL;
float gyro_y = (accel_t_gyro.value.y_gyro - base_y_gyro)/FS_SEL;
float gyro_z = (accel_t_gyro.value.z_gyro - base_z_gyro)/FS_SEL;
float accel_x = accel_t_gyro.value.x_accel;
float accel_y = accel_t_gyro.value.y_accel;
float accel_z = accel_t_gyro.value.z_accel;
float RADIANS_TO_DEGREES = 180/3.14159;
float accel_angle_y = atan(-1*accel_x/sqrt(pow(accel_y,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;
float accel_angle_x = atan(accel_y/sqrt(pow(accel_x,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES;
float accel_angle_z = 0;
float dt =(t_now - get_last_time())/1000.0;
float gyro_angle_x = gyro_x*dt + get_last_x_angle();
float gyro_angle_y = gyro_y*dt + get_last_y_angle();
float gyro_angle_z = gyro_z*dt + get_last_z_angle();
float unfiltered_gyro_angle_x = gyro_x*dt + get_last_gyro_x_angle();
float unfiltered_gyro_angle_y = gyro_y*dt + get_last_gyro_y_angle();
float unfiltered_gyro_angle_z = gyro_z*dt + get_last_gyro_z_angle();
float alpha = 0.96;
float angle_x = alpha*gyro_angle_x + (1.0 - alpha)*accel_angle_x;
float angle_y = alpha*gyro_angle_y + (1.0 - alpha)*accel_angle_y;
float angle_z = gyro_angle_z; //Accelerometer doesn't give z-angle
set_last_read_angle_data(t_now, angle_x, angle_y, angle_z, unfiltered_gyro_angle_x, unfiltered_gyro_angle_y, unfiltered_gyro_angle_z);
//Wi-Fi
if (millis() - currentTime2 > 3000) // Проверяем время для wi-fi
{
currentTime2 = millis();
latitude = String(gps.location.lat());
longitude = String(gps.location.lng());
printDateTime(gps.date, gps.time);
speedd = String(gps.speed.kmph());
accx = String(accel_angle_x);
accy = String(accel_angle_y);
accz = String(unfiltered_gyro_angle_z);
shapka = "POST /test HTTP/1.1\nHost: iotws.test.itqc.ru\nConnection: keep-alive\nContent-Length: ";
data1 = "[{\"lat\":\""+latitude+"\",\"lon\":\""+longitude+"\",\"date\":\""+dated+"\"";
smartDelay(100);
data2 = ",\"time\":\""+timed+"\",\"speed\":\""+speedd+"\",\"accx\":\""+accx+"\"";
smartDelay(100);
data3 = ",\"accy\":\""+accy+"\",\"accz\":\""+accz+"\"}]";
smartDelay(100);
length_str = String(data1.length()+data2.length()+data3.length())+"\n\n";
smartDelay(100);
Serial.print(shapka);
Serial.print(length_str);
Serial.print(data1);
Serial.print(data2);
Serial.print(data3);
shapka = "";
length_str = "";
data1 = "";
data2 = "";
data3 = "";
}
}
}
int MPU6050_read(int start, uint8_t *buffer, int size)
{
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while(Wire.available() && i<size)
{
buffer[i++]=Wire.read();
}
if ( i != size)
return (-11);
return (0); // возврат : нет ошибки
}
int MPU6050_write(int start, const uint8_t *pData, int size)
{
int n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start); // write the start address
if (n != 1)
return (-20);
n = Wire.write(pData, size); // write data bytes
if (n != size)
return (-21);
error = Wire.endTransmission(true); // release the I2C-bus
if (error != 0)
return (error);
return (0); // возврат : нет ошибки
}
int MPU6050_write_reg(int reg, uint8_t data)
{
int error;
error = MPU6050_write(reg, &data, 1);
return (error);
}
//GPS
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t) //функция вывода даты и времени
{ //через gps с московским часовым поясом(+3 часа)
if (false)
{
dated = F("********** ");
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d", d.day(), d.month() , d.year());
dated = String(sz);
}
if (false)
{
timed = F("******** ");
}
else
{
char sz[32];
if (t.hour()==22)
{
sprintf(sz, "%02d:%02d:%02d", 01, t.minute(), t.second());
}
else
{
if (t.hour()==23)
{
sprintf(sz, "%02d:%02d:%02d", 02, t.minute(), t.second());
}
else
{
if (t.hour()==24)
{
sprintf(sz, "%02d:%02d:%02d", 03, t.minute(), t.second());
}
else
{
sprintf(sz, "%02d:%02d:%02d", (t.hour()+3), t.minute(), t.second());
}
}
}
timed = String(sz);
}
smartDelay(0);
}
static void smartDelay(unsigned long ms) //что то связанное с задержкой
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
Starting wifi
POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 125
[{"lat":"0.00","lon":"0.00","date":"00/00/2000","time":"03:00:00","speed":"0.00","accx":"0.07","accy":"-2.72","accz":"0.04"}]POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 32
","accy":"-2.58","accz":"0.03"}]POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 79
[{"lat":"0.00","lon":"0.00","date":"00/00/2000"","accy":"-2.98","accz":"0.06"}]POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 79
[{"lat":"0.00","lon":"0.00","date":"00/00/2000"","accy":"-2.69","accz":"0.12"}]POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 79
[{"lat":"0.00","lon":"0.00","date":"00/00/2000"","accy":"-2.43","accz":"0.17"}]POST /test HTTP/1.1
Host: iotws.test.itqc.ru
Connection: keep-alive
Content-Length: 79