Tuesday, 19 November 2019

openCV Troubleshooting (1) : undefined reference to `cv::xxxx`


I am compiling openCV project with cmake and I am getting errors like:

test.cpp:(.text+0xc0): undefined reference to `cv::imread(std::string const&, int)'
test.cpp:(.text+0x11f): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
test.cpp:(.text+0x138): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x158): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
test.cpp:(.text+0x180): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'

The following compiling script won't cause the issue.
g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs`

But to fix cmake compile, we need to add the following red line to CMakeList.txt.

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )


ref:

Tuesday, 13 August 2019

Tinkering OpenWRT (18) : Toubleshooting MT7688AN Hardware




Problem (1) : Serial to TTL not working
     - Try use a laptop instead of a desktop. There might be compatibility issue between the motherboard and USB. Or just not enough power from USB on a desktop.
     - Try use a higher input than 3v3. Even 5v works.

ref:
https://blog.csdn.net/u012175418/article/details/52572672





Sunday, 14 July 2019

openCV Troubleshooting (0): error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

Trying to compile some example code (in Python) from the internet and got the following error:

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    img = drawFaces(img)
  File "/home/boris/facedetect_example/example2/OpenCV-demo-master/Video-FaceDetection/face_detection.py", line 26, in drawFaces
    faces = detectFaces(img)
  File "/home/boris/facedetect_example/example2/OpenCV-demo-master/Video-FaceDetection/face_detection.py", line 18, in detectFaces
    faces = face_cascade.detectMultiScale(gray, 1.2, 5)
cv2.error: OpenCV(4.1.0) /home/boris/workspace/opencv-4.1.0/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

It turns out the cascade XML or file is missing or the path to it is incorrect or the create_capture path is incorrect. Of course, this is someone else's code.

Updated this line:
face_cascade = cv2.CascadeClassifier("/home/aaaaa/OpenCV-demo/opencv-data/haarcascades/haarcascade_frontalface_default.xml")

To 
face_cascade = cv2.CascadeClassifier("/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml")

Everything works now.


ref:
https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale

Sunday, 3 February 2019

KiCAD Tips -- 0 : How to Add a Footprint Library

I was still following the blinky example:
       https://contextualelectronics.com/topic/library-setup-and-part-association/

And it appears I don't have Housing SOIC footprint in my library. Here is how I solved it.

Step 1:
I couldn't find CvPCB anymore. But you can open it in EESchema, click


Step 2:
Preferences => Manage Footprint Libraries

Step 3:
Download your needed library from the following link:
      https://github.com/KiCad/kicad-library/wiki/Footprint-Libraries
I needed:
      https://github.com/kicad/Housings_SOIC.pretty

Step 4:
From the "Manage Footprint Libraries" Dialog, click "Browse Libraries..." and choose the downloaded file of the footprints.

Step 5:
You can notice the lists update in 5 seconds.



Friday, 1 February 2019

KiCAD Tips -- 0 : How to Add a Library

The following tutorial is awesome but like may other materials online, it is in KiCAD 4.0.
                      https://contextualelectronics.com/topic/creating-schematic-symbols/

The steps to create a new library and switch from the official library directory to the project directory to edit my own library are totally gone in KiCAD 5.0.2.

But eventually, I figured it out and it is actually easier.

Step 1: Click   to open the Symbol Library Editor.

Step 2: From the search tree on the left side, input device and select C. As described in the above tutorial, we are going to create our own library based on existing library. This capacitor is just a holder.

We can see from the above picture on the top bar, it is still pointing to the CAD's own library. But we want to customize it in our own project directory. 

Step 3: 
Click  to create a new library

Step 4: save the new library in your project


and 


Step 5: 
Now you can see it is added to your library tree.

But check out the top bar, it is still saying /usr/share/kicad/library...

Step 6: Right click myBlink => new symbol
Name your new component in this library:

Step 7:
Now the working directory has been set to your project and you can edit your component in your own library.













Monday, 14 January 2019

NO_PUBKEY Issue

Hit:21 http://ppa.launchpad.net/webupd8team/sublime-text-3/ubuntu xenial InRelease
Fetched 1,490 B in 1s (1,302 B/s)                 
Reading package lists... Done
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://dl.winehq.org/wine-builds/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 76F1A20FF987672F
W: GPG error: http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0A506F712A7D8A28
W: The repository 'http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release' is not signed.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: Failed to fetch https://dl.winehq.org/wine-builds/ubuntu/dists/xenial/InRelease  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 76F1A20FF987672F
W: Some index files failed to download. They have been ignored, or old ones used instead.

