Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

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

Updated on  19th May

5 Min Read
Reading Time: 5 minutes

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.


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:

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.

Share your opinion in the comment section. COMMENT NOW

Noor Ali

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

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

Get Our Newsletter
Be the first to get the latest updates and tutorials.