Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, 8 March 2018

Resetting an SD card after writing a Linux Image from Windows

After writing the Rasbian image to my SD card for a new Raspberry Pi project I found myself wanting to reset the SD card to be used as normal storage so I could install NOOBS, but I got stuck when I couldn't work out how to delete the partitions made by writing the image on the device from Windows.

I found a Windows solution in the way of DISKPART that got the job done.

How to use DISKPART


  1. Open a new command window by either searching for "cmd" from the windows menu or pressing the windows key and 'r' and running the command "cmd" and hit enter
  2. Enter the command "DISKPART" and another command window will be opened
  3. In the new window type "LIST DISK" and hit enter
  4. Select the correct disk from the list with the command "SELECT DISK #" replacing the # with the drive number of the SD card. Make sure you get this right or you could ruin your windows installation!
  5. Enter the command "CLEAN" to clear the partition table
  6. Enter the command "CREATE PARTITION PRIMARY" to reallocate the cleaned space as the primary partition
  7. Enter the command "FORMAT FS=FAT32 QUICK" to reformat the SD card

Optionally you can use the command "ASSIGN" to reassign a drive letter to the SD card.

Sunday, 12 March 2017

HOME AUTOMATION: Teptron move control from Linux

UPDATE: this method does not appear to work with the latest Teptron Move firmware. The developer of csrmesh is looking in to the problem but there is no current solution for controlling the Teptron Move from anything other than the app.

My biggest pain point with the Teptron Move automated blinds device at the moment is the lack of app updates and any functionality other than just simply commanding the blinds from my phone. Because of this I have basically removed the devices from my blinds. You can read more about my issues with the device in an old post.

I recently started to think about how I could potentially expose the CSR bluetooth mesh that the Move uses to my wireless network. This would allow me to get the automation I'm looking for out of my OpenHAB controller. I came across a project on GitHub claiming to have reverse engineered the CSRMesh protocol for the Teptron Move and decided to give it a shot.

I did a little more digging and found some posts on the domoticz forums talking about how to integrate the linux script with the domoticz home automation solution to automated the Move units, so I decided to investigate the possibility of integrating the devices with my OpenHAB system.

Installing The Script

Git is not installed on the Ubuntu image for the Pine64. Install with the following commands:
 apt-get install git  

If it's the first time using git on your machine you need to:
  • Generate an rsa key 
  • Follow the Github instructions to add the rsa key to you github account
I also found this article useful to debug my problem.

If you don't follow these steps you will get the error:
 Permission denied (publickey).  
 fatal: Could not read from remote repository.  

Now create a new directory clone the csrmesh repository into that directory:
 mkdir Downloads  
 cd Downloads  
 git clone git@github.com:nkaminski/csrmesh.git  

Build and install the project:
 python setup.py build  
 sudo python setup.py install  

The script also needs the bluez software installed:
 sudo apt-get installed bluez  

If you are trouble getting your bluetooth running on an Raspberry Pi 3 with OpenHABian I have explained how I did it in an older post.


Bluetooth Setup

If your device doesn't support Bluetooth natively you will need to do some work with a USB Bluetooth adapter to get connected  to the Move units.

Insert the USB adapter in to the Pine64 and run the following command to check the device was recognised:
 dmesg  

 If the device was recognised by the linux kernel you should see an output something like this
 [449253.083342] input: Logitech Logitech BT Mini-Receiver as /devices/soc.0/1c1b000.ohci1-controller/usb4/4-1/4-1.2/4-1.2:1.0/input/input5  
 [449253.083769] hid-generic 0003:046D:C71B.0001: input: USB HID v1.11 Keyboard [Logitech Logitech BT Mini-Receiver] on usb-sunxi-ohci-1.2/input0  
 [449253.159418] usb 4-1.3: new full-speed USB device number 4 using sunxi-ohci  
 [449253.303636] input: Logitech Logitech BT Mini-Receiver as /devices/soc.0/1c1b000.ohci1-controller/usb4/4-1/4-1.3/4-1.3:1.0/input/input6  
 [449253.304398] hid-generic 0003:046D:C71C.0002: input,hiddev0: USB HID v1.11 Mouse [Logitech Logitech BT Mini-Receiver] on usb-sunxi-ohci-1.3/input0  
 [449253.581549] usb 4-1.1: new full-speed USB device number 5 using sunxi-ohci  
 [449253.750067] usbcore: registered new interface driver btusb  
 [449253.875233] Bluetooth: BNEP (Ethernet Emulation) ver 1.3  
 [449253.875245] Bluetooth: BNEP filters: protocol multicast  
 [449253.875282] Bluetooth: BNEP socket layer initialized  
 [470188.654809] sunxi-bt bt.28: set block: 0  
 [470188.654971] sunxi-bt bt.28: check bluetooth io_regulator voltage: 1800000  
 [470193.118153] sunxi-bt bt.28: block state already is 0  

We can now run one of the bluez commands to check if the bluetooth adapter was recognised and is available for us:
 hcitool dev  

