Step-by-step Tutorial: A simple IoT solution to collect your environmental data

A step-by-step tutorial that anyone can build easily.

In this day and age, there are so many surveillance systems out there in the market that can help you track your environment no matter where you are. Their main problem, however, is usually the price. After all, equipments that have functions such as Internet access, making calls and tracking various parameters of the surroundings are very complicated.

But what if someone tells you that even a student can create such a system and make it work? Surely many would find it a joke. But in this article I will show you that it is much easier than you think it is.


The key idea of this post is to demonstrate that even ordinary things can be used in more complex and unique situations than you think.

Here I will use some temperature and humidity sensors as an example, and you absolutely any module can be connected in the same way as shown.

For example, I have only the Grove Beginner Kit for Arduino, which only costs you 30 dollars. However, if you feel that you do not need all the sensors included, you can limit yourself to buying a Seeeduino Nano (if you need only one Grove port) for 10 dollars and a Grove Temperature and Humidity sensor for 6 dollars. In addition, you will need a computer (in my case it is Windows, but in theory C # will work everywhere). A paid (or at least free) web server is also required.

What for? Because the data about, for example, a cat lying on the temperature sensor, must be displayed somewhere. And which device does everyone always have with them? Of course, its our smartphones. And which application is always active and notify you quickly? Of course it is our messenger apps.

I chose Telegram as it is my main messenger, and secondly because writing bots for it is very simple.

There are 4 main steps to building this, which are namely:


1. Hardware


Seeeduino Lotus with Temperature and humidity sensor

Everything is simple here – we just need to connect the sensor to the DIGITAL port (yes, it matters) using the Grove cable. That’s all.


2. Seeeduino software

We need to make a sketch that would send data to the serial port of the computer via USB. This is the simplest option.

Here is a well-commented code for you.

#include "DHT.h" //connecting library
#define DHTPIN 2 //setting variable
#define DHTTYPE DHT11 //selecting our type
DHT dht(DHTPIN, DHTTYPE); //putting it together

void setup() 
{
    SERIAL.begin(9600); //connecting to serial port @9600 baud
    Wire.begin(); //connecting sensor to board
    dht.begin(); //inititating sensor
}

void loop() 
{
    float temp_hum_val[2] = {0}; //creating massive with 2 items
    if(!dht.readTempAndHumidity(temp_hum_val)){//if sensor is online
        SERIAL.print("Humidity: ");    //output mark
        SERIAL.print(temp_hum_val[0]); //output data
        SERIAL.println("");            //separator
        SERIAL.print("Temperature: "); //output mark
        SERIAL.print(temp_hum_val[1]); //output data         
    }
    else{ //if sensor NOT online
       SERIAL.println("Failed to get temprature and humidity value."); //saying "oops"
    }
   delay(3600000); //waiting ONE WHOLE HOUR (1000ms*60s*60m)
}

3. PC Client app

For me, this is probably the hardest part in the whole project. I have been writing bots in Telegram for a long time and have been familiar with Arduino programming, but this is the first time that I used C #. What was new to me was how to connect to the server from the command line and receive data from the serial port not through the Arduino IDE. No, probably the opposite – first get the data and only then send it))

Here are just some code with comments. Feel free to copy it, edit it, use it.

using System;
using System.IO.Ports;
using System.Threading;
using System.Net;
using System.IO;
namespace IoT
{
    class Program
    {
        static SerialPort _serialPort;
        public static void Main()
        {
            Console.WriteLine("Started");//boot message
            Console.SetWindowSize(16, 1);//do I need a bigger window? :D
            Console.SetCursorPosition(0, 0);//we must see the text
            _serialPort = new SerialPort();//initiating
            _serialPort.PortName = "COM3";//your port may wary
            _serialPort.BaudRate = 9600;//haha, classic
            _serialPort.Open();//listen! listen it!
            int i = 0;//let`s make a incremental counter
            while (true)//endless, brainless
            {
                string a = _serialPort.ReadExisting();//this is our output from seeeduino
                if (a != "")//we must not do a request every second, only if we have new data
                {
                    string link = "https://projects.HumanZ.space/bots/bugbot/bot.php?seed=IoT&text=" + a;//ask me if you want to see how it works with me
                    i++; //connections: one more
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);//sending
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();//we don`t use it
                    Stream resStream = response.GetResponseStream();//useless too
                    Console.Clear();//cls
                    Console.WriteLine("Connections: " + i + ".");//notify user that we are alive
                    Console.SetWindowSize(16, 1);//again
                    Console.SetCursorPosition(0, 0);//again
                }
                Thread.Sleep(1000);//wait 1 s
                
            }Console.ReadLine();//somwhy, withou it my app crushes - but VS says that it is bad code(
        }
    }
}

4. Server software

The problem with this section is that it makes no sense to describe the building of a bot and giving its full code here; there are lots of manuals with detailed descriptions on how to create bots on the Internet, and the information on the bot will probably take up half of the article despite the fact that the function itself takes only a couple of lines. Therefore, I will show only a short excerpt here. By the way, I wrote entirely in PHP and did not use any libraries.

$seed=$_REQUEST[seed];//collecting a product name
$text=$_REQUEST[text];//collecting a message
if($seed!=null&&$text!=null){//if we have both of them
    apiRequestJson("sendMessage", array('chat_id' => -1001329261925, "text" => "Отзыв из $seed:
$text"));//send a request to our special chat
}

So…

If everything is correct, the cat did not cut off the Internet cable and the server on the network is working, a notification will come to the selected channel. It looks like this:

Sorry for unknown text – it’s russian. My language)

Here are a couple of tips. Do not start the application before you plug in the board – it will crash instantly. Also, do not try to upload the firmware to the board while the application is active – the Arduino IDE will give an error, because this channel is busy.

Last but not least – I do not advise setting the update frequency more often than half an hour for such projects. After all, no one needs annoying notifications, right?

Keep moving forward, Makers!

About Author

Calendar

September 2019
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30