Thursday, August 26, 2021

Wireless Sensor Network integration to cloud service for data sharing


Wireless Sensor Network integration to cloud service for data sharing



The basic idea of the prototype is to implement the basic concept of the Internet of
 Things with a basic application are specified as an example. In the prototype implemented, I adopted the concept of a smart home to be the basic building block to implement the idea. In general, a smart home can include some kind of various types of sensors, which collect any type of data and connected to a coordinator as a gateway to be in turn connected to the Internet. In the implementation, I used temperature, and humidity sensing as an example of the data collected, these data according to the previous sections taking about the Wireless sensor networks (WSN), are goatherd via a coordinator unit to share it somewhere on the Internet, either a private hosting or a
Cloud service.

The project is composed of three layers:




The Model is an implementation of the wireless sensor network can form one of basic three networks:
  •  Mesh network
  •  Star network
  • Mesh Star hybrid network


The temperature, humidity sensors form a mesh network, to make the most use of its advantages as mentioned before. An Abstract of the layers may be as follows:

Sensor Layer

Consists of two or more Wi-Fi modules, in turn, connected itself to the temperature sensors, in our case the DHT11 sensor.




Coordinator Layer

The coordinator layer consists of A wi-Fi module powered as a stand-alone unit. works as a station and access point, in turn, itself, is connected to the Internet via another access point, and it would be any type of APs as a hotspot mobile access point or a home router.


Supervision Layer

Consists of a laptop and a Mobile device connected both to the
Internet, and to the cloud platform.

Modules and Tools:

The main controllers in the circuit are the WiFi modules, they are basically considered as MCU micro control units, as we will see in the next subsections.

 Main modules:

2* Wi-Fi module ESP8266-01
1* DHT11 Temperature humidity sensor
1* PDU (Power Distribution Unit)
Different sizes of pin headers wires


Preparations

Now before we get ready to start programming the ESP-01 module, the Esp-01 has some preparations to set up before the programming process.
1.the spaces between the ESP-01 pins are 2mm
So for a solution, I built an adaptor to make it adjustable on the breadboard.






2.Second issue is the TTL is 3.3V and our computer voltage level is 5V so it needs conversion to rise up the voltage from 3.3V-5V. We can accomplish this using USB-TTL.






Programming Process

Since I had two ESPs, my target was to effectuate the layers, as previously mentioned before
 in last sections. The first layer was the sensor layer, an ESP-01 connected to the DHT11
 sensor. The DHT11 has three pins, (VCC, GND,& Signal).



The DHT11 connection with the ESP-01 is as follows:
  1. VCC- to ESP-01 VCC to 3.3V
  2. GND- to GND
  3. Signal pin - to GPIO-02 of ESP


First, to effectuate the first layer, I considered the first ESP-01 as a Client, since its goal is to send the data collected to the cloud platform.

Using the Arduino IDE the effective function used was:
The function for setting the station mode: 
WiFi.mode(WIFI_STA);
The Function for determining whether the station is connected to the access point:

WiFi.connect(WL_CONNECTED);

Returns true when connected.

Defining Internet Parameters The Network, the ESP will be connected to

const char* WLAN_SSID = "ESP-AP";
const char* WLAN_PASS = "22446688";
IPAddress ip = localIP() ;
IPAddress gateway = (192,168,0,1);
IPAddress subnet = (255,255,255,0);
const char* hostName = "http://192.168.0.104";

To view the complete programming of Client and server ESP-01 and cloud service, 
check it on :


The WiFi model is Station and Access point. The main idea behind making it station then
 Access-point is to receive the data on a local host then pass it to the cloud API.
The operation in a pic can be presented with a client-server process.


Finally configuring the IoT platform to receive my data: The IoT platform I chose is Thingspeak because it's easy to work with actually.


The IoT platform is an open platform for the internet of things. Uses the MATLAB analysis to
analysis the data you collected,then share it to the cloud.The steps are very simple to use
 the platform,The same as using the SaaS in the cloud layer.

1.You sign up with a valid e-mail and a password.
2.Create a channel on which the data will be analyzed.
3.Set up the variables with time ,and the scale for each parameter.
 






Friday, August 20, 2021

Implementation of Convolution Neural Network

 Convolution Neural Network


Convolution Neural Network is a class of Artificial Neural Network which is a learning algorithm used in deep learning. So why CNN and what is its applications? 

CNN is mostly used in applications like :

  • Image Classification
  • Object detection
  • Neural StyleTransfare

So, for example, say you have an image of a cat and you want to build an AI that can detect that picture is a Cat, 1st thing to think about is using CNN because of its performance in image detection application and in brief you will know why.


As you have known CNN is a class of ANN that is basically an AI learning algorithm, but why will you 1st think about it and not any other learning algorithm

If you are familiar with other learning algorithms like leaner regression for example after applying it for some application and it gave you output and you retry to use the algorithm again on the same dataset and application it will give you the same output ๐Ÿ’ฏ

