0

我想通过 WiFi 从此 ESP8266WiFi 获取 DHT22 传感器值(温度和湿度)的 JSON 字符串。

    #include <DHT.h> //library DHT.h
    #include <ESP8266WiFi.h>
    #define DHTPIN 14 // GPIO14
    #define DHTTYPE DHT22 // DHT 22 (AM2302)
    DHT dht(DHTPIN, DHTTYPE, 15);

    const char* ssid = "myhotspot";
    const char* password = "mypassword";

    WiFiServer server(80);

    void setup() {
      Serial.begin(115200);
      dht.begin();
      Serial.println();
      Serial.print("Wifi connecting to ");
      Serial.println( ssid );

      WiFi.begin(ssid,password);

      Serial.println();
      Serial.print("Connecting");

      while( WiFi.status() != WL_CONNECTED ){
          delay(500);
          Serial.print(".");
      }
      Serial.println();

      Serial.println("Wifi Connected Success!");
      Serial.print("NodeMCU IP Address : ");
      Serial.println(WiFi.localIP() );

      server.begin();
      Serial.println("NodeMCU Server started");

      // Print the IP address
      Serial.print("Use this URL to connect: ");
      Serial.print("http://");
      Serial.print(WiFi.localIP());
      Serial.println("/");
    }
    void sendJsonString(float temperature, float humidity, String dataStatus, String reason){
      /*
      {
        "data" : {
          "temperature" : 0.0, // 0,NaN
          "humidity" : 0.0 
        }, 
        "status" : "OK", //OK, Error
        "reason" : ""
      }
      */
      String s = "";
      s += String("{");
      s += String("\"temperature\" : " + String(temperature) + "," );
      s += String("\"humidity\" : " + String(humidity) );
      s += String("} ," );
      Serial.println(s); //this line specifically
    }
    void loop() {
      // Reading temperature or humidity takes about 250 milliseconds!
      delayMicroseconds(250);
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      delay(2000);
      // Set up data
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      // check if returns are valid, if they are NaN (not a number) then something went wrong! 
      float temperature = t;
      float humidity = h; 
      String dataStatus = "OK";
      String reason = "";
      if (isnan(t) || isnan(h)) {
        temperature = 0;
        humidity = 0; 
        dataStatus = "Error";
        reason = "Failed to read from DHT";
      }
      // Send data
      sendJsonString(temperature, humidity, dataStatus, reason); 
    }

然后使用 NodeJS 获取 JSON 字符串并将其转换为两个字符串,一个温度和一个湿度,将其存储在变量中并在其上添加日期和时间。

    var express = require('express')
    var bodyParser = require('body-parser')
    var app = express()
    var cors = require('cors')

    app.use(cors())
    app.use(bodyParser.json())

    app.set('port', (process.env.PORT || 9999))
    app.use(bodyParser.urlencoded({extended: false}))
    app.use(bodyParser.json())

    app.post('/', function (req, res) {
      var dateTime = new Date().toISOString().replace('T', ' ').substr(0, 19);
        console.log(dateTime + ',' + req.body.temperature)
      console.log(dateTime + ',' + req.body.humidity)
        res.send('success : ' + req.body.temperature + ',' + req.body.humidity)
    })

    app.listen(app.get('port'), function () {
      console.log('run at port', app.get('port'))
    })

然后,如果可能的话,我想使用相同的 NodeJS 代码文件来使用我存储的字符串以可用于在 web 服务上制作图表的形式编写 CSV 文件,并使用与此 Raspberry Pi 项目相同的网页。

http://www.home-automation-community.com/temperature-and-humanity-from-am2302-dht22-sensor-displayed-as-chart/

我的代码中有很多不一致的地方,所以,如果这里的用户能告诉我我应该怎么做才能实现我的目标,那就太好了。我只是这个领域的一个业余爱好者。我在这里先向您的帮助表示感谢。

4

0 回答 0