Cov txheej txheem:

Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff: 4 Cov Kauj Ruam
Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff: 4 Cov Kauj Ruam

Video: Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff: 4 Cov Kauj Ruam

Video: Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff: 4 Cov Kauj Ruam
Video: Tsis Paub Yuav Ua Li Cas 2024, Hlis ntuj nqeg
Anonim
Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff
Yuav Ua Li Cas Siv MQTT Nrog Raspberry Pi thiab ESP8266/sonoff

Nyob Zoo Txhua Tus!

Hnub no kuv yuav qhia koj yuav ua li cas teeb tsa raspberry pi thiab ESP8266 raws li sonoff wifi relay hloov los tswj koj cov cuab yeej siv hauv tsev los ntawm txhua qhov chaw hauv ntiaj teb.

Los ntawm qhov kawg ntawm cov lus qhia no, yog tias koj tau ua tib zoo ua raws kuv cov lus qhia, koj yuav tuaj yeem tswj tau ib yam ntawm koj cov cuab yeej siv hauv tsev los ntawm Raspberry Pi lub davhlau ya nyob twg lossis los ntawm kev siv cov nab nab.

Yog li npaj tau los ua koj tus kheej MQTT thiab Rpi raws li Sonoff Wifi Device tswj!

Ua ntej pib qhov kev qhia no Kuv xav hais txog ob peb yam tseem ceeb,

  1. Sonoff wifi txuas hloov mus los nrog nws tus kheej tswj hwm firmware. ib zaug koj rov tsim dua nws, thawj daim ntawv thov raug tshem tawm tas li. Yog li ua kom paub tseeb tias.
  2. Sonoff wifi kis tau tus mob hloov ncaj qha los ntawm 230V AC qhov hluav taws xob, yog li yog tias koj tsis yog tus kws tshaj lij hauv kev tuav cov hluav taws xob zoo li no pab koj niam thiab txiv lossis tus kws tshaj lij.
  3. Sonoff wifi hloov pauv tau yooj yim raws li ib puag ncig esp8266-01 wifi chipset uas yog 3.3V nti, yog tias koj yuam kev thov 5V rau nws +V tus pin koj yuav ua rau nws puas.
  4. Nov rau qhov kev qhia no, Kuv tau txiav txim siab tias koj muaj koj li raspberry pi txhua qhov teeb tsa thiab wifi tau qhib nrog IP chaw nyob kom khiav cov mos txwv thiab MQTT cov lus txib hauv Terminal

** Yog tias koj npaj txhij nrog txhua 4 qib saum toj no koj zoo mus !!

Kauj ruam 1: Taw qhia + Khoom siv

Taw qhia + Khoom siv
Taw qhia + Khoom siv
Taw qhia + Khoom siv
Taw qhia + Khoom siv

Nyob Zoo Cov Menyuam! hnub no peb tab tom tsim ib txoj haujlwm zoo heev. Ntawm no hauv txoj haujlwm no, Peb tab tom yuav tswj peb cov khoom siv hauv tsev xws li AC teeb siv MQTT tus tswv lag luam.

Nco tseg: Nov yog IOT Project li no koj tuaj yeem tswj hwm koj lub cuab yeej AC los ntawm txhua qhov chaw hauv ntiaj teb

MQTT (Massage Que Telemetry Transport) yog ib qho yooj yim payload Hloov raws tu qauv siv hauv qhov tseeb ntawm Cov phiaj xwm Web. Facebook tus xa xov kuj tseem siv MQTT raws tu qauv. txaus ntawm kev qhia cia pib nrog Project.

Peb xav tau dab tsi rau Txoj Haujlwm no?

1) Siv Wifi (nyiam dua router) txuas

2) Raspberry Pi3 tus qauv uas muaj wifi qhib rau nws.

3) Sonoff Wifi IOT hloov nrog 10A 220V

4) Qee qhov txuas txuas

5) Jumper Cables

6) CP2102 USB rau TTL hloov pauv

  • Nco tseg: rau txoj haujlwm no, koj yuav tsum muaj raspberry pi txhua qhov teeb tsa nrog saib xyuas cov keyboard txuas lossis hauv lub taub hau hom nrog koj lub laptop (VNC txuas)
  • yog tias koj tsis muaj raspberry pi txhua qhov teeb tsa nrog saib lossis tsis muaj lub taub hau, koj yuav tsum ua raws li kuv lwm qhov kev qhia yuav ua li cas txuas raspberry pi hauv hom tsis muaj hau.

Kauj Ruam 2: Teeb Raspberry Pi

Teeb tsa Raspberry Pi
Teeb tsa Raspberry Pi

Hauv Cov Kauj Ruam no, peb yuav teeb tsa peb lub raspberry pi rau Txoj Haujlwm.

