Showing posts with label Home Automation. Show all posts
Showing posts with label Home Automation. Show all posts

Tuesday, July 21, 2020

How I saved my Raspberry IoT Edge host going into Denial-Of-Service (DoS) mode

Setup of IoT system

I was using a Raspberry-Pi #B (35$ ARM processor, RPi) for home autoamation running as edge router/server . I have a customized golang server that connects to my IoT sensors and updates to postgres DB (previously I used TICK stack , but I had Influxdb corruption issues, so I switched to postgres DB). All of these apps are hosted in docker-instances inside the RPi.  I still use Graphana as front-end GUI (after I added the postgres as datasource to Grapahana). 

Disk configuration to separate OS with applications

To safegaurd RPi going to Denial-Of-Service mode due to root disk-starvation, I have used extra USB-stick for customized-Apps, Postgress DB, Graphana and other docker instances. In this way, if any applications (including graphana, postgres, influx,  customized apps) fills-up disk , one can still access RPi as RPi's SD-Card  is still in good-shape (wrt to disk space).

Telegram-Bot integration

Recently I have added support for Telegram-Bot integration with my RPi , so that I can communicate to my RPi outside my home WiFI network. The integration went well and I can issue commands from Telegram app and handle in RPi (evetually I can reach my sensors). 


Telegram-Bot transient error cauing DoS situation

Everything went fine till I switched off my home WiFi network in home. The moment I switched off WiFi network, the third party Telegram-Bot framework started to fills up the USB-Disk (which is shared by docker instances, customized apps,etc) as my customized-app's stdout/stderr are routed to USB-disk (for debugging/post-mortem-analysis). Within 12 hours of night time, 8GB of data is filled up. If this fillup is continued, within another 12 hours all of my docker instances (postgres, graphana and my customized app will have DoS siatuation.

Solution

To mitigate the above DoS situation, I could route the stdout/stderr to /dev/null, but I will lose information in case of critical errors(apart from this error) from third-party-code-bases and any unhandled golang's stack-traces and panic outputs.

For handling all the above tricky situations, I have written small golang application named safeout that consumes stdout/stderr of any number of processes and redirects the output of each stdout/stderr into disk-files with checks on maximum size (with one backup file). 

I configured my customized application with above safeout . Now myApp (or any other app in docker instance ) starts redirecting  stdout/stderr to disk, they will not fill-up disk (as safeout will ensure maximum disk space limits are honored with one backup copy)

safeout Code is at Safeout

Sunday, March 1, 2015

Launchpad can survive on 1000mAh LiPo battery for 1.5 years with Radio Link to RPi board

So after last few days of work on data serialization across Rpi & LaunchPad (MSP430G2553), I integrated the Serialization library that sends couple of 32-bit /16-bit integers from MCU to RPi via NRF24L01+ chip.

Initially MCU was raking around 230uA current . After some tweaking, it came down 130uA .

As final optimization (for this round of testing), I shutdown the NRF24L01+ chip till next time radio-packet-transmission. This step lead to MCU current consumption of 25uA . So in this period (approximate 12 seconds) MCU is in LPM3 sleep-state and radio is in shutdown-state. When MCU wakes-up, it activates NRF chip again & sends radio-packets, during this tx-period, it takes around 400uA for a breif period.


So taking on average current consumption of 50uA (including radio-packet-tx current bump), using battery life calculator for 1000mAh LiPO battery's life is around 14000hours (583 days). Not bad for a starter project :)

I have just documented the initial video for the same at youtube .


I will be submitting the code in github in coming days after stabilization and cleanups.
I will do same test for MSP430F5969 (FRAM board with Super capacitor on it).

Tuesday, June 18, 2013

Got working Arduino Pro Mini for Home Automation work - 1

For my home automation projects, I have been doing lots of R&D on various factors like:

  • Low cost MCU and MCU related board (commercially available)
  • Low power consumption in deep sleep (may not be the best , but reasonable low current consumption, so that MCU can withstand power outages)
    • Lots of cheap boards are there. For the time being I have chosen following Arduino boards
  • Cheap availability of wireless connectivity solutions
    • Bluetooth 4.0
      • In coming future, I would be using BLE-Soc chips likeNordic nrf51822 and TI's cc2540 & cc2541 -- as we have cheap assembled boards at various internet shops (like alibaba, dealsextreme, ebay, etc) .These boards typically come for 6 to 12$ 
    • Zigbee
      • I tried initially, but their board's cost is high very high  even at above shops and difficult solder because of QFN-soldering, wireless antenna's design, etc.
    • Proprietary wireless protocol
      • I found Nordic radio transceiver NRF24L01+ at 2.4GHz spectrum at following site for around 1$ per board
  • Free compiler, open source libraries & knowledge base in net.  If not free some trial based compiler for doing R&D (like cc254x chips).
Arduino fits the bill, but their overall solution costs more than 15 to 20$ before an year back (with my research at that point of time). So I dropped Arduino idea and looking for other solutions. Recently I casually browsed Alibaba site and found that I can get a reasonably powered Ardunio for less than 5$ and wireless connectivity for another 1$. So brought these items and finally started doing first helloworld on this boards. 

Here are pictures for  this board
This is for connecting this board with typically USB-to-Serial-Dongle(Prolific based) and correpsonding RS232 TTL convertor



This is showing minimal connections needed for powering and burning the program to Arduino. These yellow wires do connect to above USB-RS232-Dongle (RX & TX appropriately)


Board view from top view with an empty board on side


Connections from Arduino Board to RS232

Please ensure that you connect serial port cross-connection like following:

  • Board-Longer-Side RXI to RS232 TX
  • Board-Longer-Side TXD to RS232 RX

Arduino IDE Options

  • Select proper Serial port
  • Select  board as either of these boards. For my both voltages have worked out
    • 'Arduino Prop or Pro Mini (5V, 16MHz) w/ Atmega328'
    • 'Arduino Prop or Pro Mini (3.3 V, 8MHz) w/ Atmega328'
  • Select Programmer as 'Arduino as ISP'

Actions need to be taken while pressing Upload from IDE

  • As current setup does not use DTR/RTS on RS232 and only uses RX/TX (from USB to Rs232 converter), we need to press reset button just before the flashing (i.e., when console logs shows lines something like following in verbose-burning-mode-preferences) 
 Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 

Problems/Issues faced

Serial Port not detected in Linux

Run as root, then the Arduino IDE recognizes the Serial port . Select appropriate serial port from output of 'dmesg' command in linux.

Flashing Issues

  • After selecting the board as 'Arduino Prop or Pro Mini (5V, 16MHz) w/ Atmega328' and correct serial port. After trying to flash image to board has given following error messages.

Binary sketch size: 2,654 bytes (of a 30,720 byte maximum)avrdude: stk500_recv(): programmer is not responding

  • Now enable the verbose upload option via File -- Preference --'Show verbose output during'  = 'burning' .
    • After this step , I got following messages
Binary sketch size: 2,654 bytes (of a 30,720 byte maximum)
**/arduino-1.0.5/hardware/tools/avrdude -C/opt/arduino-1.0.5/hardware/tools/avrdude.conf -v -v -v -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:/tmp/build5308589158935848821.tmp/ASCIITable.cpp.hex:i 

avrdude: Version 5.11, compiled on Sep  9 2011 at 16:00:41
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "**/arduino-1.0.5/hardware/tools/avrdude.conf"
         User configuration file is "***/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: ser_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding

avrdude done.  Thank you.
    • Another error which slightly different at end
         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [00] 
avrdude: stk500_getsync(): not in sync: resp=0x00

avrdude done.  Thank you.
  • As this not typical USB based driver (which is available in little costlier Arduino boards) and I am using hard coded RS232 wiring, I tried to flash the image by pressing the reset button on the board. This made the flashing to occur successfully. Actually if we reset the board, MCU waits for flashing-data from RX/TX pins few seconds. If no  recognized flashing data comes to RX/TX pins, MCU will boot with previous programm that is there in flash. One has to find appropriate time to press reset button -- this timing can be assisted by putting verbose-burning-option.
  • So successful flash sequence is shown as:
Binary sketch size: 2,654 bytes (of a 30,720 byte maximum)
***/arduino-1.0.5/hardware/tools/avrdude -C***/arduino-1.0.5/hardware/tools/avrdude.conf -v -v -v -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:/tmp/build5308589158935848821.tmp/ASCIITable.cpp.hex:i 

avrdude: Version 5.11, compiled on Sep  9 2011 at 16:00:41
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "***/arduino-1.0.5/hardware/tools/avrdude.conf"
         User configuration file is "***/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Send: 0 [30]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
         AVR Part                      : ATMEGA328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
