Basic Internet of Things Tutorial Using Raspberry Pi and Cloudways Laravel Cloud

by Noor Ali  May 19, 2016

Internet of Things (IoT)—or as I like to explain it—is simply the Extended Internet. It is a fabric of systems that can be a gadget, a device, a machine or even your pet that are uniquely identified over the network without human to computer interactions.

According to Google, there is the potential for 50 billion connected devices by 2020. [PullQuote]With embedded computers getting commonplace, we are reaching new avenues[/PullQuote] to explore the true potential of the Internet. We are expanding our capabilities to grab as much data as possible.

With such advancement, nobody would like to miss a chance to enter this area as soon as possible. Whether you have a cash machine at your store of which you want continuous updates, or you have your servers for which you continuously require health monitoring or it can be simple wireless nodes you have made to monitor electricity consumption of your office and home.

It will help you gain new data-driven insights and drive actions from IoT by connecting, analyzing, and integrating device data into your business processes and applications, enabling your business to deliver innovative new services faster and with less risk.

Why IoT With Cloud?

IoT ensures the continuous data collections from your deployed nodes, which in return brings large data into your server and the Cloud helps you scale up or down depending on your needs.

Tada! It’s as simple as that.

With high connectivity and backup ability, [PullQuote]your cloud will make sure you have your nodes accessible across the globe [/PullQuote]and have tendencies to grow on your demand.

Creating an IoT device via Cloudways

For this tutorial, we will use a Raspberry Pi computer to create an IoT device. Through this device, you can do the following things:

  • You can deploy your apps in a single click.
  • You can scale them in a single click.
  • You can manage your apps smoothly

Internet of Things Laravel with Rasperry pi

You can use Python at the server end to analyze your gathered data with immense computing power—”pay as you go computing” power. (Python is not officially supported as of now but us geeks can actually get our things done.)

Well, let’s start with our tutorial today. First, we look into our objective:

  • We are developing a simple Laravel based logging mechanism, which will connect our Raspberry Pi to our remote server.
  • We will then make our Pi gather local information and send it to your Cloudways server. Now we won’t be teaching here how to use GPIOs and stuff, that is something you can find at various other places. Here we will discuss integrating the marvelous PHP Laravel framework.

Prerequisites

You will need the following stuff to get things started:

  • You must have launched a Laravel app on Cloudways. (If not, click here.
  • You should have a Raspberry Pi. If not, it’s OK you can test the code on your simple machine.
  • You must have some knowledge of PHP, Python, MySQL, and a little bit about Laravel.
  • We will be using VIM, so you better know how to exit and save. 😀

Getting Started

We will make two routes that present the entry point for our remote nodes to push the data and to get the details. We will then configure our controllers to create a database of our logs. In the end, we will make our views display our logs in table form.

Establishing a Connecting With MySQL Database

Well, we will need a database of course.

  • Inside Cloudways, navigate to Application > Access Details > Launch Database Manager.
  • Click on the Create Table button.
  • Name the table “CPU” and add the following fields:
    • id (int auto incr)
    • date
    • time
    • usage (float)
    • status (string)
  • Now in your public_html folder, there is a .env file. Add your database details like following:

  • Don’t forget to change XxXxxx with your Database name and password.  

Making Our Routes

You will need this API routes as entry points. Through these, we can push our data.

  • Routes are placed in App/Http/routes.php
  • Add the two following routes

  • [PullQuote]First Route pushes the data CPU usage and CPU status[/PullQuote]. It then passes it to our Controller (LogController) which has the function StoreData. We will make this controller in a moment.
  • Second Route simply calls the function ShowData of our controller LogController.

Creating a Controller

We require this to safely handle requests for IoT devices.

  • Controllers are placed in App/Http/Controllers/<Controller_Name>.php
  • By default, there is a Controller made for you. We will just extend this controller to make our own controller named LogController.
  • Make App/Http/Controllers/LogController.php file.
  • Paste the following code.

To use the controllers we have to define the namespace. We have to give the fetching directories of our Views and Models.

Creating the View

This will create a presentable output for your data logs.

  • Views are placed in public_html/resources/views folder
  • Make a view and use showdata.blade.php as its name. This is the naming conventions PHP uses for Views.
  • Paste the following code.

So, this was all we had to do on the server side. You can alter your code according to your desired results. Now let’s get our little script running on a Raspberry PI device which brings us the CPU usage to our server. I have used a sneaky way to find out CPU usage. :p You can use your own method to make the script shorter.

Now, let’s test our code.

Run the Python Code in your Pi:

You will see the following results.

Now, let’s visit our Laravel app. Paste the link in your browser. I have my live app here:

http://phplaravel-14171-43979-113153.cloudwaysapps.com/datashow

Your logs should be somewhere like this.

The World Is Your Oyster (or Raspberry Pi)

So here you go with our tiny little objective, which I guess is enough for you guys to carry on with your IoT projects. You can use different charts libraries too to get real time plotting.

I have seen this LavaCharts which is powerful graph and charts library of Laravel. I will try to make something out of it if I get chance. Till then see ya—and oh yeah, here is the repo link in case you need the complete project.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...

  • Nice Article once again, I have a Project am trying to work on with Laravel and Raspberry PI, I don’t know how much knowledge you have in this areas but what I actually want to do is to read the Level of Gas in my Cylinder through the Raspberry PI and send the Information back to my Server at every interval of time frames.

    • Noor Jafri

      Hey, thanks for your remarks. Well this would be the great what you are trying to implement but first you need to checkout the transducer you might want to use with Rpi. Now reading the Gas Cylinder is quite different, considering there is only one type of Gas present in this cylinder, you can use Pressure Sensors on your valves, I found quite few on my first google, grab any I2C, that will work with PI. You might need to adjust your readings in your code for your tempreature. So first get your data ready on your pi then we can simply push the data on the server. EZ PZ