Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

Weather Station IoT

Posted on March 24, 2018March 24, 2018 by Yılmaz Cemalettin

 

PROJECT: Weather Station IOT

Introduction

In the beginning, I try to learn , what is the meaning of IOT and what can do with use this technology. My supervisor give me the topics for learning coding for IoT system. In my projects, I have used these technologies.

Nodemcu ESP 8266:

  • MQTT client
  • Sensor Coding: Rain, Ldr, Pressure, Temperature, Humidity

Node.js:

  • MQTT client
  • Database using MongoDB
  • Chart.js
  • Socket programing with Socket.io

Raspberry Pi 3:

  • MQTT server

Hardware Part

In my projects, I try to use cheap and useful components. For example, NodeMcu board is more useful than Arduino Uno for this projects also It have wifi module with interval and cheaper than Arduino Uno.

Equipment

  • 4 Nodemcu Esp8266
  • 1 Raspberry Pi
  • 2 Rain Sensor
  • 4 LDR (Light Sensor)
  • 2 BMP 180 (Pressure Sensor)
  • 4 DHT11 (Temperature and Humidity Sensor)
  • 4 Buzzer
  • 4 Led
  • 3 10k Resistor
  • 2 Diode 1N4001 1A 50V
  • Jumper
  • 3.7 V Battery

Nodemcu Esp8266

 

Nodemcu is have own WiFi module and also have 1 Analog I/O and 8 Digital I/O. Also Nodemcu allows deep sleep system with using timer. The NodeMCU is a development card have the ESP8266 WiFi module with the firmware installed. Developed using the ESP8266 SDK, the Extra supports GPIO, PWM, IIC, 1-Wire and ADC connections without the need for a microcontroller. On the CP2102 USB – Serial converter is integrated. I used this module for communication each other and server.

Raspberry Pi 3

I used raspberry pi for MQTT server and Node.js server. Those server only work in localhost.

Rain Sensor

When this sensor panel be wet resistivity is increase and therefore current is decrease I read these current value and in the Arduino IDE I convert to value my own style. I read analog value with using Nodemcu analog input part.

LDR

When light shows up on the LDR resistivity is decrease so current is increase and I read the current with Nodemcu analog input port and after limiting this value(output) I use these value for Light’s value

DHT11 (temperature and humidity sensor)

I used these sensor with own Arduino library code and these sensor have Analog output

Bmp180 (pressure sensor)

These sensor also have Temperature sensor. I use these sensor with own Arduino library code. Also I calculated altitude with pressure value.

Battery

C:\Users\cceem_000\AppData\Local\Microsoft\Windows\INetCache\Content.Word\WhatsApp Image 2017-08-29 at 10.03.09.jpeg I use this battery with own charger module. I removed this battery to my own power bank. This battery voltage 3.7 V and 2500MaH and this is good for this project.

Circuit

I design to circuit like bellow figure. Also I add to buzzer and Led for some warning. For example when Nodemcu connect internet Buzzer work 1 seconds and try to connect to MQTT server when It connected Buzzer works 3 times in 6seconds. If not connect server buzzer works more times. Therefore I can know Nodemcu works or not without serial monitor. C:\Users\cceem_000\AppData\Local\Microsoft\Windows\INetCache\Content.Word\F3F4PFPIOLP1KZ9.MEDIUM.JPG

Software Part

In the software part of my projects is contain Web and Arduino programing.

Arduino

Firstly, Nodemcu is connect the WiFi and after that try to connect Mqtt server this server IP is raspberry pi’s IP and start to 10000msec delay time. It means, Nodemcu send to Sensor values to raspberry pi server each 10 sec. Also you can change this delay time with using website.

Station1
Arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <dht.h>
dht DHT;
 
// Define NodeMCU D3 pin to as temperature data pin of  DHT11
#define DHT11_PIN D3
 
// Update these with values suitable for your network.
const char* ssid = "PiTON STAJER";
const char* password = "Piton.Stajer.26";
const char* mqtt_server = "192.168.1.126";
 
//192.168.1.123
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[1500];
 
int looptime =10000;
int sensorPin = A0;    // input for LDR and rain sensor
int enable1 = 15;      // enable reading LDR
int enable2 = 13;
int buzzerpin = 14;
int ledpin = 12;
 
 
int sensorValue1 = 0;  // variable to store the value coming from sensor LDR
int sensorValue2 = 0;  // variable to store the value coming from sensor Rain sensor
 
 
void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
     // delay(500);
      Serial.print(".");
      led (1, 400);
       }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  buzzer(1, 1000);
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void buzzer (int a, int b){
for (int x=0; x<a; x++){
digitalWrite(buzzerpin, HIGH);
delay(b);
digitalWrite(buzzerpin, LOW);
delay(b);
}
}
 
