The BME280 data sending problem via TYWE3S

muammer
Posts: 7

Hello,

I have BME280 I2C (0x76) + TYWE3S + Arduino Pro Mini. I created an UI on Tuya IOT according TYWE3S had PID. It's working and there is no debugging error.

So there is a STH30 sketch on Arduino IDE Library. I tryed some config many times. My module is connecting my smart phone and I'm seeing right UI but it doesn't receive datas from Pro Mini.

How can I change configurations from STH30 to BME280 on Arduino IDE adding Pressure?

yangjie
Posts: 209

Re: The BME280 data sending problem via TYWE3S

Instead of using a real BME280 device, you can use get_humi() or get_temp() to get one of your preset values. Make SURE everything except the BME280 driver is properly reported. After the part of debugging except BME280 driver is finished, it will be reported by getting real data.

Also note that you should avoid using delay() in your programs.Instead, use millis(). Because delay() is a blocking delay, it affects the data sent and received between the mcu and the module.

Here's an example of using millis() instead of delay() :

Code: Select all

delay(3000);

is equivalent to:

Code: Select all

static unsigned long last_time = 0;

void setup()
{
	...
	last_time = millis();
}

void loop()
{
	...
	if (millis()- last_time >= 3000) {
		last_time = millis();
		...
		// It is executed every 3 seconds
	}
	...
}

muammer
Posts: 7

Re: The BME280 data sending problem via TYWE3S

There is same problem..

This is the program I typed.

#include <Wire.h>
#include <TuyaWifi.h>
#include <pocketBME280.h>

TuyaWifi my_device;
pocketBME280 mySensor;

#define BME280_I2C_ADDR 0x76

#define DPID_PRESSURE 101
#define DPID_TEMPERATURE 102
#define DPID_HUMIDITY 103
#define DPID_BATTERY 104
#define DPID_CONNECTION 105

unsigned char led_state = 0;
int wifi_key_pin = 7;
int analogInPin = A0;
int connectionPin = 2;
const float batteryVoltageDividerRatio = 3.0;
const float batteryVoltageThreshold = 5.0;

int pressure = 0;
int pressure1 = 0;
int temperature = 0;
int humidity = 0;
int batteryLevel = 0;
int batteryVoltage = 0;
int connection = 0;
int connection1 = LOW;

float previousPressure = 0;
float previousTemperature = 0;
float previousHumidity = 0;
float previousBatteryLevel = 0;
float previousConnection = 0;
float previousConnection1 = 0;

unsigned char dp_array[][2] =
{
{DPID_PRESSURE, DP_TYPE_VALUE},
{DPID_TEMPERATURE, DP_TYPE_VALUE},
{DPID_HUMIDITY, DP_TYPE_VALUE},
{DPID_BATTERY, DP_TYPE_VALUE},
{DPID_CONNECTION, DP_TYPE_ENUM},
};

unsigned char pid[] = {"xxxxxxxxxxxxxxxx"};
unsigned char mcu_ver[] = {"1.0.0"};

/* last time */
static unsigned long last_time = 0;

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


Wire.begin();

//Initialize led port, turn off led.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//Initialize networking keys.
pinMode(wifi_key_pin, INPUT_PULLUP);

my_device.init(pid, mcu_ver);
//incoming all DPs and their types array, DP numbers
my_device.set_dp_cmd_total(dp_array, 6);
//register DP download processing callback function
my_device.dp_process_func_register(dp_process);
//register upload all DP callback function
my_device.dp_update_all_func_register(dp_update_all);


last_time = millis();
}

