Last active
May 9, 2016 13:00
Star
You must be signed in to star a gist
NodeMcu + DHT22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Fichier dht22.lua | |
-- | |
-- Permet la lecture de la température et le pourcentage d'humidité | |
-- Envoi les valeurs obtenus dans une socket TCP | |
-- | |
local dht = require 'dht' | |
DHT_PIN = 4 | |
SERVER_IP = "xxx.xxx.xxx.xxx" | |
SERVER_PORT = 8888 | |
DEEP_SLEEP_DURATION = 60000000 | |
conn = net.createConnection(net.TCP, false) | |
-- Lorsque la connexion TCP est réalisé | |
-- Lecture de la température et du pourcentage d'humidité | |
conn:on("connection", function() | |
-- meter = { id = node.chipid(), voltage = adc.readvdd33()} | |
meter = { id = node.chipid() } | |
meter.status, meter.temperature, meter.humidity, temperature_dec, humidity_dec = dht.read(DHT_PIN) | |
conn:send(cjson.encode(meter)) | |
conn:close() | |
end) | |
-- Apres fermeture de la connexion TCP mis en veille prolongée | |
conn:on("disconnection", function() | |
node.dsleep(DEEP_SLEEP_DURATION) | |
end) | |
conn:connect(SERVER_PORT, SERVER_IP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Fichier init.lua | |
-- | |
-- Executé par défaut par le nodeMCU | |
-- Réalise la connexion Wifi | |
-- Appel des fichiers de configuration | |
WIFI_SSID = "xxxxxxxxxxxxxx" | |
WIFI_PASSWORD = "xxxxxxxxxxxxxxxxx" | |
WIFI_MODE = wifi.PHYMODE_G | |
-- Paramètres de connexion wifi | |
wifi.setmode(wifi.STATION) | |
wifi.setphymode(WIFI_MODE) | |
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD) | |
wifi.sta.connect() | |
tmr.alarm(0, 2000, 1, function() | |
-- Validation de la connxion wifi | |
if wifi.sta.status() == 5 then | |
-- Chargement des différents fichiers | |
files = file.list() | |
for k, v in pairs(files) do | |
if k ~= "init.lua" then | |
dofile(k) | |
end | |
end | |
tmr.stop(0) | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment