Web control for the E-Stim 2B

One of the things that I've included in my Estim Control/Remote software is an HTTP remote mode, which is designed specifically to allow the box to be controlled from a web page. This section of my site gives you a little more information about that.

Note that web control is a work in progress, and I hope to make it a bit more clever in future.

Back to eStim index

The basics

To use web control, you need my Mac software, connected to your 2B. Go to the Preferences screen, click the Slave mode radio button, tick the HTTP slave option, and make sure the Slave timeout is set to a sensible value - ten minutes, or 600 seconds is a reasonable default. Then click Done, followed by Connect on the main screen.

Prefs, version 1.0.2

The person controlling the box then goes to a web page that's configured with the details of how to reach your box, clicks Connect, and the page refreshes with details of what the box is set to. They can then change settings and the page updates within a second or two. Ideally, you'd probably also be communicating using something like Skype at the same time.

The local software continues to control the box too, so you can click Stop if there's a problem, for example.

What's HTTP mode for?

Some people might want to control a box when they're on the move. The obvious solution would be an Android app, but while I managed to learn RealStudio very quickly, that's because it does make it very simple to create apps like this. The free Android dev tools don't, and it costs money just to register to develop for iOS, so I'm not bothering with those just yet. So, some sort of control over the web would be handy. I'll try not to make this too technical, but for various reasons, it's hard to maintain a permanent connection over the web between an app and a server; web browsers tend to fetch a bit of information (like a web page), then disconnect. So, a real time updating of the display, as with the apps, is not so straightforward.

This is what the 'HTTP slave mode' checkbox is for on the preferences page. When it's ticked, and the software is running in Slave mode, a modified version of the Estim protocol is used, which is suitable for being called from JavaScript using some pretty standard and widely supported functions (there's a new system, called WebSockets, which will one day solve this problem, but not just yet).

To compliment the web control, you need a web page, and I've created one, which is shown here. It doesn't have quite as many bells and whistles as the main interface, but it does work:

Web controller

This is launched via a button on a private web page I have for myself and Boss; it opens in a pop-up window, and as you can see there are buttons for the main settings, power, mode and STOP. When it opens, you won't see anything, of course, so clicking Connect/Refresh attempts to make a connection to the remote box - the address is coded into the web page. If the connection is successful, the display updates with the current state of the unit, and then you can start playing.

Web communication isn't always 100% reliable, so if the script doesn't receive a valid response, it pops up an error message, and urges you to check the settings.

As far as the end with the 2B is concerned, things look pretty much like they do in normal Slave mode, except that the 'Connection from' message appears only briefly, each time a button is pressed on the web page. There isn't the same real time updating at each end; the local Mac will update to reflect changes you make on the box, as normal, but on the web page an update happens only after a command has been sent, or the Connect/Refresh button is pressed. But, it does work, which is the main thing. Here's a picture when it's in action:

Web control, connected

Making things safe, and easier to use

After the first proper use of this with someone else driving the box, two issues cropped up. The first was safety - their internet connection died, and I had no idea - I just thought they were making me suffer for a long time. So, now the Mac software sets both outputs to zero if no command - even just a status refresh - has been received from a remote computer within the period set by the Slave timeout option; that countdown resets each time a command is received. So if you're planning on shocking someone for a long session, just make sure you click Connect/Refresh from time to time, if you've not altered any settings.

It's also possible, because of the way that the web page updates, that the person with the box might have changed the settings substantially, while the remote web user wasn't doing anything. You could turn the level down to 20 from 50, for example. What should happen then, when the web user clicks the + button, and the level on their screen is showing 50? The web code will send the command to set the level to 51, which could be a nasty surprise. So, the software on the Mac checks web commands, and if they'd result in a level change of more than five steps, they're ignored, and the current status is sent to the browser. In this example, the web page would update to show the new level of 20, rather than jumping the value back up to 51.

The other thing that became apparent is that it can be tedious getting to a specific level - and sometimes, that might be quite high, depending on the person, the sensation you want, and the electrodes. Altering all the settings by clicking the plug and minus buttons can take a while, so now there's a set of boxes at the bottom of the web screen, which wasn't there in the first version. This allows direct setting of the levels, in the current mode and power level. The box will take thirty seconds (or more) to get to the settings specified.

Using Direct settings

To use this mode, first select the power level and mode that you want; that will typically set both outputs to zero, and both settings to fifty.

Now, enter the values for the four settings in the boxes, and click Go. If you want it to take more than 30 seconds, you can also type a time (in seconds) in the fifth box. When you click Go, a special 'extended command' is sent to the Estim Control/Remote software, which starts the box increasing, in just the same way (and with the same displays on-screen in the Favourites window) as it does when setting to a locally saved favourite setting. At the end, the output will continue, just as if you'd selected a favourite and used the Set to selected button on the Mac.

