pwm.setup(3, 1000, 000)
pwm.setup(4, 1000, 000)
pwm.start(3)
pwm.start(4)
LED1_current=000
LED1_target=000
LED2_current=000
LED2_target=000
SWITCH1_state = 0
SWITCH2_state = 0
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,"GETTEMP") then
print("Received temperature request")
conn:send("25.33")
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,"SWITCH2") then
SWITCH2_state=tonumber(string.sub(payload, 9) )
print("Received SWITCH2 target Value: "..SWITCH2_state)
if SWITCH2_state == 1 then
pwm.setduty(4, 1023)
LED2_current = 1023
elseif SWITCH2_state == 0 then
pwm.setduty(4, 0)
LED2_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 )
elseif 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 ("easyIoT dimmer/switch/temperature sensor for nodeMCU")
print ("Based on QuinLED_ESP8266_V0.4")
const String ESP8266_IP_ADDRESS = "192.168.1.50";
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("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";
}
// dimmer node N3S0 (red LED)
const String ESP8266_IP_ADDRESS = "192.168.1.50";
const String NODE_ADDRESS = "N3S0";
/*
This code is running one time when program is enabled
*/
public void Setup()
{
EventHelper.ModuleChangedHandler((o, m, p) =>
{
if (m.Domain == "Virtual" && m.Address == NODE_ADDRESS)
{
Console.WriteLine("\r\n"+ m.Domain +" "+ m.Address +" in program id "+ Program.ProgramId.ToString() +" property "+ p.Property +" value "+ p.Value);
Console.WriteLine("Program was triggered by change of "+ p.Property +" for node id "+ m.Address);
switch (p.Property)
{
case "Sensor.DimmerLevel":
int myVal;
myVal = ConvertRange(0, 100, 0, 1023, Convert.ToInt32(p.Value));
Console.WriteLine("Mapped "+ p.Property +" value of "+ p.Value +" to pwm value "+ myVal);
sendCommand(myVal.ToString());
break;
case "Sensor.DigitalValue":
Console.WriteLine("triggering \"Sensor.DimmerLevel\" event...\r\n");
sendToServer("SWITCH1="+p.Value);
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 + "\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);
// Close everything.
stream.Close();
client.Close();
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace + "\r\n");
}
}
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.
cdj wrote: Dennis, thanks for your amazing howto. Just a couple of questions :
1) Can you use static ip address in esp with this modified firmware ? You write "you need to connect module to your wifi, then note its IP address"... so always in dhcp ?
2) The two gpio acts separately ? for ex. one for dimmer and one for switch? (i suppose no)
3) Can we use EasyIOT dimmer project with mosfet in this application ?
Dario
Please Log in or Create an account to join the conversation.
Dennis wrote: 1) I think its possible to use static IP instead of DHCP with nodeMCU, see github.com/nodemcu/nodemcu-firmware/issues/12
(but I consider DHCP a good solution here, since we can control in one central place which module gets which IP address, without modifying the modules themselves...)
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.