void led (int a, int b){
for (int x=0; x<a; x++){
digitalWrite(ledpin, HIGH);
delay(b);
digitalWrite(ledpin, LOW);
delay(b);
}
}
 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "Station_1-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
      
      buzzer(1,300);
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("wheather/command/1/#");
    } else {
      buzzer(3,300);
      
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 4 seconds");
      if (WiFi.status() != WL_CONNECTED){
        buzzer(1,3000);
        setup_wifi();
      }
      // Wait 6 seconds before retrying
      led (30, 100);
      // delay(3000);
    }
  }
} //end reconnect()
 
 
 
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.print("Command is : [");
  Serial.print(topic);
  int p =(char)payload[0]-'0';
  String Konu = String(topic);
  if(Konu == "wheather/command/1/now")
  {
   Serial.print(topic);
   publishing(1);
  }
  // if MQTT comes a 1 message, show temperature
  else if(Konu == "wheather/command/1/delay")
  {
   payload[length] = '\0';
   int intvalue = atoi((char*)payload);
   looptime = intvalue*1000;    
   Serial.print(looptime);
    
  }
  else if (Konu == "wheather/command/1/sleep"){
    payload[length] = '\0';
    int intvalue = atoi((char*)payload);
    Serial.print(intvalue);
    ESP.deepSleep(intvalue * 1000000, WAKE_RF_DEFAULT);
    delay(1000);
  }
  
}
 
 
void publishing(int a){
  int chk = DHT.read11(DHT11_PIN);
     String msg = String(a) ;
     msg= msg + "\n" ;
     msg= msg+ DHT.temperature + "\n" + DHT.humidity ;
     msg = msg+"\n" ;
     msg=msg+ ldr();
     msg=msg+"\n";
     msg=msg+ rain();
     msg=msg+"\n";
     msg=msg+ bmpsensorcall(0);
     msg=msg+"\n";
     msg=msg+ bmpsensorcall(1);
     msg=msg+"\n";
     msg=msg+ bmpsensorcall(2);
     msg=msg+"\n";
     msg=msg + (looptime/1000);
     msg=msg+"\n";
    
     char message[1500];
     msg.toCharArray(message,1500);
     Serial.println(message);
     //publish sensor data to MQTT broker
  
 
   if (a == 1){
    client.publish("wheather/station/1", message);
    delay(500);
   }
   else if (a == 2){
    client.publish("wheather/station/2", message);
   delay(500);
   }
   else if (a == 3){
    client.publish("wheather/station/3", message);
    delay(500);
   }
   else if (a == 4){
    client.publish("wheather/station/4", message);
    delay(500);
   }
  
  
  //client.publish("wheather/station/1", message);
  
  }
 
void setup() {
 
 
pinMode(enable1, OUTPUT);
pinMode(enable2, OUTPUT);
pinMode(buzzerpin, OUTPUT);
pinMode(ledpin, OUTPUT);
  
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  int chk = DHT.read11(DHT11_PIN);
  Serial.print(" Starting Humidity: " );
  Serial.print(DHT.humidity, 1);
  Serial.println('%');
  Serial.print(" Starting Temparature ");
  Serial.print(DHT.temperature, 1);
  Serial.println('C');
}
float ldr(){
digitalWrite(enable1, HIGH);
sensorValue1 = analogRead(sensorPin);
sensorValue1 = constrain(sensorValue1, 100, 820);
sensorValue1 = map(sensorValue1, 100, 820, 0, 1000);
Serial.print("Light intensity:  ");
Serial.println(sensorValue1);
digitalWrite(enable1, LOW);
delay(100);
 
return sensorValue1;
}
 
float rain(){
digitalWrite(enable2, HIGH);
 
delay(500);
sensorValue2 = analogRead(sensorPin);
sensorValue2 = constrain(sensorValue2, 150, 400);
sensorValue2 = map(sensorValue2, 150, 400, 1000, 0);
 
Serial.print("Rain value:       ");
Serial.println(sensorValue2);
Serial.println();
delay(100);
 
digitalWrite(enable2, LOW);
 
return sensorValue2;
}
 
///////BMP//////
 
float bmpsensorcall (int a){
    if(!bmp.begin()) {
Serial.print("Failed to read from BMP sensor!!");
while(1);
}
 
sensors_event_t event;
bmp.getEvent(&event);
if (a == 0){
Serial.print("Pressure:         ");
Serial.print(event.pressure);
Serial.println(" hPa");
return event.pressure;
}
else if (a == 1){
float temperature;
bmp.getTemperature(&temperature);
Serial.print("Temperature:      ");
Serial.print(temperature);
Serial.println(" degrees Celcius ");
return temperature;
}
//--- extra----you can measure the altitude with the temperature and the air pressure
else if (a == 2){
float seaLevelPressure = 1013;
Serial.print("Altitude:         ");
Serial.print(bmp.pressureToAltitude(seaLevelPressure,event.pressure));
Serial.println(" m");
return bmp.pressureToAltitude(seaLevelPressure,event.pressure);
}
 
}
 
 
void loop() {
if (!client.connected()) {
    reconnect();
  }
  
  client.loop();
  long now = millis();
 
  // read DHT11 sensor every 6 seconds
  if (now - lastMsg > looptime) {
      Serial.print(looptime);
     lastMsg = now;
     publishing(1);
     publishing(2);
     publishing(3);
     publishing(4);
 
  }
}

 

