It's Brent

Fixing Raspberry Pi Wifi from Dropping

Posted on May 22, 2014

I noticed some issues with my Raspberry Pi dropping the Wifi and not reconnecting. After some tinkering I was able to get it fixed. I am using the Edimax EW-7811Un network adapter, which works awesome for it’s size and price. I originally created a script that checked network status and tried to bring the network back up that ran on a cron job, but it never worked properly. Here is the following solution that I got working.

First thing is to go into the /etc/ifplugd/action.d/ directory and move the ifupdown to a backup file (ie ifupdown.original). Then use the following command to copy the ifupdown from wpa_supplicant.

cp /etc/wpa_supplicant/ifupdown.sh ./ifupdown

The next thing is to disable power management on the Edimax wifi adapter. Power management will cause the device to sleep if it is not in use, but sometimes not wake up. To check if it is on, run the following command. If you see a 1 printed, then power management is turned on.

cat /sys/module/8192cu/parameters/rtw_power_mgnt

If you that command returned a 1, then either create or modify the file /etc/modprobe.d/8192cu.conf using vim or whichever editor you are familiar with. In that file, add the following text.

# Disable power saving
options 8192cu rtw_power_mgnt=0

After you have done all this, reboot your Pi. Once rebooted and run the command to check your power management, you should get a 0. This has solved most of my issues with the Wifi not working. My only remaining issue is a baby monitor that interferes on the same frequency.

Update to iOS Html to Pdf Conversion

Posted on April 01, 2013

My previous post on how to convert or print UIWebView / html / url to pdf has generated a ton of traffic to my site. Lots of Google searches hit my site. I have always wanted to update this code, possibly make it easier to use. Even package it up into a single easy to use class.

Today I’m happy to say that I finally did that. I released BNHtmlPdfKit on GitHub just a little bit ago. BNHtmlPdfKit easily turns HTML data from an HTML string or URL into a PDF file on iOS.

One of the things that always bothered me about my previous solution was the generated PDF files looked absolutely horrible because they were rendered at 72 dpi. I also knew that somehow there must have been a way to get the PDF file to generate using the UIPrintFormatter.

Turns out the solution was a tiny hack. Create a custom UIPrintPageRenderer and override paperRect and printableRect. This will cause the UIPrintFormatter to return a pageCount as well as render the document.

Check out the Readme and feel free to fork the project!

In use by:

Hacking My Garage With A Raspberry Pi

Posted on March 29, 2013

Several months ago our Craftsman garage door openers stopped responding to our remotes. I tried everything I could think of to fix this problem, short of buying anything new. Garage door opener parts are expensive, to the point buying new openers is a better option.

After much research I found out that RF interference was the issue. No new remote or logic board was going to solve the issue, even getting new openers wouldn’t solve the issue.

By knowing the wall switches work by simply closing the circuit I set out for a wireless solution that we could control from our iPhones. I wanted to try my hand at an Arduino, but getting WiFi to one isn’t cheap. I then discovered the Raspberry Pi had programmable pins. Getting wireless to the Pi was nearly $80 cheaper, plus it allowed me to leverage languages I already knew. I soon placed an order on Amazon for all my parts.

Don’t forget (assuming you don’t have these lying around):

After getting the parts in I wrote a quick node.js app that serves a single page and also has a 2 backend requests to open each garage. I also quickly wrote an iOS app as well. I’m using Forever to make sure the app stays up. To install node.js and forever from your command line on the Raspberry Pi enter these commands.

$ sudo apt-get install node
...
$ sudo npm install forever -g

The only issue I came across was that the relay I bought turned on when firing low, and off when high. The Raspberry Pi has a default state of low, but the GPIO pins are set to input mode so the relay doesn’t immediately fire until the pin is set for output.

As far as wiring goes, I have 2 sets of door switches. The regular wall switches that came with the openers which are right next to each other, then a separate set of switches wired in another part of the garage. I went to Lowes and got some Alarm Wire (it has 4 wires inside of it), this made me only have to make one run from the Pi. I just connected my wires to the back of each wall switch using the COM and NO ports from the relays. It doesn’t matter how these are connected as you are just completing the circuit. I used this guide in my plans for this project.

Raspberry Pi With SainSmart Relay Wiring

I have open sourced the node.js app at brentnycum/garage-node. I have also open sourced the iOS app at brentnycum/garage-ios, in order to put this on your device you need an iOS Developer Account from Apple. I ended up creating my own fork of pi-gpio to handle using the additional GPIO pins on the Raspberry Pi Model B Rev 2.0 that I used, as I soldered the relay to the board instead of using the pins. The wiring above matches my node app.

To get the node.js app from Github you can either choose to download the zip file, or you can use the simple command.

$ git clone https://github.com/brentnycum/garage-node.git

Once you have the code on your Raspberry Pi, move into the app’s directory and run the npm install command to install the dependencies. npm is the node package manager.

