Friday, August 29, 2014

Arduino + Ethernet Shield + Temperature Sensor + WAMP Server

Welcome to my tutorial !!


In this tutorial, I will teach how to update temperature table with Arduino.


What do you need ?
1. Arduino UNO


2. Arduino Ethernet Shield


3. Temperature Sensor TMP36


4. WAMP Server- Download here

What knowledge you need have??
1. Arduino IDE
2. PHP & MySql


Overview of uploading data to database with WAMP
This document explains the procedure to connect Arduino System to a Server and then make an HTML request to update the database. Here are the following steps:
1. Arduino makes a HTTP request through a Php file and passes the data to these files(Php language is used to talk to server)
2. Php files contain necessary information to connect to database and then pass the values to database(using mysql statements) given by the Arduino System.
3. Finally temperature table in the database gets updated with the data .


What you need?
  1. Server which has a static IP address/ Computer which has a static IP address (LAN connection). If computer, Wamp/Xampp Server running on your computer.
Wamp comes with Apache, MySql and Php. (See below for this file)
  1. Php files running on your server.
  2. A SQL table in database.
  3. Ethernet Shield connected to Arduino Uno through pins 10, 11, 12, 13.
  4. Cat 6 cable and power injectors.
  5. Some knowledge on php and mysql programming.


Challenges
This looks simple but there are many challenges to achieve this. This document will make you meet those challenges and helps you to send data to database. Here are some challenges i faced:
  1. Network configurations of Server and Arduino
  2. Establishing connection between Ethernet Shield and Server.
  3. Proper HTML requests from Arduino
  4. Giving Access to Arduino to talk to server (Setup Access).
  5. Setting up Remote phpmyadmin
  6. Proper php files


I believe that these challenges are baby steps to be done before updating database.  Each of these challenges are explained here:
  1. Network configurations of Server and Arduino
Make sure that Arduino and Server are connected to LAN. Obtain the IP address for both server and Arduino. If using wamp, IP address of the machine becomes the server IP address.
  1. Establishing connection between Ethernet Shield and Server
Confirm whether you have a stable connection or not between Arduino Ethernet and server. Remember pinging an ip address from your computer. If using windows the command is:
-->ping ipaddress
  • Connect Ethernet Shield into Internet and obtain its IP Address. Then ping this IP address from your machine using cmd. Proceed next, if you are able to send and receive packets.
  • Do it other way now. Ping your machine from your Ethernet Shield. Use this library and example to ping your machine: ICMP Ping for Arduino
If above two steps of pinging is successful, then you can confirm that there is a connection between Arduino Ethernet and your machine.
  1. Proper GET Html requests from Arduino
Before proceeding any further, check with the webclient example of Arduino here: WebClient. In the webclient example above, server is google, so its just to make sure that you have a proper GET Html request. If you are able to make proper HTML requests here, then that’s great. Later at the end we will update this server address to be our machine and then make GET request to call php files.
  1. Giving Access to Arduino to talk to server (Setup Access)
So your WAMP is already setup with everything working as expected, and you're ready to push/pull data to database, but there is something that should be done before.Fortunately, you come just the right place to get the solutions to solve this issue. In this document for WAMP, I will show you how to install and setup outside access for WAMPSERVER. To access your database, no matter it's within your LAN or worldwide, you need to set up these three steps.
Note: I assume that you are using WAMP on your machine
  • STEP 1: Enable Associate Port in all Firewalls