If everything is in order with your setup you should see an output like the one below and you can continue on to the next section:
 ubuntu@pine64:~$ hcitool dev  
 Devices:  
     hci0  00:1F:20:51:CC:AA  

If you don't see any devices listed then you will have to do some work to enable the Bluetooth adapter. You should have a utility installed called rfkill that is used to enable and disable wireless devices. If it's not installed you can install it with the command:
 sudo apt-get install rfkill  

Run the following command to see your connection status:
 ubuntu@pine64:~$ rfkill list  
 0: sunxi-bt: Bluetooth  
     Soft blocked: Yes  
     Hard blocked: no  
 1: phy0: Wireless LAN  
     Soft blocked: no  
     Hard blocked: no  
 2: phy1: Wireless LAN  
     Soft blocked: no  
     Hard blocked: no  
 3: hci0: Bluetooth  
     Soft blocked: Yes  
     Hard blocked: no  

The following command will remove the soft block on the Bluetooth adapter:
 sudo rfkill unblock bluetooth  

Now run the "rfkill list" command again and you should see the soft block has been removed.

Success! The Bluetooth output has been enabled and we should be able to see available Bluetooth devices if we run the hcitool dev command again. We can confirm the device is working by scanning for available Bluetooth devices:
 hcitool scan  

The above command will scan for regular Bluetooth devices but will not be able to to see Low Energy (BLE) devices. In order to scan the available BLE devices we need to run the command:
 sudo hcitool lescan


Testing The Script

Typically we would do a hcitool scan to get a devices MAC address, but the csrmesh network doesn't allow us to do this (at least in my setup). I've connected to the CSRMesh network from my Windows laptop and used that connection to gather the device address to include in the script.
The address I grabbed from my PC was: BTHLE\Dev_43c55b040006

The Github documentation tells us to format the command in the following way:
 ./bin/csrmesh-cli move --pin 8888 --dest 43:c5:5b:04:00:06 --objid 1 --position 255  

You need to make sure you are running this command from within the csrmesh folder that was cloned earlier. If everything worked you should see your Move unit move to the set position and the output:
 Running: gatttool -b 43:c5:5b:04:00:06 --char-write-req -a 0x0021 -n 0fe2bd00809bb9d83b76b48585673a77b747ff  
 Characteristic value was written successfully  

Note: The only gotcha here is the pin for the Move units. Here you will need to remember the password you set to the CSRMesh network when you first set up your move unit. If like me you have forgotten this, perform a factory reset on the unit by removing the battery, hold down the stop key then re-insert the battery. The notification light on the bottom of the device should be on solid. Keep holding the stop key for at least 5 seconds at which point the device should be reset. In my experience you don't get any feedback that it has performed the reset. With the device having been factory reset you can restart the Android app and enter a new password.

The next step here is to now enable the device for use with OpenHAB.


UPDATE:
I've written a post on how to get this script working with openHAB here

Sunday, 5 March 2017

HOME AUTOMATION: Mounting network storage device on Raspberry Pi

I run a NAS storage device to serve up media content as well as a place to backup photos and videos.
I've found it really handy to be able to connect to my network storage from my Raspberry Pi but it took me a while to work out the right configuration for my setup. Here are the steps I followed:

Raspberry Pi 1 Configuration:

Install the cifs-utils package. This is the package we need to connect the the network drive
 sudo apt-get install cifs-utils  

Make a directory where you would like to mount the network storage:
 sudo mkdir /home/pi/NAS  

We are going to use two files to do the network drive mounting, the first file will store the network drive and mounting information and the second will hold the login credentials information:
  1. /etc/fstab - network drive information
  2. /root/.servercred - hidden file with network credential information

The cifs-utils package creates a file named fstab that holds the network drive information.
Edit the fstab file and add the network location and password:
 sudo nano /etc/fstab  

To begin with you file should look something like this:
 proc           /proc      proc  defaults         0    0  
 /dev/mmcblk0p1 /boot      vfat  defaults         0    2  
 /dev/mmcblk0p2 /          ext4  defaults,noatime 0    1  
 # a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that  

Add the following line to the bottom of the fstab file:
 proc           /proc      proc  defaults         0    0  
 /dev/mmcblk0p1 /boot      vfat  defaults         0    2  
 /dev/mmcblk0p2 /          ext4  defaults,noatime 0    1  
 # a swapfile is not a swap partition, so no using swapon|off from here on, use dphys-swapfile swap[on|off] for that  
   
 //192.168.1.100/ /home/pi/NAS cifs credentials=/root/.servercred,sec=ntlmv2,iocharset=utf8,file_mode=0777,dir_mode=0777 0  

Hit ctrl-x to exit nano, then type y and hit enter to save the file.
Now we need to create the server credential information:
 sudo nano /root/.servercred  

Add your username and password to this file (including network homegroup)
 username=KLIMOVSKI\andrew  
 password=1234abcd  

If you get the error "sudo: nano: command not found" the you will need to install nano:
 sudo apt-get install nano  

Finally, attempt to mount the network drive by typing:
 sudo mount -a  

If everything worked the command should execute and you should not see any output. You can now test the network drive by navigating to the directory we created earlier.