Tuesday, August 31, 2021

Re-publish RF433 data from a weather station to MQTT with an SDR USB dongle

Thanks to a 433MHz USB dongle and some folks on the web who wrote nice tools, it is pretty easy to collect and interpret the radio data send from my 60€ Aliexpress weather station to its base station, and re-publish them on a MQTT broker.

Oh, and it runs on a sngle-board computer like a Raspberry, a Beagle Bone or an Orange PI.

We need rtl-sdr to talk to the SDR USB radio, and rtl_433 to interpret and publish it as MQTT (it even knows already about the data format for this weather station).

Friday, August 27, 2021

NGINX configuration block to run any fastcgi bash, perl, python scripts


NGINX configuration block to run fastcgi scripts by their shebangs

This configuration will use the shebang at the start of the script to select the proper interpreter.

''
LOCKFILE=/var/lock/$(basename $0).lock
[ -f $LOCKFILE ] && exit 0
trap "{ rc=$?; rm -f $LOCKFILE ; exit $rc; }" EXIT
touch $LOCKFILE
''

''I had to be able to run bash, perl and other kinds of CGI programs but found it difficult to gather all the bits on nginx + fastcgi.

Do not forget to install ''apt install fcgiwrap'' for the generic tool, not the one for PHP.

''
# Bash, perl ... CGI scripts
# Make sure to allow them in this setting of /etc/php/*/fpm/pool.d/www.conf:
#    security.limit_extensions = .sh .py .pl .cgi .exe .php
location ~ ^/cgi.*(\.cgi|\.py|\.sh|\.pl)$ {
root /home/www/cgi-bin;
# Check if CGI script exists
fastcgi_split_path_info ^(.+?\.sh)(/.*)$;
try_files $fastcgi_script_name =404;
# Remove /cgi-bin/ or /cgi/ prefix in path
rewrite ^/cgi(-bin)?/(.*) /$2 break;
# Set parameters
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/www/cgi-bin$fastcgi_script_name;
# apt install fcgiwrap
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
''

Here is a minimalist example of a CGI script made with bash. Just do not forget to return proper HTTP data else you will end up with an error that tells ''upstream prematurely closed FastCGI stdout while reading response header from upstream''):

''
#!/bin/bash
printf "HTTP/1.0 200 OK\r\n"
printf "Content-type: text/plain\r\n\r\n"

printenv | grep ^HTTP_ 

echo "Query string is $QUERY_STRING"
''

Monday, August 9, 2021

Open Web Analytics for Dokuwiki

Customized PHP header for Open Web Analytics best suited to dokuwiki :



''<?php
$actionkind=@$_GET['do'];
if($actionkind=='') $actionkind='page';
if($actionkind=='page' || $actionkind=='search') {
        require_once('/var/www/stats.mysite.com/owa_php.php');
        $owa = new owa_php();
        $owa->setSiteId('4bda91f72a70c48a2ad1d6cdc11523af');
        $owa->setPageTitle(tpl_pagetitle(null,true));
        $owa->setPageType($actionkind);
//      if(@$_GET['q']!="") $owa->setSearchTerms(@$_GET['q']);
        // Set other page properties
        //$owa->setProperty('foo', 'bar');
        $owa->trackPageView();
}
?>''

It helps sorts the collected data and avoids logging administrative accesses. Note that I did not investigate further to make the "search terms" work Dokuwiki ''q'' argument.

Wednesday, August 4, 2021

Profile gcc code and produce a graphical tree

Obviously compile with profile, then use ''gprof'' and ''dot'':

''

gprof myprog.bin | gprof2dot.py -s | dot -Tpng -o profiler.png && gwenview profiler.png

''

Saturday, April 24, 2021

Simple MQTT handler in bash

How to subscribe to an MQTT topic efficiently in bash.

This is hosted on a Raspberry Pi which controls a pool pump via a solid state relay on GPIO 4, and these topics :

  • /pool/pump/action 0  # Command, set externally (subscribed)
  • /pool/pump/state  1  # Feedback state (published)


''

#!/bin/bash -feu
set -o pipefail

mqtt()
{
action="$1"
shift
"mosquitto_${action}" -h 192.168.1.1 -p 8833 -t "$@"
}

handler()
{
new_state=$(echo "$1" | sed 's/[^01]//g')
[[ -z "$new_state" ]] && new_state="0"

if /usr/local/bin/gpio_out.sh 4 "$new_state"; then
echo "pump state change OK, now $new_state"
else
echo "ERROR: pump state change fail towards $new_state"
fi

# Ping back
real_state=$(cat /sys/class/gpio/gpio4/value)
mqtt pub '/pool/pump/state' -m "$real_state" -r
}