In order to communicate between your computer and Ethernet Shield, either within your LAN or worldwide, you need to enable HTTP port in all firewalls. Different people use different firewalls and each firewall has its own way to enable port, so here I show the most basic firewall that comes with your Windows
Default port for HTTP is port 80. Note: If your ISP blocks port 80 for some reasons, you can use port 81 or 8080, and remember to set up that same port in WAMP as well.
To enable that associate port in Windows Firewall, left click on Start menu and select Control Panel (setfw1.jpg) that will pop up Control Panel windows. Next, under Security section, select "Allow a program through Windows Firewall" (setfw2.jpg) which will result the pop up of "Windows Firewall Settings" (setfw3.jpg). On the Exceptions tab, left click on "Add port..." which pops up windows "Add a Port" (setfw4.jpg), in which you fill in the informations as follows:
Name: HTTP
Port number: 80
Protocol: TCP
When done, click OK and close all windows
Note: If you have more than one firewall, you need to enable this associate port in all firewalls. Please, consult your firewall provider for more informations
  • STEP 2: Put your WAMP Server in Online Mode

WAMP server comes with off-line mode by default. To put your WAMP server in online mode, left click on WAMP tray icon to access Main Menu, then select "Put Online" (setpo.jpg).
  • STEP 3: Enable Associate Port in all Routers

For example, your computer with WAMP installed has LAN IP 192.168.0.100; basically, you need to enable port on router as follows:
Service name: HTTP
Start port: 80
End port: 80
Server IP address: 192.168.0.100
However, different routers have different ways to enable port. Some call Port Forwarding, some call Virtual Server while some others call Services. Here we include two screenshots for the two most popular known: Port Forwarding (setpf1.jpg) and Virtual Server (setpf2.jpg)
Note: If you use different port other than 80, make sure to enable that same port here
Here are some popular default IP address to login the settings of the routers:
http://192.168.0.1
http://192.168.1.1
http://192.168.2.1
And also here are some popular default login informations:
Username: admin
Password: admin (or password, or blank)
  1. Setting up Remote phpmyadmin

Remote MySQL through PHPMyAdmin on WAMP

For users' own protections, WAMP by default is blocked from outside access; thus, if you wish to remote access your MySQL, you need to make some "little" changes in settings, and in this exmaple for WAMP, we will show you what steps you need to do to achieve it ;).

Security First!

When granting access to outside world, your MySQL server can be accidently modified by anyone; therefore,  set up password protection as well as the login screen for phpMyAdmin.
Here are the three steps:
  • STEP1
Open file phpmyadmin.conf in folder C:\wamp\alias and find these lines below.
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
  • STEP2
Then edit to make a change as follows.
Order Allow,Deny
Allow from all
  • STEP3
Now you can remote access your MySQL through phpMyAdmin if you've already set up Virtual Hosts with similar script below; please, check our Virtual Hosts guide for more detail.
ServerName pma.your-domain.com
ServerAlias pma
DocumentRoot "C:/wamp/apps/phpmyadmin3.1.3.1"
And access worldwide with your subdomain as follows
http://pma.your-domain.com
Or access locally with your local domain as follows
http://pma
  1. Proper php files
We need three php files. When Arduino makes a GET request, it passes data to the called php file. This called php file executes another php file which makes connection between the called and server. Then the data passed through this called php. The third php file is to display the data on the web.


