短笛君 发表于 2026-11-8 22:51:13

【瑞萨BLE/WIFI模块测评】WiFi 模块发送温湿度数据

# DA入网流程二

上一篇把板子送上 Wi-Fi 了,这一篇接 HS3003 温湿度传感器,把数据发到自己的网页上。

板子还是 RAECO4M2 V2.0(R7FA4M2AD3CFP),模块 DA16200,e2 studio / FSP 6.5.0 / GCC Arm 13.2。

```
上电 → 连接 Wi-Fi → 3 秒后点亮 LED → 每 5 秒:读传感器 → 屏上显示 → HTTP 上传
```

## 接线

HS3003 挂到屏那条 I2C 上就行,不用额外引脚:

| 信号 | 引脚      | 说明                        |
| ------ | ------------- | ------------------------------- |
| SCL| P408 / SCL3 | OLED 0x3C 与 HS3003 0x44 共用 |
| SDA| P409 / SDA3 | 同上,1.5 kΩ 上拉            |

两个器件地址不同,IIC仅需要访问不同地址即可 这部分在驱动层已经做好了封装

## HS3003 数据读取

数据手册的时序很短:写一个字节的测量命令 `0x00`,等转换,读四个字节。

```c
/* 1. Trigger a conversion. */
    if (!oled_bus_write(HS3003_I2C_ADDRESS, &command, 1U))
    {
      g_hs3003_error_count++;

      return false;
    }

    /* 2. Wait for it, the datasheet allows up to 6.6 ms; 50 ms is plenty and
   *    keeps the timing out of the equation when the bus is shared. */
    R_BSP_SoftwareDelay(HS3003_CONVERSION_MS, BSP_DELAY_UNITS_MILLISECONDS);

    /* 3. Read the four result bytes. */
    if (!oled_bus_read(HS3003_I2C_ADDRESS, buffer, sizeof(buffer)))
    {
      g_hs3003_error_count++;

      return false;
    }
```

四个字节是「湿度高、湿度低、温度高、温度低」,每个值占 14 位,高字节的头两位是状态位, 所以都要先 `& 0x3F`:

```c
uint32_t const raw_humidity    = (((uint32_t) buffer & 0x3FU) << 8) | (uint32_t) buffer;
    uint32_t const raw_temperature = (((uint32_t) buffer & 0x3FU) << 8) | (uint32_t) buffer;

    *p_humidity_rh100 = (int32_t) ((raw_humidity * 10000U) / (uint32_t) HS3003_RAW_MAX);
    *p_temperature_cc = (int32_t) ((raw_temperature * (uint32_t) HS3003_TEMP_SCALE) /
                                 (uint32_t) HS3003_RAW_MAX) - HS3003_TEMP_OFFSET;
```

对应手册的公式:

```
RH% = 100 × raw / 16383
T℃= 165 × raw / 16383 − 40
```

​**用定点而不是浮点**​:返回值单位是 0.01(2345 = 23.45 ℃),精度对温湿度足够, 屏上显示和拼 URL 都只需要两位小数,还省掉软浮点的格式化和链接。raw 最大 16383, 乘 10000 或 16500 都在 32 位内,不会溢出。先前测评已经HS3003的初始化工程 感兴趣的同学可以翻翻我以前的测评贴

## 温湿度数据上传至云端

### 构建数据请求帧格式

```
http://192.168.1.100/api/sensor?t=23.45&h=56.78&n=12
```

`t` 温度、`h` 湿度、`n` 第几次上传。服务端按照对应格式去提取字符串解析即可 此处为内网通信 没有放到公网中,所以就采用了明文传输 不在考虑加密这些

```c
/* 2345 -> "23.45", -512 -> "-5.12" */
static uint32_t url_append_fixed2(char * p_text, uint32_t position, uint32_t capacity,
                                  int32_t value)
{
    if (value < 0)
    {
      ...
      value = -value;
    }

    position = url_append_u32(p_text, position, capacity, (uint32_t) value / 100U);

    if (position < (capacity - 1U))
    {
      p_text = '.';
      position++;
      p_text = '\0';
    }

    /* Always two decimals, including the leading zero of 0.05. */
    uint32_t const fraction = (uint32_t) value % 100U;

    if (fraction < 10U)
    {
      position = url_append(p_text, position, capacity, "0");
    }

    return url_append_u32(p_text, position, capacity, fraction);
}
```

那个 `fraction < 10` 的分支是必须的:不然 23.05 会变成 `23.5`。

### 通过DA16200将数据发送到服务端

指令名和格式放在 `wifi_config.h`,代码里只拼不硬编码:

```c
#define UPLOAD_AT_PREFIX   "AT+HTTPC=GET,\""
#define UPLOAD_AT_SUFFIX   "\"\r\n"
```

拼出来是:

```
AT+HTTPC=GET,"http://192.168.1.100/api/sensor?t=23.45&h=56.78&n=12"
```

发送复用 AT 传输层那个阻塞函数,只是命令换成外部传进来的:

```c
bool wifi_transport_command_raw(char const * p_command, uint32_t timeout_ms)
{
    if (NULL == p_command)
    {
      return false;
    }

    return wifi_transport_command((uint8_t const *) p_command,
                                  (uint32_t) strlen(p_command),
                                  timeout_ms,
                                  NULL,
                                  false);
}
```

判断成功,模块回 `OK`,`ERROR` 和超时都算失败。应答原文会留在 `g_wifi_at_response` 里,另外把前 63 个可见字符抄到 `g_upload_reply`,调试时直接看这个变量。

## 主循环

联网成功之后就是读、显示、上传、等,没有 RTOS:

```c
static void flow_upload(wifi_ui_status_t * p_status)
{
    for (;;)
    {
      int32_t temperature_cc = 0;
      int32_t humidity_rh100 = 0;

      bool const read_ok = hs3003_read(&temperature_cc, &humidity_rh100);

      p_status->temperature_cc = temperature_cc;
      p_status->humidity_rh100 = humidity_rh100;
      p_status->sensor_valid   = read_ok;
      ...
      p_status->sequence++;
      p_status->state = WIFI_UI_STATE_UPLOADING;
      wifi_ui_show(p_status);

      upload_result_t const result = upload_sensor(temperature_cc, humidity_rh100,
                                                   p_status->sequence);

      p_status->state = (UPLOAD_RESULT_OK == result) ? WIFI_UI_STATE_UPLOAD_OK
                                                      : WIFI_UI_STATE_UPLOAD_ERROR;
      wifi_ui_show(p_status);

      /* LED follows the last upload: on when the module answered. */
      led_set(UPLOAD_RESULT_OK == result);

      delay_ms(APP_UPLOAD_PERIOD_MS);
    }
}
```

屏上是这样的:

```
+--------------------------------+
| 上传成功                     |
| T 23.45C                     |
| H 56.78%                     |
| 192.168.1.23 #12               |
| [############################] |
+--------------------------------+
```

LED1 入网成功后亮,之后每上传一次跟着变:成功常亮、失败熄灭

!(https://www.eefocus.com/forum/data/attachment/forum/202611/08/225027pkrhtrlr5xnhrqgz.png)

打开监控网页 即可看到MCU端通过wifi模块上报来的数据,界面会显示设备所在的IP地址


页: [1]
查看完整版本: 【瑞萨BLE/WIFI模块测评】WiFi 模块发送温湿度数据