Skip to main content

Posts

Showing posts from 2019

How to start python script by batch file on window

How to start python script by batch file on window Reference https://datatofish.com/batch-python-script/ Assumption - Your python script is manage.py - Create a batch file "run.bat" which is placed in the same directory with your python script. run.bat source code :: find python.exe path @ for %%i in (python.exe) do @ set py = %%~$PATH:i ::run main program %py% %CD% \manage.py ::wait for stop pause manage.py source code print ( "Surprisingly, The naughty kitten defeated a giant python" ) Looking more sample batch file https://www.robvanderwoude.com/batexamples.php

Develop the first python client test application by Paho mqtt

Develop the first python client application by Paho I. Purpose: 1. Try to send messages between 2 clients via MQTT protocol. Then, we will create 2 clients: - One for publishing (or send) the message. Other for subscribing (or receive message). II. Pre-condition: - You already installed Mosquitto broker, Python. III. Step by step 1. Download example from Github https://github.com/eclipse/paho.mqtt.python 2. Modify subscriber(client) source In folder example/client_sub.py. Modify some pieces of source code. before mqttc.connect("mqtt.eclipse.org", 1883, 60) mqttc.subscribe("$SYS/#", 0) after mqttc.connect("192.168.203.76", 1883, 60) mqttc.subscribe("test/topic1", 0) note: "192.168.203.76": IP address of machine which Mosquito broker installed on. 1883: mqtt port 3. Modify publisher(client) source In folder example/publish_single.py. before: publish.single("paho/test/sing

How to test Mosquitto broker on Windows machine

Install Mosquito broker https://mosquitto.org/download/ 1. Install service easily by some click. 2. Check operation Reference: http://www.steves-internet-guide.com/install-mosquitto-broker/ - open command prompt and check installed directory cd C:\Program Files\mosquitto mosquitto -h or mosquitto -v - start service mosquitto -p 1883 - check port 1883 is listening or not netstat -a - add system environment variables. Add below string to the system environment path. It helps you call mosquito in the command line window. ;C:\Program Files\mosquitto Example Reference: https://www.win.tue.nl/~lrahman/iot_2016/tutorial/MQTT_2016.pdf 1. Open the first command windows 2. Start mosquitto by command mosquitto -p 1883 3. Open the second command windows 4. Allow client subcribe the topic by command mosquitto_sub -t 'test/topic' -v This time, a client has the ability to handle the message from publisher. 5. Open the third command windows 6. Publish message

[Bài dịch] Độ phủ của test case bao nhiêu là đủ? Code Coverage ?

Trên một ngôi chùa nhỏ tên là Toàn Âm Tự trên núi Lỗ Tề (Lotte Tower) Dự án kiểm thử phần mềm đếm số hạt bụi mịn 2.5pm ở kinh đô Hà Nộ đang bắt đầu triển khai phase Unit test. Sáng sớm, một hòa thượng mới nhập chùa một năm hỏi đại sư Thích Đề Mô - trưởng dự án.  Thưa đại sư, con sẵn sàng chạy Unit test cho chức năng đo bụi mịn, vậy chẳng hay đại sư muốn tỷ lệ phủ (code coverage) là bao nhiêu? Đại sư nhìn chú với ánh mắt hiền từ và nói: "Con ah, đừng lo lắng quá về tỷ lệ phủ, con hãy cố gắng viết thật nhiều những test case tốt." Chú vâng lời, cúi chào và rời đi. Ánh nắng cuối thu len lói qua những màn sương lờ mờ chiếu rọi khắp sân chùa. Mặt trời đã lên quá ngọn tre đằng ngà. Một hòa thượng khác đến hỏi câu hỏi tương tự cho chức năng hiển thị trên mobile app. Đại sư từ tốn chỉ vào nối nước đang sôi dưới bếp và hỏi: "Con hãy cho ta biết, ta nên cho bao nhiêu gạo vào nồi nước đang sôi kia?" Chú hòa thượng lúng túng đáp: "Thưa đại s

[OpenCV] Simple example C++ and ubuntu

