× This is the optional category header for the Suggestion Box.

Add TCP *Relay node* (esp8266 only needed)

9 years 8 months ago #644 by EasyIoT

VasilijHCN wrote: How to make range conversion inside automation ?
I need map values from 0-100(wheel value) to 1-1000(data to send).
Then i change *dimmer wheel* ranges to 0-100%
Still problems present - dimmer wheel values jumping sometimes randomly, until iot server restart.(little progress - in lua code fully removed fading functions, in dimmer wheel minimum value set to 1)
Update- all dimming wheels minimum parameters set to 1, except step. Don't use simultaneously Web control on i.e. PC and phone. In this conditions all stable, no more random jumping values.


Go in dimmer module configuration and search for property Settings.MaxValue and change value 100 to 1000 and Settings.MinValue change 0 to 1.
The following user(s) said Thank You: VasilijHCN

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

9 years 8 months ago #645 by VasilijHCN

Go in dimmer module configuration and search for property Settings.MaxValue and change value 100 to 1000 and Settings.MinValue change 0 to 1.

This is done, i think about - in programm conversion, dimmer wheel actualy 1-100, inside automation 1-100 range converted to 1-1000.
like this
val = map(Sensor.DimmerLevel, 1, 100, 1, 1000);

Don't forget to use Basic authorization with username and password when you call EasyIoT server.

How to do it ?

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

9 years 8 months ago - 9 years 8 months ago #646 by EasyIoT

VasilijHCN wrote:

Go in dimmer module configuration and search for property Settings.MaxValue and change value 100 to 1000 and Settings.MinValue change 0 to 1.

This is done, i think about - in programm conversion, dimmer wheel actualy 1-100, inside automation 1-100 range converted to 1-1000.
like this
val = map(Sensor.DimmerLevel, 1, 100, 1, 1000);

Don't forget to use Basic authorization with username and password when you call EasyIoT server.

How to do it ?


Add Authorization parameter in request:
-- Untested code!
client.call {
	headers = {
		Authorization = "Basic "..mime.b64 (login..':'..password),
	},
	... -- add your arguments
}
-- End of untested code!

