nodeMCU dimmer/switch/temp.sensor without arduino

6 years 7 months ago #796 by VasilijHCN
Anyone has a DS18b20 lua & automation code for ?

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

6 years 7 months ago #799 by lewys.martin
I ordered a NodeMCU test board just now,
I will try to develop some more scripts for it when it arrives and will share here!
The following user(s) said Thank You: VasilijHCN

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

6 years 7 months ago - 6 years 7 months ago #800 by Dennis
Hello all!

@Dario: I think you will need to modify the "read" function in Vladimirs BMP085 query module, so it can return either temperature or pressure, depending on what you query. This makes things a lot easier. For example, you can compare original and modified DHT22.lua module... Then, adjusting the init-lua webserver part to react to "GETTEMP" and "GETPRESSR" (?) is piece of cake (almost), the automation code in eIoT does not need modification (or, very little - only the command that is sent, maybe the module value name that is being altered)

@Vasilij, I'd be interested too in DS18b20, only adjustment of esp8266 lua code should be needed, easyIoT automation code for polling the temperature can stay as is, I think. Only thing I know is, the ds18b20 example on nodeMCU github repo is not working (thats what they say over at the esp8266 forums, at least)


@lewys: I've ordered some ESP modules with all pins broken out. What does the dev board have in addition - onboard voltage supply maybe? Opto couplers? edit- this one I ordered:





And to all, I'm thinking about splitting up the code in small parts (maybe one for each node type, sensors, actors etc - maybe also common or useful combos like switch/temperature or dimmer/light sensor, or,...), then you could fork the github project, further develop it on your own and issue a pull request if you implement any cool features ort improvements, to have me join your code with the existing base. This way we could build a nice and complete library of easyIoT nodeMCU nodes for any desired kind of sensor/actor together.

What do you think?

(btw, I added some stability improvements to the DHT11 part, since the node sometimes rebooted on me every few hours while being queried, and also sometimes a query did not return any value at all. Now with the fixed code, the node has been online for 18 hours without a single reboot. If things remain stable, I'll upload new version)

regards
Attachments:
The following user(s) said Thank You: cdj

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

6 years 7 months ago - 6 years 7 months ago #802 by VasilijHCN
Look at this
homes-smart.ru/index.php/oborudovanie/be...-servisa-narodmon-ru
Here is alternative firmware with many sensor type support, in free version maybe we can get temperature info from it.
Sensors read command is http://ESP_IP/sensors
Data are output through a point from a comma. (:)
I get this dsw1:17.9;dsw2:17.9;
dsw -ds18b20 sensor(dsw1 dsw2 sensor numbers)
In IOT automation we need request temperature from http://192.168.1.113/sensors
and extract temperature data from dsw1:17.9;dsw2:17.9;
My programming skill is to low for this.

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

6 years 7 months ago - 6 years 7 months ago #806 by cdj
Dennis as usual You're right! I have a dimmer/dht11 node in test from some days and there are lots of reboot...

Now i've tried a couple of hours to adapt dht for bmp180....obviously without success (i'm not a coder)
If i try to launch manually dofile("bmp085.lua").read(sda, scl) node restart :whistle:

and i don't understand if sda and scl is on GPIO0 and GPIO2 or reverse!
EDIT: IT WORKS, sda and scl must be replaced with pins !

Here is the files....if someone want to take a look....max goal is to read pressure and also temp or altitude (but this i think that is a calculated value?) ...

Thanks in advice and regards

init.lua :
--
--print("init")
--tmr.delay(1000000)
--print("read")
--myval = dofile("dht22.lua").read(4,false,false)
--print(myval)
-- Example:
-- dofile("bmp085.lua").read(sda, scl)
--print("BMP180", dofile("bmp085.lua").read(sda, scl))


print("Initializing...")

wifi.setmode(wifi.STATION)
wifi.sta.config("XXXXX","XXXXX")
wifi.sta.setip({ip="192.168.0.51",netmask="255.255.255.0",gateway="192.168.0.1"})