Prerequisite: Opencv is already installed. Source code: //include high gui headers #include "opencv2/highgui/highgui.hpp" //we are using standard and OpenCV namespaces using namespace cv; using namespace std; int main( int argc, const char** argv ){ //create a window named theWindow namedWindow("theWindow", CV_WINDOW_AUTOSIZE); //wait user hit some key waitKey(0); //destroy window named theWindow destroyWindow("theWindow"); return 0; } Build command: g++ -o firstcv $(pkg-config --libs --cflags opencv) main.cpp if error happened. /tmp/cceyijPd.o: In function `main': main.cpp:(.text+0x3d): undefined reference to `cv::namedWindow(cv::String const&, int)' main.cpp:(.text+0x53): undefined reference to `cv::waitKey(int)' main.cpp:(.text+0x70): undefined reference to `cv::destroyWindow(cv::String const&)' /tmp/cceyijPd.o: In function `cv::String::String(char const*

[Rust] How to install Rust on ubuntu

This is fast food, No more detailed explanation. Open your terminal. Just type and type a dozen of commands as below: //download curl software sudo apt-get install curl //using curl to download rust curl https://sh.rustup.rs -sSf | sh //type 1 to install Rust by default //after install completely, set PATH environment source ~/.profile source ~/.cargo/env //Check version rustc --version //Write the first program //Create directory mkdir  rs_dir cd rs_dir //Open vi editor to write source code vi first_proc.rs //Copy and paste below code in vi editor fn main()  {      println!("Hello World again!");  } //Save source code by vi manipulators with a keyboard. press "i" press "shift+insert" press ":" type wq enter //compile source code rustc first_proc.rs //run first program ./first_proc.rs More detail, please check out below references: https://www.rust-lang.org/tools/install https://www.tecmint.com/install-r

[Linux][SSH] Sự khác nhau giữa authorized_keys và known_hosts file trong thư mục ~/.ssh

2 file này xuất hiện khi máy ubuntu của bạn vừa là server vừa là client known_hosts Khi máy của bạn là client, known_hosts sẽ lưu lại public key của những server(host) bạn đã truy cập trước đó. authorized_keys Khi máy của bạn là server, authorized_keys lưu lại public key của những client đã kết nối đến máy của bạn trước đó. Các câu hỏi khác liên quan: 1. Làm thế nào để cấu hình ssh server? 2. Làm thế nào để share public key từ client cho server? 3. Có mấy kiểu giải mã, mã hóa dữ liệu khi sử dụng ssh? 4. Làm thế nào để tạo ra cặp mã hóa private and public? 5. Cơ chế sử dụng key? References: https://security.stackexchange.com/questions/20706/what-is-the-difference-between-authorized-keys-and-known-hosts-file-for-ssh

[Linux] miscellaneous

Create directory tree in linux using C plusplus https://www.tutorialspoint.com/how-can-i-create-directory-tree-using-cplusplus-in-linux Example is not correct at all when it handle "status" it should be: #include < bits / stdc ++. h > #include <iostream> #include <sys/stat.h> #include <sys/types.h> using namespace std ; int main () {     int status ;    status = system ( "mkdir -p TP/My_Folder/test" ); // Creating a directory     if ( status ! = 0 )       std::cerr << "Error : " << strerror ( errno ) << std::endl ;     else       std::cout << "Directories are created" << std::endl ; } What is default permission of command  " system ( "mkdir -p TP/My_Folder/test" ); "? The answer is 755 or drwxr-xr-x What is drwxr-xr-x? It looks like 4 groups:  d  rwx  r-x  r-x d: directory (file is "-") rwx: owner can read/write/

[Git] Handle trailing space when patching with git

After complete coding one module in development branch. Next phase is merging. Obviously, you can merge source code automatically without no errors happen. However, life is not dream. Create a patch git diff HEAD > newTariff.patch Apply patch git apply newTariff.patch then problem happens Problem git apply newTariff.patch:106: trailing whitespace. patch does not apply Route cause: Nguyên nhân: Do trong source code có những dấu space thừa (ô vuông màu đỏ hình dưới) Some whitespaces is existed in your patch. (red area in below pictures) Fix Clean white space and patch again. Cách khắc phục Xóa những dấu space này đi và thực hiện patch lại Make up after complete coding. Find and clean whitespace before create patch file. Sửa sau khi code xong Kiểm tra sau khi code xong có lỗi này không git diff HEAD --check Prevent whitespace by manually when typing source code Sửa ngay khi đang code =>Bật chức năng hiển thị các dấu whitespace, hoặc remove trailing

Một số kiến thức cơ bản trong lập trình firmware

Qủa thực tình là trong quá trình làm việc, mình rất hay đụng phải những khái niệm này nhưng chẳng mấy khi chú tâm. Bug đến là ta debug, debug xong là thôi, cũng không tìm hiểu ngọn ngành ra ngô ra khoai, lần sau ta lại đụng bug :-( 1. MSB và LSB hoặc msb và lsb msb (chữ thường) : most significant bit lsb (chữ thường): least significant bit Hai khái niệm này được dùng trong phạm vi 1 byte msb là cái bít quan trọng nhất ở byte đó. Hiểu đơn giản, byte nào mà mất cái bit này đi(gán bít này bằng 0) thì giá trị của byte đó bị thay đổi đáng kể (có khi là đổi trắng thay đen, đổi từ âm sang dương :D) lsb thì ngược lại. Ví dụ: Trên hình ta có số 10100101 =  165 Mất msb thì số đó thành  00100101 =    37 Mất lsb thì số đó thành    10100100 =  164 MSB (chữ hoa) : most significant byte LSB (chữ hoa): least significant byte Hiểu đơn giản, MSB là byte quan trọng nhất trong một string hoặc buffer. LSB thì ngược lại Hai khái niệm này cũng liên quan đến việc tổ chức cá

[GTK] Example of using GtkBox with label and button

[1]Source code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <gtk/gtk.h> void on_window_closed (GtkWidget * window, gpointer data) { gtk_main_quit(); } static void destroy (GtkWidget * widget, gpointer data) { gtk_main_quit(); } static void button_clicked (GtkWidget * button, gpointer data) { g_print( "Button clicked \n " ); } int main ( int argc, char * argv[]) { GtkWidget * window, * label, * box, * button ; gtk_init( & argc, & argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW (window), "Test GTK" ); gtk_window_set_default_size (GTK_WINDOW (window), 500 , 200 ); #if 1 //Use gtk3: change 0->1, Use gtk2: keep it is 0 //Using gtk3 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5 );

[C++] One way to convert pointer of structure to vector

Of course, you can choose one method of C++ likes reinterpret which is described in below topic: https://www.geeksforgeeks.org/reinterpret_cast-in-c-type-casting-operators/ However, it is easily to find out one big problem in here is: reinterpret_cast is a very special and dangerous type of casting operator In embedded system, this problem is not allowed. Then, we should convert it by manually. The idea is, you push each structure to vector by passing number of structure to function which is used for converting. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #include <iostream> #include <list> #include <vector> #include <string.h> using namespace std; #define STR_NUM 3 //Number of structures typedef struct { char text_id[ 16 ]; char name[ 16 ]; }st_A; /* Show vector dat

What are some best practices for reducing memory usage in C?

Thiếu memory khi lập trình Arduino và cách giải quyết. Lacking memory with Arduino program. Đây là một số gợi ý để giải quyết nếu bạn đã từng thấy warning dưới: Have you ever seen any below warning? - Low memory available, stability problems may occur. - Not enough memory; see http://www.arduino.cc/en/Guide/Troubleshooting#size for tips on reducing your footprint. If the answer is YES, i would like to suggest some solutions for saving memory in this articles. Các nguyên nhân: Some root causes: 1. Khai báo biến local tràn lan. 2. Dùng hàm lcd.print or serial.print tùy tiện. 3. Cấp phát động tùy tiện, ko tính toán kỹ lưỡng số lượng phần từ trong mảng cần dùng. .... Một số gợi ý để tiết kiệm memory  Some suggestions to save memory. - Sử dụng một số kỹ thuật như #pragma pack(), union, bit field, cấp phát động.             Using some C technique #pragma pack(), union, bit field, dynamic memory allocation. - Sử dụng con trỏ mộ