0

我正在使用配置为接入点和服务器的 ESP8266 Thing Dev 板来存储来自温度传感器的数据。目标是使用 Android 应用程序访问这些数据,但目前使用的客户端是我运行 Chrome 的计算机。

传感器每 250 毫秒提供一次新的温度,这些温度被添加到字符串(JSON 格式)中。当客户端请求数据时,字符串结束(关闭 JSON 结构),并打印在服务器上。

客户端一次应该能够访问多达 2*50 个值。但是,每次发送请求时,要么只打印 2 个或 4 个值,要么页面不工作。

我的 Arduino 代码,对 arduino 使用 ESP8266 和 TMP102 库:

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Ticker.h>
#include "tmp102.h"

#define NB_MAX_TEMP 50
#define MAX_INPUT 20

const byte sensorAddress1 = 0x90;  // ADD0 -> GND
const byte sensorAddress2 = 0x92;  // ADD0 -> V+
tmp102 *thermometer1 = new tmp102(&Wire);
tmp102 *thermometer2 = new tmp102(&Wire);

// Timer
Ticker ticker;
bool flag = false;

// Wifi
const char WiFiAPPSK[] = "sparkfun"; // Wifi password
WiFiServer server(80);

int count = 0;
String s;

void setupWiFi()
{
  WiFi.mode(WIFI_AP);

  // Do a little work to get a unique-ish name. Append the
  // last two bytes of the MAC (HEX'd) to "ThingDev-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
  String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ThingDev-" + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i = 0; i < AP_NameString.length(); i++)
  AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
}

void timer_done()
{
  flag = true;
}

void acquire()
{
  int temp1 = 0, temp2 = 0;
  if (thermometer1->readTemp(temp1))
  {
    // If we're reading pins, print out those values:
    // first temp
    s += String(temp1 * 0.0625);
  }
  s += ",";
  if (thermometer2->readTemp(temp2))
  {
    // If we're reading pins, print out those values:
    // second temp
    s += String(temp2 * 0.0625);
  }
  return;
}

void setup()
{
  Wire.begin(); // start the I2C library
  Serial.begin(115200);
  thermometer1->init(sensorAddress1);
  thermometer2->init(sensorAddress2);

  //Set default config.
  thermometer1->writeConf(TMP102_DEFAULT_CONF);
  thermometer2->writeConf(TMP102_DEFAULT_CONF);

  setupWiFi();
  server.begin();

  // timer every 0.25s
  ticker.attach(0.25, timer_done);
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  if (flag) {
    flag = false;

    int temp1 = 0, temp2 = 0;

    if (count == 0) {
      // prepare string
      s = "HTTP/1.1 200 OK\r\n";
      s += "Content-Type: text/html\r\n\r\n";
      s += "{temp:[";
    }
    delay(1);
    // add a measurement
    s += "[";
    acquire();
    s += "],";

    count += 1;
    if (count == NB_MAX_TEMP) {
      // Prevent the string to become infinite
      count = 0;
      s = s.substring(0, s.length() - 1);
      s += "]};";
    }
  }
   if (client.available() > 0) {
      // Read the first line of the request
      String req = client.readStringUntil('\r');
      client.flush();
      Serial.println(req);

      if (req.indexOf("/read") != -1) {
        // End the string
        s.setCharAt(s.length() - 1, '\0'); 
        s += "]};";
        client.print(s);
        Serial.println(s);
        count = 0;
      }
    }
    delay(1);
    Serial.println("Client disconnected");

    // The client will actually be disconnected
    // when the function returns and 'client' object is detroyed
}

以及我在串行控制台上获得的示例(JSON 字符串与我在服务器页面上阅读的相同):

GET /read HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html

{temp:[[24.00,24.56],[24.00,24.56];
Client disconnected
GET /favicon.ico HTTP/1.1
Client disconnected
GET /read HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/html

{temp:[[24.00,24.56];
Client disconnected

我究竟做错了什么 ?我在使用阻塞功能吗?/favicon.ico 请求是否会造成麻烦?

有关信息,我的代码的接入点部分和传感器部分已单独测试并按预期工作。

提前致谢。

4

0 回答 0