blog

MPY-CROSS: The ultimate MicroPython precompilation tool. Download available

MicroPython is a lean and efficient implementation of the Python programming language that is designed to run on microcontrollers and other resource-constrained devices. One of the key features of MicroPython is its ability to run Python code that has been precompiled into bytecode, which can help to improve the performance of the interpreter and reduce the amount of memory used by the program.

One of the tools that is commonly used to precompile Python code for MicroPython is mpy-cross. This tool is a command-line utility that can be used to convert Python code written in MicroPython into bytecode, which can then be executed on a MicroPython-enabled device.

The latest version of mpy-cross is version 6. This version contains new features, bug fixes and performance improvements over version 5. The main feature of this version is that it supports the latest version of MicroPython and it includes a new option to control the optimization level of the generated bytecode, which can help to reduce the size of the generated code and improve performance.

Another feature is that it now supports the compilation of Python modules, which can be used to improve the performance of the interpreter and reduce the amount of memory used by the program. Additionally, mpy-cross can be used to create standalone MicroPython executables that can be run on devices without a full MicroPython installation.

Using mpy-cross is very simple. Once you have installed the tool, you can use it to convert Python code into bytecode by running the following command:

mpy-cross myfile.py

Full information about its capabilities is available in help text bellow:

usage: mpy-cross-6.exe [<opts>] [-X <implopt>] <input filename>
Options:
--version : show version information
-o : output file for compiled bytecode (defaults to input with .mpy extension)
-s : source filename to embed in the compiled bytecode (defaults to input file)
-v : verbose (trace various operations); can be multiple
-O[N] : apply bytecode optimizations of level N

Target specific options:
-msmall-int-bits=number : set the maximum bits used to encode a small-int
-march=<arch> : set architecture for native emitter; x86, x64, armv6, armv6m, armv7m, armv7em, armv7emsp, armv7emdp, xtensa, xtensawin

Implementation specific options:
  emit={bytecode,native,viper} -- set the default code emitter
  heapsize=<n> -- set the heap size for the GC (default 2097152)

In addition to the performance benefits, mpy-cross is also a useful tool when working with MicroPython scripts that are short on RAM memory. When running a MicroPython script, the interpreter must load the entire script into memory in order to execute it. This can be a problem for devices with limited RAM, as it may not be able to load large or complex scripts.

By precompiling the script using mpy-cross, the size of the script is reduced, which means that less memory is required to load and execute the script. This can be particularly beneficial when working with memory-constrained devices, such as those with limited RAM or flash storage.

For example, if you have a script that is using up all of the available RAM on your device, you can use mpy-cross to precompile the script and see if that reduces the amount of memory used by the script. You can also use the new option to control the optimization level of the generated bytecode, which can further help to reduce the size of the generated code and improve performance.

In summary, mpy-cross is a valuable tool for MicroPython developers working with memory-constrained devices. By precompiling Python scripts into bytecode, it can help to reduce the amount of memory required to load and execute the script, which can improve the performance of the device and enable the execution of more complex scripts.

The -O option is a command-line switch that can be used when running mpy-cross to control the level of optimization that is applied to the generated bytecode. When using this option, you can specify a numerical value from 0 to 3, with 0 indicating no optimization and 3 indicating the highest level of optimization.

The higher the optimization level, the more aggressive the optimization will be. At level 3, the mpy-cross will try to make the code as small as possible and run as fast as possible, but this may increase the compilation time.

The optimization level can have a significant impact on the size and performance of the generated bytecode. For example, at level 0, the generated bytecode will be larger and may run slower, while at level 3, the generated bytecode will be smaller and may run faster.

The default optimization level is -O2, which provides a good balance between code size and performance. If you are working with a device that has limited flash storage and/or RAM, you may want to try using a higher optimization level, such as -O3, to reduce the size of the generated bytecode.

It's worth noting that when you are using mpy-cross to compile modules, the -O option applies only to the module and not to the whole project.

