Altitude Calculation for Dummies

9 years 1 month ago - 9 years 1 month ago #848 by cdj
Ok guys, i've a pressure from a NodeMCU bmp180 with ip 192.168.0.51 and virtual node N5S0.
I'm not a programmer, and i'm trying to make a working automation to valorize another virtual node (set as Sensor.Distance) N7S0.

Operation for calculate altitude is : (pressure - 101325)*843/10000

I've tried this code reading pressure from IP and valorizing virtual node :
const String ESP8266_IP_ADDRESS = "192.168.0.51";
const String NODE_ADDRESS = "N7S0";
/*
  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()
{
  Altitude = (QueryServer("GETPRESSURE")- 101325)*843/10000;
  String response = Altitude;
  Console.WriteLine(response);
  ModuleHelper.SetProperty("Virtual", NODE_ADDRESS, "Sensor.Distance", response.ToString());
  EventHelper.SetEvent("Virtual", NODE_ADDRESS, "Sensor.Distance");
}
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";
}

where you can see line :
Altitude = (QueryServer("GETPRESSURE")- 101325)*843/10000;
  String response = Altitude;

No result, what's wrong ?
Thanks
Dario

EDIT: Tried also this code (unsuccessful) from official Automation howto part.III without operation (just to test if it works):
public void Setup()

{

   EventHelper.ModuleChangedHandler((o, m, p) =>

    {

 

      // altitude

      if (m.Domain == Domains.Virtual && m.Address == "N5S0" && p.Property == "Sensor.Pressure")

        DriverHelper.ProcessCommad(Domains.Virtual, "N7S0", "ControlLevel", p.Value);

        return true;

    });

}

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

9 years 1 month ago #849 by Dennis
Hello,

the response returned by "QueryServer(GETPRESSURE)" is of type "String", so when you call it, your variable "Altitude" contains a String object.
You can check that it contains value by using "console.WriteLine", good for debugging. For doing calculations, you need to convert this String value to a decimal Float value first... you cannot calculate with letters.

Again, you should check result of successful calculation with console.writeline function. Then to set module property, you need to convert this value back to String (using the ToString function), and to see your changes in GUI you also need to trigger an event AFAIK (at least when you use "SetProperty" then you also need "SetEvent", no idea if things are different with "processCommad")

regards

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

9 years 1 month ago - 9 years 1 month ago #852 by cdj
Hey Dennis, don't forget that i'm not a coder :)

Something like this ? :
String Pressure = QueryServer("GETPRESSURE");
  Console.WriteLine(Pressure);
  float result;
  result = Convert.ToSingle(Pressure);
  Console.WriteLine(result);
  float Altitude = (result- 101325)*843/10000;
  Console.WriteLine(Altitude);
  String response = Altitude.ToString();
  Console.WriteLine(response);
  ModuleHelper.SetProperty("Virtual", NODE_ADDRESS, "Sensor.Distance", response.ToString());
  EventHelper.SetEvent("Virtual", NODE_ADDRESS, "Sensor.Distance");

I've no idea if syntax is correct....

Tnx
Dario

EDIT: It Works, but something is wrong in the formula :
it respond me that my home is at -8456,22 mt :P

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

9 years 1 month ago - 9 years 1 month ago #865 by cdj
Uhm with new 0.7 something goes wrong in this code, why?
String Pressure = QueryServer("GETPRESSURE");
  Console.WriteLine(Pressure);
  float result;
  result = Convert.ToSingle(Pressure);
  //Console.WriteLine(result);
  float Altitude = (result-1013.25)*8;
  String response = Altitude.ToString();

EDIT: ok it doesn't like 1013.25 but like 4053/4 ! Solved !

Thanks

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

Time to create page: 0.233 seconds

Forum latest

  • No posts to display.