But if you use ANN and its family each time you use the algorithm the output is different because it has a learning rate that is different each time you enter input, that is why you choose ANN for some applications because you don't want each time you enter a cat image, for example, the AI recognize it as it did in the first time which will always be far from right.


How CNN works?

Image from Medium

Like I have mentioned earlier if you have an image of a cat and you want the CNN to identify the image as a cat, 1st thing the NN will do is to detect the edges in the image by applying a vertical or horizontal edge detection filter





So applying this filter will show the vertical edges of an image, making the darker parts more clear and white parts clear on some sides.

Applying the filter is called convoluting, and there are many types of edge detection filters like Sobel, Schorr filter, and so on. These filters have values that are fixed and they found that these values can be learned using the NN to make the detection more clear and it is done on the RGB image.







Some issue that faces CNN work is if the image is too small applying the filter will already reduce the size of an image like the image above it (6 * 6 * 3) after applying the  filter it became (4 * 4), so one solution to this problem is Padding.

Padding as the name imply is adding more pixels around the image so after applying the filter it does not shrink.

Types of Layers in CNN:
  1. Convolution
  2. Pooling
  3. Fully Connected

The pooling layer is used to reduce the convoluted output of the Conv. layer.



After applying the filters on the RGB image it becomes a bigger representation so it can reach sometimes  (3000 * 3000 * 1000) and that is another challenge of CNN already, so we use padding to reduce the representation by applying a layer of pooling.

Pooling also has other types like Average pooling or Max pooling we don't want to dive into details here just the general idea of how CNN works.

How to implement CNN to identify your friend face or to detect an object :

Here is a good tutorial on github to impelement your CNN.






Saturday, April 3, 2021

Weather prediction using Machin Learning (ML) in Python



 


With a low budget, you cant create your 1st  AI/ML project, and in this blog, I will guide you throw how to create an AI weather forecasting prototype.



General Information before we start:
I.Preface of AI
II.Machine learning 5 core stages


I.Preface of AI:

AI is a simulation of human intelligence in a machine that acts like humans and mimics their actions.

Examples for famous AI:
Sofia the robot & HAL in HAL spaceship, Self-driving cars, Google assistant
Google-powered AI- predictions, Ridesharing App.s Uber & Careem...etc

There are three main types of AI:

1.Narrow AI:-
Narrow AI is mainly designed to perform one task at a time, which is referred to as ''weak AI'' also due to the fact that it focuses on one task, to simply improve something that already works, but can work better performed by AI.
Examples for it, probably all the existing AI systems.

2.General AI:-
General AI or as it's called Strong AI; is the purpose we still conduct research on AI, it's designed to think like humans and make decisions on its own without previous training, in other words, the machine teaches itself by itself. 

3.Conscious AI:-
Artificial consciousness (AC), also known as machine consciousness (MC) or synthetic consciousness, is a field related to AI and cognitive robots. The aim of the theory of artificial consciousness is to "Define that which would have to be synthesized were consciousness to be found in an engineered artifact".[1]

How to create your AI prototype:
1.Define the problem you want to solve using AI
2.Design and build your system, you can start with mind mapping your idea.
3.Collect your data and make sure it's enough, alongside downloading the dataset to improve your training.
4.Prepare your data
5.Decide the form of output from your AI system.
6.Choose the right algorithms to train your model.
7.Don't forget to be economizing as much as you can.๐Ÿ‘€
8.Choose an easy IDE to start programming, and of course an easy programming language.

...Ok after doing all of that how will you start? 
before we get to the exciting part ๐Ÿ˜ you shall know how will you train your dragon ..oops your model๐Ÿ˜…

There are four types of learning:
1.Supervised
2. Unsupervised
3. Semi-supervised
4. Reinforced

1. Supervised Learning:
Is done by exposing the AI to data it's output is already known.
Your algorithm uses the output to decide either the answer is right or not.

2. Unsupervised Learning:
This type of learning is used to teach an AI with data that you don't know its output.
By exposing the algorithm to the data and let it decide what it sees by itself.

The system is not given a pre-determined set of outputs or correlations between inputs and outputs or a “correct answer.”

3. Semi-supervised Learning:
This type of learning falls between the 1st two types of learning and the learning process is done by using a dataset that half/some of it have a predetermined output and the other half doesn't have a pre-determined output.

4. Reinforced Learning:

It's a type of dynamic programming that uses reward and punishment as a learning technique. This learning algorithm learns only by interacting with the right environment,  It receives rewards by performing correctly and penalties for doing so incorrectly.

๐Ÿšฉ Mostly the learning type that is used is Supervised learning because we still didn't reach the strong AI that can teach itself by itself.
So Let's get to the model,...๐Ÿ˜ƒ
The model target is to create a weather forecasting AI with different algorithms and compare the results.

II. Machine learning 5 core stages:




1. Get Data/Data collection:
Whatever means you choose to get your data you are free, also as I said before it's better to add another dataset pre-downloaded from websites that makes their dataset available for geeks ๐Ÿ˜

If you want to know more about datasets and what they are and where to find them 
check DataQuest.