$ cd garage-node
$ npm install

After all the dependencies are installed you can run the app. Once it’s running you will be able to point your browser to your Raspberry Pi at port 3000 (ie http://192.168.1.105:3000).

$ node app.js
or
$ forever start app.js

Be sure to change the config.js file if you are using any different pins.

Additional Reading:

How I Grew As A Programmer in 2012

Posted on December 30, 2012

Learning to be a better, faster, and more efficient programmer is difficult when you act like you already are. To become a better programmer I had to look from the outside at myself and pick apart my flaws that I didn’t see in front of my face. Even after programming for over 13 years there is still plenty of room to grow.

Early Bird Gets The Worm

Since I started working from home with my current job 3 years ago I’ve been a little lazy with my mornings. The morning is my absolute most productive time of the day. At my previous office job I would be one of the first to arrive, after waking up to cook an awesome breakfast. Before this year I was barely able to get to the other side of the house by 8. While I’m still not getting up as early as I’d like, I’m working on it. I’ve gained more of my mornings back, and as a result gained a lot more productivity.

Getting Things Done

I was not a full follower of the whole GTD thing. In fact I’m not an Scrum / Agile type person either. Most of my projects for my job are me working alone, which probably lead to this. In the past my list of things to do has been kept in my head as I have a pretty good memory. Keeping this list in my head has gotten harder as I’ve gotten older and had a kid. The best solution I have found has been Trello. Working as a consultant with many clients and even more projects, it’s difficult to find project management software that doesn’t charge an arm and a leg for a high amount of projects. Trello is currently still free for an unlimited number of projects.

Reading Later

There have been several read it later apps pop up, I immediately skipped these. Earlier in the year Read it Later became Pocket, and went free. I gave it a shot. By pushing all the articles I found during the day while waiting on compiles to reading at night, it felt as if I gained an extra hour a day.

No Amount of Help Is Too Small

Not committing to open source projects has been a weakness of mine. It took me a while to realize that I don’t have to implement full blown features to help out on an open source project, fixing trivial bugs is helpful. There is no patch too small.

Changing of Text Editors

As a programmer changing text editors is a big deal, so for the first time since 2005 I have changed text editors. I switched from TextMate to Sublime Text. I used TextMate to get me through my Bachelors and Masters, then continued to use TextMate into my professional career. So even thinking of switching was a big deal. I tried out Sublime and never looked back, it’s speed and customizability won me over.

Skipping Frameworks

There are tons of frameworks out there that make our jobs much simpler. In the past I’ve been quick to jump to use some frameworks, now I have apps that are fully relient upon code that isn’t maintained anymore. I built several iOS apps using Three20 in years past, now Three20 has pretty much gone of the way of the Dodo. When I originally started using Three20, development was full speed ahead and not slowing down plus Facebook was behind it. Three20 breaks, and breaks a lot these days. I’ve patched things locally, but they are mere bandaids on a large wound. Stripping out Three20 from these apps would take more time than it’s worth. Now I weigh my decisions on using any framework.

Just Buy the $2.99 App

I have a bad habit of anytime that I see an app that I like, I feel I need to build it myself instead of paying for it. Cutting out the features I don’t need and shoving in my own. Now, unless it’s something I can complete in 5 minutes, it’s worth more to spend $2.99 and go about my day.

Side Projects

I have always kept a ton of side projects going, mainly to myself. Most never see the light of day, and most I never use again. I’m now starting a rule that if I can’t open source it or make money off of it, then I will not work on it. I have wasted too many hours of my life that I will never get back on these projects. While every project has been enjoying, there never has been much gain for me or for anyone else.

My Daughter

Having my daughter last September has made me think a lot about the future. I no longer have just myself, or my wife to worry about. I have someone who is dependent upon me to provide everything they need. She has a long ways to go. I want to make sure she has every chance in life, and gets as much education as she can.

To provide these things for her, I can’t help but always be a better programmer than I was the day before.

Switching to Jekyll

Posted on August 15, 2012

After over 5 years of running Wordpress for my blog I’ve decided to hang it up. Wordpress is nothing short of great, but it is overkill for what I need. If you look at my archives it’s not like I have written to the lengths of a novel. It’s taxing to run a database for that. Using Wordpress for client’s sites has been great and offered an easy way for them to be able to change information themselves, and I will probably continue to use it in that regard. So long and farewall my good friend. Welcome to my new friend, Jekyll.

Hopefully this will finally push me to start writing again. I have a few posts that I’d love to write.

Ember Nycum

Posted on September 11, 2011

Ember Nycum

Our daughter, Ember Nycum, was born on September 8th. Check out http://embernycum.com for more pictures and information. Yes, I'm a proud geek dad.

Bouncer on GitHub

Posted on June 11, 2011

Created my first jQuery plugin and put it on GitHub tonight. It’s called Bouncer. Bouncer is a plugin that is designed to enforce rules on your input fields in much the same way a bouncer would serve at a club. With Bouncer you can set which and which type of characters are allowed or disallowed, as well as imposing a max limit of characters.

github.com/brentnycum/bouncer

I have been wanting to learn jQuery a little bit more and thought this was something I could easily see myself using a lot. I seem to only use ready made plugins in my projects and never touch it more than binding events to simple actions. Fork it and improve it if you want, willing to accept Pull Requests.

Printing / Converting UIWebView to PDF

Posted on June 01, 2011

Please see my newer post Update to iOS HTML to PDF Conversion. It contains my open source project BNHtmlPdfKit. This post is only being kept for historical purposes.

Note: My solution isn’t perfect. It lacks crispness since the document is rendered at 72 ppi. You will also have to work with your HTML so that it doesn’t get cut off when going to a new page.

I had a client that requested printing in their iPad app, which is no problem now with iOS 4. The app was a set of forms with inputs like text fields, switches, etc. Pretty standard stuff. I decided the easy way with so much dynamic content was to render everything to HTML and use UIMarkupTextPrintFormatter. This would handle displaying text without calculating metrics on everything at the very least. Easy enough and it worked well. Then the client also wanted to be able to print to PDF and email the document. This is where the fun started.

I figured that I would be able to use the same UIMarkupTextPrintFormatter to generate out my PDF for me, after all it was a UIPrintFormatter subclass. UIPrintFormatter contains the method, - (void)drawInRect:(CGRect)rect forPageAtIndex:(NSInteger)pageIndex. I played around and got nothing but blank pages. Even the pageCount property on my UIMarkupTextPrintFormatter was returning 0. After a bunch of Google searches I still kept coming back to the same question on StackOverflow. Which the answer seemed to suggest that nothing is drawn till the system takes over.

Next plan of action was to see if I could convert a UIWebView to PDF. I was brought back once again to another question on StackOverflow. Which technically works, but doesn’t really handle multiple pages. This is when I decided to roll my own solution.

First thing, I need my HTML, nothing fancy here just a NSString. Next I need to create a UIWebView and load it with my HTML string. I also need a UIWebViewDelegate. We are going to assume that we are printing on a 8.5” x 11” sheet of paper, using 1” margins all around. So 6.5 * 72 for width and 9 * 72 for height.

NSString *html = ...;

UIWebView *webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, 6.5 * 72, 9 * 72)];
[webView setDelegate: self];
[webView loadHTMLString: html baseURL: nil];