Example 1: Pulling data from Database
Create a rec.php file in www folder of wamp. Here is the code:
<?php
echo "Hello World";
?>
Run the code from Arduino and you will see Hello World on Serial Monitor. Here is the Arduino Code:
Pulling data
/*
 Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
 0x90, 0xA2, 0xDA, 0x0F, 0x3A, 0xDC};
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
IPAddress server(192,168,2,9);   // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,2,8);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
// Open serial communications and wait for port to open:
 Serial.begin(9600);
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }

 // start the Ethernet connection:
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to configure Ethernet using DHCP");
   // no point in carrying on, so do nothing forevermore:
   // try to congifure using IP address instead of DHCP:
   Ethernet.begin(mac, ip);
 }
 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");

 // if you get a connection, report back via serial:
 if (client.connect(server, 80)) {
   Serial.println("connected");
   // Make a HTTP request:
  //client.print("GET /rec.php HTTP/1.1\n");
   client.println("GET /rec.php HTTP/1.1");
   client.println("Host: 192.168.2.9");
   client.println("Connection: close");
   client.println();
 }
 else {
   // kf you didn't get a connection to the server:
   Serial.println("connection failed");
 }
}

void loop()
{
 // if there are incoming bytes available
 // from the server, read them and print them:
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }

 // if the server's disconnected, stop the client:
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();

   // do nothing forevermore:
   while(true);
 }
}
Example 2: Pushing temperature data to Database
Follow this tutorial: You can follow this Push to Database

Else you can use php scripts from above tutorial and use my code to push temperature data to WAMP Server

#include<Ethernet.h>
#include<SPI.h> 
const int temperaturePin = A0;
// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3A, 0xDC };                                       
// For the rest we use DHCP (IP address and such)
IPAddress ip(192,168,2,8);
EthernetClient client;
IPAddress server(192,168,2,9); // IP Adres (or name) of server to dump data to
int  interval = 5000; // Wait between dumps

void setup() {

  Serial.begin(9600);
     while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
    if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }


  Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask       : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP     : ");
  Serial.println(Ethernet.dnsServerIP());
}

void loop() {
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
      float tem = getTemp();
    Serial.println( tem );
    Serial.println("-> Connected");
    if(client.connected()){
    // Make a HTTP request:
    client.print( "GET /add_data.php?");
    client.print("serial=");
    client.print( "TempSensor" );
    client.print("&&");
    client.print("temperature=");
    client.print( tem);
    client.println( " HTTP/1.1");
    client.println( "Host: 192.168.2.9" );
    //client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
  }
  }
  else {
    // you didn't get a connection to the server:
    Serial.println("--> connection failed/n");
  }

  delay(interval);
}
float getTemp() {
  float voltage, V,t ;
  voltage = analogRead(temperaturePin) ;
  V = voltage * 0.004882814 ;
  t = (V - 0.5) * 100.0;
  return t;
}



WAMP- Windows, Apache, MySql, Php
WampServer is a platform for Web development on Windows for dynamic Web applications using the Apache2 server, PHP scripting language and a MySQL database. It also has PHPMyAdmin to easily manage your databases.

Installation:
  • Double-click the downloaded file and follow the instructions.Everything is managed by the installer WampServer. Default Wamp Server comes with the latest versions of Apache, MySQL and PHP.
  • Once installed, you can manually add additional versions of Apache, PHP or MySQL (Only compiled VC9, VC10 or VC11). Explanations to do will be given on the  forum .
  • Each version of Apache, MySQL and PHP has its own settings and its own files (data for MySQL).
Use WampServer:
  • Automatic creation of a "www" directory during installation (typically c: \ wamp \ www).
  • Create a subdirectory for your project and drop it your PHP files
  • Click the "Localhost" link WampServer menu or open your favorite browser and go to http://localhost address




3 comments:

  1. I wonder could you upload the circuit diagram?

    ReplyDelete
    Replies
    1. Can I know where you are struck?? I have mentioned everything in the blog.

      Delete
  2. Hello everyone,

    Here is the WAMP file:

    WAMP- Windows, Apache, MySql, Php
    WampServer is a platform for Web development on Windows for dynamic Web applications using the Apache2 server, PHP scripting language and a MySQL database. It also has PHPMyAdmin to easily manage your databases.

    Download here: http://www.wampserver.com/
    Installation:
    Double-click the downloaded file and follow the instructions.Everything is managed by the installer WampServer. Default Wamp Server comes with the latest versions of Apache, MySQL and PHP.
    Once installed, you can manually add additional versions of Apache, PHP or MySQL (Only compiled VC9, VC10 or VC11). Explanations to do will be given on the forum .
    Each version of Apache, MySQL and PHP has its own settings and its own files (data for MySQL).
    Use WampServer:
    Automatic creation of a "www" directory during installation (typically c: \ wamp \ www).
    Create a subdirectory for your project and drop it your PHP files
    Click the "Localhost" link WampServer menu or open your favorite browser and go to http://localhost address

    ReplyDelete