In summary, the -O option in mpy-cross is used to control the level of optimization that is applied to the generated bytecode. By specifying a numerical value from 0 to 3, you can determine how aggressive the optimization should be and make trade-offs between code size and performance. Keep in mind that the default optimization level is -O2, which provides a good balance between code size and performance, and if you are working with a memory-constrained device, you may want to try using a higher optimization level to reduce the size of the generated bytecode.

always check all your files for viruses

MicroPython release.mpy versionVirusTotal report
v1.19 and upVersion 6Read
v1.12 - v1.18Version 5Read

Host your website at home

If you want to share your thoughts, ideas, or business with the world, setting up your own website is a great way to do it. While there are many hosting options available, hosting your website from home can be a cost-effective and convenient solution. By following these steps, you can set up a local network and host your own website with a few simple tools and some basic technical know-how.

Before you get started, you will need to make sure you have the right hardware in place. Here are the basic requirements for hosting a website at home:

A computer: This will be the server that hosts your website. It needs to be stable, reliable, and connected to the internet via a wired or wireless connection. Ideally, you should choose a computer with a fast processor and plenty of storage to ensure that your website can handle a large number of visitors. In most cases, any Raspberry Pi single board computer should be OK for entry stile website-blog.

A router: This device creates a local network, allowing multiple devices to connect to the internet and communicate with each other. There are many different router options available, ranging from basic models to more advanced ones with additional features such as parental controls and guest networking.

A domain name: This is the unique address that people will use to access your website on the internet. You can purchase a domain name from a registrar, such as GoDaddy or Namecheap. Choose a domain name that is easy to remember and reflects the content of your website.

A domain name is a unique address that people use to access your website on the internet. It acts as a human-readable label for your website's IP address, which is a series of numbers that identifies your server on the internet.

There are several reasons why a domain name is required when hosting a website:

  • A domain name makes it easy for people to find and remember your website. Without a domain name, people would have to remember a long string of numbers in order to access your website, which is not only difficult but also not very user-friendly.
  • A domain name gives your website a professional look and feel. By using a domain name that is related to your business or personal brand, you can establish credibility and make a good impression on your visitors.
  • A domain name can help you rank higher in search engine results. Search engines tend to give preference to websites with a relevant and well-known domain name, which can help drive more traffic to your website.
  • A domain name can protect your brand. By registering a domain name that is similar to your business name or brand, you can prevent others from using it and potentially confusing your customers.

Overall, a domain name is an important investment for any website owner. It helps establish your online presence and makes it easier for people to find and access your website.

You have to take care of security very seriously, as you are opening access to wide world. Having default passwords and insecure software leaves you and your server vulnerable.

To create a local network, you will need to connect your router to your modem and configure it according to the manufacturer's instructions. This typically involves accessing the router's web-based configuration page and entering your network name (also known as the SSID) and password.

You may also need to configure additional settings, such as the type of encryption used to secure your network or the frequency of your wireless signal. Consult your router's documentation or online resources for specific instructions.

In order for your website to be accessible from the internet, you will need to assign a static IP address to your server. This will ensure that your website can be found at the same address every time someone tries to access it.

To assign a static IP address, you will need to access your server's network settings and specify a fixed IP address. This may vary depending on your operating system. On a Windows computer, you can assign a static IP address by going to “Control Panel” > “Network and Internet” > “Network and Sharing Center” > “Change Adapter Settings” > “Properties” > “Internet Protocol Version 4 (TCP/IPv4)” > “Properties” > “Use the following IP address.” On a Mac, you can assign a static IP address by going to “System Preferences” > “Network” > “Advanced” > “TCP/IP” > “Configure IPv4” > “Manually.”

Most routers have a built-in firewall to help protect your network from external threats. You will need to configure your firewall to allow incoming connections to your website. This typically involves creating a “firewall rule” that allows traffic on a specific port to pass through the firewall.

In addition to configuring your firewall, you will also need to set up port forwarding, which directs traffic from the internet to your server's IP address. This is typically done through the router's web-based configuration page, where you can specify the port number and the local IP address of your server.

