libjpeg example – encode JPEG to memory buffer instead of file

Q: How can I use libjpeg to encode directly to memory on my embedded system without using a file?

A: If you’re encoding images into JPEG on your ARM/Linux embedded platform then you’re most likely using libjpeg, and if you’re encoding quickly then you’re most likely using its drop-in replacement libjpeg-turbo!

Most of the examples of how to use the libjpeg API show how to encode directly to a file, but it’s also possible to use the API to encode to a memory buffer instead. This is handy if you want to transmit the JPEG via MQTT or something and don’t actually need a JPEG file – you can avoid the overhead of writing to your flash filesystem and having to read the data from it before sending.

Of course, one easy way of achieving this if your embedded system has a RAM disk (as a lot of ARM/Linux based systems will) is to just get libjpeg to encode to file on the RAM disk, in this way you can avoid writing to flash, however you will still have to open the file and read in all of the data into a buffer afterwards.

The trick to getting libjpeg to encode directly to  memory is make a call to jpeg_mem_dest() (instead of to jpeg_stdio_dest() for a file), this allows the caller to specify an output buffer for the jpeg data (as well as its size).

You can either supply a pointer to a buffer, or if you pass NULL, the library will allocate a buffer for you – either way, you must free() the output buffer once you are finished with it!

Some things to note are:

  • You can pass in a pointer to a buffer that you have already malloc()’ed
  • If you pass in NULL, the lib will allocate a buffer for you.
  • If the buffer that you specify (or that the lib automatically malloced()’ed) turns out to be too small to hold the JPEG data then the lib will free() the buffer and malloc() a new one (this will probably also involve a memory copy).
  • After encoding the size variable will contain the number of JPEG bytes in the output buffer (i.e. it no longer contains the size of the buffer used!)
  • If you want to re-use the same buffer for each encode, then just pass in the same buffer pointer each time with it’s original size and don’t free() it after encoding.

Here is an example of a c++ function that encodes to memory, it uses a buffer that’s allocated by the library:

 