Here is working java example, how to properly set basic auth:
String auth = new String(Base64.encode((sUsename + ":" + sPassword).getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
		request.addHeader("Authorization", "Basic " + auth);
The following user(s) said Thank You: VasilijHCN

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

9 years 8 months ago - 9 years 8 months ago #652 by Dennis
Hey all,

just finished polishing up the automation program code for the nodeMCU-based esp8266 dimmer module:

- no need to delete Sensor.DigitalValue from module, instead we take advantage of it and use it to put dimmer to full or zero duty cycle (i.e. on/off functionality, like the buttons say)

- no need to remove the fading component in LUA firmware, instead just set both dimmer timers to 1000ms instead of 5000ms in "init.lua", and it works nicely

- module min/max range ca be set to 0-100 ("...percent"?), and inside automation program some mapping to the pwm res. of 0-1023 is being done


here is my easyIoT automation code:
// dimmer node N3S0 (red LED)

const String ESP8266_IP_ADDRESS = "192.168.119.58";
const String NODE_ADDRESS = "N3S0";

/*
This code is running one time when program is enabled
*/
public void Setup()
{
	int myVal;
  	EventHelper.ModuleChangedHandler((o, m, p) =>
	{
		if (m.Domain == "Virtual" && m.Address == NODE_ADDRESS) 
		{
  			Console.WriteLine(m.Domain +" "+ m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);
          	Console.WriteLine("Triggered by change of " + p.Property + " for node id " + m.Address);
          	switch (p.Property)
            {
              case "Sensor.DimmerLevel":
              	myVal = ConvertRange(0, 100, 0, 1023, Convert.ToInt32(p.Value));
				Console.WriteLine(p.Property  + " value of " + p.Value + " mapped to " + myVal);
				sendCommand(myVal.ToString());
               	break;
              case "Sensor.DigitalValue":
              	Console.WriteLine("triggering \"Sensor.DimmerLevel\" event...");
              	ModuleHelper.SetProperty(m.Domain, m.Address, "Sensor.DimmerLevel", (Convert.ToInt32(p.Value)*100).ToString());
            	EventHelper.SetEvent(m.Domain, m.Address, "Sensor.DimmerLevel");
              	break;
              default:
              	break;
            } //end switch
        }//end if
	return true;
	});//end EventHelper
}

/*
This code is running periodicaly when program is enabled.
Cron job detirmine running period.
*/
public void Run()
{}

private void sendCommand(string value)
{
	sendToServer("LED1_target="+value); // LED1 - here we set channel
}

private void sendToServer(String message)
{
	try
	{
		Console.WriteLine("TCP client command:" + message);
		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);
		// Close everything.
		stream.Close();
		client.Close();
	}
	catch(Exception e)
	{
		Console.WriteLine(e.StackTrace);
	}
}

private static int ConvertRange(
    int originalStart, int originalEnd, // original range
    int newStart, int newEnd, // desired range
    int value) // value to convert
{
    double scale = (double)(newEnd - newStart) / (originalEnd - originalStart);
    return (int)(newStart + ((value - originalStart) * scale));
}

Nice thing to note, we can use this automation code as well for *relay* nodes, running on the very same nodeMCU LUA firmware, since the automation program also evaluates Sensor.DigitalValue changes.

One automation code, one esp8266 firmware*, two purposes - just select the appropriate virtual module's type accordingly (and, of course, attach either a relay or a mosFET to your Esp8266).

*(edit - better set the dimmer/fader delay in init.LUA firmware to something like 1ms for relay use!)

regards
The following user(s) said Thank You: VasilijHCN, tonyn79

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

9 years 8 months ago #655 by tonyn79
Thank you for the code example. What are you saving this code as to the ESP? Is it possible you can post you init file? Thanks again, this is an awesome addition.

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

9 years 8 months ago - 9 years 8 months ago #656 by Dennis
Thanks.

The code I wrote is just for easyIoT automation script - to be used in combination with a virtual node - to send commands to a esp8266 running nodeMCU with a custom LUA init script.

I got the LUA init script code from here, as well as instructions on how to flash the interactive nodeMCU firmware to the esp8266 and how to load LUA script onto it: blog.quindorian.org/2015/01/esp8266-wifi...mer-part-3-of-x.html

I used the ESPlorer Tool described there to upload (be aware that the init script does need nodeMCU 0.9.4 to work, it crashed when I tried with 0.9.5)
esp8266.ru/esplorer/

Only modification I made to the LUA init script was changing the inital values of Fadetime1 and Fadetime2, heres the "init.lua" i'm currently running on my esp8266:
pwm.setup(3, 1000, 005)
pwm.setup(4, 1000, 005)
pwm.start(3)
pwm.start(4)

LED1_current=005
LED1_target=005
LED2_current=005
LED2_target=005

Fadetime1=1000
Fadetime2=1000

Stepcounter1=0
PosStepcounter1=0
DimTimer1=0

Stepcounter2=0
PosStepcounter2=0
DimTimer2=0

wifi.setmode(wifi.STATION)
wifi.sta.config("MYSSID","MYPASSWORD")



srv=net.createServer(net.TCP) 
srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload) 
   
    print("Input:"..payload) 
 
    if 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

    if string.find(payload,"LED2") then
        print("Received LED2 Target Value")
     LED2_target=tonumber(string.sub(payload, 13) )
     
     Stepcounter2=(LED2_target)-(LED2_current)
     
     if (Stepcounter2) < 0 then
      PosStepcounter2=(Stepcounter2)*-1
      else PosStepcounter2=(Stepcounter2)
     end
     
     if (PosStepcounter2) == 0 then
      PosStepcounter2=(PosStepcounter2)+1
      else PosStepcounter2=(PosStepcounter2)
     end
          
     DimTimer2=(Fadetime2)/(PosStepcounter2)

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

      print (Fadetime2)
      print (Stepcounter2)
      print (PosStepcounter2)
      print (DimTimer2)
      print (LED2_current)
      print (LED2_target)


    tmr.alarm(1, (DimTimer2), 1, function() 
     if LED2_current < LED2_target then 
      LED2_current = (LED2_current + 1) 
      pwm.setduty(4, LED2_current)
    elseif LED2_current > LED2_target then 
      LED2_current = (LED2_current - 1) 
      pwm.setduty(4, LED2_current)
    elseif LED2_current == LED2_target then tmr.stop(1)
     end end )
    end
    end)
    end)

print ("Booted to QuinLED_ESP8266_V0.4")

If one would want to write a pure "relay node" LUA init script for the esp8266, here are some examples:
www.nodemcu.com/index_en.html#fr_5475f7667976d8501100000f

regards

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

Time to create page: 0.297 seconds

Forum latest

  • No posts to display.