While the settings are being updated, the web display will change, and give you a progress display, as shown here:

Direct settings

When the sequence has finished, the display will return to normal, and you can adjust settings manually.

Remote favourites (24th Sept, v 1.1.2)

In the latest update to the code, I've added Favourites support. This works with the remote box, and some extended commands that I've added to it. Here's the second of the page with the C and D settings, and just above the Direct settings boxes:

Remote favourites

Once you're connected to the box, the first time you want to use Favourites, just click on the menu, and the list of favourites will be requested, and the menu updates. You can then select an item from it, and click Launch Favourite to set the box to that setting (including the ramp time specified, which can't be edited on the web page). As with Direct settings, the controls are locked out and a progress message is displayed as the box sets itself to the requested settings.

You can also save the settings you've reached, by clicking Save current. All the settings will be stored on the remote Mac, in the Favourites list. They'll be named as normal, except that the name will be prefixed "R-", so you can see easily which ones were created via remote control, and which ones weren't - though of course, you can rename them anything you like later.

Technical info about HTTP Slave mode

For the technically minded, in HTTP Slave mode, the software expects to receive an HTTP request, such as is generated by the standard XMLHttpRequest command in Javascript. It looks for the GET function, discards everything up to and including the / and then takes the command from after it. Essentially, the command to set channel A to level 44 would be requested by calling the URL http://some.place.on.line:7846/A44 where some.place.on.line is the IP address or remote hostname for the Slave mode software. Any other headers in the request are ignored.

The response is an HTTP 1.0 200 OK, followed by a couple of headers (necessary to allow the script to work with most browser security), and then the standard Estim response as the body.

Obviously, this introduces a fair bit of overhead, but at least it works. I've wrapped things up with an estim.js javascript, so sending a command is simply done by calling stimCommand('A44') for example; the script will update the page with the results, if they're valid, or display a pop-up warning otherwise. The associated CSS formats the buttons and the rest of the page.

Extended commands

One of the things that the script has prompted me to add to the Mac software is 'extended commands.' These will eventually make it easier to replicate some of the functions, like playing back sequences, or saving settings, via the web. Those things aren't possible at all over the internet with the eStim protocol, while some other things are just fiddly and a bit unreliable to do over the net. Principally, to make it simple to get the box to a specific level without clicking buttons dozens of times, I wanted a way to directly set the output, but I also wanted it to be increase slowly - you don't want to suddenly shock someone with a high level.

It would be possible to make the estim.js file send a whole series of commands, but in practise, I found that that wasn't reliable, and some commands might be lost. So I created a mechanism to send extended commands, the first of which was the GOTO command that is used by the Direct settings option on the web interface.

Version 1.1.2 has added lots more extended commands, and I've documented them on this page, together with more details of how it works. From version 1.1.3, you can also control the random loop mode via the web page, with version 1.2 or higher of the web files. Version 1.1.5 of Estim Control/Server adds some extra commands, including some that I recommend using for safer web control, and version 1.3 of the web script has been updated. The example page now automatically tries to connect to the server when it's loaded, and if it detects a suitable version of the software, will add buttons that control both channels at the same time, and use the safer relative commands to change channel A and B outputs.

Adding HTTP control to a website

You don't need to be a programming guru to make all this work - it could be added to any website simply by copying the three files and editing one line to update the information about the address of the box you want to control. The files can be downloaded here, as a zip. For a gallery that shows what it looks like in use, check this page.

You can test the script by simply opening the electro.html file in a browser that's running on the same computer as Estim Control/Server; set the software to Slave mode, tick the HTTP box, click Done, and then Connect in the application. You'll then be able to click Connect/Refresh on the web page, and control the box.

When you're happy that all is working, and want to have the page on the internet somewhere (I'd advise behind some sort of password protection), you will need to update one line with the information to make the connection to your box.

Copy all three files into a folder on your web server, and open the estim.js file in an editor.

At the top, you will see a line that says

var eStimConnection = 'http://localhost:7846/' ;

Edit that line, and change localhost to the appropriate IP address (and change the 7846 to a different port, if necessary), so that it points to the publicly accessible address or hostname that can be used to reach the computer on which you're running Estim Control/Remote. You will probably need to arrange access through your firewall, and possibly set up portforwarding; I can't offer individual help on that, but you might find useful advice at Port Forward.

Note that for the latest version, 1.3, to work properly, you must update all three files, if you have older versions - estim.js, estim.css and electro.html

Back to eStim index