Mqtt

Nodemcu is the client of the server for send value. In this case each Nodemcu have specific topic and message. Message contains to sensor value and topic contains the Which Nodemcu send this message. For example; “client.publish(“wheather/station/1”, message); “wheather/station/1” is the topic of the message it means this Nodemcu is the 1.Station. You can see my code link end of the report.

Mqtt Callback

Also Nodemcu can receive message come from other client for example (web site). You can change delay time and you can set sleep time. I design to callback function for these. Callback function listen to Server for specific topics. These are “wheather/command/1/delay” and “wheather/command/1/sleep” when receive these topics message change to delat time or sleep time

Node.js

I use Node.js for Website, Database and Mqtt Server.

You can contact me for Node.js proect file

Web Site

I design to theme with using CSS and EJS. Ejs is similar to HTML but EJS is more useful and work with Node.js. Also I used to Socket.io for when values come to server page is not change only value is changed so Website is not use a lot of cache memory. Also I used to Node.js Express for more functional structure for Website. Also I visualized old data and live data with Chart.js graph

Chart.js

Chart.js is the best graph library for Node.js . Also these graphs is dynamic and live. I can use all data in the same graph. Above graph is the live data for station 1. It show only 10 new data. You can change display for example You can show only Altitude and Temperature value and close other values showing. Also secon figure show old values graph with using MongoDB database C:\Users\cceem_000\AppData\Local\Microsoft\Windows\INetCache\Content.Word\2.png

Socket.io

Socket.io is one of the socket programing library for javascript. Usually this use for chat application. However, I use this library for live data what came from sensors. Also these socket.io change icon in case of value that came from sensors. C:\Users\cceem_000\AppData\Local\Microsoft\Windows\INetCache\Content.Word\1.png

Icon

These icon changes in the webpage with sensors data. Give the status for weather station. For example(cloudy, rainy, humid, sunny, dark, bright, hot, cold ,etc). In the above figure show Status and live values with using socket.io

Bootstrap

I use bootstrap for design display website with using css and html . Bootstrap allows the table visualization and display settings for all kind of resolutions.

Database

I use to MongoDB for database, MongoDB is noSQL database therefore I can save big data without table so I can use these data fastly. Above figure show the database query you can show the results with chart or value. Bellow figure shows query result value C:\Users\cceem_000\AppData\Local\Microsoft\Windows\INetCache\Content.Word\5.png

Mqtt Server

For the Mqtt server, I use Mosca library for Node.js Mqtt application and I use port 1883 for Mqtt server. In this Mosca library is one of the socket programing. Also It is very simple.

Problems

I encountered a few problems and difficulties while working on the project. These are often software problems. Since I was not familiar with Node.js, I learned a lot about it, so I had difficulty writing the database and communication codes. For example, MongoDB save date with ISODate format but Chart.js can’t handle this format but I can change to ISOdate to String format. So I change manually format in loop. Also Communication not fast enough If I start to all 4 station and sensor with 1 seconds delay time. Server receive all data without any drops but Socket.io can’t handle these data therefore Webpage can’t show all data without lost data. And localserver can’t handle big traffic. In other problem is the Nodemcu sleep system I can turnoff the Nodemcu with specific time but I can wake the sleep before the scheduled time. I want to wake Nodemcu with using ping but I can’t find to how to do it.

Development

First of all, My projects communication system works only localhost. We can development this and I can reach all station other internet connection. For this, I install the VPS system in Raspberry Pi for create own hosting for projects. Also I can develop sleep system if I can solve the problem that mentioned above. I can add the battery life for each station. Also Web –page can be professional dashboard and add the machine learning and data analyze for big data.

 

 

5 thoughts on “Weather Station IoT”

  1. JohnHour says:
    July 31, 2018 at 8:55 am

    Hi, @ Yılmaz Cemalettin, I want Node.js proect file from you, can you share to me the code? Thank you.

    Reply
  2. besma says:
    April 13, 2019 at 10:13 am

    can you share the node.js server plz, thanks.

    Reply
  3. Dat says:
    April 17, 2019 at 8:42 am

    Hi, @ Yılmaz Cemalettin, I want Node.js file from you, can you share to me the code? Thank you very much

    Reply
  4. Roberto Wiest says:
    September 23, 2019 at 11:45 am

    Hi @ Yılmaz Cemalettin, could you share your complete project with me. I am wanting to implement something similar to what you have and I am very interested in how you implemented it.

    If you can share the whole project I would be very grateful.

    Thanks.

    Reply
  5. mario says:
    September 25, 2019 at 1:20 pm

    hi, can you share with me Node.js project file. thanks a lot

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme