Thursday, 21 March 2013

PySerial, Python, Raspberry Pi and issuing AT commands to a Mobile Dongle

For anyone who saw my last post, they will know that I've been working on a number of projects - the current one is to do with the RP and texting.

I couldn't find any modules out there that I liked to talk to the dongle, so I decided to make my own functions to do the job. It's not perfect, but it's doing the job for me and will be expanded on over time.

See below:

You'll need PySerial for this to work, and also create a serial object called 'dongle'
example: dongle = serial.Serial(port="/dev/ttyUSB0",baudrate=115200,timeout=2,rtscts=0,xonxoff=0)

# write a command to the dongle, check the line out buffer for the correct response and return it
# cmd is the command we issue
# success is what we're looking for in the buffer if it was successful
# fail is what we're looking for if it fails
# AT is whether we put the "AT+" in front of the command
# if we pass "pass" as the cmd, then we don't write anything - useful if we're looking for 2 outputs from a command (such as +CMGS and OK when we send a message)

def WriteBuffer(cmd, Success, Fail, AT):
if (cmd == "pass"):
pass
elif (AT == "Y"):
dongle.write('AT'+cmd+'\r')
elif (AT == "N"):
dongle.write(cmd+'\r')
Found = "False"
BufferCounter = 0
while (Found == "False"):
Buffer = dongle.readline()
if Buffer.find(Fail) > -1:
return Fail
if Buffer.find(Success) > -1:
if (Success == "+CMGW:"):
SuccessValue = Buffer.split(": ")
Success = SuccessValue[1]
if (Success == "+CMGR:"):
Next = dongle.readline()
SuccessValue = str(Buffer)+","+str(Next)
Success = SuccessValue
Next = dongle.readline()
Next = dongle.readline()
if (Success == "+CSQ:"):
SuccessValue = Buffer.split(": ")
Success = SuccessValue[1]
Next = dongle.readline()
Next = dongle.readline()
if (Success == "+CBC:"):
SuccessValue = Buffer.split(": ")
Success = SuccessValue[1]
Next = dongle.readline()
Next = dongle.readline()
if (Success == "+CPAS:"):
SuccessValue = Buffer.split(": ")
Success = SuccessValue[1]
Next = dongle.readline()
Next = dongle.readline()
return Success
if BufferCounter > 5:
return "timeout"
BufferCounter = BufferCounter + 1

Function to send a text below - you can add captures for errors that are thrown back:

# sendnumber = mobile, message = the message we want to send

def SendText(SendNumber, SendMessage):
WriteBuffer(('+CMGS="%s"') % SendNumber, ">", "Error", "Y")
WriteBuffer(SendMessage, ">", "Error", "N")
WriteBuffer(chr(26), "+CMGS:", "Error", "N")
WriteBuffer("pass", "OK", "Error", "N")

As I said, it's not perfect, but it does allow you to issues commands and get back the output quite easily.



E160G and Raspberry Pi

I thought i'd share a few of my findings on trying to use an E160G mobile dongle with my Raspberry Pi. I have a project where the Pi is sending text messages, and to do this i'm using Python and PySerial.

Everything has been working fine with it, got the modem connected and tested out the AT commands using minicom. Everything was pretty happy until we got down to sending.

Everything seems to work fine on the surface, you enter send string:

AT+CMGS="mobilenumberhere"

and then it returns the expected > for the message, so I enter the message and then end the message (char26 or ctrl+z)

Message sends, I see it arrive on my phone.. but then the dongle doesn't respond with a message ID number as expected (+CMGS), nor does it respond ok. It just crashes and sits there doing nothing.

I went through various things, my original htought was a power issue as it only happened when sending - tried 3 different powered USB hubs all to no avail. I thought I was going mad.

Eventually, I tried a different dongle (an E173) and it worked perfectly. Therefore at this moment in time, I believe there to be some kind of incompatibility between the E160G and the Pi - or at least MY E160G and the Pi.

I've wasted far to much time on this, so thought I would share my thoughts.

For anyone interested, I've written a some functions to work with Python and the dongles for sending adn collecting messages - see my next blog post.

Later

Monday, 25 February 2013

Psst.. Like freebies and reviewing products? Try Bzzagent

Not sure if anyone out there reading my blog will be interested in this, but I'm going to be taking a break from technology for a moment.

Before Christmas I came across a site called Bzzagent. You register, answer some surveys and then (hopefully) you get signe dup to campaigns to match your likes/interests and get items sent through the post for free for you try and review.

I got my first campaign in Febuary 2013 - Vanish Oxy action.

What did I get?

- 2 x 500g packs of Vanish
- Handful of £1.50 off vouchers for Vanish to give out to friends and family
- Booklet for removing stains
- Information about the campaign
- A rather nice little white cardboard box (yeah i'm pretty sad, I like little boxes and recycle them for sending ebay sales in)

So far I've used the Vanish on my daily washing and saw no difference (half a scoop), however washed a stained old Duvet that I've had for years and it's come out nice and white again. Good job Vanish!

So if you like free products and to review them give Bzzagent a go! You've got nothing to lose :) I'm looking forward to the next campaign already: https://www.bzzagent.co.uk/


2013 Web Design Trends

As part of the other projects that I have, I also undertake web-site design for clients (not just as part of larger projects).

I've had a few clients interested in a new website, but I havne't managed to capture their imagination with any of the designs that they have so far seen.

2013 seems to be the year of the Large Photo Background. People love minimal websites, they're easy to navigate and have very little content for the users to update (and believe me, users hate providing and udpate content!).

I made a few didferent mock-ups using Large Photo Background and bam, caught.

Certainly seems to be a trend that people are loving at the moment. Below are a few of my favourite websites out there suing this trend (working 25/02/2013):

http://www.davidia-int.hr/
Decorating and Architecture - what more do you need to show off these skills than some amazing photos? This type of website certainly seems to lend itself to this indsutry sector and get the most out of what they do.

http://ringvemedia.com/
Travel - we go places to see and experience them, so full photo websites will naturally be the perfect way of showing people what's there. I love the dull cityscape shown, it gives it real feeling.

http://www.davidnolangallery.com/
Art - galleries are all about displaying art, so why shouldn't our websites be an extension of this? This is another one of those sectors that this type of web-design was born for.

http://www.designhousestockholm.com/
Design - Show off your companies items in full-page glory! 

Closing Words
Great you say, these are (nearly) all high-end company/designer websites. Yes, they are - but this trend lends itself to even the smallest of small clients in rural areas. I have one in a remote village, they do boring things. Nothing exciting can get taken from what they do, but their office is in a glorious small village surrounded by fields. Green Fields. Endless Green Fields with a blue sky. That for me, was inspiration as it's best - so you can now guess what it looks like.

Large scale photos, minimal information but something to keep people interested.

Bah bah bah dah dah... i'm loving it


Thursday, 21 February 2013

PageGate and SMS Messaging using a USB/3g Dongle

Anyone out there use PageGate? Anyone out there heard of PageGate?

It's another system that I maintain for a client. Basically, it acts as a gateway allowing emails, modems and other things to communicate with it and then forward a text or email on to a specific recipient. 

Why? That's not important, suffice to say it routes hundreds of emails and turns them into texts via AQL. AQLs service has been great so far (thanks guys) but a lot of cash is being burnt by having to pass it through another portal so the decision has been made to send the texts ourselves using a £10 USB dongle.

Thats the plan. Enter Des to make it work.

PageGate does support USB dongles, but it doesn't make it very clear (the product is great, but there are so many things it can do it isn't all documented).

A bit of playing around and yielded the following method:

1: Install and get your USB dongle working in the system
2: Open PageGate admin and go to Connectors
3: Select your connector (1 in my case) and give it an Auto init string and set the Serial Port to the correct COM port number that the dongle is tied to.
4: Now go to your carrier section
5: Add a new carrier and name it (GiffGaff in this case for us)
6: Set the following: Protocol GSM-AT, Baud 11500, Parity None, Data Bits 8, Stop Bits 1, Init String AT+CSQ and click apply
7: Now go to your recipients and create a new one
8: In settings, set the carrier to the one we just created above
9: In ID/PIN enter the mobile number of the person you want the text to go to (this is the bit not well documented!)

That's it - give it a test and you should find your text arrive straight to he mobile device.