hauv qhov kev qhia no, peb siv sonoff ESP8266 raws li tus neeg siv khoom thiab raspberry pi ua tus neeg muag khoom mos txwv.

  1. qhib koj lub raspberry pi davhlau ya nyob twg thiab ntaus

    1. sudo apt-tau hloov tshiab
    2. sudo apt-tau hloov kho dua Ib zaug Koj raspberry Pi tau hloov kho nrog lub khoos phis tawm tshiab kawg koj zoo mus nrog ib qhov haujlwm ntawm koj tus pi
  2. Tam sim no ntaus cov lus txib hauv qab no rau nruab Mosquitto

    1. wget
    2. sudo apt-key ntxiv mosquitto-repo.gpg.key
    3. cd /etc/apt/sources.list.d/
    4. sudo wget
    5. sudo -i ua
    6. apt-tau hloov tshiab
    7. apt-tau nruab mosquitto
    8. apt-tau nruab mosquitto-cov neeg siv khoom
  3. Thaum koj tau ua tiav tag nrho cov lus qhia saum toj no koj muaj koj lub tshuab npaj nrog mosquitto broker teeb tsa thiab koj tau npaj rau qib tom ntej.
  4. Ib zaug ntxiv hauv koj lub davhlau ya nyob twg ntaus cov lus txib hauv qab no:
    1. sudo pip nruab paho-mqtt
    2. sudo pip3 nruab paho-mqtt
  5. Tam sim no txhawm rau tshuaj xyuas seb koj tus neeg siv mositto tau ua tiav lossis tsis qhib python2 lossis python3 plhaub thiab ntaus cov lus hauv qab no

    1. ntshuam paho.mqtt.client
    2. qhov no yuav tsum tsis rov qab dab tsi. yog tias rov qab ib txoj kab lossis kev ua yuam kev kos cov kauj ruam saum toj no dua thiab ua raws cov theem ua tib zoo ib zaug ntxiv.
  6. Tam sim no koj Raspberry Pi Ib feem yuav luag tiav thiab koj zoo mus rau Sonoff thiab Arduino ib feem tam sim no

Kauj Ruam 3: Teeb tsa Sonoff Esp8266 Device rau MQTT

Teeb tsa Sonoff Esp8266 Ntaus rau MQTT
Teeb tsa Sonoff Esp8266 Ntaus rau MQTT
Teeb tsa Sonoff Esp8266 Ntaus rau MQTT
Teeb tsa Sonoff Esp8266 Ntaus rau MQTT
Teeb tsa Sonoff Esp8266 Ntaus rau MQTT
Teeb tsa Sonoff Esp8266 Ntaus rau MQTT

Tam sim no hauv theem no, peb yuav teeb tsa peb cov neeg siv khoom uas yog Sonoff.

sonoff yog lub cuab yeej siv hluav taws xob hauv tsev uas los nrog kev teeb tsa ua ntej rau wifi-raws li kev siv lub tsev thiab hauv/iOS daim ntawv thov los tswj cov khoom siv hla wifi.

Tam sim no ntawm no yog qhov yooj yim hack rau reprogramme nws hauv txoj kev uas peb xav tau. ib zaug reprogramme nrog Arduino lub tswv yim koj yuav plam thawj lub khoos phis tawj ua ntej ntawm sonoff.

  1. ua ntej, qhib koj lub cuab yeej sonoff ua tib zoo thiab saib rau pinouts pom hauv daim duab DANGER: sonoff ntaus ntawv ua haujlwm ntawm 220V ncaj qha AC koj yuav tsum ntsaws nws ua ntej ua ntej qhib lossis programming.
  2. Tam sim no pom cov duab nqes rau nws qhov pinout, ntawm sab hauv qab ntawm PCB muaj ib qho esp8266 nti uas koj pom zoo ib yam li koj li esp8266-01 module.
  3. Tam sim no qhib Arduino IDE thiab ntxiv qhov tsim nyog Zip cov tsev qiv ntawv. PUBSUB_client Library
  4. Tsis tas li, koj yuav tsum muaj pawg esp8266 ntxiv rau koj lub tswv yim Arduino. yog tias tsis tau ntxiv ua raws cov kauj ruam

    1. Qhib koj Arduino IDE thiab nyem rau "File -> Preferences".
    2. "https://arduino.esp8266.com/stable/package_esp8266com_index.json"
    3. Hauv "Tus Thawj Saib Xyuas Haujlwm Ntxiv URLs" ntxiv qhov txuas saum toj no thiab nyem rau "OK"
    4. Mus rau "Cov Cuab Yeej -> Pawg Thawj Coj -> Tus Thawj Saib Xyuas", ntaus "ESP8266" thiab nruab nws.
    5. Rov mus dua rau "Cov Cuab Yeej -> Pawg Thawj Coj" thiab xaiv "Generic ESP8266 Module".
    6. Tam sim no koj tau ua tiav ntxiv lub rooj tsavxwm rau koj Arduino IDE.
  5. Tam sim no koj yuav tsum tau txuas ib tus poj niam txuas rau lub sonoff ntaus ntawv raws li qhia hauv daim duab.
  6. Tam sim no coj tus USB rau TTL CP2102 lossis FTDI pawg thawj coj thiab ua cov kev txuas hauv qab no raws li cov duab pom saum toj no.

    1. RX of FTDI to TX of Sonoff
    2. TX of FTDI to RX of Sonoff
    3. GND ntawm FTDI rau GND ntawm Sonoff
    4. 3.3V ntawm FTDI rau 3.3V ntawm Sonoff
  7. NCO TSEG: Nco ntsoov tias koj tsis tau muab lub rooj tsav xwm sonoff nrog 5V. nws yog 3.3V sib xws yog tias koj txuas 5V rau nws tus Pin fais fab, koj tuaj yeem hlawv koj lub cuab yeej sonoff. Li no zoo dua siv 3.3v muab FTDI board nkaus xwb.
  8. tam sim no ua qhov txuas raws li qhia hauv duab.
  9. Ib qho koj tau ua tiav nrog cov kauj ruam saum toj no. txuas koj lub FTDI/USB rau TTL board rau laptop/PC thiab qhib Arduino IDE.
  10. Luam cov cai.

