So finally after many months of development, design, build, rebuild and writing of code. I have some data and information on my beehive! Its remarkable how much data you can get and learn about an environment by monitoring a few points over time. At the moment the BeeBetaBox is currently loading up, sending a HTTP  POST to an end point with lots of data about the beehive every 10 mins. This data is rolled into a database and then assembled together to generate graphs showing temperature over time Because I capture the GPS co-ordinates of the hive as well we can then use this to make a lookup against a weather service and provide the weather predictions for the next 3 days. Handy if your hive is remote and you want to see about a visit soon!

BeeSafe Graph

So after spending a considerable amount of time using Sakis – the 3G dongle software. I feel I need to write a better post on how to connect your Debian based system / Raspberry Pi to the internet using a 3G connection.

– Be warned, Data on the 3G network is RARELY all you can eat, ensure you have an appropriate data plan before you let your Pi loose.

– 2nd Warning.. 3G dongles consume huge quantities of power, if your running the project off a battery setup, write some code to disconnect the 3G stick when not in use to prevent it from eating all the power just staying connected.

With that being said, to download and install the 3G software:

sudo apt-get -y update

then

sudo apt-get -y upgrade

then

sudo apt-get -y install ppp

Once the updates have taken route and ppp has been installed you will need to download sakis;

wget “http://www.sakis3g.org/versions/latest/armv4t/sakis3g.gz”

then

gunzip sakis3g.gz

then

chmod +x sakis3g

finally

sudo ./sakis3g –interactive

This will take you to the interactive window used by Sakis, you can configure the 3G dongle here and then issue the connect command. The software has been set out to be easy to interact with.

Depending on the Simcard you have in the dongle, it may attempt to download the latest settings automatically and then offer them to you as a connection method.

If you don’t get that luxury you can manually enter your APN, username and password manually in the prompts.

One feature that I LOVE about Sakis is that you can call the connect command from within another program, lets say my Pi gathers some data up and then needs to send it somewhere on the net, you can tell the program to connect to Sakis like this:

./sakis3g connect APN=”CUSTOM_APN” CUSTOM_APN=”giffgaff.com” APN_USER=”giffgaff” APN_PASS=”password”

This string tells Sakis to connect to the Giffgaff network and can be followed by:

sudo ./sakis3g disconnect

How to add a custom APN to the sakis config file:
Using Nano create a file called sakis3g.conf stored in /etc/

sudo nano /etc/sakis3g.conf

Here you can add the APN details like this:

APN=CUSTOM_APN
CUSTOM_APN=”giffgaff.com”
APN_USER=”giffgaff”
APN_PASS=”password”

and then connect the 3G dongle by doing this:

sudo ./sakis3g connect

Super handy if you need to bring some 3G connectivity to your project! 🙂

Following on my expansion into telephony posts (See a previous one, on making FreePBX work with Twilio). A lot of people have been asking me to provide more information on how to make your phone say things or play and MP3 file.  In particular while in a conference room…

Lets break this down into two parts; Making ‘Say or Play Bots’ and then adding our ‘Say or Play Bot’ to a conference and doing something.

Making a ‘Say or Play Bot’

Using Twilio there are lots of ways to make an automated voice say something to a caller / listener;

Twimlet Message (https://www.twilio.com/labs/twimlets/message) Static Message or MP3 file
TwimelBin (http://twimlbin.com) Static Message
Static XML hosted on your webserver Static Message
Dynamic web script that generates custom Say XML hosted on your webserver Dynamic Message
Dynamic web script that plays a custom message pending some criteria Dynamic Message

Twimlet Message:

In this example we are going to use a Twimlet message to say “Hi, This is you’re 9 AM Meeting Reminder, The current time is 8.55 AM. You have a meeting with Bob in 5 minutes.”

The message could be anything, in this case I’ve choose to use a meeting reminder message. If you follow the URL: https://www.twilio.com/labs/twimlets/message You can input your message into the Twimlet generator and it will spit out the URL you need to use with Twilio to get your message spoken:

http://twimlets.com/message?Message%5B0%5D=Hi%2C%20This%20is%20you’re%209%20AM%20Meeting%20Reminder%2C%20The%20current%20time%20is%208.55%20AM.%20You%20have%20a%20meeting%20with%20Bob%20in%205%20minutes.&

As you can see our message has now been encoded into a URL form, the spaces between words have been filled in with %20 if we were to buy a phone number now and use this in the Voice URL we would hear this message.

Twimelbin:

Twimelbin is an external tool that you can use to test writing your TwiML (XML) writing skills.  It will validate your TwiML and ensure that the syntax is correct, you can then use the URL link provided to reference this TwiML in your Twilio account.  In this example I’m going to copy and paste my TwiML here so you can see what the structure looks like:

<?xml version=”1.0″ encoding=”UTF-8″?>
<Response>
<Say>Hi Caller.</Say>
<Say>I’m just in the shower at the moment</Say>
<Say>Please let me wash my hair in peace and I will call you back later today</Say>
<Say>Thanks!</Say>
</Response>

Here you can see I have linked lots of ‘Say’ commands together in one response. We could if we wanted to be annoying (if!) get each command to be spoken in a different voice or even language (https://www.twilio.com/docs/api/twiml/say) but ill skip that for now.

When your finished composing your Twimelbin entry you take the URL at the top of the page and use this to look up your TwiML in your phone number URL.

Static XML hosted on your own machine:

Static XML and a Twimelbin response are exactly the same, the only difference that one is hosted on your platform and one is hosted by Twimelbin. There are benefits to both; I won’t go into the merits of self hosting vs a 3rd party for any kind of HTTP activity here.

Dynamically generated TwiML response: 

This is where is gets fun! With a dynamic script we can make our message be customised to the caller, the time of day, the weather or any other factor we want! Lets say for example that we have two callers; Mathew and Steve. Mathew’s number is +44123456 and Steve’s is: +44987654 as we are generating this response on demand we can input these names into the <Say> response and give the caller a more personal response.

In this example I’m using PHP, but you could easily use another web language.

<?php

$people = array(
“+44123456″=>”Mathew”,”+44987654″=>”Steve”);

// if the caller is known, then greet them by name
if(!$name = $people[$_REQUEST[‘From’]]) $name = “Caller”;

// now greet the caller
header(“content-type: text/xml”);
echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;
?>
<Response>
<Say>Hello <?php echo $name ?>.</Say>
</Response>

In this example, I made an array of data – mine and Steve’s number and then used the array to look up the name, if either Steve or I called the Twilio number from those numbers it would say Hi ‘Steve / Mathew’ if none of the numbers were recognised the caller would be just greater as just a ‘Hello Caller’

You can use the same kind of dynamic scripting language to play specific files, this could be to either specific times of the day, caller ID’s or special events.

I won’t go into the code here but the basic set up is:

IF event is true

Do this

Else (if not true)

Do this

So for example:

If time = before 12

Play the morning MP3 file

Else (If not true)

Play the Afternoon MP3 file

an example of this in PHP would be:

<?php

if (date(‘H’) < 12) {
$mp3_file=”http://domain.com/morning_mp3.mp3″;
}
else
{
$mp3_file =”http://domain.com/afternoon_mp3.mp3″;
}

// Play the AM / PM file to the caller
header(“content-type: text/xml”);
echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;
?>
<Response>
<Play><?php echo $mp3_file; ?></Play>
</Response>

When this script is called it will check the time, if the clock is before 12 it will fetch the morning MP3 file and if its in the afternoon  it will fetch the afternoon mp3 file. You could go one step further and make an evening file. But for this setup lets assume an morning and afternoon setup 🙂

So whats a bot?

In simple terms a bot is a program / application that just does one thing. In the use case here you could have a phone bot that people could ring and it would recite company open hours.

Example:

http://twimlets.com/message?Message%5B0%5D=Hello%20and%20welcome%20to%20Mathew’s%20super%20store.%20Our%20open%20hours%20are%208%20AM%20to%207%20PM%20Monday%20to%20Friday.%20&Message%5B1%5D=We%20are%20open%20Saturdays%2C%20from%2010%20AM%20to%203%20PM.%20We%20are%20not%20open%20Sundays&Message%5B2%5D=Have%20a%20nice%20day.&

 

This Twimlet just informs the callers of the times, Mathew’s superstore will be open.

 

In recent years, the need for physical internet security has grown, with websites being comprised constantly, we need a way to identify the real you from the internet you..

Enter the world of Two Factor Authentication. This pairs something you have, and something you know.. in our case, a mobile phone and a password.

Imagine signing into a website, after you put your username and password in the website sends you a SMS message or a quick voice call to actually make sure its you.

Using Twilio as the SMS / voice gateway this is possible and really easy to implement, particularly into a PHP server.

You will need:

  1. A Twilio Account – with Twilio Phone number
  2. A PHP webserver
  3. A copy of the TwoFactor Auth script found at: https://github.com/dotmat/TwilioTwoFactorAuth

If you haven’t already, please sign up for a trial account at Twilio : https://www.twilio.com/try-twilio

Once you have signed up you will need to edit the file: TwoFactorAuthProcessor.php placing your AccountSID, Auth Key and Twilio phone number in the top part of the file.

I have included in the git a quick index page that you can fill in, the page will make a HTTP POST to the processor and generate a two-factor passcode which it will either call or SMS to your phone.

Using Two-Factor authentication on your website will make your service more secure and provide peace of mind to your customers / users that even with a security breach, your users remain safe and malicious users are not able to gain access to your platform as they do not have the end users phone – something needed to pickup the two factor key.