Thursday, October 8, 2020

Simple Javascript Log Function

Simple log function with date/time prefix


1
2
3
4
5
const log = function(data)
{
  var dateTime = '[' + new Date().toUTCString() + '] ';
  console.log(dateTime, data);
};

Saturday, March 28, 2020

Custom Pytyhon Lock with statement

class TimeoutLock(object):
  """Customized threading.Lock with timeout functionality for with statement."""

  def __init__(self):
    self.lock = threading.Lock()

  @contextlib2.contextmanager
  def acquire(self, timeout_ms: int):
    """Tries to acquires lock within specified timeout.

    Args:
      timeout_ms (int): timeout for acquiring lock.

    Yields:
      The lock acquiring result.
    """
    ok = self.lock.acquire(blocking=True, timeout=timeout_ms/1000)
    logging.info('****** LOCK acquired ********')

    yield ok
    if ok:
      self.lock.release()
      logging.info('********* LOCK released ************')
    else:
      logging.warning('couldn\'t acquire lock, likely timed out.')

  def release(self):
    """Wrapper for threading lock."""
    if self.lock.locked():
      self.lock.release()

Tuesday, November 19, 2019

Disable LAN/WLAN Power Saving Mode in RaspberryPi

Raspberry pi connection may be lost as in raspbian or maybe other linux, the power saving mode for network interfaces is enabled by default.  They supposed to wake up by external events but if they got IP from dhcp and it get expired then no route can be made to them.

Here is some way to disable power saving on LAN (eth0) and WiFi (wlan0) interface.

LAN:
Add the following parameter to /boot/config.txt

dtparam=eee=off


WiFi:
Add the following command in /etc/rc.local

/sbin/iw dev wlan0 set power_save off

Wednesday, October 30, 2019

Arduino + Tinygo + Docker + Avrdude

Using golang to program arduino uni board

1- Code
package main

import (
    "machine"
    "time"
    // "fmt"
)

const led = machine.LED

func main() {
    // fmt.Println("Test")
    println("Hello, TinyGo")
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        led.Low()
        time.Sleep(time.Second)

        led.High()
        time.Sleep(time.Second)
    }
}
* fmt is not supported, cannot be used.

2- Compile using tinygo docker image 
docker run --rm -v $(pwd):/src tinygo/tinygo:0.9.0 
tinygo build -o /src/test1.hex -size=short -target=arduino /src/tinygo/tinygo1.go
* The source is in $(pwd)/tinygo/tinygo1.go

3- Upload by avrdude in command line

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avrdude 
-C/Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf 
-v -patmega328p -carduino -P/dev/cu.usbserial-1440 -b115200 -D -Uflash:w:$(pwd)/tinygo/test1.hex:i
* I got command from Arduino IDE, by enabling verbose mode for upload and run a sample upload

Saturday, October 26, 2019

Raspberry Pi Testing/Unstable Repository

Add the following one to /etc/apt/sources.list

deb http://mirrordirector.raspbian.org/raspbian/ testing main contrib non-free rpi

Wednesday, October 10, 2018

Test DHCP Server

Testing dhcp server works as expected, like after configuring MAAS dhcp

sudo nmap --script broadcast-dhcp-discover -e cbr0

Tuesday, June 6, 2017

Trasnfer Docker Image Offline

Export docker image:
docker save -o export-docker-image.tar my_image:latest

Import docker image in destination:
docker load -i export-docker-image.tar