2. Clean & prepare your data/ Preprocessing:
Data preprocessing is the most basic important step actually in the whole process of creating your AI. Preprocessing means you prepare your data for the next step which is training, so what do you need to do?... well you start by eliminating the parameters you don't need in your dataset, then..import the libraries you will use, after importing libraries you need to also import the dataset as a CSV or "comma-separated value" then define the dependent and independent variables in our dataset, mostly they are referred to as X & Y. And then you start identifying and handling the missing values the last step is encoding the categorical data, which the information that has specific categories within the dataset.

3. Train your model:
Training your model is basically the core of the AI learning process, as the more you train and test differently each time the more intelligent AI you get.
And by train and test differently each time I mean, you feed different input data to your model. In this way, your model will build a learning pattern or better say thinking pattern finally the prediction of your model will be the closest to the correct value.

Training is done by feeding your input data to your algorithm to get an output.

4. Testing model:
In the testing stage, you start to give your AI model a new input and see how correct the answer will be if the answer was correct then the model is complete and your mission is accomplished.

5. Improving the model:
After all, an AI is a mimic of the human brain, it needs to be trained more and tested on new projects and ideas to improve its performance.

Also, there are more useful ways to improve the AI model accuracy I recommend you look at this useful article.

Ok so this project is composed of two parts, 1st the hardware part where you collect your data, and the 2nd part is where you connect it to the AI.

this is a simple block diagram of the hardware:

______________________________





How can you plan it
  1. The temperature/Humidity sensor will read the data which is the two physical parameters Temperature & Humidity.
  2. Node MCU will read it, where I can read them and create a dataset for a      good period of time like for a month.
  3. Save the dataset in an XLs sheet and then to programming the AI.  Or as mentioned before you can train using a separate dataset.
  4. The IoT platform I used is Thing Speak.
  5. AI model sends a request to Thing Speak to get the last reading, then make the prediction.



Hardware part:
It's the easiest part, you will start with connecting the DHT sensor to the node
 
Image from: "Electronics wings"

  • VCC- 3.3 VCC
  • GND-GND
  • data - D2 "Any digital pin you wish"
๐Ÿ’ฌThen you can start programming using whatever IDE you prefer if you are familiar with programming node MCU and sensors, and if you are not I recommend you start with Arduino IDE it's easy peasy lemon squeezy.

Programming The Node MCU (WiFi module)
For programming Node MCU there are different tools and languages to program it, but the easiest is Arduino IDE as it's user-friendly.

code explanation on github.


The most important part of the sensors reading is sending the post request to Thing Speak the IoT platform, for more details about how to create your channel see this link๐Ÿ‘‡ 

To send a request to a website you either GET data or POST data,
a request is composed of three main lines:
"as I am writing in  C language"

  1.     client.print("POST /update HTTP/1.1\n");
  2.     client.print("Host: api.thingspeak.com\n");
  3.     client.print("Connection: close\n");
1st line contains what is the purpose of your request and that is more than a GET or POST request it can be more than this you can read more about it under "Web requests". then the IP version 1.1.

2nd line is the hostname
3rd line is what do you want to do with your connection after the request is complete.


AI Model programing
For programming the AI model I chose JupyterLab it's based on Jupyter Notebook, I also used conda prompt as the package manager, it's one of Anaconda project tools.

For installing any of these tools the links are inserted above๐Ÿ‘†.
So let's install the libraries we need,

you will need these libraries for your model to complete the 5 main stages,

  1. Pandas, for data manipulation.
  2. Numpy, for working with arrays.
  3. Tkinter, for building the GUI.
  4. Sklearn, for the training algorithms.
  5. Matplotlib, for plotting the results and it's based on MatLab.
  6. Thingspeak, to get the latest reading from the sensors.

Let's talk a bit about how will you code,

1st you have to import the libraries we mentioned before, but more specifically you have to know what training algorithm you will use,
like if the task you want your AI to do is a continuous predictions you will use a regressor algorithm like predicting weather or annual earning or in Gaming, but you have to use classifier algorithm for finding/predicting the label of the class.

I used the Ensemble training algorithm for the better prediction,
basically ensembling technique is based on using mutible training algorithm and making a better prediction.

Ensemble technique capable of performing both regression and classification tasks with the use of multiple decision trees and a 
technique called Bootstrap Aggregation, commonly known as bagging.
*Bagging is training each decision tree on diffrent data sample.

Algorithms I used for ensemble,

1.Decision tree regresion.
2.K-neerest neighbor (K-NN) regressor.
3.Randome Forest Regression.
4.Linear regression.

Connecting AI model to IoT platform 
As mentioned before you have to know what is the read and write keys to your channel on ThingSpeak, in the code
you have to define the read key of your channel and import ThingSpeak liberary in python code.

Complete Code on Git Hub:

















Wireless Sensor Network integration to cloud service for data sharing

Wireless Sensor Network integration to cloud service for data sharing The basic idea of the prototype is to implement the basic  concept of ...