Realtime Temperature Monitoring System using Raspberry Pi

Realtime temperature sensing is one of the common requirement. There are a lot of digital thermometers and temperature monitoring devices available in online shopping sites. But most of them just monitors and displays the realtime values. It does not have any intelligence.  The one we are going to build is a smart temperature monitoring system. This system can be used for monitoring atmospheric temperature as well as liquid temperature.

The following blog post explains the set up of a digital temperature monitoring system.

Digital Temperature Monitoring System

We will enhance the above system by adding analytical capability. So that we can analyse and show the temperature trends. The block diagram below shows the high level architecture of the system.


As shown in the above diagram, the system has three blocks.

  • Edge Device & Sensor (Raspberry Pi & Sensor)
  • Data Storage Server
  • Dashboards for the end user

The following are the software components required for this project

  • MQTT for sending the data from the edge to the server.
  • PostgreSQL for storing the data in the server.
  • Python based backend
  • HTML Web UI

I am not going to explain the working of MQTT in this blog post. This was already explained in one of my earlier posts.

Before we start implementing the solution, lets summarize the story line.

  • The requirement is to perform realtime temperature monitoring and analyse the trends & patterns using the historic data.
  • A temperature sensor is attached to a Raspberry Pi which acts as the edge device.
  • Need provision to support multiple edge devices.
  • Capability to monitor the temperature from anywhere

Bird’s eye view of the system

Here we have considered multiple edge devices and also considered the provision of web and mobile application.


Data Model Design

In the PostgreSQL database, we need two base tables for storing the data. We will be able to store data from multiple edge devices located at different locations using this data model. This is a very basic data model. We can enhance this based on our requirement.

  • device_info – This has the metadata of the edge devices. This includes the location details of the device. The column names are given below
    • device_id, device_name, location
  • temperature_data – We store the temperature data from each of the edge devices in this tables. The column names are given below.
    • device_id, timestamp, value

Now let us start developing the application from the edge device. We will modify the program to send the messages to an MQTT topic with the timestamp. The temperature readings will be sent to the server once in every minute. The message format will be as follows. We will be using epoch timestamp in seconds and temperature in Degree Celsius.

{"device_id":"xxx", "timestamp":1584284353, "value": 27.01}

Now lets develop a small python program that send this values to the MQTT topic. For this, we need an MQTT broker to be up and accessible from the Raspberry Pi

Here my central server is a CentOS 7 server and I will be using mosquitto MQTT. The installation steps are explained very detailed in this blog post.

In the central server, these messages will be collected and stored in the database tables.

A sample view of the temperature_data table is shown below.

device_id timestamp value
device_01 1587825234 27
device_02 1587825234 28
device_03 1587825234 23
device_04 1587825234 28
device_05 1587825234 30
device_06 1587825234 26
device_07 1587825234 22
device_08 1587825234 28
device_09 1587825234 32
device_10 1587825234 29
device_11 1587825234 31

Now from this table, we can query and get the required information based on the user requirement. We can either develop custom visualization using javascript or we can query the DB using workbenches or we can even connect & visualize data using visualization tools like Apache Superset, PowerBI etc.


With this I have explained the highlevel architecture and implementation of a sample IoT system. This system can be scaled further by using a proper time series database instead of the Postgres DB.

Green House Farming

What is Green House Farming ?

Green House Farming is a technique in which the crops are cultivated in a controlled enclosed environment. This is basically to control the effects due to whether changes.

Advantages of Green House

  • Plants will get an environment with consistent temperature and Humidity
  • Plants will be protected from birds and other organisms
  • The moisture content in the soil will not evaporate easily
  • Easy to control pests
  • Easy to maintain the fields
  • The environment will not get affected because of the external weather

The picture of one of the greenhouses that I visited in the recent past is shared below.


Rose plants are cultivated in this Green House. The plants are planted in well arranged lines and drip irrigation is established across the plantation.