I needed to make a UIWebViewDelegate because if we try to do anything with our UIWebView now, it would still be empty. The system needs to parse and render our HTML. Now we need to implement the -(void)webViewDidFinishLoad:(UIWebView *)webView method in our delegate. This will let us know when the system is done rendering. The first thing we will do is get the height of our UIWebView. I tried adding the heights of the UIWebView subviews but it was never quite right, so I decided to use good ol’ JavaScript. We also have to make sure we aren’t drawing beyond the height of our HTML or else we get that grey background you are used to when scrolling beyond the bounds of the page in Mobile Safari. Next we need to change the currentOffset of the UIWebView for each page, which is now kind of a hack using the lastObject of the subviews which should be a UIScrollView. Then lastly render.

-(void)webViewDidFinishLoad:(UIWebView *)webView {
	// Get the height of our webView
	NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

	int height = [heightStr intValue];

	// Get the number of pages needed to print. 9 * 72 = 648
	int pages = ceil(height / 648.0);

	NSMutableData *pdfData = [NSMutableData data];
	UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );

	for (int i = 0; i < pages; i++) {
		// Check to see if page draws more than the height of the UIWebView
		if ((i+1) * 648 > height) {
			CGRect f = [webView frame];
			f.size.height -= (((i+1) * 648.0) - height);
			[webView setFrame: f];
		}

		UIGraphicsBeginPDFPage();
		CGContextRef currentContext = UIGraphicsGetCurrentContext();
		CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins
		[[[webView subviews] lastObject] setContentOffset:CGPointMake(0, 648 * i) animated:NO];
		[webView.layer renderInContext:currentContext];
	}

	UIGraphicsEndPDFContext();

	...
}

From here you can use our NSData object pdfData to save out to file, or perhaps in my case attach to an email.

Now as the note at the beginning of the post said, this isn’t an ideal solution. The generated content doesn’t look the best but it works. I could have done the better thing and drawn out all of my content using Core Graphics / Quartz.

Please see my newer post Update to iOS HTML to PDF Conversion. It contains my open source project BNHtmlPdfKit. This post is only being kept for historical purposes.


ABOUT BRENT

I am a software developer living in Shreveport, LA with my wife, daughter, and 2 dogs. Most of my time is spent developing for the web using Rails, on mobile for iOS, and sometimes for the Mac. When I'm not programming I follow the Texas Rangers through the ups and downs of all 162 games.

You can find me on Twitter and Github.

Follow @BrentNycum