- Posts: 111
- Karma: 2
- Thank you received: 12
Please Log in or Create an account to join the conversation.
piman wrote: Hi can anyone help, I have an Arduino IDE sketch that works on ESP 8266 producing a server that allows LED pin to be change via a web page but I'm having trouble creating an automation code to be used within EasyIoT with this node. I would appreciate some help possibly example code.
Kind regards Andy thank you.
Please Log in or Create an account to join the conversation.
const String ESP8266_IP_ADDRESS = "192.168.1.134";
const String NODE_ADDRESS = "N31S0";
/*
This code is running one time when program is enabled
*/
public void Setup()
{
EventHelper.ModuleChangedHandler((o, m, p) =>
{
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("ind1="+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 SendTrigger()
{
string msgURL = "http://192.168.1.134/";
Uri address = new Uri(msgURL);
Console.WriteLine("Sensor.DimmerLevel");//Send request to
try
{
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(address) as System.Net.HttpWebRequest;
request.Method = "GET";
request.ContentType = "text/http";
using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
{
}
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
}
return 0;
}
Please Log in or Create an account to join the conversation.
const String ESP8266_IP_ADDRESS = "192.168.1.141";
const String MODULE_ADDRESS = "N31S0";
/*
This code is running one time when program is enabled
*/
public void Setup()
{
//System.Diagnostics.Process.Start("CMD.exe","");
EventHelper.ModuleChangedHandler((o, m, p) =>
{
Console.WriteLine(m.Domain +" "+ m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);
if (m.Domain == "Virtual" && m.Address == MODULE_ADDRESS && p.Property == "Sensor.DimmerLevel")
sendCommand(p.Value);
return true;
});
}
/*
This code is running periodicaly when program is enabled.
Cron job detirmine running period.
*/
public void Run()
{
}
private void sendCommand(string ledValue)
{
sendToServer("/led/"+ledValue+'/');
}
private void sendToServer(String message)
{
try
{
//Console.WriteLine("TCP client command:" + message);
Int32 port = 80;
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);
}
}
Please Log in or Create an account to join the conversation.