Cov txheej txheem:

Chaw Nres Nkoj Huab Cua: ESP8266 Nrog Tsaug Zog, SQL, Teeb Duab los ntawm Flask & Plotly: 3 Kauj Ruam
Chaw Nres Nkoj Huab Cua: ESP8266 Nrog Tsaug Zog, SQL, Teeb Duab los ntawm Flask & Plotly: 3 Kauj Ruam

Video: Chaw Nres Nkoj Huab Cua: ESP8266 Nrog Tsaug Zog, SQL, Teeb Duab los ntawm Flask & Plotly: 3 Kauj Ruam

Video: Chaw Nres Nkoj Huab Cua: ESP8266 Nrog Tsaug Zog, SQL, Teeb Duab los ntawm Flask & Plotly: 3 Kauj Ruam
Video: Sib Hawm Dhau (Time Passed) Music Video by: Deeda/Dib Xwb 2024, Lub Xya hli ntuj
Anonim
Chaw Nres Tsheb Huab Cua: ESP8266 Nrog Kev tsaug zog tsaug zog, SQL, Teeb duab los ntawm Flask & Plotly
Chaw Nres Tsheb Huab Cua: ESP8266 Nrog Kev tsaug zog tsaug zog, SQL, Teeb duab los ntawm Flask & Plotly

Puas yog qhov lom zem kom paub qhov kub, av noo, lossis lub teeb ci ntawm koj lub sam thiaj? Kuv paub kuv yuav. Yog li kuv tau ua lub chaw nres tsheb huab cua yooj yim los sau cov ntaub ntawv ntawd. Cov hauv qab no yog cov kauj ruam uas kuv tau ua los tsim ib qho.

Cia peb pib!

Kauj Ruam 1: Huab Cua Chaw Nres Tsheb nrog Lub Teeb, Kub thiab Cov Vaum Sensors

Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Cov Vaum Sensors
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Cov Vaum Sensors
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej
Chaw Nres Tsheb Huab Cua Nrog Lub Teeb, Kub thiab Vaum Cov Cuab Yeej

Thaum kuv npaj tsim lub chaw nres tsheb huab cua, Kuv tau npau suav kom muaj chaw nres tsheb huab cua puv ntoob uas muaj cua nrawm, ntsuas cua nag, ntsuas lub hnub ci puv lub hnub ci, tab sis tig tawm, qhov ntawd yuav tsis pheej yig, thiab kev yuav khoom yuav xaus tsawg kawg $ 100. Kuv tso tag nrho cov kev xaiv thiab pib tsim ib qho nrog $ 10, ntau dua lossis tsawg dua. $ 10 yog tus nqi ntawm cov khoom siv yooj yim ntawm chaw nres tsheb huab cua raws li cov hauv qab no.

Nov yog ntu:

1. ESP8266 Wemos hom khoom raug nqi $ 2.39 pcs ntawm Aliexpress. Kuv xav qhia Wemos hom vim tias nws EPS8266 yog qhov yooj yim rau program, hloov kho, thiab muaj 4MB nyem lossis ntau dua.

2. Wemos Charger-Boost Shield nqi $ 1.39 pcs. Nov yog lwm qhov txiaj ntsig los siv hom khoom no. Nws muaj lub rooj txhawb siab rau Lithium roj teeb (nominal voltage = 3.7V) mus rau 5V rau ESP8266. Lub Rooj Tswjhwm Saib kuj tseem tuaj nrog kev xaiv them nrog qhov them nqi tam sim no = 1M.

*Nco tseg: Muaj qhov pheej yig dua rau Lithium roj teeb them/txhawb nqa. Tus nqi no yog $ 1.77 rau 5 daim. Txawm li cas los xij, thaum kuv siv lub rooj tsav xwm no rau ESP8266 (txawm yog Wemos's lossis liab qab ESP8266), hom pw tsaug zog ntawm ESP8266 ua rau rov pib dua txoj cai tom qab ua ESP8266 hauv lub voj ntawm pw tsaug zog-rov pib dua, uas yog qhov txaus ntshai heev. Yog tias koj paub tias muaj dab tsi tshwm sim, thov hu rau kuv.

3. Wemos kuj muaj ntau daim thaiv rau qhov kub thiab txias tab sis kuv tab tom tsim los ntawm cov khoom ib leeg. Photoresistor (lossis lub teeb nyob ntawm tus tiv thaiv-ldr, pheej yig), lub teeb pom kev zoo xws li BH1780 lossis TSL2561 (li 0.87-0.89c pcs), ntsuas kub xws li DS18B20 (75c txhua), thiab cov av noo thiab qhov sib xyaw ua ke xws li DHT22 ($ 2.35 ntawm no) lossis SHT21 ($ 2.20 ntawm no). Tus nqi tag nrho rau lub sensor ~ $ 4.

4. Lithium roj teeb. Kuv khaws ib qho los ntawm 7.4V Canon Roj Teeb uas yog ob lub roj teeb 3.7V ua ke lossis 18650 Lithium roj teeb. Txhua 18650 tus nqi kwv yees li $ 5 ib leeg. Kuv muaj daim duab qhia pom lub kua muag poob ntawm lub roj teeb pob. Ceev faj txawm li cas los xij, luv-Circuit thaum txiav los ntawm cov yas npog tuaj yeem tsim cua sov, thiab hlawv.

5. PCB board, jumper, xaim, soldering, koj lub sijhawm, tej zaum qee qhov txuj ci debugging.

Cia cov khoom sib txuas ua ke ua raws cov txheej txheem saum toj no.

Tom qab ntawd, saib rau txoj haujlwm hauv kab teeb tsa. Nws tsuas yog ua haujlwm ib zaug thiab xaus los ntawm kev hais kom tsaug zog.

teeb tsa tsis muaj dab tsi () {Serial.begin (115200); Serial.println ("Pib Lub npe hu ua" + Txoj hlua (SENSORNAME)); teeb_wifi (); ncua (100); Hlau.begin (); tus pinMode (ldrPin, INPUT); SHT21.begin (); yog (! tsl.begin ()) {Serial.print ("TSL2561 tsis pom"); thaum (1); } ncua (100); ldr = analogRead (ldrPin); tsl.enableAutoRange (muaj tseeb); tsl.setIntegrationTime (TSL2561_INTEGRATIONTIME_13MS); ncua (100); sensors_event_t kev tshwm sim; tsl.getEvent (& kev tshwm sim); yog (event.light) lux = event.light; lwm yam Serial.println ("Sensor tshaj");

h = SHT21.getHumidity ();

t = SHT21.getTemperature (); tempSensor.setWaitForConversion (tsis tseeb); tempSensor.begin (); ncua (100); yog (tempSensor.getDeviceCount () == 0) {Serial.printf ("DS18x20 tsis pom ntawm tus pin %d / n", ds18b20); Serial.flush (); ncua (1000); } ncua (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); ncua (100); client.setServer (mqtt_server, mqtt_port); client.setCallback (hu rov qab); rov txuas dua (); ncua (100); ESP.deepSleep (3e8); // 300 lab micro vib nas this, 300 vib nas this, 5 feeb; }

Thaum lub sijhawm debugging lossis teeb tsa, hais kom ESP.deeps pw () kom muaj Serial nyeem txuas ntxiv mus tas li. Raws li ib txwm muaj, tag nrho cov cai txhawm rau upload rau ESP8266 tau tuav ntawm no (GitHub).

Nco ntsoov tso lub jumper nruab nrab ntawm RST thiab D0/GPIO16 kom ua rau muaj kev sawv tom qab pw tsaug zog ib ntus.

Tam sim no, sijhawm los tso cov cai siv Arduino IDE rau ESP8266.

Kauj Ruam 2: MQTT: Qhov Nruab Nrab Yooj Yim rau Tshaj Tawm thiab Sau Npe Cov Ntaub Ntawv

MQTT: Lub Nruab Nrab Yooj Yim rau Tshaj Tawm thiab Sau Npe Cov Ntaub Ntawv
MQTT: Lub Nruab Nrab Yooj Yim rau Tshaj Tawm thiab Sau Npe Cov Ntaub Ntawv
MQTT: Qhov Nruab Nrab Yooj Yim rau Tshaj Tawm thiab Sau Npe Cov Ntaub Ntawv
MQTT: Qhov Nruab Nrab Yooj Yim rau Tshaj Tawm thiab Sau Npe Cov Ntaub Ntawv

Ua ntej, Kuv tau nyiam siv MQTT xa thiab tau txais cov ntaub ntawv hla cov ntsuas sib txawv thiab cov neeg siv khoom hauv kuv lub tsev. Qhov ntawd yog vim qhov yooj yim xa cov ntaub ntawv tsis txwv los ntawm cov ncauj lus, thiab cov neeg siv tsis txwv kom sau npe mus rau ib lub ncauj lus los ntawm MQTT broker. Qhov thib ob, Kuv tsis tsim nyog los tham txog MQTT qhov tob. Kuv tau paub MQTT qee zaum xyoo tas los (2017) thaum ua raws cov lus qhia los teeb tsa lub chaw nres tsheb huab cua thiab ntsuas ntsuas siv Node-RED. Txawm li cas los xij, Kuv yuav sim kuv qhov zoo tshaj plaws los nthuav qhia rau koj qee cov ntaub ntawv. Lwm qhov chaw zoo pib yog Wikipedia.

Yog tias koj tsis muaj sijhawm los nyeem txog qhov kev xav, thiab xav teeb tsa MQTT broker, kuv tshaj tawm lwm qhov kev qhia tsuas yog ua li ntawd. Saib cov ntawv no, thiab nqes mus rau Kauj Ruam 4.

Txhawm rau piav qhia dab tsi yog Message Queuing Telemetry Transport (MQTT) hauv kuv qhov kev nkag siab, kuv tau npaj daim duab qhia saum toj no. Hauv qhov tseeb, MQTT yog tus txheej txheem ISO, thiab cov khoom lag luam xws li mos txwv thiab mositto-tus neeg siv khoom, ob pob khoom kuv tau siv tsim MQTT tus tswv lag luam ntawm Raspberry Pi, yuav tsum ua raws li tus txheej txheem ntawd. MQTT tus neeg muag khoom tom qab ntawd dhau los ua tus nruab nrab rau cov tshaj tawm txhawm rau thawb cov lus rau hauv thiab cov neeg siv yuav mloog cov ncauj lus tseem ceeb.

Kev sib xyaw ntawm Arduino PubSubclient lub tsev qiv ntawv nrog ArduinoJson, ua tsaug rau nws tus tsim lub hauv paus thiab bblanchon, ua kom yooj yim dua rau cov tinkers thiab cov tsim tawm rau txheej txheej ntawm cov cuab yeej los ntawm cov cuab yeej mus rau cov cuab yeej siv khoom lossis cov neeg siv kawg.

Cia txav mus nrog tsim Database thiab tso saib qee cov ntaub ntawv.

Kauj Ruam 3: Txuag Cov Ntaub Ntawv rau SQL thiab Muab Tso rau ntawm Web Server

Txuag Cov Ntaub Ntawv rau SQL thiab Qhia Lawv ntawm Web Server
Txuag Cov Ntaub Ntawv rau SQL thiab Qhia Lawv ntawm Web Server
Txuag Cov Ntaub Ntawv rau SQL thiab Qhia Lawv ntawm Web Server
Txuag Cov Ntaub Ntawv rau SQL thiab Qhia Lawv ntawm Web Server

Kuv siv sqlite3 los tsim lub hauv paus rau lub web server. Nruab sqlite3 hauv Rapberry Pi los ntawm:

sudo apt-tau nruab sqlite3

tsim cov ntaub ntawv thiab lub rooj los ntawm kev ntaus rau hauv lub davhlau ya nyob twg:

sqlite3 weatherstation.db

Tsim cov ntaub ntawv huab cua (id INT PRIMARY KEY, sijhawm DATETIME, ldr INT, tls2561 INT, ds18b20 REAL, tsht21 REAL, hsht21 REAL)

.exit // kom tawm ntawm kab lus sqlite thiab rov qab mus rau Linux davhlau ya nyob twg

Txhawm rau mloog cov ncauj lus tshaj tawm los ntawm chaw nres tsheb huab cua, Kuv tau siv Paho tsev qiv ntawv nrog Python:

#! /usr/bin/python3# tau txais los ntawm: > # 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.

Pom zoo: