Mundarija:

Ob -havo stantsiyasi: ESP8266 chuqur uyquda, SQL, kolba va grafik bo'yicha grafik: 3 qadam
Ob -havo stantsiyasi: ESP8266 chuqur uyquda, SQL, kolba va grafik bo'yicha grafik: 3 qadam

Video: Ob -havo stantsiyasi: ESP8266 chuqur uyquda, SQL, kolba va grafik bo'yicha grafik: 3 qadam

Video: Ob -havo stantsiyasi: ESP8266 chuqur uyquda, SQL, kolba va grafik bo'yicha grafik: 3 qadam
Video: ESP8266 Часть 1. Вводная 2024, Noyabr
Anonim
Ob -havo stantsiyasi: ESP8266, chuqur uyqu, SQL, kolba va plotli grafik
Ob -havo stantsiyasi: ESP8266, chuqur uyqu, SQL, kolba va plotli grafik

Balkoningizdagi harorat, namlik yoki yorug'lik intensivligini bilish qiziq bo'larmidi? Men bilardim. Shunday qilib, men bunday ma'lumotlarni yig'ish uchun oddiy ob -havo stantsiyasini yaratdim. Keyingi bo'limlar men uni qurish uchun qilgan qadamlardir.

Qani boshladik!

1 -qadam: yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi

Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi
Yorug'lik, harorat va namlik sensori bo'lgan ob -havo stantsiyasi

Men ob-havo stantsiyasini qurishni rejalashtirganimda, shamol tezligi, yomg'irni o'lchash, to'liq spektrli quyosh sensori bo'lgan to'laqonli ob-havo stantsiyasiga ega bo'lishni orzu qilardim, lekin ma'lum bo'lishicha, bu arzon bo'lmaydi va sotib olish narxi tugashi mumkin. kamida 100 dollar. Men to'liq variantlardan voz kechdim va ko'pini yoki kamroqini 10 dollarga qurishni boshladim. 10 dollar - bu quyi qismlar kabi ob -havo stantsiyasining asosiy komponentlarining narxi.

Mana bu qismlar:

1. ESP8266 Wemos brendi Aliexpressda 2,39 dollar turadi. Men Wemos brendini tavsiya qilardim, chunki uning EPS8266 -ni dasturlash, yangilash va 4MB yoki undan ko'p fleshka ega bo'lish osonroq.

2. Wemos Charger-Boost Shield narxi $ 1.39 dona. Bu brenddan foydalanishning yana bir afzalligi. Lityum batareyani (nominal kuchlanish = 3.7V) ESP8266 uchun 5V gacha kuchaytiruvchi taxtasi bor. Kengash, shuningdek, maksimal zaryad oqimi = 1M bo'lgan zaryadlash opsiyasi bilan birga keladi.

*Eslatma: Lityum batareyani zaryad qilish/kuchaytirish uchun arzonroq variant mavjud. Bu 5 dona uchun 1,77 dollar turadi. Biroq, men bu taxtani ESP8266 (Wemos yoki yalang'och ESP8266) uchun ishlatganimda, ESP8266 ning chuqur uyqu rejimi ESP8266 uyquni tiklash-uyqu holatiga o'tkazgandan so'ng darhol tiklanishni ishga tushirdi, bu juda zerikarli. Agar nima bo'layotganini bilsangiz, iltimos, menga pochta qutisini yuboring.

3. Wemos -da harorat va namlik uchun bir nechta qalqon bor, lekin men alohida komponentlardan quraman. Fotorezistor (yoki nurga bog'liq qarshilik-ldr, arzon), BH1780 yoki TSL2561 kabi yorug'lik sensori (taxminan 0,87-0,89c dona), DS18B20 kabi harorat sensori (har biri 75c) va namlik va harorat kombinatsiyasi DHT22 (bu erda $ 2.35) yoki SHT21 (bu erda $ 2.20). Sensorning umumiy narxi ~ 4 dollar.

4. Lityum batareya. Men 7.4V Canon batareyasidan qutqarib oldim, bu ikkita 3.7V seriyali yoki 18650 Lityum batareyadir. Har bir 18650 tasi taxminan 5 dollar turadi. Menda kameraning batareya to'plami yiqilganini ko'rsatadigan rasm bor. Ehtiyot bo'ling, plastik qopqog'ini kesib o'tishda qisqa tutashuv haddan tashqari issiqlik va kuyishga olib kelishi mumkin.

5. PCB taxtasi, jumper, sim, lehim, sizning vaqtingiz, ehtimol, disk raskadrovka qobiliyatlari.

Sim qismlarini birgalikda yuqoridagi sxemaga muvofiq keltiring.

Keyin, sozlash tsiklidagi vazifani qidiring. Bu bir martalik vazifalar va uyqu buyrug'i bilan tugaydi.

void setup () {Serial.begin (115200); Serial.println ("Boshlanish tuguni" + String (SENSORNAME)); o'rnatish_wifi (); kechikish (100); Wire.begin (); pinMode (ldrPin, INPUT); SHT21.begin (); if (! tsl.begin ()) {Serial.print ("TSL2561 topilmadi"); vaqt (1); } kechikish (100); ldr = analogRead (ldrPin); tsl.enableAutoRange (rost); tsl.setIntegrationTime (TSL2561_INTEGRATIONTIME_13MS); kechikish (100); sensors_event_t hodisasi; tsl.getEvent (& hodisa); if (event.light) lux = voqea.light; else Serial.println ("Sensorning ortiqcha yuklanishi");

h = SHT21.getHumidity ();

t = SHT21.getTemperature (); tempSensor.setWaitForConversion (noto'g'ri); tempSensor.begin (); kechikish (100); if (tempSensor.getDeviceCount () == 0) {Serial.printf ("DS18x20 pin %d / nda topilmadi", ds18b20); Serial.flush (); kechikish (1000); } kechikish (100); tempSensor.requestTemperatures (); t18 = tempSensor.getTempCByIndex (0); Serial.printf ("\ nLight: %d lux / t", lux); Serial.printf ("LDR: %d /1024 / t", ldr); Serial.printf ("T: %0.2f *C / t", t); Serial.printf ("H:%0.2f / t", h); Serial.printf ("HIC: %0.2f / t", hic); kechikish (100); client.setServer (mqtt_server, mqtt_port); client.setCallback (qayta qo'ng'iroq qilish); qayta ulanish (); kechikish (100); ESP.deepSleep (3e8); // 300 million mikro soniya, 300 soniya, 5 daqiqa; }

Nosozliklarni tuzatish yoki sozlash paytida, ESP.deepsleep () ni ketma -ket o'qishni doimiy ravishda bajarishga buyruq bering. Har doimgidek, ESP8266 -ga yuklash uchun to'liq kod bu erda joylashtirilgan (GitHub).

Chuqur uyqudan keyin uyg'onish uchun RST va D0/GPIO16 orasidagi o'tish moslamasini kiyishni unutmang.

Endi kodni Arduino IDE yordamida ESP8266 -ga yuklash vaqti keldi.

2 -qadam: MQTT: ma'lumotlarni chop etish va obuna bo'lish uchun moslashuvchan vosita

MQTT: ma'lumotlarni nashr etish va obuna bo'lish uchun moslashuvchan vosita
MQTT: ma'lumotlarni nashr etish va obuna bo'lish uchun moslashuvchan vosita
MQTT: ma'lumotlarni nashr etish va obuna bo'lish uchun moslashuvchan vosita
MQTT: ma'lumotlarni nashr etish va obuna bo'lish uchun moslashuvchan vosita

Birinchidan, men o'z uyimdagi turli datchiklar va mijozlar orqali ma'lumotlarni yuborish va qabul qilish uchun MQTT -dan foydalanishni yaxshi ko'raman. Sababi, mavzu bo'yicha toifalarga bo'lingan cheksiz ma'lumotlarni yuborish moslashuvchanligi va cheksiz mijozlar MQTT brokeridan bitta mavzuga obuna bo'lish. Ikkinchidan, men MQTTni chuqur muhokama qilishga layoqatsizman. Men MQTT bilan ba'zan o'tgan yili (2017) Node-RED yordamida ob-havo stantsiyasi va datchiklarni o'rnatish bo'yicha ko'rsatmalarni o'qiganimda bilib oldim. Qanday bo'lmasin, men sizga ba'zi ma'lumotlarni taqdim etishga harakat qilaman. Boshlash uchun yana bir yaxshi joy - bu Vikipediya.

Agar sizda nazariya haqida o'qishga vaqtingiz bo'lmasa va MQTT brokerini o'rnatmoqchi bo'lsangiz, men buning uchun boshqa o'quv qo'llanmasini joylashtirdim. Ushbu xabarni toping va 4 -bosqichga o'ting.

Tushunishim bo'yicha Message Queuing Telemetry Transport (MQTT) nima ekanligini tushuntirish uchun men yuqoridagi kabi diagramma tayyorladim. Xulosa qilib aytganda, MQTT-bu ISO standarti va men chivin va chivin-mijoz kabi mahsulot, men Raspberry Pi-da MQTT brokerini qurgan ikkita paket bu standartga mos kelishi kerak. MQTT brokeri keyinchalik noshirlarga xabar yuborish va obunachilarga maqsadli mavzuni tinglash vositasiga aylanadi.

Arduino PubSubclient kutubxonasining ArduinoJson bilan kombinatsiyasi, uning yaratuvchisi knolleary va bblanchon tufayli, tinkerlar va ishlab chiquvchilarga sensorlardan maqsadli uskunaga yoki oxirgi mijozgacha bo'lgan asboblar to'plamini osonlashtiradi.

Ma'lumotlar bazasini yaratishda davom etamiz va ba'zi ma'lumotlarni ko'rsatamiz.

3 -qadam: Ma'lumotlarni SQL -ga saqlang va ularni veb -serverda ko'rsating

Ma'lumotlarni SQL -ga saqlang va ularni veb -serverda ko'rsating
Ma'lumotlarni SQL -ga saqlang va ularni veb -serverda ko'rsating
Ma'lumotlarni SQL -ga saqlang va ularni veb -serverda ko'rsating
Ma'lumotlarni SQL -ga saqlang va ularni veb -serverda ko'rsating

Men veb -server uchun ma'lumotlar bazasini yaratish uchun sqlite3 dan foydalanardim. Sqlite3 -ni Rapberry Pi -ga quyidagicha o'rnating:

sudo apt-get sqlite3 ni o'rnating

terminalga yozib ma'lumotlar bazasi va jadval yaratdi:

sqlite3 ob -havo stantsiyasi.db

Jadval ob -havo ma'lumotlarini yaratish (id INT PRIMARY KEY, DATETIME vaqti, ldr INT, tls2561 INT, ds18b20 REAL, tsht21 REAL, hsht21 REAL);

.exit // sqlite buyruq satridan chiqish va Linux terminaliga qaytish

Ob -havo stantsiyasi tomonidan chop etilgan mavzuni tinglash uchun men Python bilan Paho kutubxonasidan foydalandim:

#! /usr/bin/python3# qabul qilingan: > # binh nguyen, august 04, 2018, from time import localtime, strftime, sleep import paho.mqtt.client as mqtt import sqlite3, json

mqtt_topic = 'balcony/weatherstation'

mqtt_username = "johndoe" mqtt_password = "password" dbfile = "/path/to/databse/weatherstation.db" mqtt_broker_ip = '192.168.1.50'

# the callback for when the client receives a connack response from the server.

def on_connect(client, userdata, flags, rc): print("connected with result code "+str(rc)) client.subscribe(mqtt_topic) # the callback for when a publish message is received from the server. def on_message(client, userdata, msg): thetime = strftime("%y-%m-%d %h:%m:%s", localtime())

topic = msg.topic

payload = json.dumps(msg.payload.decode('utf-8')) sql_cmd = sql_cmd = """insert into weatherdata values ({0}, '{1}', {2[ldr]}, {2[tsl2561]}, {2[ds18b20]}, {2[tsht21]}, {2[hsht21]})""".format(none, time_, payload) writetodb(sql_cmd) print(sql_cmd) return none

def writetodb(sql_cmd):

conn = sqlite3.connect(dbfile) cur = conn.cursor() cur.execute(sql_command) conn.commit()

client = mqtt.client()

client.on_connect = on_connect client.on_message = on_message client.username_pw_set(username=mqtt_username, password=mqtt_password) client.connect(mqtt_broker_ip, 1883, 60) sleep(1) client.loop_forever()

to display data from use another sql command to query data from the database such as:

sql_command = """ select * from weatherdata order by thetime desc limit 1000;"

this sql command is included in the app.py that uses flask framework and plotty to make a web server and plotting a graph.

the complete code is hosted on the github.

if the esp8266 cannot read the ds18b20, it assigned a value of -127 as the temperature which skews the relative range of other readable temperatures. i cleaned up those values by set a null value to those equals to -127:

sqlite3 weatherstation.db

sqlite3> update weatherdata set ds18b20 = null where ds18b20 = -127;

to set up an environment for this mini web server, i used the shared libraries on raspberry pi. a virtualenv is a better option if the web server is hosted on a powerful computer. start the web server by:

python3 app.py

press control + c to stop the server.

the web server is set to auto-refreshed for every 60 seconds. you can change the interval in index.html file:

battery performance:

i did not measure the current between the normal state or sleep state of esp8266. many others did so. the first google search turned to this page. the normal state of esp8266 consumes about 100ma depends on the rate of transmitting and wifi activity. the deep-sleep state needs in the range of micro a, which a thousand times less.

for 5-minute interval between sleeping and waking up, one single lithium 18650 (2000mah) could fuel my weather station for 12 days. the same battery only enough for esp 8266 ran less than a day with a normal working state. the one i took from the camera battery pack (did not know the capacity) was enough to run the weather station with deep sleep for 5-6 days.

thank you for spending time with me to this end.

Tavsiya: