nodeMCU dimmer/switch/temp.sensor without arduino

9 years 8 months ago #729 by Dennis
First of all, nice to hear you got it working.

Regarding "christmas": do you have easyIoT open on more than one device?

If I have easyIoT opened on smartphone and pc at the same time, and change dimmer value, it sometimes goes crazy with the LEDs for me too. But the node itself is not the problem, it is more like if easyIoT is going into some kind of race condition, and values start changing rapidly and endlessly in the easyIoT console. It will never recover by itself from this state. If I restart easyIoT server, everythings fine again...

As long as I just open easyIoT one one device, everything is fine. I think this is a bug in easyIoT, since automation is strictly triggered by events, which happen on server only (at least this is my understanding). If I trigger an event on one client by changing dimmer value, it should change the GUI of other clients to display the changed value, but no other events *should* be triggered subsequently from other clients because of GUI changes!

But obviously, something changes the modules values from more than one location (read: from the clients), which induces said "loop frenzy" or christmas behavior when >1 client ist connected to easyIoT server.

Which easyIoT build are you using, 0.7b2 beta? And running on windows or raspberry?

The LUA script does not recognize anything - it just receives commands for gpio0 or 2 over wifi, and executes them. No matter what you connect to the gpio.

And yes, DHT11/22 and also BMP180 should work too, I'm waiting for shipments from china so I can develop something in this direction! :)

See here whats possible with nodeMCU's stock LUA modules:
github.com/nodemcu/nodemcu-firmware/tree/master/lua_modules

But, if we connect two sensors, then we have no more gpio's left for dimming and switching... so I just ordered some bigger ESP modules with more (hopefully all?) gpio's broken out, for testing. We'll see.

The lua DHT11 script is a good find! I've found something similar for ds18b20 here: vaasa.hacklab.fi/2015/01/12/esp8266-ds18b20-thingspeak-nodemcu/

Seems like nodeMCU community is growing quickly. :)

regards

Please Log in or Create an account to join the conversation.

9 years 8 months ago #730 by cdj

Dennis wrote: First of all, nice to hear you got it working.

Regarding "christmas": do you have easyIoT open on more than one device?

If I have easyIoT opened on smartphone and pc at the same time, and change dimmer value, it sometimes goes crazy with the LEDs for me too. But the node itself is not the problem, it is more like if easyIoT is going into some kind of race condition, and values start changing rapidly and endlessly in the easyIoT console. It will never recover by itself from this state. If I restart easyIoT server, everythings fine again...


Yes, i've restarted my easyiot (running on raspy b+)
and seems to be more stable with only one easyiot web interface opened... but not very stable...could be better!

As long as I just open easyIoT one one device, everything is fine. I think this is a bug in easyIoT, since automation is strictly triggered by events, which happen on server only (at least this is my understanding). If I trigger an event on one client by changing dimmer value, it should change the GUI of other clients to display the changed value, but no other events *should* be triggered subsequently from other clients because of GUI changes!

But obviously, something changes the modules values from more than one location (read: from the clients), which induces said "loop frenzy" or christmas behavior when >1 client ist connected to easyIoT server.

Which easyIoT build are you using, 0.7b2 beta? And running on windows or raspberry?

Yes V0.7b2 running on Raspberry B+

The LUA script does not recognize anything - it just receives commands for gpio0 or 2 over wifi, and executes them. No matter what you connect to the gpio.

Ok perfect...

And yes, DHT11/22 and also BMP180 should work too, I'm waiting for shipments from china so I can develop something in this direction! :)

See here whats possible with nodeMCU's stock LUA modules:
github.com/nodemcu/nodemcu-firmware/tree/master/lua_modules

But, if we connect two sensors, then we have no more gpio's left for dimming and switching... so I just ordered some bigger ESP modules with more (hopefully all?) gpio's broken out, for testing. We'll see.

Ok or we can use more than 1 module !!!
For example we can use a module for dht and bmp and other one for dimming and relaying.....


The lua DHT11 script is a good find! I've found something similar for ds18b20 here: vaasa.hacklab.fi/2015/01/12/esp8266-ds18b20-thingspeak-nodemcu/

Seems like nodeMCU community is growing quickly. :)

regards


Yes but here i need help.
I've put in init.lua first the dht scripts and then your scripts with

getTemp()
conn:send(Temperature)

but i've obtained just two things :

1) When i reset esp it gives me "insufficient memory" or something similar

2) firmware start and restart in loop (need to reflash module)

obviously tested with 0.9.4 and 0.9.5....

Do you think that you can try to put only necessary code in your init.lua? I'm not a great programmer, as you had understood :P

Please !
Thanks a lot
Dario

Please Log in or Create an account to join the conversation.

9 years 8 months ago - 9 years 8 months ago #731 by Dennis
I found a DHT11 for testing, so here's some working code.

First, upload the following "dht22.lua" (just slightly modified version of library obtained from nodeMCU@github):
------------------------------------------------------------------------------
-- DHT11/22 query module
--
-- LICENCE: http://opensource.org/licenses/MIT
-- Vladimir Dronnikov <dronnikov@gmail.com>
--
-- Example:
-- print("DHT11", dofile("dht22.lua").read(4))
-- print("DHT22", dofile("dht22.lua").read(4, true))
-- NB: the very first read sometimes fails
------------------------------------------------------------------------------
local M
do
  -- cache
  local gpio = gpio
  local val = gpio.read
  local waitus = tmr.delay
  --
  local read = function(pin, dht22)
    -- wait for pin value
    local w = function(v)
      local c = 255
      while c > 0 and val(pin) ~= v do c = c - 1 end
      return c
    end
    -- NB: we preallocate incoming data buffer
    --     or precise timing in reader gets broken
    local b = { 0, 0, 0, 0, 0 }

    -- kick the device
    gpio.mode(pin, gpio.INPUT, gpio.PULLUP)
    gpio.write(pin, 1)
    waitus(10)
    gpio.mode(pin, gpio.OUTPUT)
    gpio.write(pin, 0)
    waitus(20000)
    gpio.write(pin, 1)
    gpio.mode(pin, gpio.INPUT, gpio.PULLUP)
    -- wait for device presense
    if w(0) == 0 or w(1) == 0 or w(0) == 0 then
      return nil, 0
    end
    -- receive 5 octets of data, msb first
    for i = 1, 5 do
      local x = 0
      for j = 1, 8 do
        x = x + x
        if w(1) == 0 then return nil, 1 end
        -- 70us for 1, 27 us for 0
        waitus(30)
        if val(pin) == 1 then
          x = x + 1
          if w(0) == 0 then return nil, 2 end
        end
      end
      b[i] = x
    end
    -- check crc. NB: calculating in receiver loop breaks timings
    local crc = 0
    for i = 1, 4 do
      crc = (crc + b[i]) % 256
    end
    if crc ~= b[5] then return nil, 3 end
    -- convert
    local t, h
    -- DHT22: values in tenths of unit, temperature can be negative
    if dht22 then
      h = (b[1] * 256 + b[2])
      t = (b[3] * 256 + b[4])
      if t > 0x8000 then t = -(t - 0x8000) end
    -- DHT11: no negative temperatures, only integers
    -- NB: return in 0.1 Celsius
    else
      h = (10 * b[1])
      t = (10 * b[3])
    end
    return t
  end
  -- expose interface
  M = {
    read = read,
  }
end
return M

then, this init.lua:
--
--print("init")
--dofile("dht22.lua").read(4)
--tmr.delay(1000000)
--print("read")
--myval = dofile("dht22.lua").read(4)
--print(myval)
--


pwm.setup(3, 1000, 000)
pwm.start(3)

LED1_current=000
LED1_target=000

SWITCH1_state = 0

Fadetime1=1000

Stepcounter1=0
PosStepcounter1=0
DimTimer1=0


wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PASS")



srv=net.createServer(net.TCP) 
srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload) 
   
    print("Input: "..payload) 
 
	if string.find(payload,"GETTEMP") then
	 print("Received temperature request")
	 --print("init")
      dofile("dht22.lua").read(4)
      tmr.delay(1000000)
      print("read")
      myval = (dofile("dht22.lua").read(4)/10)
      print(myval)
	 conn:send(myval)

     elseif string.find(payload,"SWITCH1") then
     SWITCH1_state=tonumber(string.sub(payload, 9) )
     print("Received SWITCH1 target Value: "..SWITCH1_state)
     if SWITCH1_state == 1 then
          pwm.setduty(3, 1023)
          LED1_current = 1023
      elseif SWITCH1_state == 0 then
          pwm.setduty(3, 0)
          LED1_current = 0
        end
     

     
    elseif string.find(payload,"LED1") then
     LED1_target=tonumber(string.sub(payload, 13) )
     print("Received LED1 Target Value: "..LED1_target)

     Stepcounter1=(LED1_target)-(LED1_current)
     
     if (Stepcounter1) < 0 then
      PosStepcounter1=(Stepcounter1)*-1
      else PosStepcounter1=(Stepcounter1)
     end
     
     if (PosStepcounter1) == 0 then
      PosStepcounter1=(PosStepcounter1)+1
      else PosStepcounter1=(PosStepcounter1)
     end
          
     DimTimer1=(Fadetime1)/(PosStepcounter1)

     if (DimTimer1) == 0 then 
      DimTimer1=(DimTimer1)+1
      else DimTimer1=(DimTimer1)
     end

      print (Fadetime1)
      print (Stepcounter1)
      print (PosStepcounter1)
      print (DimTimer1)
      print (LED1_current)
      print (LED1_target)


    tmr.alarm(0, (DimTimer1), 1, function() 
     if LED1_current < LED1_target then 
      LED1_current = (LED1_current + 1) 
      pwm.setduty(3, LED1_current)
    elseif LED1_current > LED1_target then 
      LED1_current = (LED1_current - 1) 
      pwm.setduty(3, LED1_current)
    elseif LED1_current == LED1_target then tmr.stop(0)
     end end )
    

    end
    end)
    end)

print ("easyIoT dimmer/switch/temperature sensor for nodeMCU")
print ("Based on QuinLED_ESP8266_V0.4")


Connect DHT11 data pin to GPIO-02 (and LED to other GPIO if you want).
reboot to start init.lua.

My DHT11 only is capable of returning full "integer" temperatures, so no decimals... (always 20.0, 23.0 etc) is this right?

BTW sorry if code looks rubbish, I'm still new to LUA...

edit: the first commented lines are all thats needed to get temperature readout vial serial console. If you want humidity, replace the line "return t" (at the end) with "return h" in dht22.lua...

regards

Please Log in or Create an account to join the conversation.

9 years 8 months ago #732 by Dennis

2) firmware start and restart in loop (need to reflash module)

obviously tested with 0.9.4 and 0.9.5....


Ouch, I had this happen too at the beginning, and attributedt it to 0.9.5 not working... :unsure:

no idea why this happens, sorry..

regards

Please Log in or Create an account to join the conversation.

9 years 8 months ago #733 by EasyIoT

First of all, nice to hear you got it working.

Regarding "christmas": do you have easyIoT open on more than one device?

If I have easyIoT opened on smartphone and pc at the same time, and change dimmer value, it sometimes goes crazy with the LEDs for me too. But the node itself is not the problem, it is more like if easyIoT is going into some kind of race condition, and values start changing rapidly and endlessly in the easyIoT console. It will never recover by itself from this state. If I restart easyIoT server, everythings fine again...


This is EasyIoT server beta 7 issue. Sometimes Dimmer control on virtual driver is going in loop and change values. It helps only if you restart server. I will fix that in final release.
The following user(s) said Thank You: Dennis

Please Log in or Create an account to join the conversation.

9 years 8 months ago #736 by cdj
Thanks a lot, tomorrow I try everything. But I think that optimal is to have TEMP AND HUM together... Suppose that in automation we can do a GETHUM request or something similar, but how to combine lua? We need two files dht22, one for temp and one for HUM???

Thanks again! :woohoo:

Please Log in or Create an account to join the conversation.

Time to create page: 0.297 seconds

Forum latest

  • No posts to display.