The newly formed rose buds are wrapped with a net protector to maintain proper shapes and protect the buds from other damages. These nets will ensure controlled development during budding. A high quality rosebud will be large in size (long bud with a well formed, heavy base). If you observe closely, you can see these nets in the buds present in the above picture. A sample image of the rose bud net is posted below.


Role of Automation & AI in the modern Agriculture

Food is one of the fundamental need of any living organism. For humans in the current age, food is something that has to be purchased from the shops. Once humans became civilized, the freely available food became a commodity product. The idea behind work and salary was all for food in the earlier ages. Later it was for food, medicine and shelter. Now the priorities has changed and we all are running behind the fast moving world.

Now everyone are busy. Getting clean & good food is very difficult.

  • Vegetables are full of pesticides & chemicals
  • Meat is poisonous with antibiotics
  • Rice & grains contains harmful chemicals. People even uses plastic like materials for manufacturing similar materials
  • Water and water bodies are polluted
  • Air is polluted
  • Soil is polluted

Everything is polluted….!!!

In general, the food that we eat is not good for health and it may even take our life. What is the solution ?.

People including me are busy with their work and they don’t have time to do any cultivation. I came from an agricultural background and I have farm lands also. But now everything is unused. Now the food production is getting reduced drastically. It is basically because of the following reasons.

  • The cost of farming is high.
  • No steady income.
  • Lack of stability in the price of agricultural products and market.
  • The major share goes to the middle man
  • High labour cost. Most of the conventional farming needs more man power and the cost of man power is more in the current society.
  • Poor social status.
  • Dependency on climate and the sudden climatic changes.

Because of all these factors new generation is not even thinking about agriculture. I also moved away from agriculture because of all these factors.

Currently I stay in an apartment surrounded by farm lands. When I observed the cultivation process and the deadly harmful pesticides that they use, I got shocked. None of these vegetables are in an eatable condition. I started thinking and researching about the ways to control this.

One of the images that made me re-thing is shared below. The picture shows two oxen with their mouths masked. The reason for the mask is to avoid them eating the cabbage leaves in the farmland while ploughing the fields. Those cabbages were sprayed with highly poisonous chemicals.


One of my goals in this year is to start an organic farm. Automation is required to improve the efficiency and reduce the human effort. Now my blog posts will include the updates and progress about my organic farm and my learning.

What is MQTT and Where is it used widely ?

MQTT is a very light weight protocol used commonly in sensor communications. MQTT stands for MQ Telemetry Transport. Unlike other messaging protocols, this is very simple, light weight and requires only very low bandwidth. Because of these features, it is widely used in sensor networks. Now with the emerge of Internet of Things, MQTT became very popular. Since it is very light, it consumes very less power and is ideal for low power applications. MQTT messages are delivered asynchronously through a publish-subscribe mechanism. The message packets are specially crafted in such a way to reduce the data size.

The basic communication model in an MQTT system is shown below.


Common terminologies in MQTT

Publisher – The one who publishes the messages. Usually the sensors emits the messages. For example in a temparature monitoring scenario, the temparature sensor emits the temparature frequently and the values are displayed in the mobile application. Here the sensor is publisher and the mobile application is the subscriber.

Subscriber – Subscriber is the one who subscribes the messages.

MQTT Client – A client can be a subscriber or publisher. A single client can act as a publisher as well as subscriber. MQTT client libraries are available in almost all programming languages. The complete list is available in MQTT wiki.

MQTT Broker/Server -All the clients are connected to the broker. This is the heart of the MQTT system. Depending upon the implementation, a broker can handle thousands of concurrently connected MQTT clients. The broker needs to be configured in such a way that it is accessible to all the clients. A client can be configured within any NAT network, but the broker needs to directly accessible from all the clients. There are several broker variants available. Some of them are Mosquitto, HiveMQ, Mosca, emqttd etc. Based on your choice and your application needs, you can choose one among these.

Topic – This is a UTF-8 string used by the broker to filter the messages for each connected client. There can be N number of topics. Topic is case sensitive.

MosquittoMQTT is one of the widely used MQTT servers. I have used Mosquitto in many projects. This can be easily installed in Linux and Windows.

By default MQTT uses TCP port 1883 for communication and for SSL enabled communication, it uses port 8883. These are configurable values and it can be changed.


The messages can be secured with username and password. Communication with SSL is also possible. Apart from these, there is no advanced security mechanism present in MQTT. But we can have our own custom message encryption/decryption logic.

There are a lot to explain about MQTT. Instead of writing more, I feel it is better to demonstrate the working of a real implementation. I will be publishing a post on the real implementation of MQTT soon.

Raspberry Pi 3 released.

Raspberry Pi 3 got released this week. Still remembering the moment before 3 years, my colleague introduced me about this magic device. Then I googled about this device and read the details. I am using this device from the past one year . I have used Raspberry Pi B+, Pi2 and now waiting for the delivery of Pi3 from element14. It is one of the superb devices that I have ever used. I can use my electronics knowledge and computer science knowledge using this. Now the Pi3 came with builtin wifi and bluetooth module so that it can be connected to a wifi or a bluetooth device without any external peripheral. Physical appearance is same as that of Pi2, but this version is more powerful. Now we can say Good Bye..!!! to wifi adapter modules. This will be a very big hit in the IoT market. Expecting more wonders from element14. 🙂

Booting Raspberry Pi2 for the first time

Today I and my colleagues were trying to install Raspbian OS on Raspberry Pi 2 B version. The steps that we followed are explained below.
Before explaining the steps, I am listing down the hardware and softwares that we used.

  • Raspberry Pi2 B version
  • NOOBS 1.4.1 Offline and Network Installer (Release date 2015-05-11).
  • Transcent 16 GB Class 10 memory card
  • Raspberry Pi charger (We used a 5Volt 2A charger)
  • Memory Card Reader.
  • SD Formatter (
  • Windows Laptop
  • Monitor
  • HDMI Cable

We used the steps mentioned in the raspberry pi website for reference. There is an excellent video that explains each and every step for the setup.

The steps we followed are listed below.

  • Inserted the memory card into the Card reader and plugged into the windows machine.
  • Formatted the memory card using the SD Formatter with “FORMAT SIZE ADJUSTMENT” set to “ON”.
  • Copied the NOOBS offline installer zip file to the formatted memory card and extracted it.
  • Unplugged the card reader and put the memory card into the Pi2.
  • Supplied power to the Pi2 which is connected to the monitor using HDMI Cable
  • The Pi2 booted up. We have to select the OS to be installed.
  • Select one OS. Now the OS will be installed and your pi will be ready to use.

We faced an issue while performing the installation. The Pi2 was not booting up. Finally after some struggle, we figured out the issue. We used a phone instead of a memory card reader for connecting the memory card to the computer for formatting. Because of this, the Pi was not booting. We figured out this issue from the status of the ACT LED (Green LED). The green LED should blink in case of normal operation. If the green LED is not blinking, means the Pi is not reading the SD card. In our case the green LED was not blinking. So we started investigating and reviewing the steps that we followed for the SD card set up and finally we found the culprit… Our Mobile Phone . So my suggestion is to use a card reader. Never use any camera or mobile phone instead of card reader. This may unnecessarily waste your time and you will not get any clue of the error also.

Wifi Adapter Set up for Rasperry Pi

For connecting Raspberry Pi with internet, we have three options, one is through LAN cable, another is using a supported internet dongle and the other way is to using a wifi adapter. Here explaining about the steps that I did for configuring a Netgear N150 WNA1000MV2 wifi adapter with Raspberry pi. Raspberry pi supports lot of wifi adapters. The list of the hardware supported by raspberry pi is available in this url. I had one Netgear Wifi adapter (N150 WNA1000M) and it was working fine without any config changes. Because of that reason, I bought another wifi adapter with the same model number from an online shopping site. But when I unpacked it, I realized that they upgraded the product and a new version of the same product came with model number WNA1000MV2. When I connected it with the pi, it was not working. My raspberry pi OS was Raspbian.



A portion of the output of dmesg command that contains the details of the wifi adapter is shown below.


[ 3.401856] usb 1-1.2: new high-speed USB device number 4 using dwc_otg
[ 3.523552] usb 1-1.2: New USB device found, idVendor=0846, idProduct=9043
[ 3.532493] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 3.541771] usb 1-1.2: Product: WNA1000Mv2
[ 3.547744] usb 1-1.2: Manufacturer: Realtek
[ 3.553858] usb 1-1.2: SerialNumber: 00e04c000001

To make this working, we have to do some modifications.
Add the following contents to the /etc/rc.local file

modprobe 8192cu
echo "0846 9043" > /sys/bus/usb/drivers/rtl8192cu/new_id
ifdown wlan0
ifup wlan0

Then add the following contents to /etc/modprobe.d/netgear_n150.conf file

install 8192cu /sbin/modprobe --ignore-install 8192cu; echo "0846 9043" > /sys/bus/usb/drivers/rtl8192cu/new_id

This worked perfectly for me. Hope this will help..!!!

Python program to find the distance between two GPS locations

GPS is a very important technology that we use in our daily life for navigation, finding distance between two places, estimating the time for journey etc. Now Internet of things is getting hotter in the market and lot of people including college students are doing POCs with ideas related to IoT. If we are developing an application that includes GPS, finding the distance between two places is a common requirement. The program given below finds the straight distance between two GPS coordinates. Here the distance is calculated by assuming earth as a circle with radius 6371 kilometers. If you want the distance in miles, use 3956 miles as the radius of earth.

A Small experiment using GPS module

Full form of GPS is global positioning system. GPS is one of the very superb inventions that help us very much. GPS finds the position by making satellites as reference. It is accurate upto some meters. Now a days almost everyone in the world are using gps for navigation. Here I am sharing my experience with a gps module. GPS modules are available in electronic shops. The picture of a gps module is shown below.

gps1The gps unit that I bought is having an RS232 as well as a TTL interface. We can connect this device to a computer, raspberry pi or any micro-controller very easily. I connected and did the experiments using this device on raspberry pi, laptop and micro-controller. Here I am explaining a sample python program that will find the coordinates using this gps module. I used a python library called pynmea for reading and parsing the data returned by the gps module.

First power up the gps module. This depends on the type of gps module that you are using. The module I used supports 12V and 5V input voltage. Then connect the gps module to your computer using the RS232. If your laptop or PC is not having the RS232 port, don’t worry, you can use an RS232 to USB converter cable. Check the serial port in which this device is connected (This can be found from the device manager). Before we start writing the program, ensure that the pynmea library is installed in your computer.

First we will check whether the device is working properly. So we will write a small python program to read the data from the serial port.

__author__ = 'Amal G Jose'

import serial
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM5'
while True:
    print ser.readline()

In my case the device was connected to COM5 port. Change the port accordingly and execute this program. If you are getting values in the console, then you are good to go.

Then execute the following program to parse the data and find the coordinates.

Some keys do not produce the correct characters in Raspberry Pi

I have a raspberry pi B+ with raspbian OS installed. In this set up, most of the keys were not producing the proper characters. Among the characters, ‘|’ was the character that I missed a lot, because I was not able to do any grep in the linux command line. I tried re-configuring the keyboard, but that didn’t helped me. Then I found the solution.

I changed the keyboard layout to US.

The command for doing the same is given below.

setxkbmap -layout us

If the above solution is not working, you can manually edit the following file and change the keyboard layout. Change the layout to US (use small letter).