For example, if you are using Apache as your web server software and want to host your website on port 80, you would create a firewall rule that allows incoming traffic on port 80 and set up port forwarding to direct traffic on port 80 to your server's IP address.

There are many different options for web server software, including Apache, NGINX or LightTPD. Once you have chosen a web server, you will need to install and configure it according to the manufacturer's instructions.

Depending on your web server software, you may need to create a virtual host configuration file, which specifies the domain name and document root of your website. You will also need to place your website's files in the appropriate directory on your computer.

Once you have set up your web server and configured your router and firewall, you should be able to access your website from a browser on a device connected to your local network. You can also use a tool like “What's My IP” to verify that your website is accessible from the internet.

Remember to keep your router and computer secure by using strong passwords and keeping your software up to date. With a little bit of planning and setup, you can easily host your own website from the comfort of your own home.

View saved WiFi password on windows

Have you ever wanted to know password that was entered on your computers? Here is and easy way, no need to install. Everything is build-in windows to provide you this information.

Open Command Prompt (start → run → cmd)

netsh wlan show profile

You'll get a list of Wifi's, that you have ever connected. To get password of particular wifi, type following command:

netsh wlan show profile MyWifiName key=clear

just change MyWifiName to something from your list. In “Security settings” group, Attribute “Key Content” you'll see a password saved in your OS.

2022/08/11 12:16 · Ignas · 0 Comments

Push Notifications on Droidscript

NOTE: Proof Of Concept

So, you are developing DroidScript application and your requirement is to send Push-Notifications to user phone. Also, you want to handle correctly those notification messages, so each message would have different outcome: open particular page on your app or do whatever is needed.

Looks like there are some options not to use Google Firebase infrastructure for notifications, but get away with other tool. As this project is more of PoC- there will not be very mature back-end solution or front end. This project is more like showing the way how Push-Notifications can be implemented in you DroidScript app. Obviously, there are more solutions available, and I assume each one witch supports JavaScript should work. But we are learning, so it good to know basics.

This project is to let you understand what it basically needed to have this functionality. No paid tools or licenses are required, event for testing purpose no back-end hardware is required- Public free options can be used.

Basic concept of a project is following: there is Droidscript application, it has Service that runs on background. Service utilise MQTT protocol for receiving messages and creating Android Push-Notifications. Clicks on notification is handled by Main application. Each client should subscribe to unique topic (for PoC I simply used topic notification). Then back-end engine should send messages to required topics.

For MQTT messages, you can fire up your own MQTT Broker or use the Public one. Droidscript MQTT plugin work by communicating with broker user Web Sockets. Wikipedia: WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C

This is not needed if you want to test with public brokers. You can skip this part

If you have chosen to set up your own hardware- please follow this write-up further. For this example I use Mosquitto (by Eclipse) software broker that works on mostly any hardware and operating system.

Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.

My hardware is Raspberry Pi 2B+. The same broker can be used in IOT projects, for example with ESP32 to send you sensors value. Here I will cover configuration required for Web Sockets and MQTT Broker. How this can be installed search here. If you are on linux, after installing broker you need to chaneg configuration. Edit following file with a command sudo nano /etc/mosquitto/mosquitto.conf or any other way you like it.

# if you need to have closed system- you need to
# provide were login details are saved
# if allow_anonymous is set to false
password_file /etc/mosquitto/public/public.passwd
listener 1884
allow_anonymous false

# web sockets configuration
listener 9004
protocol websockets

After changing configuration, you should be able to connect to your Broker via MQTT. For checking it, you can use HiveMQ WebSocket Client. Provide your broker IP, port (and login if needed). If it connects succesfully, you can subscribe to topic $SYS/# Here you will get all statistics how your Mosquitto is running.

The next step is to prepare software side. To work with MQTT- plugin is needed that is can be downloaded from droidscript application. Install it

Create new JavaScript application in your DroidScript . Your project must have two files: main file that is named after you project, and another, named “Service.js”. This tells your DroidScript application to run Service.js as Android service and listed for DroidScript messages.

//Called when application is started.
function OnStart()
{
    //Start our service.
    svc = app.CreateService( "this","this", ()={
        // On service ready
        console.log( "Service Ready" );
    });
    svc.SetOnMessage( OnServiceMessage );
 
    //This will cause your service to start at boot.
    //(Set it to "none" if you need to stop it starting)
    app.SetAutoBoot( "Service" );
 
    var id = app.GetNotifyId();
    if( id ) HandleNotification(id);
}
 
//Called when application is resumed.
//(eg. When user returns from home screen)
// or after notification is clickeed while app was open
function OnResume() {
    app.ShowPopup( "On resume!", "Short" );
	var id = app.GetNotifyId();
    if( id ) HandleNotification(id);
}
 
 
function HandleNotification(id) {
    app.CreateNotification().Cancel(id)
    app.Alert( id, "Notification ID" );
}
 
//Called when messages comes from our service.
function OnServiceMessage( msg ) {
    console.log( "Service Message: " + msg );
}

Here is code for your service file. File must be named exactly this way to make it work as Android service

Service.js
app.LoadPlugin( "MQTT" );
//Note: When running background services on newer Huawei phones, you
//may need to go into the Android battery saving settings and enable 
//DroidScript to run in the background if you want background 
//notifications to work when the phone is locked.
 
//Called when service is started.
function OnStart()
{
	//Force service to foreground on newer devices (required).
	if( app.GetBuildNum() > 25 ) {
        app.SetInForeground( "Waiting for notification...");
	}
 
	// my private mosquitto configuration
	var mosquitto_options = {
    	servers:[{
    		host: "bukys.eu",
    		port: 9004
    	}],
    	keepalive: 1800,
    	username: "myUser",
    	password: "myPass",
    }
    // Unique identifier for each device
    if(app.IsAPK())
        mosquitto_options.clientId = "Android-" + app.GetDeviceId()
    else
        mosquitto_options.clientId = "IDE-" + app.GetDeviceId()
 
    // Public mosquitto connection
    // client = mqtt.connect( 'ws://broker.hivemq.com:8000/mqtt' );
 
    client = mqtt.connect( mosquitto_options );
    client.on( 'connect', ()=>{
        client.subscribe( 'notification', {qos:2} );
 
        // send client id as message to topic "clients"
        client.publish("clients", mosquitto_options.clientId)
    });
 
    // create Android notification when MQTT message arrives to subscribed topic
    client.on( 'message',  (topic, message)=>{
        not = app.CreateNotification();
        not.SetMessage(
            "ticker",
            "MQTT "+topic,
            "Msg: " + message,
            "Message: "+ message
        );
        not.Notify( message );
    });
 
    setInterval(()=>{
        client.publish("heartbeat", new Date().toLocaleString())
    }, 60000)
}

Word of warning- android system limits web services. In my POC i've set up message publication each 1 minute to HeartBeat topic, so I would know how service behaves under the hood. On Android 11 Samsung device, at first it sends message each 1 minute, but later gradually increasing time between hear-beets. On my test average is 7 minutes. Looks like it does work correctly and on receiving MQTT message- prepare notification. Sure, if you unlock your device, heartbeat is sent each minute. You may need to play around with various battery optimisation setting for your application.

There are various MQTT protocol settings (Retained and QoS) may ensure that message is received even if device is disconnected upon sending notification message to broker from back-end. Client will get it when it connect to it unique topic.

Log Micropython console text to file

Here is a small snippet, how you can write everything what you get on console to file in your Micropython device. This may help you to debug long running devices.

import io, os
 
class logToFile(io.IOBase):
    def __init__(self):
        pass
 
    def write(self, data):
        with open("logfile.txt", mode="a") as f:
            f.write(data)
        return len(data)
 
# now your console text output is saved into file
os.dupterm(logToFile())
 
# disable logging to file
os.dupterm(None)

Older entries >>

  • blog.txt
  • Last modified: 2022/10/26 10:27
  • by Ignas