//
// Encodes a 256 Greyscale image to JPEG directly to a memory buffer
// libJEPG will malloc() the buffer so the caller must free() it when
// they are finished with it.
//
// image    - the input greyscale image, 1 byte is 1 pixel.
// width    - the width of the input image
// height   - the height of the input image
// quality  - target JPEG 'quality' factor (max 100)
// comment  - optional JPEG NULL-termoinated comment, pass NULL for no comment.
// jpegSize - output, the number of bytes in the output JPEG buffer
// jpegBuf  - output, a pointer to the output JPEG buffer, must call free() when finished with it.
//
void encode_jpeg_to_memory(unsigned char* image, int width, int height, int quality,
                            const char* comment, unsigned long* jpegSize, unsigned char** jpegBuf) {
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW row_pointer[1];
    int row_stride;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    cinfo.image_width = width;
    cinfo.image_height = height;
    // Input is greyscale, 1 byte per pixel
    cinfo.input_components = 1;
    cinfo.in_color_space = JCS_GRAYSCALE;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    //
    //
    // Tell libJpeg to encode to memory, this is the bit that's different!
    // Lib will alloc buffer.
    //
    jpeg_mem_dest(&cinfo, jpegBuf, jpegSize);
    jpeg_start_compress(&cinfo, TRUE);
    // Add comment section if any..
    if (comment) {
        jpeg_write_marker(&cinfo, JPEG_COM, (const JOCTET*)comment, strlen(comment));
    }
    // 1 BPP
    row_stride = width;
    // Encode
    while (cinfo.next_scanline < cinfo.image_height) {
        row_pointer[0] = &image[cinfo.next_scanline * row_stride];
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
}

The we can use this function to encode an 8bit image like this:

 

void test_encode_jpeg_to_memory() {
    int width = 1920;
    int height = 1080;
    // Create an 8bit greyscale image
    unsigned char* image = (unsigned char*)malloc(width * height);
    // With a pattern
    for (int j = 0; j != height; j++) {
        for (int i = 0; i != width; i++)
            image[i + j * width] = i + j;
    }
    // Will hold encoded size
    unsigned long jSize = 0;
    // Will point to JPEG buffer
    unsigned char* jBuf = NULL;
    // Encode image
    encode_jpeg_to_memory(image, width, height, 85, "A Comment!", &jSize, &jBuf);
    printf("JPEG size (bytes): %ld", jSize);
    //
    // Now, do something with the JPEG image in jBuf like stream it over UDP or sommit!
    //
    // Free jpeg memory
    free(jBuf);
    // Free original image
    free(image);
}

 

 

Get the Subversion (SVN) URL for a file using Tortoise SVN?

How can I get the Subversion (SVN) URL for a single file using Tortoise SVN?

If you’re still rocking a venerable Subversion (SVN) repository to keep all of your software safe, then Tortoise SVN is a great user-interface to all of the SVN delights,  but how can you easily retrieve or copy an SVN URL to a single file in the repo?  I find that the easiest way to get an SVN URL to a folder is to right-click on the local folder and choose the ‘Repo-browser’ menu option, when the browser shows, the URL can be easily copied from the URL text box at the top of the browser screen.

 

Now, to get the URL for an individual file, just click on the file within the Repo-browser and it’s URL will be displayed in the URL box!

 

Get URL for single file in SVN

 

From here you can copy and paste to the heart’s content!

Fire-And-Forget wrapper for sending simple UDP data using boost::asio libraries

So I had a problem, as part of an embedded software system I was working on I needed to periodically send some GPS information via UDP datagrams to other devices on the network – really simple stuff, transmit a string to an IP address on a given port, Fire And Forget, send a string from here to there – end of story!

Now my normal port of call these days for networking in C++ is to reach for the boost::asio libs, as mentioned here, this library is very powerful and flexible and can handle a huge number of different networking scenarios.

All well and good, but sometimes I wish that I could just call a simple function and not have to remember or worry about all of boost stuff as it can be a proper head-wreck and there’s a lot of typing as the namespaces are so long! Python has me really spoiled, it makes so many things simple and easy to use!

So I wrote a little C++ wrapper class that just has 1 function called send() that can send a string or the contents of a binary buffer, the class is called: boost_udp_send_faf (faf stands for Fire-and-Forget!).

It is a very basic wrapper around the boost libs and only handles very simple transmission use-cases, but these use-cases probably cover about 80% of my UDP transmission needs!

To use the class for a single Fire-and-Forget send:

//
#include "boost_udp_send_faf.h"
boost_udp_send_faf("192.168.1.44", 8861).send("The message!");
//

This sends the message to 192.168.1.44 on port 8861.

If your program needs send multiple datagrams, then we can keep the socket open and reuse the end-point like this:

//
#include "boost_udp_send_faf.h"
boost_udp_send_faf sender("192.168.1.44", 8861);
for (auto i = 0; i != 10; i++) { sender.send("Lots of messages! :-/"); }
//

The socket will remain open until the ‘sender’ object goes out of scope.

It’s probably best to add exception handling to your calls as they can fail for lots of reasons and as ever networking is very flaky & unpredictable.

//
try {
    boost_udp_send_faf("192.168.1.44", 8861).send("The message!");
} catch (const boost::system::system_error& ex) { cerr << "Send failed: " << ex.what(); }
//

This will also ensure that the socket is closed once the message is sent as the object will go out of scope when execution leaves the try block.

The code for boost_udp_send_faf can be found in this repo on GitHub, but I have in-lined it below as well.

For an easier way to receive UDP data see: boost_udp_receive_rar

 

#pragma once
//   Copyright 2018 Kevin Godden
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
#include <boost/asio.hpp>
#include <string>
//
// Fire-and-Forget (FAF) transmission for UDP
// using the boost::asio lib. Very simple wrapper
// around the boost libs.
//
// Can throw the following exception:
//	boost::system::system_error
//
// Synopsis --->
//
// For a single Fire-and-Forget send:
//
// #include "boost_udp_send_faf.h"
//
// boost_udp_send_faf("192.168.1.44", 8861).send("The message!");
//
//
// To send multiple datagrams while keeping the socket open and
// reusing end-point:
//
// #include "boost_udp_send_faf.h"
//
// boost_udp_send_faf sender("192.168.1.44", 8861);
//		
// for (auto i = 0; i != 10; i++) {
//     sender.send("Lots of messages! :-/");
// }
//
// The socket will remain open while the boost_udp_send_faf object is in scope
// so to control when the socket is closed, control the scope of the object.
//
// If you want to do anything fancy like using scatter-gather buffers etc. then
// just use the boost libs!  This is for real simple stuff!
//

class boost_udp_send_faf {
    boost::asio::io_service io_service;
    boost::asio::ip::udp::socket socket;
    boost::asio::ip::udp::endpoint remote_endpoint;
public:
    boost_udp_send_faf(const std::string& ip_address, const int port, const bool broadcast = false) : socket(io_service) {
        
        // Open socket
        socket.open(boost::asio::ip::udp::v4());
        // I wouldn't recommend broadcasting unless you are
        // in complete control of your subnet and know
        // what's on it and how it will react
        if (broadcast) {
            boost::asio::socket_base::broadcast option(true);
            socket.set_option(option);
        }
        // make endpoint
        remote_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address::from_string(ip_address.c_str()), port);
    }
    // Send a string to the preconfigured endpoint
    // via the open socket.
    void send(const std::string& message) {
        boost::system::error_code ignored_error;
        socket.send_to(boost::asio::buffer(message), remote_endpoint, 0, ignored_error);
    }
    // Send some binary data to the preconfigured endpoint
    // via the open socket.
    void send(const unsigned char* data, const int len) {
        boost::system::error_code ignored_error;
        socket.send_to(boost::asio::buffer(data, len), remote_endpoint, 0, ignored_error);
    }
};

 

 

Use PIC Timer2 not Timer0 for accurate Interval Timing on an Embedded system

Recently I was struggling to achieve very accurately synchronised camera and light triggers for a real-time computer vision project that I was working on, my original PIC embedded system for triggering everything using a PIC micro-controller had a fairly reliable accuracy of about 250us which was sufficient for a few years, but for various reasons this needed to be reduced by a factor of 10 to about 25us. The problem was that as I reduced the timer’s overflow period it started to become quite inaccurate and erratic.

It is possible to achieve quite accurate timing on your embedded software system using a PIC, however it can take a bit of work to tune things. Most articles that introduce the use of timers on the PIC use the Timer0 module as an example, maybe because it has a ‘0’ in it?

Now, when using Timer0 to produce a repeated time interval, its High & Low overflow registers must be manually reset in the interrupt handler to setup the next time period, all while the timer is still counting away.

This it makes it difficult to know what values to put into the registers to get an accurate overflow period – you end up trying to account for the interrupt latency and counting assembly instructions in the interrupt handler to estimate times etc.

The upshot is that it’s hard to get a reliable overflow period with a desired accuracy when using Timer0.

Example, initial Timer0 setup code:

void init_timers() {
    // See http://www.enmcu.com/software/timer0calculatorandcodegeneration
    // for values.
    INTCON.GIE=1;         // global interrupt enable
    INTCON.PEIE=1;        // peripheral interrupt enable
    INTCON.TMR0IF = 0x0;  // Clear timer0 overflow interrupt flag
    INTCON.TMR0IE = 1;    // enable the timer0 by setting TRM0IE flag
    T0CON.T08BIT = 0;     // 16 Bit timer
    T0CON.T0CS = 0;       // Internal clock
    T0CON.PSA = 1;        // Set scaler to 1:4
    // Set timer counts
    TMR0H = TIMER_HIGH;
    TMR0L = TIMER_LOW;
    T0CON.TMR0ON = 1;     // Turn Timer0 on.
}
// interrupt handler for the timer overflow
void interrupt(void) {
    // Is this a Timer 0 interrupt?
    if (INTCON.TMR0IF) {
        // yes!
 
        // Reset the timer values
        // These are actually now incorrect
        // as time has elapsed since the timer
        // overflow and now!
        TMR0H = TIMER_HIGH;
        TMR0L = TIMER_LOW;
        
        // Reset interrupt flag
        INTCON.TMR0IF = 0;
        // Increase some counter
        ++counter;
    }
}

The best thing to do is to ditch Timer0 and switch to Timer2 instead. Timer2 automatically handles this kind of pattern (and it’s not much harder to use even though it’s got a ‘2’ in its name!).

When using Timer2 you don’t need to reload the overflow registers and so it is much easier to get an accurate and reliable overflow period! Have a look here if you need help in figuring how to setup Timer2 for your desired timing parameters – it’s very handy!

void init_timers() {
  
    // Have a look at the URL below for setting up Timer2
    // http://eng-serve.com/pic/pic_timer.html
    //
    // Setup Timer2 for 40KHz, period = 25us
    
    // Timer2 Registers
    // Prescaler = 1 - TMR2
    // PostScaler = 1 - PR2 = 200 
    // Freq = 40000.00 Hz 
    // Period = 0.000025 seconds
    T2CON |= 0;         // bits 6-3 Post scaler 1:1 thru 1:16
    T2CON.TMR2ON = 1;   // bit 2 turn timer2 on;
    T2CON.T2CKPS1 = 0;  // bits 1-0  Prescaler Rate Select bits
    T2CON.T2CKPS0 = 0;
    PR2 = 200 - 1;      // PR2 (Timer2 Match value), 200 - 1 (1 = magic value, KG)
    // Interrupt Registers
    INTCON = 0;           // clear the interrupt control register
    INTCON.TMR0IE = 0;    // bit5 TMR0 Overflow Interrupt Enable bit...0 = Disables the TMR0 interrupt
    PIR1.TMR2IF = 0;      // clear timer1 interrupt flag TMR1IF
    PIE1.TMR2IE = 1;      // enable Timer2 interrupts
    INTCON.TMR0IF = 0;    // bit2 clear timer 0 interrupt flag
    INTCON.GIE = 1;       // bit7 global interrupt enable
    INTCON.PEIE = 1;      // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
}
void interrupt(void) {
    // Is this a timer 2 interrupt?
    if (PIR1.TMR2IF == 1) {
        // Yes, all we have to do is clear the
        // flag, we don't need to reload any registers!
        // Clear interrupt flag
        PIR1.TMR2IF = 0;        
        ++counter;
    }
}

Once Timer2 is initialised in this manner, it will cycle automatically without the need to reload any values yielding a stable (and hopefully more accurate) time period.

Using ‘Mod’ on (small) Embedded Systems while Avoiding Time Penalties

The Mod/Modulo (% in C) operator is incredibly useful in many situations, it calculates the remainder from an integer division, for example 10 Mod 3 = 1 (10 Div 3 = 3, 3 * 3 = 9, 10 – 9 = 1).

Although you can happily call the % operator from your embedded C program it is important to be aware that it is typically implemented using integer division and multiplication, which is fine if your embedded system support these in hardware, if not they will be implemented in software sometimes resulting in huge time penalties (for example, on many PIC chips).

Often this won’t be a problem, but if you’re programming a real-time system these delays can be devastating! A big sting-in-the-tail from an innocent looking operator %

So what can be done:

a.) Avoid using % in your time-sensitive code, perhaps use running sums and remainders instead.

b.) Use a power-of-2 in your Mod call instead.

If the value of the right-hand operator isn’t crucial and both arguments are positive, then switching to a power of 2 will make it much easier and faster to calculate, a power-of-2 Mod can be calculated with an AND instruction.

a % b == a & (b - 1)    // if b is a power of 2 and a, b are both +ve.

Your compiler should figure out that you are using a power-of-2 and automatically carry out the optimisation, but to be safe, check the output assembly code. If it hasn’t optimised then you can replace your code with a & (b – 1)

Python Monty Hall Problem Simulation

The Monty Hall Problem is a very (to me at least) counter-intuitive probability mind-experiment which contorts my brain and fascinates me at the same time, I have been mulling it over the last few weeks and wanted to write a little simulator to see if the numbers come out as predicted (if not expected, and indeed they do!). I can just about understand the probabilistic arguments, but I still find it very confusing, as soon as I think that I grok it – my ‘understanding’ disappears into the night! I am used to being this bamboozled when reading about quantum mechanics or something, but I find it fascinating that such an apparently simple problem can be so deceptively deep!

Summary

There are 3 doors, behind one lies a car, while behind the other two are goats. A player chooses a door at random. Monty opens one of the other doors to show that there is a goat behind it. Monty then asks the player if they would like to stick with their original choice of door or switch to the other un-opened door.

If the player sticks with their door then their chance of winning the car should be 1/3. If the player switches door then their chances of winning the car increases to 2/3!!!

If you are having problems understanding the outcome, I find it helps to imagine that there are a million doors rather than 3. After you choose your door (1/1,000,000 chance of hiding the car) Monty opens up 999,998 doors that hide goats to leave one door still closed. Now which door do you think is most likely to hide the car? The one you choose, or the one that Monty avoided opening while he opened all 999,998 other doors?! It seems obvious to me that the other door that Monty left un-opened has a massively higher chance of hiding the car than your original choice! As N reduces to 3 this ‘obviousness’ reduces greatly however! This simulator allows you to experiment with more then 3 doors for this reason.

