SimConnect to website/webserver

Hi All

I am a web developer creating sites in PHP, I’ve developed a custom window in sim that displays a webapp.

The app allows the user to create a logbook, record entries etc; import .pln flightplans and edit them, creating a pdf export / printable doc. It also displays POH docs etc.

I’d like to pull in simvars into the app and I was (hoping) that I might be able to access data through the window and pass it to the server. The custom panel code has the code below…

var IngamePanelCustomPanelLoaded = false;
document.addEventListener('beforeunload', function () {
  IngamePanelCustomPanelLoaded = false;
}, false);

class IngamePanelCustomPanel extends HTMLElement {
  constructor() {
    super();
  }
}
window.customElements.define("ingamepanel-flightcase", IngamePanelCustomPanel);

window.onmessage = function (e) {
  var fred = 'question';
  var request = e.data
  var response = {
    "read": [],
    "write": []
  }
  if (request.read) {
    request.read.forEach(element => {
      let simvar = SimVar.GetSimVarValue(element.name, element.unit)
      response.read.push({
        "name": element.name,
        "unit": element.unit,
        "value": simvar
      })
    });
  }
  if (request.write) {
    request.write.forEach(element => {
      SimVar.SetSimVarValue(element.name, element.unit, element.value)
      response.write.push({
        "name": element.name,
        "unit": element.unit,
      })
    });
  }
  var iframe = document.getElementById("CustomPanelIframe");
  iframe.contentWindow.postMessage(response, '*');
};

checkAutoload();

I’m using the example from someone else’s custom panel code to load my site - I think they have written it in Node.js and sadly have no experience with node.

I might need to learn C++ to create a simconnect app if the above is no good??? Before I dive into it.

Is this possible? Can a simconnect app allow me to send data to a web server in the cloud rather than locally on a person’s machine? Can anyone help???

Thanks

Matt

I’ve been working with Python SimConnect for the past couple of months: GitHub - odwdinc/Python-SimConnect: Python interface for MSFS2020 SimConnect.dll

It has its limitations but works quite well. You can find my fork here: GitHub - mracko/MSFS-Mobile-Companion-App: Mobile Companion App for MSFS 2020

Hope this helps. It saved me from learning C++. For now at least… :slight_smile:

1 Like

Thanks so much!

Will take a look. Do you know if the Python SimConnect can send data to a webserver rather than a local network?

You can. I haven’t done it myself, but have a look at https://ngrok.com/

Btw. you should be able to figure out the Python SimConnect library just by looking at the code. I suggest you also take a look at a Flask tutorial. That’s the library it uses for the web server.

1 Like

Replying to this (late).

I am one of the contributors to Python-SimConnect and have just built a new implementation called Find My Plane which uses a client running on the sim pc to send sim data from SimConnect to a web server.

This server then displays a moving map of the plane location or answers REST API calls which other services can make of it.

All free and open source.

Full source code for client and server are linked from the site and available on github: https://findmyplane.live/

API documentation: https://findmyplane.live/api

2 Likes

Could one run both the python simconnect app service and a node/vue app locally at the same time, from the same app?

Thanks all for the messages.

I’ve got an implementation running now using window.postMessage() to send data cross domain to my webserver which is then displayed in an iframe in the custom window. I can send any simvar that way and my site then records the data and add’s it to a db.

This enables two way communication, so at the moment I am pulling together the different parts to log a flight. I am primarily a PHP dev so the backend will be written in that.

Once I get the technology working the idea is to see how far I can get aircraft state recorded at the end of a flight and then reload that on launch later on.

I was poking about your code, you seem to call the SimConnect var of TITLE which is odd as whenever I call TITLE it crashes the sim (even if I’m using the “SimVars example” watcher). So the question is what are you doing different (in Python Simconnect) in order to call TITLE without crashing the sim?

Since the SimVars watcher crashes the sim.

I actually raised is as a zendesk ticket on the matter, so not sure if it’s a MSFS or SimConnect bug, or something else. Or is it due to the fact Python SimConnect is using SDK 0.6.1 (which I just threw into simvars and it still crashed the sim). At a loss here, between 'how did you get it to work" and “waiting for asobo to fix if it’s a problem there side”

Sidenote: your server repo seems to be missing/not public on Github.

Edit: I found the issue.

Python SimConnect only sends DATUM 11
SimVars watched sends DATUM 4 and DATUM 11
And DATUM 4 (float 64) for TITLE crashes the sim.

whenever I call TITLE it crashes the sim

Interesting, as I’ve never had this issue. I happily call TITLE just like any other variable and have never experienced or had a report of a crach.

The repo that does the actual work in getting the data from the sim is the base Python-Simconnect repo. I am a contributor here but I’ve only really contributed the front end which does stuff with the data rather than dealing with the sim itself. I’d suggest raising an issue to ask how the Python wrapper got this working when you can’t and I’m sure the contributors who deal with the sim interaction can assist.

Sidenote: your server repo seems to be missing/not public on Github.

Thanks. I temporarily made it private last night as I added tweeting functionality (https://twitter.com/FindMyPlaneSim) and was a little tired to hide the twitter api secrets properly. I’ll do this today and make the repo public again.

Yeah seeing it work for your program, I went diving and found the problem. (The Lib I use requests TITLE (or any vaiable I ask for) as a Float64 and as the requested type (STRINGV) and the float64 call causes a CTD) Not traced why the SimVars watcher example does it yet. 1am code diving.

I never turned the SimConnect server log on to go diving as I assumed it was a Sim Bug. (Granted SimConnect issues really shouldn’t cause a CTD in the sim but thats another story) The SimVars watcher example really isn’t very good anyway…

All good, was just being nosy. I do something similar with a “web relay”, just sockets instead of HTTP Post.

Hi @Topper1988

It’s been a while since I had the time to play with this. I got as far as creating a near tech demo using a custom window and sending/receiving messages via JavaScript. However cross protocol conflicts stopped me going that route.

I’ve started playing with your Python SimConnect code and it looks great! Thank you!

I have a quick question however.

I have created a very quick php file to make get/set requests to see how I can use it:

<?php
  $url = 'http://localhost:5000/datapoint/GPS_POSITION_LAT/get';
  $curl = curl_init($url);
  $response = curl_exec($curl);
  curl_close($curl);
  var_dump($response);
?>
    
<form method="post" action="http://localhost:5000/datapoint/FUEL_TANK_LEFT_MAIN_QUANTITY/set">
  <input name="value_to_use" value="30"/>
  <button action="submit">go</button>
</form>

My code above returns 50.44391659967501 bool(true) for the PHP part, fantastic! I can get it to read SimVars by sending curl requests to your code, and secondly with the form submission I can set the fuel quantity to 30, :slight_smile: :+1:

Taking it a step further do you know how I could use a form to send multiple set commands from one form? For example setting fuel, lat/long etc? I’m assuming I need to somehow split the URL in the action down and send one by one?

Thanks so much for your help (or anyone else)! :slight_smile:

Matt

Hello, I can help with this given I wrote the web server functionality for the Python SimConnect library.

There is the functionality to get multiple datapoints from a single request as I have bundled multiple commonly used datapoints into datasets. For instance /dataset/navigation returns a bunch of commonly needed datapoints such as lat, lon, altitude, etc.

There is not the functionality to set multiple datapoints through a single request. The way that I deal with doing this in, for instance, my cockpit companion app (GitHub - hankhank10/MSFS2020-cockpit-companion: Web wrapper for reading and writing data from Microsoft Flight Simulator 2020) is to use javascript/jquery to send the request without refreshing the page. This would be the right route for you as well.

1 Like

Hi not sure if it is too late to revive this discussion. I am looking to build something similar but I am a pure node.js JS developer. Perhaps you can set me on the right bath and I would love to collaborate with some of what you guys are doing.

I just want to have a node.js app that captures the sim variables and push it to a web service.

Does anyone have in JS opensource code that shows how to connect to the simconnect and pulls the variables? I think I can fifure out how to post the data to a web service.