srv=net.createServer(net.TCP) 
  srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload)
         
          print("Input: "..payload) 
          if string.find(payload,"GETPRESSURE") then
          print("Received pressure request")
          --print("init")
               dofile("bmp085.lua").read(sda, scl)
               tmr.delay(1000000)
               print("reading value")
               myval = (dofile("bmp085.lua").read(sda, scl))
               print("read "..myval)
          conn:send(myval)
       
          elseif string.find(payload,"GETTEMP") then
               print("Received temperature request")
               --print("init")
               --what is command for read temp???
               --dofile("dht22.lua").read(4,false,true)
               tmr.delay(1000000)
               print("reading value")
               --myval = (dofile("dht22.lua").read(4,false,true)/10)
               print("read "..myval)
               conn:send(myval)

   
     end -- end if
     
    end) -- end srv conn:on receive function

    conn:on("sent",function(conn)
        print("Closing connection")
        conn:close()
    end) -- end srv conn:on sent function

  end) -- end srv:listen function

print ("easyIoT Pressure/temperature sensor for nodeMCU")
print ("Based on QuinLED_ESP8266_V0.4")
print ("Version 0.6 (c) 2015 by DennisSc ")

EDIT:LAST VERSION SENT FROM VLADIMIR
bmp085.lua :
------------------------------------------------------------------------------
-- BMP085 query module
--
-- LICENCE: http://opensource.org/licenses/MIT
-- Vladimir Dronnikov <dronnikov@gmail.com>
-- Heavily based on work of Christee <Christee@nodemcu.com>
--
-- Example:
-- print(dofile("bmp085.lua").read(sda, scl))
------------------------------------------------------------------------------
local M
do
  -- cache
  local i2c, tmr = i2c, tmr
  -- helpers
  local r8 = function(reg)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.TRANSMITTER)
    i2c.write(0, reg)
    i2c.stop(0)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.RECEIVER)
    local r = i2c.read(0, 1)
    i2c.stop(0)
    return r:byte(1)
  end
  local w8 = function(reg, val)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.TRANSMITTER)
    i2c.write(0, reg)
    i2c.write(0, val)
    i2c.stop(0)
  end
  local r16u = function(reg)
    return r8(reg) * 256 + r8(reg + 1)
  end
  local r16 = function(reg)
    local r = r16u(reg)
    if r > 32767 then r = r - 65536 end
    return r
  end
  -- calibration data
  local AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MC, MD
  -- read
  local read = function(sda, scl, oss)
    i2c.setup(0, sda, scl, i2c.SLOW)
    -- cache calibration data
    if not AC1 then
      AC1 = r16(0xAA)
      AC2 = r16(0xAC)
      AC3 = r16(0xAE)
      AC4 = r16u(0xB0)
      AC5 = r16u(0xB2)
      AC6 = r16u(0xB4)
      B1  = r16(0xB6)
      B2  = r16(0xB8)
      MC  = r16(0xBC)
      MD  = r16(0xBE)
    end
    if not oss then oss = 0 end
    if oss <= 0 then oss = 0 end
    if oss > 3 then oss = 3 end
    -- get T
    w8(0xF4, 0x2E)
    tmr.delay(5000)
    local t = r16(0xF6)
    local X1 = (t - AC6) * AC5 / 32768
    local X2 = MC * 2048 / (X1 + MD)
    t = (X2 + X1 + 8) / 16
    -- get raw P
    w8(0xF4, 0x34 + 64 * oss)
    tmr.delay((4 + 3 ^ oss) * 1000)
    local p = (r8(0xF6) * 65536 + r8(0xF7) * 256 + r8(0xF8)) / 2 ^ (8 - oss)
    -- normalize P
    local B6 = t * 16 - 8 - 4000
    local X1 = B2 * (B6 * B6 / 4096) / 2048
    local X2 = AC2 * B6 / 2048
    local X3 = X1 + X2
    local B3 = ((AC1 * 4 + X3) * 2 ^ oss + 2) / 4
    X1 = AC3 * B6 / 8192
    X2 = (B1 * (B6 * B6 / 4096)) / 65536
    X3 = (X1 + X2 + 2) / 4
    local B4 = AC4 * (X3 + 32768) / 32768
    local B7 = (p - B3) * (50000 / 2 ^ oss)
    p = B7 / B4 * 2
    X1 = (p / 256) ^ 2
    X1 = (X1 * 3038) / 65536
    X2 = (-7357 * p) / 65536
    p = p + (X1 + X2 + 3791) / 16
    -- Celsius * 10, Hg mm * 10
    p = p * 3 / 40
    -- has floats? divide by 10
    if 1/2 ~= 0 then
      t, p = t / 10, p / 10
    end
    --
    return t, p
  end
  -- expose
  M = {
    read = read,
  }