For more info: https://en.wikipedia.org/wiki/Monty_Hall_problem

This program is an experimental simulator to see what numbers we get when the player decides to stick or switch. It can be found on GitHub.

#! /usr/bin/python
#   Copyright 2019 Kevin Godden
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
import random
"""
    Simulates the Monty Hall problem with 3 doors
    The Monty Hall problem is a very (to me at least) counter-intuitive probability mind experiment which
    contorts my brain and fascinates me at the same time. I can just about understand the
    probabilistic arguments, but I still find it confusing.  This program is an experimental
    simulator to see what numbers we get when the player decides to stick or switch.
    If you are having problems understanding the outcome, I find it helps to imagine that there
    are a million doors rather than 3.  After you choose your door (1/1,000,000 chance of hiding the car)
    Monty opens up 999,998 doors that hide goats to leave one door still closed.  Now which door do you think
    is most likely to hide the car? The one you choose, or the one that Monty avoided opening while
    he opened all 999,998 other doors?!  It seems obvious to me that the other door that Monty left un opened
    has a massively higher chance of hiding the car than the original choice!  As N reduces to 3 this
    obviousness reduces greatly however!  This simulation can be run with more than 3 doors to experiment
    with this...
    Summary:
    --------
    There are 3 doors, behind one lies a car, while behind the other two are goats.
    A player chooses a door at random.  Monty then opens one of the other doors to show that
    there is no car behind it.  Monty asks the player if they would like to stick with their
    door or switch to the other un-opened door.
    If the player sticks with their door then their chance of winning the car should be 1/3
    If the player switches door then their chances of winning the car increases to 2/3!!!
    If you want to run the game with more doors to see how the probabilities change
    just change the door_count value below.
    For more info: https://en.wikipedia.org/wiki/Monty_Hall_problem
"""
# Should the player switch their guess or stick
# with their original one?
switch = True
# The number of doors in the game, should be 3 for
# the problem as normally stated.
door_count = 3
# The number of times to run the simulation
game_count = 10000
# Should we print lots of messages?
# If door_count is large change this to True to
# make simulation run faster.
quiet = False
def pick_random_door():
    """
    Pick a door at 'random', doors are 0-indexed so this will
    return a number between 0 and door_count - 1
    :return: the chosen door
    """
    return random.randint(0, door_count - 1)
def pick_monty_doors(car_door, players_guess):
    """
    Pick the doors for Monty to open, of the number
    of doors is 3 then Monty opens up the first door that doesn't
    have a car and that play hasn't already chosen...
    If door_count > 3 then Monty will open up all of the doors
    that hide goats avoiding the door that has been already picked
    :param car_door: The door behind which the car resides
    :param players_guess: The door which the play choose
    :return: A list of the indices of the doors that Monty opens
    """
    monty_doors = []
    for d in range(0, door_count):
        if len(monty_doors) == door_count - 2:
            break
        if d == car_door:
            continue
        if d == players_guess:
            continue
        monty_doors.append(d)
    return monty_doors
def pick_other_door(players_guess, monty_doors):
    """
    The player has chosen to switch their chosen door, so we
    need to pick another one for them.  We pick the door that
    a.) Doesn't match the player's original guess
    b.) Wasn't chosen by Monty
    :param players_guess:  The door that the player originally picked
    :param monty_doors: The doors that Monty has opened (usually just 1)
    :return: The other door's index
    """
    for d in range(0, door_count):
        # Can't switch to the player's original guess
        if d == players_guess:
            continue
        # Don;t pick a door that Monty has opened
        if d in monty_doors:
            continue
        return d
def run_game():
    """
    Run a single game
    :return:  win/loose --> True/False
    """
    # Pick the door with the car
    car_door = pick_random_door()
    if not quiet:
        print("Car is behind door %d" % car_door)
    # Player chooses a door
    players_guess = pick_random_door()
    if not quiet:
        print("Player has guessed door %d" % players_guess)
    # Monty opens up a door that doesn't have a
    # car behind it.
    monty_doors = pick_monty_doors(car_door, players_guess)
    if not quiet:
        print("Monty opens: " + str(monty_doors))
    # Does the player switch doors after Monty
    # opens his??
    if switch:
        players_guess = pick_other_door(players_guess, monty_doors)
        if not quiet:
            print("Player switches to door %d" % players_guess)
    if not quiet:
        if players_guess == car_door:
            print("Player wins!")
        else:
            print("Player looses ;-(")
    return players_guess == car_door
