Lab 3.2: Arduino and the Web


In this lab, we’ll learn how to use Arduino to connect to the internet. Things you will need for this lab:

  1. Arduino board.
  2. Wifi Shield or Ethernet Shield.

We’ll create a simple project to demonstrate how to communicate with a web server and send data to it. The Arduino board establishes a connection with the server using Wifi shield and sends a dummy value as sensor reading where it is stored in a database and also displayed to the user in a browser.

First we’ll look at the Arduino sketch. The setup() first checks if wifi shield is present and connects to wifi a network if wifi shield is detected. loop() is also quite straight forward, first a connection is established with the server. Then a GET request is made to the server passing the sensor type and sensor reading (dummy reading in this example). Then the program get the response from the server as acknowledgement and prints it to the serial port in order to verify that the GET request was successfully received. The program consistently prints information about the status of program to the serial port so that progress can be viewed via serial monitor. Make sure to change the servers address in the sketch with the IP address of you laptop



Server Side

On the server side, first create mysql table for storing data:

we will use two PHP scripts. One for receiving the data from Arduino and storing it into a database. Make sure to change to update mysql username, password, and database name in both scripts..


The second PHP script retrieves the sensor readings from the database and displays them to the user. The page is refreshed every few seconds so that newly received sensor readings can be displayed.


You can view the sensor readings sent by Arduino to the remote server using the following link. Please note that you might need to change the server address as the server address changes with each reboot.


Lab Exercise 3.2

  • Include a real sensor instead of the dummy sensor and send it’s readings to the server. You can refer to Lab 3.1 to see how to read sensors.
  • Modify the code so that it automatically samples the sensor every 30 seconds and send the data to the server.

Confirm that your code is working and data is successfully being sent to the server.

Submission Guidelines

    1. Copy the final exercise files into lab-3.2 folder in your main git repository.
    2. Add, commit, and push your changes to the remote server.