11. Tam sim no xaiv qhov tsim nyog COM chaw nres nkoj los ntawm Cov cuab yeej ntawv qhia zaub mov thiab xaiv lub cuab yeej ua ib qho khoom siv ESP8266. Tam sim no ntaus lub pob upload. Nws yuav siv sijhawm li ib feeb lossis tsawg dua los tso cov cai rau hauv esp8266 nti ntawm sonoff ntaus ntawv.

12. Tam sim no koj tau teeb tsa nrog sonoff thiab Arduino programming. thiab tam sim no koj yuav tsum pom cov coj ntawm sonoff board ntsais muag // raws li peb tau sau 5 zaug ntsais muag thaum pib.

13. Koj zoo mus rau kauj ruam tom ntej.

Kauj Ruam 4: Tswj Koj Tus Menyuam Los Ntawm Lub Vev Xaib

Tswj Koj Sonoff Los Ntawm Lub Vev Xaib
Tswj Koj Sonoff Los Ntawm Lub Vev Xaib

Tam sim no, qhov no yog kauj ruam kawg rau peb txoj haujlwm.

  1. tam sim no qhib koj VNC chaw taws teeb duab sib txuas ntawm raspberry pi los ntawm kev nkag mus rau tus IP chaw rau koj Pi
  2. tam sim no qhib lub davhlau ya nyob twg thiab pib ntaus cov lus txib hauv qab no
  3. ntawm no coj yog lub ntsiab lus thiab "0" yog them nyiaj
  4. hloov tus IP chaw nyob hauv kev hais kom ua nrog koj Rpi IP chaw nyob
  5. Thiab es tsis txhob "0" sau "1" uas yuav qhib rau koj txuas ntawm lub sonoff wifi hloov.

mosquitto_pub -h 192.168.0.104 -t coj -m "0"

Cov lus qhia saum toj no yuav tua koj lub relay.

mosquitto_pub -h 192.168.0.104 -t coj -m "1"

Cov lus qhia no yuav tig rau koj qhov txuas.

Tam sim no koj tau ua tiav nrog kev tswj hwm koj li sonoff los ntawm Rpi davhlau ya nyob twg.

koj tuaj yeem tshawb xyuas los ntawm kev txuas lub sonoff wifi hloov mus rau lwm lub network wifi sib txawv thiab raspberry pi mus rau kev sib txuas wifi sib txawv. Koj yuav tsum muaj peev xwm tswj hwm kev xa tawm los ntawm txhua qhov chaw hauv ntiaj teb.

Tam sim no txawm tias ntxiv koj xav sim ON/OFF ntawm koj sonoff txiav kuv tau sau ib tsab ntawv nab nab

uas xa "1" ua ntej thiab tom qab 6 vib nas this nws xa "0" thiab cov txheej txheem no rov ua dua hauv lub voj.

ntshuam paho.mqtt.publish li luam tawm lub sijhawm

Thaum Muaj Tseeb:

print ("Xa 1 …") luam tawm.single ("ledStatus", "1", hostname = "Koj tus broker IP") time.sleep (6) print ("Sending 0 …") publish.single ("ledStatus", "0", hostname = "Koj tus broker IP") time.sleep (3)

Cov cai saum toj no yooj yim tig rau thiab tawm ntawm qhov txuas ntawm lub sonoff wifi relay hloov.

tsis txhob hnov qab hloov koj li RaspberryPi tus IP chaw nyob hauv tus nab npawb nab npawb saum toj no. tam sim no khiav cov ntawv no los ntawm txhaj rau qhov xaiv khiav module thiab koj yuav tsum pom ntawm lub plhaub

xa "1"..

xa "0"..

xa "1".. tej yam zoo li no.

Nov yog nws rau Cov Lus Qhia no. ntsib koj tom ntej nrog tus tshiab.

Nyob zoo !!!!

thiab tsis txhob hnov qab ua raws thiab qhia cov lus qhia no thiab sim nws ntawm koj tus kheej!

ib qho kev ua xyem xyav tshaj tawm hauv nqe lus pom zoo txais tos!

Pom zoo: