两种arduino功能可以单独工作读取传感器+通过HTTP发送数据,但是不能一起工作,为什么?

问题描述

我是Arduino编程的新手,在我的程序中遇到了一个烦人的错误。使用中的Arduino是Arduino Uno WiFi Rev2,传感器如下,使用grove Base Shield: https://wiki.seeedstudio.com/Grove-IMU_10DOF_v2.0/(IMU传感器) https://wiki.seeedstudio.com/Base_Shield_V2/

我有两个功能,它们可以永远永远正常工作:

  1. 将数据发送到后端:
void sendToBackend(String data,String type) {
    if(client.connect(HOST_NAME,HTTP_PORT)){
        client.print("POST /");
        client.print(type);
        client.println(" HTTP/1.1");
        client.println("Host: xxx.xxx.x.xx");
        client.println("Connection: Keep-Alive");
        client.print("Content-Length: ");
        client.println(data.length());
        client.println(); // end HTTP header
        client.println(data);
    }
}
  1. 读取传感器数据:
void getAccel_Data(void) {
    accelgyro.getMotion9(&ax,&ay,&az,&gx,&gy,&gz,&mx,&my,&mz);
    Axyz[0] = (double) ax / 16384;
    Axyz[1] = (double) ay / 16384;
    Axyz[2] = (double) az / 16384;
}

getAccel_Data(void)随grove软件库一起提供。 我现在想做的是以下事情:

void loop() {
    getAccel_Data(); // works when sendToBackend is commented out
    sendToBackend("test","test"); // works when getAccel_Data is commented out
    delay(300);
}

进口:

#include "Wire.h"

// imports for acceleration sensor
// I2Cdev and mpu9250 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 "mpu9250.h"
#include "BMP280.h"

// imports for lcd display
#include "rgb_lcd.h"

// imports for WiFi
#include <SPI.h>
#include <WiFiNINA.h>

参数:

// acceleration sensor parameters:
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
mpu9250 accelgyro;
I2Cdev   I2C_M;
uint8_t buffer_m[6];
int16_t ax,ay,az;
int16_t gx,gy,gz;
int16_t mx,my,mz;
float heading;
float tiltheading;
float Axyz[3];
float Gxyz[3];
float Mxyz[3];
#define sample_num_mdate  5000
volatile float mx_sample[3];
volatile float my_sample[3];
volatile float mz_sample[3];
static float mx_centre = 0;
static float my_centre = 0;
static float mz_centre = 0;
volatile int mx_max = 0;
volatile int my_max = 0;
volatile int mz_max = 0;
volatile int mx_min = 0;
volatile int my_min = 0;
volatile int mz_min = 0;
float temperature;
float pressure;
float atm;
float altitude;
BMP280 bmp280;

int status = WL_IDLE_STATUS;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
...

设置:

void setup{
    Serial.begin(38400);
    
    setup_lcd_display();
    setup_wifi();
    setup_acceleration_sensor();
    
    // general setup
    delay(1000);
    Serial.println("     ");   
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)