avrdude: Send: A [41] . [80]   [20] 
.....
         Hardware Version: 2
         Firmware Version: 1.16
avrdude: Send: A [41] . [84]   [20] 
..............
avrdude: Recv: . [10] 
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: Send: A [41] . [81]   [20] 
.......
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: AVR device initialized and ready to accept instructions

Reading | avrdude: Send: u [75]   [20] 
avrdude: Recv: . [14] . [1e] . [95] . [0f] . [10] 
################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
.................
.................
avrdude: Recv: . [10] 
###avrdude: Send: U [55] . [00] . [05]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 
avrdude: Send: t [74] . [00] ^ [5e] F [46]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [ee] . [0f] . [ff] . [1f] . [05] . [90] . [f4] . [91] . [e0] - [2d] . [09] . [94] . [f8] . [94] . [ff] . [cf] A [41] S [53] C [43] I [49] I [49]   [20] T [54] a [61] b [62] l [6c] e [65]   [20] ~ [7e]   [20] C [43] h [68] a [61] r [72] a [61] c [63] t [74] e [65] r [72]   [20] M [4d] a [61] p [70] . [00] , [2c]   [20] d [64] e [65] c [63] : [3a]   [20] . [00] , [2c]   [20] h [68] e [65] x [78] : [3a]   [20] . [00] , [2c]   [20] o [6f] c [63] t [74] : [3a]   [20] . [00] , [2c]   [20] b [62] i [69] n [6e] : [3a]   [20] . [00] ! [21] . [00] . [00] . [00] . [00] . [00] . [95] . [03] . [c8] . [00] + [2b] . [03] \ [5c] . [03] < [3c] . [03] . [85] . [03] 
avrdude: Recv: . [10] 
## | 100% 0.59s

avrdude: verifying ...
avrdude: 2654 bytes of flash verified
avrdude: Send: Q [51]   [20] 
avrdude: Recv: . [14] 
avrdude: Recv: . [10] 

avrdude done.  Thank you.




So with minimal booting is done with this board, next step is to connect Nordic Wireless chips and try wireless connectivity.


Tuesday, March 16, 2010

129$ Plug Computer with WiFi/Bluetooth/SD-card/eSATA with Linux/JVM

Recently Marvell has released new plug computer models were released. New models have Wi-Fi and Bluetooth and these Plug sized computers run Linux also can run Java programs and consume approximately 5W of power. Opportunities to for these plug uses are plenty: Home automation, Robotos, Automobiles solutions, etc.

Here is the descriptions of various models:
Model FeaturesCost ($)
SheevaPlug Dev Kit(Original Plug)1.2GHz Sheeva Core CPU, 512 MB DDR2 RAM, 512MB NAND Flash,1x Gb Ethernet, 2x USB 2.0, RTC, GPIO, UART 99
GuruPlug Server - Standard 1.2GHz Sheeva Core CPU, 512 MB DDR2 RAM, 512MB NAND Flash,1 x Gb Ethernet, 2 x USB 2.0, RTC, MicroSD card slot, Wi-Fi 802.11 b/g, Bluetooth and U-SNAP I/O for Home Automation using ZigBee or Z-Wave, Linux Kernel 2.6.32, 7 GPIO 100
GuruPlug Server - PLUS1.2GHz Sheeva Core CPU, 512 MB DDR2 RAM, 512MB NAND Flash,2x Gb Ethernet, 1x eSATA 2.0 port -3Gbps SATAII, 3 x USB 2.0, RTC, MicroSD card slot, Wi-Fi 802.11 b/g, Bluetooth and U-SNAP I/O for Home Automation using ZigBee or Z-Wave, Linux Kernel 2.6.32, 7 GPIO, UART 129
GuruPlug Display1.2GHz Sheeva Core CPU, 512 MB DDR2 RAM, 2 GB NAND Flash,1x Gb Ethernet, 1 HDMI connector or Touch Panel Display, 3 x USB 2.0, RTC, MicroSD card slot, Wi-Fi 802.11 b/g, Bluetooth , Linux Kernel 2.6.28, 7 GPIO179
More info visit Globascale site for GuruPlugs
For old Plug info here
BTW, I am not any way related to Globalscale/ Marvell :)