export -f mqtt
export -f handler


action={1-}
cfg=$(basename $0 .sh).service
if [[ $action = 'stop' ]]; then
systemctl stop $cfg

elif [[ $action = 'start' ]]; then
systemctl restart $cfg

elif [[ $action = 'install' ]]; then

cat > /etc/systemd/system/$cfg << EOF
[Install]
WantedBy=multi-user.target
[Service]
Restart=always
RestartSec=3
ExecStart=$(realpath $0)
#Type=simple
#Restart=on-failure
EOF

systemctl daemon-reload &&
systemctl restart $cfg &&
systemctl enable $cfg

else # here is the deal

# Run only once
LOCK_FILE="/var/lock/$(basename $0 .sh).lock"
exec 9> "$LOCK_FILE" && flock -n 9 || exit 5
trap "rm -f '$LOCK_FILE'; logger 'Stopped $0 watcher on $(date)'" EXIT
logger "Started $0 watcher on $(date)"

# Wait for orders
mqtt sub '/pool/pump/action' |\
xargs -n 1 -I {} /bin/bash -c 'handler "$@"' _ {}

fi

''

This small script embeds its own ''systemd'' config, just run it with ''install' as the sole parameter.

Wednesday, March 3, 2021

Install CH552 support for platformio (and CH5xx variants) with ch5xduino support

https://community.platformio.org/t/platformio-8051-model-medium-linker-err/18099

''
mkdir ~/.platformio/boards
cd ~/.platformio/boards
cat > ch55xduino.json << 'EOF'
{
  "build": {    
    "f_cpu": "24000000",
    "size_iram": 256,
    "size_xram": 1024,
    "size_code": 14336,
    "size_heap": 128,
    "mcu": "ch552t",
    "cpu": "mcs51"
  },
  "frameworks": [],
  "upload": {
    "maximum_ram_size": 1280,
    "maximum_size": 14336,
    "protocol": "stcgal",
    "stcgal_protocol": "stc15",
    "protocols": [
      "stcgal"
    ]
  },
  "name": "Generic CH552",
  "url": "http://www.wch.cn/product/CH552.html",
  "vendor": "WCH"
}
EOF

platformio boards|grep ch5xduino
cd
mkdir myproj552
cd myproj552
platformio init --board ch55xduino

cat > platformio.ini <<'EOF'
[env:ch552]
platform = intel_mcs51
board = ch552
build_flags =
  --model-medium
  --LC:/Users/caoyf/.platformio/packages/toolchain-sdcc/lib/medium
  -IC:
  -IC:/Users/caoyf/Documents/PlatformIO/Projects/ch552_gpio/src/Driver/inc
  -Isrc/PLAT/inc

extra_scripts = link_medium.py
'EOF'

cat > link_medium.py
Import("env")
env.Append(LINKFLAGS=["--model-medium"])

''

Tuesday, July 7, 2020

Install KiCad 5.1 on xubuntu (no-brainer since the packages are somehow broken)

Big mess. The easiest is to get rid of everything and not rely entirely on the Debian packages. I had to gather some info around and I ended up with the following.

Beware, all existing kicad executable and configuration will be wiped out, as I think that 5.1 does not play well with former versions. Especially, libraries and footprints do not show up after installation. This also makes it annoying to import a former KiCad project in 5.1. I had none so I "simply" started from blank projects, as KiCad is much easier to design new packages and footprint than Eagle CAD I was using so far.


Microreview: dual outlet aquarium air pump


It took me a while to notice the little magic button on the back! Switching it got me twice the throughput ... and twice the noise, but it is still OK. I checked it by myself and emptied a 1.5L bottle in about 50 seconds with one outlet.

This is the pump I used to cool the deposited materiels in my old, other blog post http://www.tridimake.com/2016/05/3d-printing-cooling-with-air-pump-aquarium.html

Soldering consumables

Soldering consumables

This cheap "No Clean Flux Dispensing Pen" pen is a bargain. I used one I chose randomly for years, and largely beyond its recommended date. So I was glad to see it still on the market: "951" DJP CW8100 (about ~5€ delivered from banggood).



I also bought this tip "rejuvenator" (about ~7€ delivered from banggood). It works surprisingly well to remove oxidization that could not be "wiped out" with metal sponges. No fumes, no odor, it kinds of "boils dry" instantly when the tip is dipped in it.
The container states it is some common soldering paste but I am glad it is not, as I already have all kinds of solder paste.




Friday, April 17, 2020

LinuxCNC on an old Dell D430 with a dock

http://www.10k3d.com/2015/06/my-journey-into-world-of-cnc-machining_22.html