def run_games():
    wins = 0
    game = 0
    while game < game_count:
        game += 1
        # Run a game and see if we win!
        if run_game():
            wins += 1
    print("Games: %d" % game)
    print("Wins: %d" % wins)
    print("------------------------------")
    if wins == 0:
        print("you didn't win any games!")
    else:
        win_rate = wins / game
        print("Win rate: %f" % win_rate)
if __name__ == "__main__":
    run_games()

boost::split – warning C4996: ‘std::copy::_Unchecked_iterators::_Deprecate’: Call to ‘std::copy’ with parameters that may be unsafe

If you’re using the very handy boost::split() on Visual Studio, then you may run into the following annoying warning:

warning C4996: ‘std::copy::_Unchecked_iterators::_Deprecate’: Call to ‘std::copy’ with parameters that may be unsafe – this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ ‘Checked Iterators’

It’s a big warning that rightly litters the compiler’s output!!

Everything’s OK, it’s just VS whinging about std::copy() being called with pointers. As the warning mentions, you can disable the warnings by defining _SCL_SECURE_NO_WARNINGS, its probably best to define it only for the affected files and not your whole project however!

Another way is to explicitly instruct that warning 4996 be ignored in the code like this:

//
// Suppress annoying warning for boost:split()
//
#if defined(_MSC_VER) && _MSC_VER >= 1400 
#pragma warning(push) 
#pragma warning(disable:4996) 
#endif 
#include 
#if defined(_MSC_VER) && _MSC_VER >= 1400 
#pragma warning(pop) 
//

More Info

Remote Desktop Connection Closes / Disappears silently soon after Connecting

I had a strange problem with my Windows 10 Remote Desktop connections over the last few days, soon after connecting the connection would simply disappear without any message or warning, this would happen typically under a minute after connecting to the remote machine – amusing, but quite frustrating if you wanted to get anything done!

I searched around and couldn’t find any mention of similar things happening to other folk, but there was some mention of cleaning out stale connection data, so I managed to make the problem go away by going to the Remote Desktop connection manager and deleting the ‘Saved Credentials’ from the connection. When I reconnected everything was fine and has remained so ever since!

Connections from multiple Browsers to Mosquitto MQTT via Web Sockets not Working

If you are using the paho javascript MQTT client library to connect to MQTT via web sockets, you might notice that you can’t connect from multiple browser sessions at the same time. This may be because you are using the same client ID for each connection – you should use a different client ID for each connection.

client = new Paho.MQTT.Client('192.168.1.123', 9001, 'some-client-id');  // Doesn't work

To get around this you could generate a random client Id like this:

client = new Paho.MQTT.Client('192.168.1.123', 9001, Math.random().toString());

This generates a thoroughly meaningless/ugly client Id, but it works…

There is some worry that using an approach like this can lead to a buildup of stale sessions on the server side as the browser will pass a completely different client Id on each connection/refresh but I am not sure, I think this may only affect persistent sessions, will keep an eye on it…

A recipe for building a Web UI using MQTT for your IoT / Embedded App

Cook up a simple & tasty web UI for your embedded project with just a few simple ingredients!  Served on a bed of wholesome Linux goodness on an SBC, (my favourites are: Raspbery Pi or Odroid N2 but substitute alternatives for your own taste) – this is sure to delight family and friends alike!

Ingredients

For the back-end you will need:

Your embedded App  – this is the centrepiece of your creation, it will do some magic and communicate with the UI via MQTT.

python3 – always handy for thickening the sauce!

Flask – tiny & easy to set-up python system for serving your UI’s web pages and static files, perhaps a little lighter on the palette than the ubiquitous Node.js?

Mosquito – for sending MQTT messages between your app and the web UI for updating status and sending commands, MQTT lends a more contemporary taste than the sometimes overused AJAX!

For the front-end you will need:

bootstrap – presentation is soo important!

paho-js – for communicating with MQTT.

HTML & Javascript – the prefect seasoning, no need for some of the heavier spices like Angular.

How To Cook

(details coming soon, hopefully!)