void loop()
{
my_device.uart_service();

//Enter the connection network mode when Pin7 is pressed.
if (digitalRead(wifi_key_pin) == LOW) {
delay(80);
if (digitalRead(wifi_key_pin) == LOW) {
my_device.mcu_set_wifi_mode(SMART_CONFIG);
}
}
/* LED blinks when network is being connected */
if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {
if (millis()- last_time >= 500) {
last_time = millis();

Code: Select all

  if (led_state == LOW) {
    led_state = HIGH;
  } else {
    led_state = LOW;
  }
  digitalWrite(LED_BUILTIN, led_state);
}

}

/* get the temperature and humidity */
get_bme280_values(&pressure, &temperature, &humidity, &batteryLevel, &connection);

/* report the temperature and humidity */
if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD))
{
my_device.mcu_dp_update(DPID_PRESSURE, pressure, 1);
my_device.mcu_dp_update(DPID_TEMPERATURE, temperature, 1);
my_device.mcu_dp_update(DPID_HUMIDITY, humidity, 1);
my_device.mcu_dp_update(DPID_BATTERY, batteryLevel, 1);
my_device.mcu_dp_update(DPID_CONNECTION, connection, 1);
}
if (millis()- last_time >= 5000) {
last_time = millis();
}
}

void get_bme280_values(int *pressure_value, int *temperature_value, int *humidity_value, int *batteryLevel_value, int *connection_value)
{
mySensor.startMeasurement();

int32_t pressure1 = mySensor.getPressure(); // hPa
pressure = map(pressure1, 92750, 124750, 0, 400);
temperature = (mySensor.getTemperature() / 100);
humidity = (mySensor.getHumidity() / 1024);
batteryVoltage = analogRead(A0) * (5.0 / 1023.0) * batteryVoltageDividerRatio;
batteryLevel = map(batteryVoltage, 5.0, 9.0, 1, 100);
connection1 = digitalRead(connectionPin);

previousPressure = pressure;
previousTemperature = temperature;
previousHumidity = humidity;
previousBatteryLevel = batteryLevel;
previousConnection = connection;


if (pressure <= 0) pressure = 0;
if (batteryLevel < 1) batteryLevel = 0;
if (connection1 == LOW) connection = 1;
if (connection1 == HIGH) connection = 0;

if (pressure != previousPressure temperature != previousTemperature humidity != previousHumidity connection != previousConnection
abs(batteryLevel != map(previousBatteryLevel, 5.0, 9.0, 0, 100)) < 1 > 1) {
void dp_update_all(void);
}
}

unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
{
/* all DP only report */
return TY_SUCCESS;
}

void dp_update_all(void)
{
my_device.mcu_dp_update(DPID_PRESSURE, pressure, 1);
my_device.mcu_dp_update(DPID_TEMPERATURE, temperature, 1);
my_device.mcu_dp_update(DPID_HUMIDITY, humidity, 1);
my_device.mcu_dp_update(DPID_BATTERY, batteryLevel, 1);
my_device.mcu_dp_update(DPID_CONNECTION, connection, 1);
}

yangjie
Posts: 209

Re: The BME280 data sending problem via TYWE3S

Whether the data of BME280 can be normally obtained without connecting to tuya cloud?

muammer
Posts: 7

Re: The BME280 data sending problem via TYWE3S

Yes, I see bme280 data without sketch parts of Tuya on Arduino IDE. When I add the coding related Tuya to the sketch, the codes I see on the Serial Monitor turn into meaningless shapes. When I convert the codes consisting of these meaningless shapes via converter programs they are hexadecimal. I think that BME280 data has Bytes incompatibility with Tuya and that it will be fixed by editing the CRC validation on sketch, but I don't know how to do it.

muammer
Posts: 7

Re: The BME280 data sending problem via TYWE3S

Or I don't know which is the correct '.h' bme280 library for Tuya.

yangjie
Posts: 209

Re: The BME280 data sending problem via TYWE3S

muammer 2024年 Aug 6日 17:50

Or I don't know which is the correct '.h' bme280 library for Tuya.

What does startMeasurement do?

muammer
Posts: 7

Re: The BME280 data sending problem via TYWE3S

yangjie 2024年 Aug 6日 17:56
muammer 2024年 Aug 6日 17:50

Or I don't know which is the correct '.h' bme280 library for Tuya.

What does startMeasurement do?

It's a start measurement cycle part of pocketBME280.h

muammer
Posts: 7

Re: The BME280 data sending problem via TYWE3S

I retyped some parts of code but it doesn't work again.

#include <Wire.h>
#include <TuyaWifi.h>

TuyaWifi my_device;

/* bme280 */
#define BME280_I2C_ADDR 0x76

/* Data point define */
#define DPID_PRESSURE_CURRENT 101
#define DPID_TEMPERATURE_CURRENT 102
#define DPID_HUMIDITY_CURRENT 103
#define DPID_BATTERY_CURRENT 104
#define DPID_CONNECTION_CURRENT 105

static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length);

unsigned char led_state = 0;
int wifi_key_pin = 7;
int analogInPin = A0;
int connectionPin = 2;
const float batteryVoltageDividerRatio = 3.0;
const float batteryVoltageThreshold = 5.0;

/* Current device DP values */
int pressure_value = 0;
int pressure1 = 0;
int temperature_value = 0;
int humidity_value = 0;
int batteryLevel_value = 0;
int batteryVoltage = 0;
int connection_enum = 0;
int connection1 = LOW;

float previousPressure = 0;
float previousTemperature = 0;
float previousHumidity = 0;
float previousBatteryLevel = 0;
float previousConnection = 0;
float previousConnection1 = 0;

/* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type.

  • dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP
    */
    unsigned char dp_array[][2] =
    {
    {DPID_PRESSURE_CURRENT, DP_TYPE_VALUE},
    {DPID_TEMPERATURE_CURRENT, DP_TYPE_VALUE},
    {DPID_HUMIDITY_CURRENT, DP_TYPE_VALUE},
    {DPID_BATTERY_CURRENT, DP_TYPE_VALUE},
    {DPID_CONNECTION_CURRENT, DP_TYPE_ENUM},
    };

unsigned char pid[] = {"xxxxxxxxxxxxxxxx"};
unsigned char mcu_ver[] = {"1.0.0"};

/* last time */
unsigned long last_time = 0;

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

// Initialise I2C communication as MASTER
Wire.begin();

int8_t bme280_crc(int8_t *data, int8_t length);

//Initialize led port, turn off led.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//Initialize networking keys.
pinMode(wifi_key_pin, INPUT_PULLUP);

my_device.init(pid, mcu_ver);
//incoming all DPs and their types array, DP numbers
my_device.set_dp_cmd_total(dp_array, 6);
//register DP download processing callback function
my_device.dp_process_func_register(dp_process);
//register upload all DP callback function
my_device.dp_update_all_func_register(dp_update_all);


delay(300);
last_time = millis();
}

void loop()
{
my_device.uart_service();

//Enter the connection network mode when Pin7 is pressed.
if (digitalRead(wifi_key_pin) == LOW) {
delay(80);
if (digitalRead(wifi_key_pin) == LOW) {
my_device.mcu_set_wifi_mode(SMART_CONFIG);
}
}
/* LED blinks when network is being connected */
if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) {
if (millis()- last_time >= 500) {
last_time = millis();

Code: Select all

  if (led_state == LOW) {
    led_state = HIGH;
  } else {
    led_state = LOW;
  }
  digitalWrite(LED_BUILTIN, led_state);
}

}

/* get the pressure and temperature and humidity and batteryLevel and connection */
get_bme280_values(&pressure_value, &temperature_value, &humidity_value);

/* report the pressure and temperature and humidity and batteryLevel and connection */
if ((my_device.mcu_get_wifi_work_state() == WIFI_CONNECTED) || (my_device.mcu_get_wifi_work_state() == WIFI_CONN_CLOUD))
{
my_device.mcu_dp_update(DPID_PRESSURE_CURRENT, pressure_value, 1);
my_device.mcu_dp_update(DPID_TEMPERATURE_CURRENT, temperature_value, 1);
my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity_value, 1);
my_device.mcu_dp_update(DPID_BATTERY_CURRENT, batteryLevel_value, 1);
my_device.mcu_dp_update(DPID_CONNECTION_CURRENT, connection_enum, 1);
}
delay(1000);
}

void get_bme280_values(int *pressure_value, int *temperature_value, int *humidity_value)
{
uint8_t i2c_data[6];

Wire.requestFrom(BME280_I2C_ADDR, 6);

Code: Select all

// Read 6 bytes of i2c_data

// temperature msb, temperature lsb, temperature crc, humidity msb, humidity lsb, humidity crc
if (Wire.available() == 6) {
for (int i = 0; i < 6; i++) {
i2c_data = Wire.read();
}

Code: Select all

    // CRC validation
    if ((bme280_crc(i2c_data, 2) == i2c_data[2]) && (bme280_crc(i2c_data+3, 2) == i2c_data[5])) {
      
        // Example calculation, replace with the correct formula from your sensor's datasheet
        *pressure_value = ((((i2c_data[0] * 256.0) + i2c_data[1]) * 100) / 65535.0); // Example conversion

        // Calculate temperature
        *temperature_value = (((((i2c_data[0] * 256.0) + i2c_data[1]) * 175) / 65535.0) - 45) * 100;

        // Calculate humidity
        *humidity_value = ((((i2c_data[3] * 256.0) + i2c_data[4]) * 100) / 65535.0) * 100;

        // Calculate pressure

    } else {
        // CRC failed, set values to 0
        *temperature_value = 0;
        *humidity_value = 0;
        *pressure_value = 0;
    }
}

}

/**

  • @description: check sht30 temperature and humidity data

  • @param {unsigned char} *data

  • @param {unsigned int} count

  • @return {*}
    */
    unsigned char bme280_crc(unsigned char *data, unsigned int count)
    {
    unsigned char crc = 0xff;
    unsigned char current_byte;
    unsigned char crc_bit;

    /* calculates 8-Bit checksum with given polynomial */
    for (current_byte = 0; current_byte < count; ++current_byte)
    {
    crc ^= (data[current_byte]);
    for (crc_bit = 8; crc_bit > 0; --crc_bit)
    {
    if (crc & 0x80)
    crc = (crc << 1) ^ 0x31;
    else
    crc = (crc << 1);
    }
    }
    return crc;
    }

/**

  • @description: DP download callback function.
  • @param {unsigned char} dpid
  • @param {const unsigned char} value
  • @param {unsigned short} length
  • @return {unsigned char}
    /
    unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
    {
    /
    all DP only report */
    return TY_SUCCESS;
    }

/**

  • @description: Upload all DP status of the current device.

  • @param {*}

  • @return {*}
    */
    void dp_update_all(void)
    {

    if(pressure_value != previousPressure < 1 > 1){
    my_device.mcu_dp_update(DPID_PRESSURE_CURRENT, pressure_value, 1);
    previousPressure = pressure_value;
    }


    if(temperature_value != previousTemperature < 1 > 1){
    my_device.mcu_dp_update(DPID_TEMPERATURE_CURRENT, temperature_value, 1);
    previousTemperature = temperature_value;
    }


    if(humidity_value != previousHumidity < 1 > 1){
    my_device.mcu_dp_update(DPID_HUMIDITY_CURRENT, humidity_value, 1);
    previousHumidity = humidity_value;
    }


    if(batteryLevel_value != previousBatteryLevel < 1 > 1){
    my_device.mcu_dp_update(DPID_BATTERY_CURRENT, batteryLevel_value, 1);
    previousBatteryLevel = batteryLevel_value;
    }

    if(connection_enum != previousConnection < 1 > 1){
    my_device.mcu_dp_update(DPID_CONNECTION_CURRENT, connection_enum, 1);
    previousConnection = connection_enum;
    }
    }

yangjie
Posts: 209

Re: The BME280 data sending problem via TYWE3S

muammer 2024年 Aug 6日 17:45

Yes, I see bme280 data without sketch parts of Tuya on Arduino IDE. When I add the coding related Tuya to the sketch, the codes I see on the Serial Monitor turn into meaningless shapes. When I convert the codes consisting of these meaningless shapes via converter programs they are hexadecimal. I think that BME280 data has Bytes incompatibility with Tuya and that it will be fixed by editing the CRC validation on sketch, but I don't know how to do it.

meaningless shapes: Probably because the arduino and tuya modules communicate using the same Serial port that your serial output uses.

The MCU and tuya modules communicate using hex.

Post Reply