''
sudo apt-get install git-core
sudo apt-get install libpci-dev
git clone git://git.kiszka.org/smictrl.git && cd smictrl
git clone git://github.com/zultron/smictrl-deb.git debian
fakeroot dpkg-buildpackage -uc -us
sudo dpkg -i ../smictrl_*.deb
sudo sed -i '/exit 0/ismictrl -s 0' /etc/rc.local
sudo reboot
''

Adding bCNC to the laptop:
''
git clone https://github.com/vlachoudis/bCNC.git
''

Profile c++ code


To profile an executable and get a nice visual of it (you need the expected flags when compiled of course):

First you need to compile with argument ''-pg'', so that gcc inserts its code all everywhere to time your function calls.

Once you run the project, it will generate a big file named ''gmon.out''. This is mostly unreadable per se. And ''gprof'' hardly makes it more readable.

Here comes ''gprof2dot.py'' and ''dot'', which generate very readable graphs:
''
gprof exe/router.exe | gprof2dot.py -s | dot -Tpng -o profiler.png && gwenview profiler.png
''

Note that ''gprof'' must be given the executable name. It will not run it in anyway, but it needs the symbol table to give you proper names.


How to submit a patch to the linux kernel

How to submit a patch to the linux kernel



On Ubuntu/Debian you probably need ''sudo apt-get install git-email'' first, unless you want to do it entirely manually (uuh).

''
git log
git format-patch HEAD~
scripts/checkpatch.pl 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
scripts/get_maintainer.pl
git send-email 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
''

You could use ''git format-patch --cover-letter HEAD~'' to generate a blank "introductory" email in order to describe your post further than with the sole summary of the patch itself.

Here is a ''~/.gitconfig'' example for git to send emails:
''
[sendemail]
  ; setup for using git send-email; prompts for password
  smtpuser = jf@gmail.com
  smtpserver = smtp.googlemail.com
  smtpencryption = tls
  smtpserverport = 587
''

Note: you might have to create a specific Google "application password" for the provided pseudo "login" prompt that ''git'' will print.

The tool ''./scripts/get_maintainer.pl'' in linux will help you identify the recipients. It does this very nicely, instead of having to guess from the ''MAINTAINERS'' file.

Finally, to submit the real thing to the linux list https://lkml.org/:
''
git send-email --cc-cmd='./scripts/get_maintainer.pl --norolestats 0001-scripts-config-allow-colons-in-option-strings-for-se.patch' 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
''

It will end up, e.g. as https://lkml.org/lkml/2020/4/10/383 

Sunday, April 14, 2019

Hack to prevent (solder) wire tangles

I just bought at Farnell some really good and really thin solder wire for small jobs.

The issue is the roll does not hold the wire coils enough si it keeps on creating knots.

I finally came down to this simple idea, which makes use of an elastic band that was offered by a random Chinese reseller, but a regular wide elastic band is probably as efficient. Just pull the last unwound wire under the band and it will hold the coils in place.

Tuesday, March 19, 2019

Pogo-pin harness to stress-test an electronic module before it gets soldered

Here I am using retractable pogo pins again (see a previous use here), within a little 3D printed harness.

This electronic tool holds modules both quickly and efficiently without any soldering. This way I can test or program them individually before they are eventually soldered on the final board.


Obviously then, things get much harder to debug, and sometimes you do not even have room for ICSP pins to test in-situ, or to re-program a microcontroller.

Wednesday, October 24, 2018

Warning/issue with micro-USB ZNTER 1250mAh AA rechargeable batteries

Not-a-deal

These rechargeable AA batteries sold under the ZNTER brand are cool... until they are damn too hot.

Actually, one of mine started to smoke heavily. Too bad, I like the idea for technophilic trekkers like me, as they require no bulky AA charger. I did not use them a lot though, and I bought 4 more lately so one was perhaps only defective on arrival (DOA).

Anyhow, the charging circuit is probably wrong if it can get to this point without shutting down. It even killed the plug from the 4-way micro USB cable.

In the end: do not buy, or at least do not charge without an eye on them!

ZNTER 1250mAh "AA" rechargeable batteries, which have an embedded circuitry powered by a micro-usb plug!

Friday, May 18, 2018

Script to add a Let’s Encrypt free certificate to an existing Lighttpd web server

Add Let’s Encrypt Certificates to Lighttpd

I wrote a bash script to help add a let's encrypt SSL certificate to a lighttpd web server via the console. Weirdly, they only provide fully automated support for Apache and Nginx as of now (but anyhow, I like to do it my way).

I highly recommend doing so not only because their certificates are free, but also because they help a lot reducing the usual manual burden to install an SSL certificate on an existing web site.


Sunday, October 22, 2017

Lost component 04H-10 5873J01