It's a great bit of software, the guys at PageGate should be proud especially with V6 our now which I haven't yet tried.

The above is working for me in the UK, so our US cousins may not get the same results. Also, the mobile number that I used was left as an 07 number and NOT changed to an international 44. Does it make a difference  I'm not sure, but it works so i'm not going to find out :)


Update: I saw someone reference this article on the PageGate forum, so will update this with a few points that seemed to confuse them: the SMS messages are sent over the GSM network and do not require a data connection. However, some of the modems you can buy use a protocol known as Hi-Link, which makes the dongle act as a local Ethernet device - this doesn't work correctly with the above as you need the dongle in its traditional operation mode. If you check the manual there are often ways of making the mode operate in this way instead.

Hope that clears a few things up.


Tuesday, 19 February 2013

Python, Raspberry Pi and Raspbian (Debian)

I've been doing a lot of development with Python on the Raspberry Pi's, and I just wanted to share a few thoughts from the middle of my project.

Coding Application
When I first started coding I was using nano of the Raspberry Pi, however I have now moved my development on to my Windows based PC. The best light-weight coding application that I have found for Python has been Notepad++. There are many alternatives out there, but I find this works well for all of the different languages that I code in.

IDLE (the real-time editor for Python) I didn't really get on with, and prefer editing the code and then running it afterwards.

Indentation
When starting to work with Python, I was constantly getting thrown by the indentation and lack of semi colons at the end of each line. It's something I came more accustomed with having spent more time coding.

Now, I'm a big fan of the way it works and I prefer it. It forces people to be neat coders and that's something that i'm all for.

GPIOs
I got a Raspberry Pi when they first were released and getting the GPIOs to work were easy enough, but no-where near as easy as it is now.

If anyne has a Raspberry Pi and hasn't yet had a chance to work with the GPIOs I would strongly recommend it. For the main project I am working on, i'm using some ADC cobbler code from adafruit which in turn pulls data from an 8 channel ADC chip. This is mounted on a separate power board which powers our unit.

On a side note, did you know you can power the Raspberry Pi by apply a voltage the 5v pin on the GPIO? A word of warning, if you do this you bypass the built in liquid fuse which protects the board - so be sure to incorporate a replacement into your power board design.

Terminal/Linux Commands
The other great thing I like about Python is being able to pass commands through to the local console of the operating system.

Import os
os.system("something here") 

This allows the command to be sent through to the local terminal - allowing greater control over the system. You can pass reboot commands, turn lan devices on and off - all sorts.

Mix this with a local light SQL server and you can do some pretty impressive things!

Closing Words
That's all i've got to say today, since my last post other projects have risen to the top of my list so i'll be carrying on with the mesh system soon and trying to get DHCP working. 

I had a play with the v3 Google API for it's map system last night and may do a quick write up on here. It's part of this project too, allowing me to plot locations of my Raspberry Pi units on a map of a given area. More about this later!

Saturday, 16 February 2013

Bridging ETH0 and WLAN0 for Raspberry Pi Meshing

Following on from my post yesterday, I decided to (quickly) give bridge the ports a go before popping out for the day (it's the weekend after all!)

Everything is pretty much the same as before, except if you are using Raspbian you'll need the brctl tool:

sudo apt-get install bridge-utils

Again, if the above fails its worth doing a sudo apt-get update

Once installed, we use the bridge command thusly if we're following on from my last post:

1: sudo brctl addrbr mesh-bridge 

2: sudo brctl addif mesh-bridge eth0

3: sudo brctl addif mesh-bridge bat0

4: if config eth0 up

5: ifconfig bat0 up

6: ifconfig mesh-bridge up

What you should find is that you now have a working bridge, but like me you probably won't be able to ping your other node anymore.

Not to worry - we've now taken bat0 out of the equation, so we need to remove the IP we set last post

7: sudo ifconfig bat0 0.0.0.0

and then add our ip to mesh-bridge instead. Assuming we are currently on Node 2, then we'll assign it 192.168.2.2

8: sudo ifconfig mesh-bridge 192.168.2.2

and then it all started working again for me. I'm going to start messing around with getting DHCP working next so we'll see what happens.