wget -nc https://dl.winehq.org/wine-builds/winehq.key && sudo apt-key add winehq.key && sudo apt update



boris@boris-home:~$ wget -nc https://dl.winehq.org/wine-builds/winehq.key && sudo apt-key add winehq.key && sudo apt update
--2019-01-15 00:43:38--  https://dl.winehq.org/wine-builds/winehq.key
Resolving dl.winehq.org (dl.winehq.org)... 151.101.126.217
Connecting to dl.winehq.org (dl.winehq.org)|151.101.126.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3220 (3.1K) [application/pgp-keys]
Saving to: ‘winehq.key’

winehq.key          100%[===================>]   3.14K  --.-KB/s    in 0s      

2019-01-15 00:43:38 (77.1 MB/s) - ‘winehq.key’ saved [3220/3220]

OK
Ign:1 http://linux.dropbox.com/ubuntu wily InRelease
Ign:2 http://dl.google.com/linux/chrome/deb stable InRelease                   
Hit:3 http://linux.dropbox.com/ubuntu wily Release                             
Hit:6 http://archive.canonical.com/ubuntu xenial InRelease                     
Hit:7 http://repository.spotify.com stable InRelease                           
Ign:8 http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  InRelease
Hit:9 http://dl.google.com/linux/chrome/deb stable Release                     
Get:10 https://dl.winehq.org/wine-builds/ubuntu xenial/main amd64 Packages [145 kB]
Hit:11 http://ca.archive.ubuntu.com/ubuntu xenial InRelease                    
Hit:12 http://ppa.launchpad.net/fpoussin/ppa/ubuntu xenial InRelease           
Hit:13 http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release
Get:14 https://dl.winehq.org/wine-builds/ubuntu xenial/main i386 Packages [151 kB]
Get:15 http://ca.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]   
Get:16 http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release.gpg [481 B]
Hit:18 http://ppa.launchpad.net/gezakovacs/ppa/ubuntu xenial InRelease         
Ign:16 http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release.gpg
Hit:19 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/ubuntu xenial InRelease
Get:20 http://ca.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB] 
Hit:21 http://ppa.launchpad.net/webupd8team/sublime-text-3/ubuntu xenial InRelease
Hit:22 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
Reading package lists... Done 
W: GPG error: http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 0A506F712A7D8A28
E: The repository 'http://download.opensuse.org/repositories/home:/emby/xUbuntu_16.04  Release' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

boris@boris-home:~$ 


You can see the key issue from WinHQ is gone now.

###########################################################

$ sudo -i 
 # apt-get clean 
 # cd /var/lib/apt 
 # mv lists lists.old 
 # mkdir -p lists/partial 
 # apt-get clean 
 # apt-get update

sudo sed -i '/opensuse/ s/^/#/' /etc/apt/sources.list.d/*.list


Now the key issue from opensuse should also be gone.


ref:
https://askubuntu.com/questions/1103320/the-following-signatures-couldnt-be-verified-because-the-public-key-is-not-avai
https://ubuntuforums.org/showthread.php?t=1869890&page=3
https://askubuntu.com/questions/522114/cant-get-rid-of-gpg-error-http-download-opensuse-org


Tuesday, 1 January 2019

Troubleshooting when Setting up Github Page

At the step of setting up Ruby locally for testing, I got this:

Fetching gem metadata from https://rubygems.org/..............
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies.....
Fetching concurrent-ruby 1.1.4
Installing concurrent-ruby 1.1.4
Fetching i18n 0.9.5
Installing i18n 0.9.5
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 4.2.10
Installing activesupport 4.2.10
Fetching public_suffix 2.0.5
Installing public_suffix 2.0.5
Fetching addressable 2.5.2
Installing addressable 2.5.2
Using bundler 1.17.3
Fetching coffee-script-source 1.11.1
Installing coffee-script-source 1.11.1
Fetching execjs 2.7.0
Installing execjs 2.7.0
Fetching coffee-script 2.4.1
Installing coffee-script 2.4.1
Fetching colorator 1.1.0
Installing colorator 1.1.0
Fetching ruby-enum 0.7.2
Installing ruby-enum 0.7.2
Fetching commonmarker 0.17.13
Installing commonmarker 0.17.13 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/boris/workspace/my_Github_Website/codemonkeyboris.github.io/vendor/bundle/ruby/2.5.0/gems/commonmarker-0.17.13/ext/commonmarker
/usr/bin/ruby2.5 -r ./siteconf20181231-18337-kelz2o.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in
/home/boris/workspace/my_Github_Website/codemonkeyboris.github.io/vendor/bundle/ruby/2.5.0/gems/commonmarker-0.17.13 for inspection.
Results logged to
/home/boris/workspace/my_Github_Website/codemonkeyboris.github.io/vendor/bundle/ruby/2.5.0/extensions/x86_64-linux/2.5.0/commonmarker-0.17.13/gem_make.out

An error occurred while installing commonmarker (0.17.13), and Bundler cannot continue.
Make sure that `gem install commonmarker -v '0.17.13' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  github-pages was resolved to 193, which depends on
    jekyll-commonmark-ghpages was resolved to 0.1.5, which depends on
      jekyll-commonmark was resolved to 1.2.0, which depends on
        commonmarker

#####################################################

Solution:

$sudo apt-get install ruby-all-dev
$bundle install --path vendor/bundle


ref:
https://help.github.com/articles/setting-up-your-github-pages-site-locally-with-jekyll/
https://github.com/github/pages-gem/issues/133



Wednesday, 26 December 2018

Tinkering OpenWRT (17) : trouble shooting (build source code) target 'world' failed problem

I am trying to compile openwrt and got the following error:

include/toplevel.mk:216: recipe for target 'world' failed

I tried using 15.05 and checked out 18.06 (yes the head master is not newest 18.06) and I got the similar problem. Only the line number is sometimes different. After googling a loooooot, I found the simple solution.

$ make distclean
$ make clean

maybe also make defconfig

And $ make V=99

After over night...I found the image in bin/targets.


ref:
https://www.right.com.cn/forum/thread-186603-1-1.html
https://www.gargoyle-router.com/phpbb/viewtopic.php?t=8011&start=10

Tuesday, 25 December 2018

Tinkering OpenWRT (16) : openwrt Chaos Calmer Build dependency: Please install Git (git-core) >= 1.6.5


For some reason, I'd like to build my openwrt from 15.05 Chaos Calmer. But I ran into the following problem:

~/workspace/chaos_calmer$ make menuconfig 
Checking 'working-make'... ok.
Checking 'case-sensitive-fs'... ok.
Checking 'gcc'... ok.
Checking 'working-gcc'... ok.
Checking 'g++'... ok.
Checking 'working-g++'... ok.
Checking 'ncurses'... ok.
Checking 'zlib'... ok.
Checking 'libssl'... ok.
Checking 'tar'... ok.
Checking 'find'... ok.
Checking 'bash'... ok.
Checking 'patch'... ok.
Checking 'diff'... ok.
Checking 'cp'... ok.
Checking 'seq'... ok.
Checking 'awk'... ok.
Checking 'grep'... ok.
Checking 'getopt'... ok.
Checking 'stat'... ok.
Checking 'md5sum'... ok.
Checking 'unzip'... ok.
Checking 'bzip2'... ok.
Checking 'wget'... ok.
Checking 'perl'... ok.
Checking 'python'... ok.
Checking 'svn'... ok.
Checking 'git'... failed.
Checking 'file'... ok.
Checking 'openssl'... ok.
Checking 'ldconfig-stub'... ok.

Build dependency: Please install Git (git-core) >= 1.6.5

/home/boris/workspace/openWRT_build/openwrt_RPi/chaos_calmer/include/prereq.mk:12: recipe for target 'prereq' failed
Prerequisite check failed. Use FORCE=1 to override.
/home/boris/workspace/openWRT_build/openwrt_RPi/chaos_calmer/include/toplevel.mk:140: recipe for target 'staging_dir/host/.prereq-build' failed
make: *** [staging_dir/host/.prereq-build] Error 1

##########################################################################

Solution:

Step 1: Download the patch from here:
https://gitlab.labs.nic.cz/turris/openwrt/uploads/0673001b14ab1d1769604ff0ce7d8781/git-version-check-fix.patch

Step 2: put the patch file under the chaos calmer source code

Step 3: patch it
git apply git-version-check-fix.patch


ref:
https://forum.openwrt.org/t/solved-how-to-compile-15-05-release-build-dependency-please-install-git-git-core-1-6-5-message/11623/3
https://gitlab.labs.nic.cz/turris/openwrt/issues/56
https://github.com/spack/spack/issues/5329

Tuesday, 27 November 2018

Tinkering GStreamer (0) : See myself from PC Camera


$ gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! ximagesink
Looks pretty clear: source is /dev/video0

ref:
https://github.com/umlaeute/v4l2loopback/issues/174
https://stackoverflow.com/questions/23354041/gstreamer-video-stream-error-and-breaks-dev-video-source

Saturday, 17 November 2018

Develop Micropython Firmware -- 01: Pure Python Based Driver

Micropython is almost like a mini operating system on a MCU and I'd like to build my own project standing on its shoulder.

Step 1: Setting up the environment
Just follow:
or

The awesome thing is that all the environment is in a virtual machine. Your host is safe. So finish configure the vagrant virtual box first and then compile ESP-toochain and Micropython source code in it.

pitfall 1: 
Depending on what kind of error you might run into, the following commands might help:

- vagrant box update
- vagrant box add ubuntu/trusty32 https://atlas.hashicorp.com/ubuntu/boxes/
vagrant provision

Step 2: coding the hello driver
Going into ~/micropython/drivers/, I found there are modules with C codes based and pure python based. In this tutorial, we focus on the pure python based drivers. For example, onewire and display.

step 2.1
mkdir hello_mpy

step 2.2
code hello_mpy.py:

def my_hello_mpy():
  print('This is my first micropython module.')

step 2.3
link it to esp8266 port.

cd ~/micropython/ports/esp8266/modules
ln -s ../../../drivers/hello_mpy/hello_mpy.py hello_mpy.py
ls -l
total 40
-rw-rw-r-- 1 vagrant vagrant  470 Nov 15 04:12 apa102.py
-rw-rw-r-- 1 vagrant vagrant  219 Nov 15 04:12 _boot.py
lrwxrwxrwx 1 vagrant vagrant   27 Nov 15 04:12 dht.py -> ../../../drivers/dht/dht.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 ds18x20.py -> ../../../drivers/onewire/ds18x20.py
-rw-rw-r-- 1 vagrant vagrant 1110 Nov 15 04:12 flashbdev.py
lrwxrwxrwx 1 vagrant vagrant   39 Nov 17 04:03 hello_mpy.py -> ../../../drivers/hello_mpy/hello_mpy.py
-rw-rw-r-- 1 vagrant vagrant 1425 Nov 15 04:12 inisetup.py
-rw-rw-r-- 1 vagrant vagrant  836 Nov 15 04:12 neopixel.py
-rw-rw-r-- 1 vagrant vagrant  850 Nov 15 04:12 ntptime.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 onewire.py -> ../../../drivers/onewire/onewire.py
-rw-rw-r-- 1 vagrant vagrant 1094 Nov 15 04:12 port_diag.py
lrwxrwxrwx 1 vagrant vagrant   22 Nov 15 04:12 upip.py -> ../../../tools/upip.py
lrwxrwxrwx 1 vagrant vagrant   31 Nov 15 04:12 upip_utarfile.py -> ../../../tools/upip_utarfile.py
-rw-rw-r-- 1 vagrant vagrant 2118 Nov 15 04:12 webrepl.py
-rw-rw-r-- 1 vagrant vagrant 2748 Nov 15 04:12 webrepl_setup.py
-rw-rw-r-- 1 vagrant vagrant 1624 Nov 15 04:12 websocket_helper.py

step 2.4 compile again
make axtls
make

step 2.5 
Get the .bin file out of vm.

pitfall 2:
I wasn't able to get my compiled firmware-combined.bin out from vm to host.
I eventually used vagrant-scp tool and it worked.
            https://github.com/invernizzi/vagrant-scp
It works both way, host to vm or vm to host.


Step 3: flash the new firmware
esptool.py -p /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 firmware-combined.bin

Step 4: use it
>>>import hello_mpy
>>>hello_mpy.my_hello_mpy()
This is my first micropython module.









Thursday, 15 November 2018

vagrant up issue

Ran in to problem when running:
$vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Setting the name of the VM: aws_default_1449506528859_
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Specific bridge 'en0' not found. You may be asked to s
==> default: which network to bridge to.
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 => 2200 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minut
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in th
'poweroff' state. Please verify everything is configured
properly and try again.
If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve
For example, if you're using VirtualBox, run vagrant up while the
VirtualBox GUI is open.
The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.
#######################################################
You simply need to turn on the virtualization setting in your BIOS.



Friday, 5 October 2018

Tinkering OpenWRT (15) : Use I2C Bus



Step 1: Do as the official document says

# opkg update 
# opkg install kmod-i2c-gpio-custom kmod-i2c-core 
# insmod i2c-dev 
# insmod i2c-gpio-custom bus0=0,26,12 
# dmesg | grep gpio 
Mar 23 09:01:23 openwrt kern.info kernel: [ 52.910000] Custom GPIO-based I2C driver version 0.1.1 
Mar 23 09:01:23 openwrt kern.info kernel: [ 52.910000] i2c-gpio i2c-gpio.0: using pins 26 (SDA) and 12 (SCL)
Im using my AR9331 board, and Im using GPIO 26 and 12 as my SDA and SCL repectively.
And then what? The official doc just stops here?

Step 2 : Wiring

AR9331        Slave
SDA              SDA
SCL               SCL
GND             GND       <= this is very important

Step 3 : install i2c-tools

# opkg update && opkg install i2c-tools

Step 3 : install i2c-tools
# i2cdetect 0 

WARNING! This program can confuse your I2C bus, cause data loss and worse! 
I will probe file /dev/i2c-0. 
I will probe address range 0x03-0x77. Continue? [Y/n] y
       0 1 2 3 4 5 6 7 8 9 a b c d e f 
00: -- -- -- -- -- -- -- -- 08 -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 
48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

try this...
# i2cget 0 0x48 0 w




ref:
https://wiki.openwrt.org/doc/hardware/port.i2c
https://www.cnblogs.com/fastwave2004/articles/4250604.html     !!!!
http://linux-adm5120.sourceforge.net/openwrt/i2c/

Friday, 14 September 2018

Tinkering OpenWRT (14) : Python MQTT Setup





root@JoySince:~# mosquitto_sub  -t "topic" -v
topic OFF


####################################

Could download directly from openwrt:

root@JoySince:/tmp# wget --no-check-certificate https://pypi.python.org/packages
/source/p/paho-mqtt/paho-mqtt-1.1.tar.gz
wget: not an http or ftp url: https://pypi.python.org/packages/source/p/paho-mqtt/paho-mqtt-1.1.tar.gz

Download to PC and upload it to openwrt:
scp -P 2022 paho-mqtt-1.1.tar.gz root@192.168.0.24:/tmp/

Cannot unzip it...
root@JoySince:/tmp# tar xfz paho-mqtt-1.1.tar.gz
tar: can't open 'z': No such file or directory

Unzip locally on PC and upload to openwrt
scp -P 2022 -r paho-mqtt-1.1/ root@192.168.0.24:/tmp/

cd paho-mqtt-1.1
python setup.py install



ref
https://github.com/relayr/python-sdk/blob/master/docs/manual/openwrt.txt
http://lukse.lt/uzrasai/2015-02-internet-of-things-messaging-mqtt-2-install-python-clients-on-pc-carambola-rpi/
http://www.steves-internet-guide.com/into-mqtt-python-client/
http://www.steves-internet-guide.com/publishing-messages-mqtt-client/




Thursday, 13 September 2018

Tinkering OpenWRT (13) : Setup MQTT




Step 1: Install on openWRT
opkg update 
opkg install mosquitto mosquitto-client libmosquitto
Step 2: Start up
/etc/init.d/mosquitto enable
/etc/init.d/mosquitto restart

Step 3: Install on PC with Ubuntu
Many suggest test on the same machine with localhost. But once the mosquitto_sub will hang the terminal on openwrt, so I installed MQTT on my PC and used it as the client.

$ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa 
$ sudo apt-get update 
$ sudo apt-get install mosquitto 
$ sudo apt-get install mosquitto-clients
Step 4: Start the sub on openwrt
root@JoySince:~# mosquitto_sub  -t "topic" -v

Step 5: Ping the openwrt on PC
$ mosquitto_pub -h OPENWRT_IP_ADDRESS -t "topic" -m "test message"
$ mosquitto_pub -h OPENWRT_IP_ADDRESS -t "topic" -m "enjoy"

Result on openwrt:
root@JoySince:~# mosquitto_sub  -t "topic" -v
topic test message
topic enjoy


ref:
https://mosquitto.org/blog/2011/08/mosquitto-on-openwrt/
https://blog.csdn.net/hehexiaoxia/article/details/72834625
https://loralandia.com/mosquitto-mqtt-broker-installation-on-openwrt/
https://blog.csdn.net/xiaoreqing/article/details/77571881

Monday, 3 September 2018

Tinkering OpenWRT (12) : Cross-Compile the Helloworld Program



Step 1: Download the SDK 

Download
OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2

from

http://archive.openwrt.org/barrier_breaker/14.07/ar71xx/generic/

Two things you need to know. The openWRT version running on your board, in my case, barrier_breaker. The chipset of your board, in my case, ar9331.


Step 2: Create the working structure

cd package
mkdir helloworld
cd helloworld
mkdir src
cd src
touch helloworld.c
touch Makefile

tree:

OpenWrt-SDK-ar71xx-.../package/helloworld
├── Makefile
└── src
    ├── helloworld.c
    └── Makefile
The Makefile in /src/ is just local compiling for checking if there is any coding errors.
The Makefile ourside /src/ is the real cross-compiling makefile.

Step 3: coding

~/helloworld/src/helloworld.c:
#include <stdio.h> int main(void) { printf("Hell! O' world, why won't my code compile?\n\n"); return 0; }

~/helloworld/src/Makefile:
# build helloworld executable when user executes "make" helloworld: helloworld.o $(CC) $(LDFLAGS) helloworld.o -o helloworld helloworld.o: helloworld.c $(CC) $(CFLAGS) -c helloworld.c # remove object files and executable when user executes "make clean" clean: rm *.o helloworld


Step 3: Cross-compiling Makefile


##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
 
include $(TOPDIR)/rules.mk
 
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1
 
 
# This specifies the directory where we're going to build the program. 
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
 
include $(INCLUDE_DIR)/package.mk
 
 
 
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
 SECTION:=utils
 CATEGORY:=Utilities
 TITLE:=Helloworld -- prints a snarky message
endef
 
 
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
        If you can't figure out what this program does, you're probably
        brain-dead and need immediate medical attention.
endef
 
 
 
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
 mkdir -p $(PKG_BUILD_DIR)
 $(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one
# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
 $(INSTALL_DIR) $(1)/bin
 $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

Step 4: Compile

Go to the root directory of the compiler and make:
make V=s

Step 5: Find the .ipk

If the compiling passes, you can find helloworld_1_ar71xx.ipk in

OpenWrt-SDK-ar71xx-for-linux-i686-gcc-4.6-linaro_uClibc-0.9.33.2/bin/ar71xx/packages/base

Step 6: upload the .ipk to the board

scp helloworld_1_ar71xx.ipk root@192.168.0.24:/root/

Step 7: Install the package
opkg install helloworld_1_ar71xx.ipk
Step 8: run the program
just run

$ helloworld

or

$ /bin/helloworld

or

check it with
$ opkg list




ref:
http://archive.openwrt.org/barrier_breaker/14.07/ar71xx/generic/
https://www.gargoyle-router.com/old-openwrt-coding.html
https://blog.csdn.net/luck_good/article/details/23517135
http://www.uubook.net/article/detail/116354.html





Sunday, 2 September 2018

Tinkering OpenWRT (11) : SSH Configuration and Upload Files

Coding is done with cross-compiling. After we pack the compiled package, .pkg file, we need to upload the file to the board and install.

Step 1:
Edit /etc/config/dropbear:
The first option is used if the client is connect to the openwrt AP. The second option is used if both the client and the openwrt board are connected to the router.

config dropbear
        option PasswordAuth 'on'
        option Port '22'
        option Interface 'lan'

config dropbear
        option PasswordAuth 'off'
        option Interface 'wan'
        option Port '2022'
Step 2:
ifconfig  to get the openwrt IP address.
ssh root@192.168.0.24 -p 2022

Enter the password to test the ssh connection.

Step 3:
scp -P 2022 helloworld_1_ar71xx.ipk root@192.168.0.24:/root/

ref:
https://wiki.openwrt.org/doc/uci/dropbear
https://blog.csdn.net/qq_29663071/article/details/79081103
http://blog.51cto.com/lynnteng0/1066795