elektronik:nodemcu_esp8266_erste_schritte
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende ÜberarbeitungLetzte ÜberarbeitungBeide Seiten der Revision | ||
elektronik:nodemcu_esp8266_erste_schritte [2019/01/25 20:38] – [Material] gpipperr | elektronik:nodemcu_esp8266_erste_schritte [2019/01/31 22:07] – [Version 2 für beide Sensoren] gpipperr | ||
---|---|---|---|
Zeile 459: | Zeile 459: | ||
-- Konfig Tabelle für den Zugriff | -- Konfig Tabelle für den Zugriff | ||
station_cfg={} | station_cfg={} | ||
- | --station_cfg.ssid=" | ||
- | --station_cfg.pwd=" | ||
station_cfg.ssid=" | station_cfg.ssid=" | ||
Zeile 807: | Zeile 805: | ||
* https:// | * https:// | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ---- | ||
+ | ==== Version 2 für beide Sensoren ==== | ||
+ | |||
+ | <code lua> | ||
+ | |||
+ | --init.lua | ||
+ | -- Modus definieren - Client | ||
+ | wifi.setmode(wifi.STATION) | ||
+ | |||
+ | -- Konfig Tabelle für den Zugriff | ||
+ | station_cfg={} | ||
+ | |||
+ | |||
+ | station_cfg.ssid=" | ||
+ | station_cfg.pwd=" | ||
+ | |||
+ | |||
+ | -- Konfig nicht im Flash speichern | ||
+ | station_cfg.save=false | ||
+ | wifi.sta.config(station_cfg) | ||
+ | |||
+ | -- Anmelden | ||
+ | wifi.sta.connect() | ||
+ | |||
+ | -- zeit stellen | ||
+ | -- hier über ntp dienst Zeit abfragen und richtig setze | ||
+ | -- SNTP Module einbinden! | ||
+ | -- | ||
+ | -- Single shot sync time with a server on the local network. | ||
+ | |||
+ | -- im ersten Schritt einfach setzen .-) | ||
+ | --rtctime.set(1548237072, | ||
+ | |||
+ | |||
+ | -- je 1000 ms warten und prüfen | ||
+ | tmr.alarm(1, | ||
+ | -- prüfe ob die Verbindung schon geklappt hat | ||
+ | if wifi.sta.getip() == nil then | ||
+ | print(" | ||
+ | else | ||
+ | |||
+ | ip, nm,gateway = wifi.sta.getip() | ||
+ | |||
+ | -- Konfiguration ausgeben | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | -- Timer stop | ||
+ | | ||
+ | |||
+ | end | ||
+ | end) | ||
+ | ------------- | ||
+ | |||
+ | -- je 1000 ms warten und prüfen | ||
+ | -- Uhrzeit setzen beim start | ||
+ | -- Time dann auf 1h setzen, dann wieder Uhrzeit neu setzen | ||
+ | |||
+ | |||
+ | ntptimer =tmr.create() | ||
+ | ntptimer: | ||
+ | -- prüfe ob die Verbindung schon geklappt hat | ||
+ | if wifi.sta.getip() == nil then | ||
+ | print(" | ||
+ | else | ||
+ | print(" | ||
+ | -- in my configuration the gateway has ntp feature on | ||
+ | ip, nm,gateway = wifi.sta.getip() | ||
+ | sntp.sync(gateway, | ||
+ | function(sec, | ||
+ | print(' | ||
+ | rtctime.set(sec, | ||
+ | end, | ||
+ | function() | ||
+ | | ||
+ | end | ||
+ | ) | ||
+ | |||
+ | -- get the time | ||
+ | sec, usec, rate = rtctime.get() | ||
+ | print(" | ||
+ | print(" | ||
+ | |||
+ | -- set the time time to one hour | ||
+ | ntptimer: | ||
+ | | ||
+ | |||
+ | end | ||
+ | end) | ||
+ | |||
+ | ntptimer: | ||
+ | |||
+ | |||
+ | --------------------------------- | ||
+ | --- Eigentliche Ausleselogik aufrufen | ||
+ | |||
+ | -- an welchen Pin ist die Datenleitung angeschlossen | ||
+ | avg_temp=0 | ||
+ | avg_humi=0 | ||
+ | avg_counter=0 | ||
+ | total_humi=0 | ||
+ | total_temp=0 | ||
+ | |||
+ | ------------ Sensor wählen -------------------- | ||
+ | -- welcher Sensor ist angeschlossen | ||
+ | -- sensor_type = " | ||
+ | sensor_type = " | ||
+ | |||
+ | -- | ||
+ | -- configure Sensor | ||
+ | if sensor_type == " | ||
+ | gpio_pin=2 | ||
+ | end | ||
+ | |||
+ | ----------- | ||
+ | if sensor_type == " | ||
+ | sda, scl = 2, 1 | ||
+ | i2c.setup(0, | ||
+ | print(" | ||
+ | model, version, serial = am2320.setup() | ||
+ | print(" | ||
+ | print(" | ||
+ | print(" | ||
+ | end | ||
+ | | ||
+ | --- | ||
+ | -- werte alle 30 Sekunden die Daten aus | ||
+ | -- schreibe alle 5 minuten in die DB | ||
+ | tmr.alarm(3, | ||
+ | -- prüfe ob die Verbindung schon geklappt hat | ||
+ | if wifi.sta.getip() == nil then | ||
+ | print(" | ||
+ | else | ||
+ | print(" | ||
+ | -- get the time | ||
+ | sec, usec, rate = rtctime.get() | ||
+ | print(" | ||
+ | print(" | ||
+ | | ||
+ | -- rufe die eigentliche Logik auf | ||
+ | if sensor_type == " | ||
+ | humi, temp = am2320.read() | ||
+ | humi=humi/ | ||
+ | temp=temp/ | ||
+ | print(" | ||
+ | | ||
+ | end | ||
+ | | ||
+ | if sensor_type == " | ||
+ | -- Werte auslesen | ||
+ | status, temp, humi, temp_dec, humi_dec = dht.read(gpio_pin) | ||
+ | -- prüfen ob werte gefunden wurden | ||
+ | if status == dht.OK then | ||
+ | -- Wenn Float Firmware im Einsatz | ||
+ | -- können auch Nachkommastellen ausgegeben werden | ||
+ | humi=humi; | ||
+ | temp=temp; | ||
+ | print(" | ||
+ | | ||
+ | elseif status == dht.ERROR_CHECKSUM then | ||
+ | | ||
+ | elseif status == dht.ERROR_TIMEOUT then | ||
+ | | ||
+ | end | ||
+ | end | ||
+ | |||
+ | -- build the avg of the last | ||
+ | total_humi=total_humi+humi; | ||
+ | total_temp=total_temp+temp; | ||
+ | avg_counter=avg_counter+1; | ||
+ | |||
+ | -- nur den mittelwert der Daten übertragen | ||
+ | if avg_counter == 10 then | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | end | ||
+ | | ||
+ | end | ||
+ | end) | ||
+ | |||
+ | -------------------- | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </ |
elektronik/nodemcu_esp8266_erste_schritte.txt · Zuletzt geändert: 2021/12/12 15:34 von gpipperr