04H-10 5873J01? No such references on the internet!
The little tabs are still a mystery to me.
I had a bunch of these SMT components which I did not index properly 4-5 years ago (promised, that was the last time I ever was lazy). Sadly, their reference 04H-10 5873J01 brought nothing back on the whole internet... They came in proper SMT strip and they are large enough to sport decent markings though. Alas, I called for help on G+ and/but I ended up sacrificing one open. Initially I thought they were atmospheric pressure sensors, because of the opening to the side.

But no, they are just completely dumb SMD electromagnetic buzzers. The kind with an old-school little coil that pulls a small metallic disk. Nowadays we tend to see more of the piezo electronic kind, but they still have a role because they slightly smaller and they work at a lower voltage (they are just bad in every other respect!). Being passive, they also need variable current to emit sound (they simply click once with a constant current), and I suspect they are able to vibrate at more than one frequency, i.e. they might be able to transmit sound and voice when a piezo buzzer cannot. I would have to check these ones, but I do not expect their frequency response to be good anyhow.

Tuesday, October 10, 2017

Charge a smartphone battery without the phone nor the charger

I shortly dipped my phone in sea water and the charging/battery control circuitry and micro USB plug got oxidized beyond repair (salt is really terrible). I could have saved it at the time if I had a chance to open it and carefully rinse the electronics with non-salted water immediately, but I miss my tools on holidays... While waiting for a replacement part to arrive, I had to recharge the battery the DIY way.

Recharge a smartphone battery with a super cheap lithium ion recharging module and pogo pins.
The short pieces of thick copper wires (in blue) are compulsory to present the retractable pins correctly
They are maintained in perfect contact with clamps on a soft silicon mat.

Saturday, July 15, 2017

Using PCB rivets vias for homemade double sided PCB

You may remember in a former article the super tiny rivets I bought for double sided prototyped printed circuit boards (these but check those, they are way cheaper!). I realize that I never documented how I used them. So here it is.

The "raison d'être" of these costly rivets is specifically to help doing the vias that route the signals from one side to the other side of the board. The industry does plated holes with a chemical process that deposits a layer of conductive metal on the inside walls. It both conducts the signal but it also really helps making the board sturdier, as the copper rings (well square, here), are bonded together. Without it, the risk to tear apart one of them is much more important. Actually, a few makers do it also but it is both difficult, lengthy, messy and specially risky with a lot of nasty chemicals... not for me since I mill my PCB ;)

So in general, making double sided PCBs at home means there is no such plated holes. So we usually solder a thin wire through the hole on both sides. It is time consuming and it stresses the copper pads a lot, especially when the damn wire falls down when both sides melt again when you just want to solder the other side ;)

Top and bottom layers: making vias by means of PCB rivets on a double-sided milled PCB.
The milling job is quite bad here: the traces were not cut properly on the top layer (left side, check why here),
and the layers were mis-aligned after the board was flipped over to mill the top side... I need more practice.
Also, there are better and expensive tools to fix the rivets, but this prototype worked fine in the end anyway ;)
Now, rivets still help to make more robust single sided PCBs, most notably for the connector holes that will be subject to mechanical stress. Rivets will protect and keep the small copper "rings" tightly bounded to the PCB support material, so they will be less likely torn away.

Most notably, dirt cheap bakelite boards give no second chance in this respect, while fiberglass (FR4) endure more abuse and re-soldering. I guess the bonding is much stronger with the latter.

Which PCB rivets to buy?

I recommend 0.8mm outer diameter (0.6mm ID), because there their heads leave just enough room to route a trace between them. Hence in EAGLE CAD, I make sure to configure my vias to be 0.8mm accordingly, so they fit tightly.

Wednesday, July 5, 2017

Measuring micro amps, the easiest way

You can build yours (see my former review), but you can buy them already made: these $10 sub micro ampmeters on ebay work pretty well from 50mA down to 0.001mA. This range matches nicely the currents of many sensors, including when they are in deep sleep mode. This range is very convenient for IoT autonomous projects and sensors, and the 5 digits makes it unnecessary to switch between milliamperes and microamperes like on a multimeter.

You lose one digit when plugged backwards. The feature is not really interesting except that it makes it safer (you will not burn it by accident).

$10 for these micro current meters is a bargain!
They really are straightforward to use, and they do measure very low currents: the display goes as low as 0.001 mA, ie. 1µA, but I am not sure it is reliable at that level -- anyhow, below a few micro amps, the self-discharge rate of a battery usually becomes not negligible, so there is often no point in fighting further to reduce the sleeping currents. A CR2032 coin cell can run for years at this level, so you would better start optimizing also the wake-up consumption (make sure to read this impressive in-depth review on the matter!).