end
return M

pressure.c (Automation) :
//Programma Pressione
const String ESP8266_IP_ADDRESS = "192.168.0.51";
const String NODE_ADDRESS = "N5S0";
/*
  This code is running one time when program is enabled
*/
public void Setup()
{
}
/*
  This code is running periodicaly when program is enabled. 
  Cron job detirmine running period.
*/
public void Run()
{
  String response = QueryServer("GETPRESSURE");
  Console.WriteLine(response);
  ModuleHelper.SetProperty("Virtual", NODE_ADDRESS, "Sensor.Pressure", response.ToString());
  EventHelper.SetEvent("Virtual", NODE_ADDRESS, "Sensor.Pressure");
}
private static string QueryServer(String message)
{
try
	{
		Console.WriteLine("TCP client command: " + message + "\r\n");
		Int32 port = 43333;
		System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient( ESP8266_IP_ADDRESS, port);
		Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
		System.Net.Sockets.NetworkStream stream = client.GetStream();
		stream.Write(data, 0, data.Length);
		
		data = new Byte[256];
		String responseData = String.Empty;
		Int32 bytes = stream.Read(data, 0, data.Length);
		responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
		// Close everything.
		stream.Close();
		client.Close();
		return responseData;
	}
	catch(Exception e)
	{
		Console.WriteLine(e.StackTrace + "\r\n");
	}
		
return "0.00";
}

EDIT:ADDED another automation for temp (and obviously another virtual node)
temp.c :
//Programma Temperatura Esterna
const String ESP8266_IP_ADDRESS = "192.168.0.51";
const String NODE_ADDRESS = "N6S0";
/*
  This code is running one time when program is enabled
*/
public void Setup()
{
}
/*
  This code is running periodicaly when program is enabled. 
  Cron job detirmine running period.
*/
public void Run()
{
  String response = QueryServer("GETTEMP");
  Console.WriteLine(response);
  ModuleHelper.SetProperty("Virtual", NODE_ADDRESS, "Sensor.Temperature", response.ToString());
  EventHelper.SetEvent("Virtual", NODE_ADDRESS, "Sensor.Temperature");
}
private static string QueryServer(String message)
{
try
	{
		Console.WriteLine("TCP client command: " + message + "\r\n");
		Int32 port = 43333;
		System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient( ESP8266_IP_ADDRESS, port);
		Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
		System.Net.Sockets.NetworkStream stream = client.GetStream();
		stream.Write(data, 0, data.Length);
		
		data = new Byte[256];
		String responseData = String.Empty;
		Int32 bytes = stream.Read(data, 0, data.Length);
		responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
		// Close everything.
		stream.Close();
		client.Close();
		return responseData;
	}
	catch(Exception e)
	{
		Console.WriteLine(e.StackTrace + "\r\n");
	}
		
return "0.00";
}

Dario

P.S. In the next life i want to born programmer !

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

6 years 7 months ago #807 by lewys.martin
I have not had much luck connecting esp8266 to serial on my computer with my USB serial adapters, this one has node MCU already flashed and built in USB TTL+ regulator :)

So it will be easier for me to learn on!

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

Time to create page: 0.498 seconds