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.
Please Log in or Create an account to join the conversation.
This is done, i think about - in programm conversion, dimmer wheel actualy 1-100, inside automation 1-100 range converted to 1-1000.Go in dimmer module configuration and search for property Settings.MaxValue and change value 100 to 1000 and Settings.MinValue change 0 to 1.
How to do it ?Don't forget to use Basic authorization with username and password when you call EasyIoT server.
Please Log in or Create an account to join the conversation.
VasilijHCN wrote:
This is done, i think about - in programm conversion, dimmer wheel actualy 1-100, inside automation 1-100 range converted to 1-1000.Go in dimmer module configuration and search for property Settings.MaxValue and change value 100 to 1000 and Settings.MinValue change 0 to 1.
like this
val = map(Sensor.DimmerLevel, 1, 100, 1, 1000);
How to do it ?Don't forget to use Basic authorization with username and password when you call EasyIoT server.
-- Untested code!
client.call {
headers = {
Authorization = "Basic "..mime.b64 (login..':'..password),
},
... -- add your arguments
}
-- End of untested code!
String auth = new String(Base64.encode((sUsename + ":" + sPassword).getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
request.addHeader("Authorization", "Basic " + auth);
Please Log in or Create an account to join the conversation.
// 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));
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
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")
Please Log in or Create an account to join the conversation.