Architecture/Framework - Question regarding Hardware & Desktop Applications

The Feature Request
We provide SaaS for bakeries and one part of the app caluclates the optimal dough amount for a bakery.
The workflow for guy mixing the doughs is now: 1. read the ingredient amount from the screen 2. weigh it on a scale 3. mark the checkbox on the screen.
As we close larger bakeries we need to integreate a scale to the application which automatically checks each row, resets the scale to 0 etc.

Until Now Anvil and the Web has always served us extremely well. Now that it looks like we need to access hardware we face some problems however.


Possible Routes (I thought about)

  1. Raspberry Pi + Anvil Uplink + Anvil Web Application
  • +We stay in the anvil ecosystem
  • -Does not work offline
  • -we need to maintain/update hubs that will eventually break etc.
  1. Stay in the (anvil) web and use web serials Web Serial API - Web APIs | MDN
  • -My experience with this is limited, but beeing a draft and not well tested is seems not suitable for a production level environment.
  • +stay in the anvil ecosystem
  1. Go Fully local with something like electron, Flutter, UWP etc.
  • +This is for sure the most stable way to build this
  • -I have to split my developers, add addtional tools and thus ressources.
  • ----I have to rebuild the whole app in local since splitting it is not an option

Questions
I am writing this because I like neither of the routes described above. Thus is there a way to stay in the anvil ecosystem and use serial ports with full offline capability?

  1. Is there any way anvil apps could be transformed to a native elecron (like) app for example?

  2. Is it possible to partially embedd an anvil app within a local app that has access to serial ports?

  3. Should i go fully local and stop using a tool for something it’s not made for?

Obviously this is a big coice since I have 2 Anvil Developers and a 95 % finished bakery app and it looks like the last 5 % are biting us hard. Thus any advice or experiences with such things are greatly appreciated.

Greetings from Austria,
-Mark

Another option, which I’ve implemented elsewhere, is to use the open source app server. You can embed that on whatever local device you choose.

If necessary, it could also uplink to another cloud hosted app for some functionality.

1 Like

Thanks for your promt answer!

Interessting!
Does your solution work if the device has no internet connection during runtime - since it could be on the same device?

Yes. Exactly that. The uplink is for centralised management stuff only and not necessary for the ordinary use.

1 Like

Does your Anvil app work offline?
If yes, good job!
I always require a working connection.

One way I have connected external devices to closed apps is with the keyboard.

If it reasonable to assume that the browser with the Anvil app has the focus, then this will be easy.

If you need the scale to be able to collect data even when the app doesn’t have the focus or is not running, then it could be difficult, but still feasible.

Many devices come with a driver that automatically sends a string of characters to your keyboard driver through an usb port when the measurement is taken, and they are often configurable to send some prefix characters. You could write a javascript function that listens to the keyboard at the body level and, if you see the prefix that it’s telling you that it’s the scale typing, you put the focus on the text box and populate it, or, better, you call a Python function, so the form does whatever it needs to do with it. You should configure the prefix to be a character that does not exist in the keyboard.

I have used this with Excel and VB6 applications. I did have access to serial ports, but using the keyboard was so easy that I decided I didn’t want to mess with protocols that change when you change the device.

If your scales don’t offer the keyboard driver option, then you can use a Raspberry Pi to communicate with the scale on one side and send the string of characters to the computer on the other.

1 Like

Great Option. Still - I imagine streaming some data will be more difficult. If I want to build a live view of the weight on the scale.

On second thought, a timer which refreshes ever thenth of a second might do the job…

Anyway - every solution that keeps us in anvil is welcome. We will definetly do some tests on that and report back with a video and experience on that in this post :ok_hand:

1 Like

Nice thanks for the tip! And thanks for taking the time to answer!

Will check if the scale “types” anything :ok_hand:

But since we must be able to send data back to the scale (to reset the measuring for example) this would only solve half of our problem.

-Mark

If the communication needs to be bidirectional then the keyboard option will not work.

Using the open source server, you either move the whole app that you already have to the local server or you still have the problem of communicating between the app in the local server and the online app… unless I’m missing something.

Also, using the local server will require the installation of the software on the local computer, which kind of defeats the convenience of using a web app.

I would go with option 2 and explore Web Serial.
The fact that is not well tested doesn’t bother me. I know nothing about Web Serial, but if they say it gives you access to the serial ports, then it should be the easier way.

It may be difficult (not too much) to get it to work the first time, but once it works, it works. Direct communication with devices via serial ports is usually very basic. There are no intermediate layers for security or any other purposes, and it keeps working the same way for ages, without the risk that an update to the os or to the device will break it. If you get that library to work once, you are set.

With the local app server, you could look at using pyserial on the server side.

Thanks for all the feedback on the topic @owen.campbell @stefano.menci - greatly appreciated!

Our test scale will ship next week and we will explore most of the mentioned paths in this post.
I will try to report the results and the final design choice here for future readers.

@owen.campbell
One more thing I thought about is creating a local python script which is connected via uplink and uses pyserial. It would need to be started with the app however.

What makes you think the web serial API won’t work? We have used the web USB API with serial-to-USB adapters to program embedded devices. There are some caveats but it works fine. The whole idea is allow web apps to access local devices. We have also used the uplink on Raspberry Pi’s but this seems like overkill here.

1 Like

The other approach would be to set up a serial-to Ethernet adapter on the local network, and connect to it from JavaScript in the web app. Protocols would depend on the Ethernet device, but some serial servers are programmable.

1 Like

I would call that a RaspberryPi :slight_smile:

Well I guess there are other more professional solutions, but a Pi would give you all the freedom you need.

1 Like