Automation Code Needed for dimmer IDE/ESP server

8 years 3 months ago #3454 by piman
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.

8 years 2 months ago #3458 by EasyIoT

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.


I guess you should use WebReques class:
msdn.microsoft.com/en-us/library/system....quest(v=vs.110).aspx

Also search on this forum for WebRequest.

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

8 years 2 months ago #3464 by piman
Thank you for your reply and reference,which i partly understand what is going on, unfortunately I'm finding it a bit difficult to work out not been a programmer, I've managed to piece together bits of code but still have no working example.
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;
}
:( HELp :unsure:

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

8 years 2 months ago #3468 by piman
:ohmy: okay looked again at this today ,and managed to workout how to dim the lights, cannot switch on and off need to create a switch instant between Sensor.DigitalValue and Sensor.DimmerLevel :unsure: not sure how to do this some help would be appreciated this is what I've got so far.
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.

Time to create page: 1.861 seconds

Forum latest

  • No posts to display.