DIY物聯網小型氣象站(NodeMCU + BMP180)[附code及接線圖]

早排接觸到NodeMcu呢樣玩意,類似WiFi版既arduino
用佢來整左個Wifi Weather Station, 量度氣溫,氣壓,濕度,然後upload上thingspeak,咁就可以用電腦 / 電話隨時睇返

大家可以上: https://thingspeak.com/channels/51245 呢個網睇到最新既氣象數據

Weather station主要由NodeMCU(ESP8266) + DHT11 (濕度計) + BMP180 (溫度 + 氣壓計)組成,D sensor 同nodemcu都係上taobao買

Wiring 就如圖:
  
P.S. : 如果係淘寶果D DHT11 Module,多數已經有10K的上拉電阻,不用再加

實物圖:


Program 方面就用Arduino IDE,係Board Manager 加入 ESP8266
(詳見: http://ruten-proteus.blogspot.hk ... rt-arduino-ide.html        「ESP8266 硬體套件安裝」果一part )

Code:
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Wire.h>
  4. #include <Adafruit_BMP085.h>  //Download link: https://github.com/adafruit/Adafruit-BMP085-Library
  5. #include "DHT.h" //Download link: https://github.com/adafruit/DHT-sensor-library
  6. #define DHTPIN 4

  7. DHT dht(DHTPIN, DHT11);
  8. Adafruit_BMP085 bmp;
  9. WiFiClient client;

  10. String apiKey = "打返你的thingspeak api key";
  11. const char* ssid = "你的SSID";
  12. const char* password = "你的WIFI PW";
  13. const char* server = "api.thingspeak.com";

  14. void setup() {
  15.   Serial.begin(115200);
  16.   delay(10);

  17.   Wire.begin(0, 2);
  18.   if (!bmp.begin()) {
  19.     Serial.println("Could not find a valid BMP180 sensor, check wiring!");
  20.     while (1);
  21.   }
  22.   dht.begin();

  23.   WiFi.begin(ssid, password);

  24.   Serial.println();
  25.   Serial.println();
  26.   Serial.print("Connecting with ");
  27.   Serial.println(ssid);

  28.   WiFi.begin(ssid, password);

  29.   while (WiFi.status() != WL_CONNECTED) {
  30.     delay(500);
  31.     Serial.print("Connect failed");
  32.   }
  33.   Serial.println("");
  34.   Serial.println("WiFi Connected");


  35. }

  36. void loop() {
  37.   float temp = bmp.readTemperature();
  38.   float pressure = bmp.readPressure();
  39.   pressure = pressure / 100; //Convert to hPa
  40.   float humidity = dht.readHumidity();

  41.   if (client.connect(server, 80)) {
  42.     String postStr = apiKey;
  43.     postStr += "&field1=";
  44.     postStr += String(temp, 2);
  45.     postStr += "&field2=";
  46.     postStr += String(pressure, 2);
  47.     postStr += "&field3=";
  48.     postStr += String(humidity, 2);
  49.     postStr += "\r\n\r\n";

  50.     client.print("POST /update HTTP/1.1\n");
  51.     client.print("Host: api.thingspeak.com\n");
  52.     client.print("Connection: close\n");
  53.     client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  54.     client.print("Content-Type: application/x-www-form-urlencoded\n");
  55.     client.print("Content-Length: ");
  56.     client.print(postStr.length());
  57.     client.print("\n\n");
  58.     client.print(postStr);

  59.     Serial.println("Temp: " + String(temp) + " Pressure: " + String(pressure) + " Humidity: " + String(humidity));
  60.     Serial.println(postStr);

  61.   }
  62.   client.stop();

  63.   delay(5000);
  64. }
複製代碼
小弟coding 水平麻麻,大家有建議歡迎提出!
主要都係由呢幾Part 組成,初始化sensor → 連接WiFi → 讀取sensor數據 → 將數據合併到字串postStr 中 →將數據以HTTP POST傳到thingspeak server

歡迎大家比D改善建議~ 或者有做過類似project 既都可以分享下~
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

留位~

TOP

Mine

https://thingspeak.com/channels/26162

via HKEPC Ionic Reader v1.3.3 - Android

TOP

Nice, I do the same with just a ESP8266 and send data to my own MQTT-server

TOP

回覆 1# ossyukyss


    好厲害
值得參考一下

TOP

回覆 3# kwso888


    厲害厲害

TOP

回覆 4# domeso


    請問怎樣做到的

TOP

回覆  domeso


    請問怎樣做到的
xiao 發表於 2016-12-29 15:56



    Very simple, code very similar, but uses Arduino IDE, DHT22 > GPIO 0 of ESP01, using pubsub library for MQTT server publishing.

TOP

早排接觸到NodeMcu呢樣玩意,類似WiFi版既arduino
用佢來整左個Wifi Weather Station, 量度氣溫,氣壓,濕 ...
ossyukyss 發表於 2016-12-29 12:30



    想請問香港仲有邊度有得賣nodemcu同其他accessories,上個月仲見到SSP仲有間店賣,上個星期再去已經唔見左

TOP

想請問香港仲有邊度有得賣nodemcu同其他accessories,上個月仲見到SSP仲有間店賣,上個星期再去已經唔 ...
raywan 發表於 2016-12-29 22:38


我都係上taobao買好過,sensor 又齊又平

TOP

相關文章