Introduction: Serial Data From Arduino to Raspberry Pi to MySQL Using CoAP Protocol
Introduction to Project:
In this tutorial I'm going to tell you how we can get the sensor data from Arduino Uno and sending it to the Raspberry pi through UART. Mostly We are going to do two important things. First we will use a CoAP server For making a GET request which will run on raspberry pi and in our CoAP browser we will get the DHT22 data immediately. And the second thing is we are going to make a mysql database in raspberry pi to store the value of DHT22 sensor.
Step 1: Installation of All Software
1) Raspbian OS:
This is the recommended os for raspberry pi. You can also installed other OS from third party. Raspbian OS is debian based OS. We can install it from noobs installer. you can Download here
2) Python idle:
This is the software we get in raspbian os. For this project we have used python script.
3) Arduino IDE:
You can download the latest Arduino IDE from this link:
4) Mozilla Firefox:
This is used to get the sensor data in copper which is extension of Mozilla Fire Fox. Here is a link: https://addons.mozilla.org/en-US/firefox/addon/cop...
This software is used to get data in our Smart Phone. Here is a link:
Step 2: Component Used:
1) Raspberry pi 3
2) Arduino UNO
3) DHT22 Sensor
1) Raspberry pi 3:
This is the latest version of raspberry pi. In this we have inbuilt Bluetooth and wi-fi, unlike previously we have to use Wi-Fi dongle in one of its usb port. There are total 40 pins in RPI3. Of the 40 pins, 26 are GPIO pins and the others are power or ground pins (plus two ID EEPROM pins.)
There are 4 USB Port and 1 Ethernet slot, one HDMI port, 1 audio output port and 1 micro usb port and also many other things you can see the diagram on right side. And also we have one micro sd card slot wherein we have to installed the recommended Operating system on micro sd card. There are two ways to interact with your raspberry pi. Either you can interact directly through HDMI port by connecting HDMI to VGA cable, and keyboard and mouse or else you can interact from any system through SSH(Secure Shell). (For example in windows you can interact from putty ssh.) Figure is given above.
2) Arduino UNO:
Arduino/Genuino Uno is a microcontroller board based on the ATmega328P (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button.
3) DHT22 Sensor:
The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old.
Step 3: What Is CoAP
Constrained Application Protocol (CoAP) is an Internet Application Protocol for constrained devices.
It enables those constrained devices to communicate with the wider Internet using similar protocols. CoAP is designed for use between Devices on the same constrained network, between Devices and general nodes on the Internet, and between Devices on different constrained networks both joined by an Internet.
CoAP is designed to easily translate to HTTP for simplified integration with the web, while also meeting specialized requirements such as multicast support, very low overhead, and simplicity. CoAP supports the basic methods of GET, POST, PUT, DELETE, which are easily mapped to HTTP.
Now here above was the brief description of CoAP. As I told you that you will get a lot of things about CoAP but as far as interfacing with real hardware is concerned there are very less stuff. So that's why I am going to tell you about this CoAP server and how we can interact with real hardware .
Step 4: Procedure to Run CoAP in Raspberry Pi
1: In this step we will tell you that how we can run a CoAP server in our raspberry pi.
In this tutorial, we will describe how to run a CoAP server on Raspberry Pi, and run the CoAP client on a PC. The CoAP server and clients are implemented using txThings, which is a Phyton implementation of CoAP.
txThings - CoAP library for Twisted framework
txThings is a Python implementation of Constrained Application Protocol (CoAP):
txThings is based on Twisted - asynchronous I/O framework and networking engine written in Python.
txThings uses MIT License (like Twisted itself).
Copyright (c) 2012 Maciej Wasilak
txThings Python Library Features
txThings has the following features:
support for draft-ietf-core-coap-13 - including automatic piggyback/separate response handling. No caching support. support for draft-ietf-core-block-12 (no support for server initiative though - waiting for the resolution) limited support for RFC6690 (Core Link Format) - server only.
Other nice things:
txThings works nicely on RaspberryPi txThings is compatible with Kivy - brilliant new Python GUI library (I'll post some examples soon). txThings is fully asynchronous (thanks to twisted framework)
txThings Installation HowTo
txThings is posted on Github. The easiest way to get it is to clone the repository to your local machine using the command below:
git clone git://github.com/siskin/txThings.git
Library contains CoAP code (inside "iot" directory) and three examples:
server.py - CoAP server that starts on localhost, port 5683 and hosts several resources client_GET.py - example client which performs GET request to localhost, port 5683 client_PUT.py - example client which performs PUT request to localhost, port 5683
Client_GET and client_PUT both use port 61616 - to use them simultaneously change port number in one of the clients. Server will send blockwise responses for default settings. To use txThings you need Python 2.7 with Twisted installed (I suggest using the latest Twisted version, but older releases also work - tested with 11.1).
Setting up Raspberry-Pi
1. Install OS on Raspberry Pi (set the appropriate raspi-config for all,including ssh and desktop boot)
2. Install setup tools for python (such as pip)
3. Install Twisted on Raspberry Pi
sudo apt-get install python-pip
pip install twisted==15.1.0
4. Install txThings on Raspberry Pi
pip install txThings
2. Now this is the time to move on Arduino. Here I've connected DHT22 sensor to an arduino and our arduino is connected to our raspberry pi(which is working here as an IOT gateway or server ) through USB cable to one of its USB port. But before going to connect Arduino to our raspberry pi we have to put a Arduino snippet for DHT22 interfacing to our arduino. Since we are using Raspberry pi remotely in ubuntu.
Connect your arduino to your PC to upload this code(Arduino code) Now just unplug your arduino from PC and connect it to your pi
3. Now we need to install some of the things to get data in raspberry pi
First one is Python-serial and then python-mysqldb
Now This URI we have to type in Mozilla Firefox
caop://<Raspberry Pi IP Address>:5683
now you will get the figure like above
for that we need to do a little modification in our server.py code which we got while installing txThings.
You just check your server.py code and edit accordingly. Here some of the things I highlighted
Now we need to run our server.py code
It'll take a little time
Now after clicking in GET tab here(Figure 2)you can see the temperature and humidity in Copper on firefox
Run Copper on Firefox
The Copper (Cu) CoAP user-agent is an add-on for the Firefox Web browser. It allows browsing, bookmarking, and direct interaction with CoAP resources. Simply enter a CoAP URI into the address bar.
Step 5: Getting Data in Aneska
For getting the data in our smartphone we need to install Aneska. At the end section of the video what we have posted in last step you can see how we can get data in our smartphone.
In above figure you'll see in your smartphone when you will get the sensor data
Step 6: Creating a Database of DHT22 Data
For that you have to install Apache
Mysql and php and then save data in phpmyadmin.
You can easily get about these installation in many links. So I'm not going to demonstrate that. In short you have to set up a LAMP server in your raspberry pi. I am assuming you have install these necessary things.
So Let's get started
Go to localhost/phpmyadmin or /phpmyadmin if you are using pi remotely.
Create Database give it some name then create table for that
Here I've made 6 columns. For getting the data in database from arduino serial out we have to write a small script in pi. This is the script(You can find all the files in our github page)for getting data in mysql datbase from arduino. If I run this script I can see the updated data in database.
But if we want this thing automatically let's say for every single minute then we have to edit crontab -e
Just paste this on your crontab -e
/1 * * * * python /home/pi/databasearduino/insertdb.py
Step 7: Project Code and Video
Code of this project is given below or you can download source code in GitHub link
In above video you can watch it out in action of this project .
If have any doubt regarding this project feel free to comment us below or you can mail us on firstname.lastname@example.org
And if you want to learn more about these type of project then feel free to visit our youtube channel
Thanks & Regards,