Skip to content

Weather station 2.0

Метеостанция 2.0

jaden

Published October 04, 2022

Original author: NightFlash · RussiaOriginal source (new tab)

Weather station 2.0

Components

Hardware components

Project description

Author used HW below

  • Arduino or Freeduino microcontroller (with ATmega328);
  • Ethernet-shield Wiznet w5100;
  • Digital temperature sensor DS18B20;
  • Atmospheric pressure sensor BMP085;
  • Humidity sensor DHT22;
  • Resistor 4.7 kOhm;
  • Resistor 5 kOhm.

 

Weather station 2.0

Weather station 2.0

DS18B20:

ds18b20

DHT22:

Weather station 2.0

BMP085:

Weather station 2.0

HW connection

Weather station 2.0, HW connection

Resistor R1 in the diagram is 4.7 kOhm, it is used to pull up the data line for the DS18B20 temperature sensor. Resistor R2 - 5 kΩ, pulls up the DHT22 data line. The temperature and humidity sensors are placed outdoors. I used a four-core telephone cable for this - it works great at a distance of 6 meters from the microcontroller. The pressure sensor is placed in the house. Due to the leakage of windows / doors, the atmospheric pressure outside and inside will be almost the same.

Perhaps someone will pay attention and ask the question: why is the DS18B20 connected using a three-wire circuit, and in the last article there were only two? The fact is that at that time I used a parasitic power supply for the temperature sensor. However, at large distances from the microcontroller, this may fail. Therefore, I recommend forcibly supplying power to the sensor.

If you don't have DHT22 or BMP085 sensors then just leave the corresponding Arduino pins unconnected. In the code, it will be necessary to correct only one or two lines.
Well, here's the code for the sketch:

// Sketch for Arduino to send weather data to People's monitoring.
// Version 2.0 (19.07.2014)
//
// Author: Dmitry Gladyshev (2012-2014)
// http://student-proger.ru/2014/07/meteostanciya-2-0/
 
 
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <Wire.h>
#include <BMP085.h>
#include <DHT.h>
 
bool Debug = false ; //debug mode
 
//************************************************ ********************************************
byte mac [] = { 0xDE , 0xAD , 0xBE , 0x00 , 0x00 , 0x00 } ; //MAC address Arduino 
#define BMP085_EXIST 1 // presence of atmospheric pressure sensor
#define DHT_EXIST 1 // presence of a humidity sensor
#define DS18B20_PIN 2 // pin for connecting the DS18B20 temperature sensor
#define DHTPIN 6 // DHT22 humidity sensor connection pin
#define DHTTYPE DHT22 // humidity sensor type DHT22/DHT11
#define postingInterval 600000 // interval between posting data in milliseconds (10 minutes)
//************************************************ ********************************************
 
char server[] = "narodmon.ru";
char macbuf[13];
 
EthernetClient client;
OneWire ds(DS18B20_PIN);
 
#if BMP085_EXIST == 1
BMP085 dps = BMP085();
#endif
 
#if DHT_EXIST == 1
DHT dht(DHTPIN, DHTTYPE);
#endif
 
unsigned long lastConnectionTime = 0; // time of last data transfer
boolean lastConnected = false ; // connection state
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
char replyBuffer [ 160 ] ; // buffer to send
int CountSensors; // number of temperature sensors found
long Pressure = 0;
float Humidity = 0;
 
voidsetup(){ 
 
if(Debug)
{
Serial.begin(9600);
}
 
// second to initialize ethernet
delay(1000);
// Try to connect via ethernet
if(Ethernet.begin(mac) == 0) 
{
if(Debug)
{
Serial.println("Failed to configure Ethernet using DHCP");
}
// do nothing
for(;;);
}
 
//Find out the number of thermal sensors
CountSensors = DsCount();
if(Debug)
{
Serial.print("Found ");
Serial.print(CountSensors);
Serial.println(" sensors.");
}
 
#if BMP085_EXIST == 1
Wire.begin();
dps.init();
#endif
#if DHT_EXIST == 1
dht.begin();
#endif
 
lastConnectionTime = millis () -postingInterval+15000; //first connection 15 seconds after start
}
 
voidloop()
{
//If suddenly we accidentally receive some data from somewhere,
//then just read them and ignore them to clear the buffer
if(client.available()) 
{
client. read () ;
}
 
if(!client.connected() && lastConnected) 
{
if(Debug)
{
Serial.println();
Serial.println("disconnecting.");
}
client.stop();
}
 
//if not connected and a certain time has passed, then we take a measurement,
//reconnect and send data
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) 
{
 
//formation of an HTTP request
memset(replyBuffer, 0, sizeof(replyBuffer));
strcpy(replyBuffer,"ID=");
 
memset(macbuf, 0, sizeof(macbuf));
//Convert the MAC address
for(int k=0; k<6; k++)
{
int b1=mac[k]/16;
int b2=mac[k]%16;
char c1[2],c2[2];
 
if(b1>9) c1[0]=(char)(b1-10)+'A';
else c1 [ 0 ] = ( char )( b1 ) + '0' ;
if(b2>9) c2[0]=(char)(b2-10)+'A';
else c2[0] = (char)(b2) + '0';
 
c1 [ 1 ] = '\0' ;
c2[1]='\0';
 
strcat(macbuf,c1);
strcat(macbuf,c2);
}
strcat(replyBuffer, macbuf);
 
// Reset the search for sensors (we already know the number)
ds.reset_search();
//Now in a loop we interrogate all sensors at once
 
for(int j=0; j<CountSensors; j++)
{
 
switch in;
byte present = 0;
byte data [ 12 ] ;
byte addr[8];
 
if( !ds.search(addr)) 
{
ds.reset_search();
return;
}
 
ds.reset();
ds.select(addr);
ds.write(0x44,1);
 
delay(1000);
 
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
 
for( i = 0; i < 9; i++)// we need 9 bytes 
{
data[i] = ds.read();
}
 
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if(SignBit)// negative 
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
 
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
 
char temp[3];
 
itoa(Whole,temp);
strcat(replyBuffer,"&");
 
//convert the temperature sensor address
for(int k=7; k>=0; k--)
{
int b1=addr[k]/16;
int b2=addr[k]%16;
char c1[2],c2[2];
 
if(b1>9) c1[0]=(char)(b1-10)+'A';
else c1 [ 0 ] = ( char )( b1 ) + '0' ;
if(b2>9) c2[0]=(char)(b2-10)+'A';
else c2[0] = (char)(b2) + '0';
 
c1 [ 1 ] = '\0' ;
c2[1]='\0';
 
strcat(replyBuffer, c1);
strcat(replyBuffer, c2);
}
strcat(replyBuffer,"=");
if ( SignBit ) //if the temperature is negative, add a minus sign 
{
strcat(replyBuffer,"-");
}
strcat(replyBuffer,temp);
strcat(replyBuffer,".");
if(Fract<10)
{
strcat(replyBuffer,"0");
}
return ( Fract, temp ) ;
strcat(replyBuffer,temp);
}
 
char temp[8];
long p_100, h_100;
 
#if BMP085_EXIST == 1
strcat(replyBuffer, "&");
strcat(replyBuffer, macbuf);
strcat(replyBuffer, "01=");
dps.getPressure(&Pressure);
p_100 = Pressure/1.333;
Whole = p_100 / 100;
Fract = p_100%;
itoa(Whole, temp);
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
if(Fract<10)
{
strcat(replyBuffer,"0");
}
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#endif
 
#if DHT_EXIST == 1
Humidity = dht.readHumidity();
strcat(replyBuffer, "&");
strcat(replyBuffer, macbuf);
strcat(replyBuffer, "02=");
h_100 = Humidity*100;
Whole = h_100 / 100;
Fract = h_100 % 100;
itoa(Whole, temp);
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
if(Fract<10)
{
strcat(replyBuffer,"0");
}
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#endif
 
strcat(replyBuffer,'\0');
 
if(Debug)
{
Serial.println(replyBuffer);
Serial.print("Content-Length: ");
Serial.println(len(replyBuffer));
}
 
//send request
httpRequest();
 
}
//save the last connection state
lastConnected = client.connected();
}
 
voidhttpRequest() 
{
if(client.connect(server, 80))
{
if(Debug)
{
Serial.println("connecting...");
}
// send HTTP POST request:
client.println("POST http://narodmon.ru/post.php HTTP/1.0");
client.println("Host: narodmon.ru");
//client.println("User-Agent: arduino-ethernet");
//client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(len(replyBuffer));
client.println();
client.println(replyBuffer);
client.println();
 
lastConnectionTime = millis();
}
else
{
if(Debug)
{
Serial.println("connection failed");
Serial.println("disconnecting.");
}
client.stop();
}
}
 
//Number of thermal sensors on the bus
intDsCount()
{
int count=0;
bool thatsall = false ;
byte addr[8];
do
{
if( !ds.search(addr))
{
ds.reset_search();
thatsall = true;
}
count++;
}while(!thatsall);
return(count-1);
}
 
int len ​​( char *buf )
{
int i=0;
do
{
i++;
}while(buf[i]!='\0'); 
return i;
}
 
voidreverse(char s[])
{
int i, j;
char c;
 
for(i = 0, j = strlen(s)-1; i<j; i++, j--) 
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
 
void itoa ( int n, char s [])
{
int i, sign;
 
if (( sign = n ) < 0 ) /* write sign */ 
n = -n; /* make n a positive number */
i = 0;
do { /* generate numbers in reverse order */ 
s [ i++ ] = n % 10 + '0' ; /* get the next digit */
} while (( n /= 10 ) > 0 ) ; /* delete */ 
if(sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

Pay attention to lines 17-23:

  • mac is the MAC address of the Arduino. This is your unique identifier on the people's monitoring site. Therefore, to protect against matches, I recommend using the MAC address of your computer / router / phone with the last byte changed (so that there is no collision within the local network).
  • BMP085_EXIST - the presence of an atmospheric pressure sensor. Set to 0 if you don't have one.
  • DHT_EXIST - the presence of a humidity sensor. Set to 0 if you don't have one.
  • DS18B20_PIN - pin for connecting the DS18B20 temperature sensor.
  • DHTPIN - DHT22 humidity sensor connection pin.
  • DHTTYPE - type of humidity sensor: DHT22 or DHT11
  • postingInterval - interval for posting readings. The default is 600000ms (10 minutes).

 

 

Weather Station 2.1

I am posting an updated version of the firmware for People's Monitoring. Added temperature reading from humidity and atmospheric pressure sensors. And now, if there is no connection to the network, the controller will wait for it to appear (it will help solve the problem of “freezing” after a power outage, when the router has not yet had time to start, and the arduino is already trying to get an IP address).

Everything about the connection is written in this article: http://19dx.ru/2014/07/meteostanciya-2-0/
Thanks to those who already wrote their solutions in the comments to the previous version (especially cephex ). Your path was right

Attention! The Arduino Leonardo has problems with the pressure sensor.

// Sketch for Arduino to send weather data to People's monitoring
// Version 2.1 (11/25/2014)
//
// Author: Dmitry Gladyshev (2012-2014)
// http://student-proger.ru/2014/11/meteostanciya-2-1/
 
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <Wire.h>
#include <BMP085.h>
#include <DHT.h>
 
bool Debug = false ; //debug mode
 
//************************************************ ********************************************
byte mac [] = { 0xDE , 0xAD , 0xBE , 0x00 , 0x00 , 0x00 } ; //MAC address Arduino 
#define BMP085_EXIST 1 // presence of atmospheric pressure sensor
#define BMP085_TEMP 1 // use temperature sensor in BMP085
#define DHT_EXIST 1 // presence of a humidity sensor
#define DHT_TEMP 1 // use temperature sensor in DHT22
#define DS18B20_PIN 2 // pin for connecting the DS18B20 temperature sensor
#define DHTPIN 6 // DHT22 humidity sensor connection pin
#define DHTTYPE DHT22 // humidity sensor type DHT22/DHT11
#define postingInterval 600000 // interval between posting data in milliseconds (10 minutes)
//************************************************ ********************************************
 
char server[] = "narodmon.ru";
char macbuf[13];
 
EthernetClient client;
OneWire ds(DS18B20_PIN);
 
#if BMP085_EXIST == 1
BMP085 dps = BMP085();
#endif
 
#if DHT_EXIST == 1
DHT dht(DHTPIN, DHTTYPE);
#endif
 
unsigned long lastConnectionTime = 0; // time of last data transfer
boolean lastConnected = false ; // connection state
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
char replyBuffer [ 160 ] ; // buffer to send
int CountSensors; // number of temperature sensors found
long Temperature = 0;
long Pressure = 0; // pressure
float Humidity = 0; // humidity
 
void setup() {
 
if (Debug)
{
Serial.begin(9600);
}
 
// Try to connect via ethernet until it succeeds
do
{
delay(1000);
} while (Ethernet.begin(mac) == 0);
 
//Find out the number of temperature sensors DS18B20
CountSensors = DsCount();
if (Debug)
{
Serial.print("Found ");
Serial.print(CountSensors);
Serial.println(" sensors.");
}
 
#if BMP085_EXIST == 1
Wire.begin();
dps.init();
#endif
#if DHT_EXIST == 1
dht.begin();
#endif
 
lastConnectionTime = millis () -postingInterval+15000; //first connection 15 seconds after start
}
 
void loop()
{
//If suddenly we accidentally receive some data from somewhere,
//then just read them and ignore them to clear the buffer
if (client.available())
{
client. read () ;
}
 
if (!client.connected() && lastConnected)
{
if (Debug)
{
Serial.println();
Serial.println("disconnecting.");
}
client.stop();
}
 
//if not connected and a certain time has passed, then we take a measurement,
//reconnect and send data
if (!client.connected() && (millis() - lastConnectionTime > postingInterval))
{
//formation of an HTTP request
memset(replyBuffer, 0, sizeof(replyBuffer));
strcpy(replyBuffer,"ID=");
 
memset(macbuf, 0, sizeof(macbuf));
//Convert the MAC address
for (int k=0; k<6; k++)
{
int b1=mac[k]/16;
int b2=mac[k]%16;
char c1[2],c2[2];
 
if (b1>9) c1[0]=(char)(b1-10)+'A';
else c1 [ 0 ] = ( char )( b1 ) + '0' ;
if (b2>9) c2[0]=(char)(b2-10)+'A';
else c2[0] = (char)(b2) + '0';
 
c1 [ 1 ] = '\0' ;
c2[1]='\0';
 
strcat(macbuf,c1);
strcat(macbuf,c2);
}
strcat(replyBuffer, macbuf);
 
// Reset the search for sensors (we already know the number)
ds.reset_search();
 
//Now in a loop we poll all DS18B20 sensors at once
for (int j=0; j<CountSensors; j++)
{
switch in;
byte present = 0;
byte data [ 12 ] ;
byte addr[8];
 
if ( !ds.search(addr))
{
ds.reset_search();
return;
}
 
ds.reset();
ds.select(addr);
ds.write(0x44,1);
 
delay(1000);
 
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
 
for ( i = 0; i < 9; i++ ) // we need 9 bytes of data from the sensor 
{
data[i] = ds.read();
}
 
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000 ; // sign bit
if ( SignBit ) // negative temperature 
{
TReading = (TReading ^ 0xffff) + 1;
}
Tc_100 = (6 * TReading) + TReading / 4;
 
Whole = Tc_100 / 100; // separate integer and fractional parts
Fract = Tc_100 % 100;
 
char temp[3];
 
itoa(Whole,temp);
strcat(replyBuffer,"&");
 
//convert the temperature sensor address
for (int k=7; k>=0; k--)
{
int b1=addr[k]/16;
int b2=addr[k]%16;
char c1[2],c2[2];
 
if (b1>9) c1[0]=(char)(b1-10)+'A';
else c1 [ 0 ] = ( char )( b1 ) + '0' ;
if (b2>9) c2[0]=(char)(b2-10)+'A';
else c2[0] = (char)(b2) + '0';
 
c1 [ 1 ] = '\0' ;
c2[1]='\0';
 
strcat(replyBuffer, c1);
strcat(replyBuffer, c2);
}
strcat(replyBuffer,"=");
if ( SignBit ) //if the temperature is negative, add a minus sign 
{
strcat(replyBuffer,"-");
}
strcat(replyBuffer,temp);
strcat(replyBuffer,".");
if (Fract<10)
{
strcat(replyBuffer,"0");
}
return ( Fract, temp ) ;
strcat(replyBuffer,temp);
}
 
char temp[8];
long p_100, h_100;
 
#if BMP085_EXIST == 1
//get atmospheric pressure value
strcat(replyBuffer, "&");
strcat(replyBuffer, macbuf);
strcat(replyBuffer, "01=");
dps.getPressure(&Pressure);
p_100 = Pressure/1.333;
Whole = p_100 / 100;
Fract = p_100%;
itoa(Whole, temp);
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
if (Fract<10)
{
strcat(replyBuffer,"0");
}
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#if BMP085_TEMP == 1
//get the temperature value from the pressure sensor
strcat(replyBuffer, "&bmpt=");
dps.getTemperature(&Temperature);
if (Temperature < 0)
{
SignBit = 1;
Temperature = -Temperature;
}
else
{
SignBit = 0;
}
Whole = Temperature / 10;
Fract = Temperature % 10;
itoa(Whole, temp);
if (SignBit)
{
strcat(replyBuffer, "-");
}
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#endif
#endif
 
#if DHT_EXIST == 1
//get humidity value
Humidity = dht.readHumidity();
strcat(replyBuffer, "&");
strcat(replyBuffer, macbuf);
strcat(replyBuffer, "02=");
h_100 = Humidity*100;
Whole = h_100 / 100;
Fract = h_100 % 100;
itoa(Whole, temp);
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
if (Fract<10)
{
strcat(replyBuffer,"0");
}
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#if DHT_TEMP == 1
//get the temperature value from the humidity sensor
strcat(replyBuffer, "&dhtt=");
Temperature = dht.readTemperature()*100;
if (Temperature < 0)
{
SignBit = 1;
Temperature = -Temperature;
}
else
{
SignBit = 0;
}
Whole = Temperature / 100;
Fract = Temperature % 100;
itoa(Whole, temp);
if (SignBit)
{
strcat(replyBuffer, "-");
}
strcat(replyBuffer, temp);
strcat(replyBuffer, ".");
if (Fract<10)
{
strcat(replyBuffer,"0");
}
itoa ( Fract , temp ) ;
strcat(replyBuffer, temp);
#endif
#endif
 
strcat(replyBuffer,'\0');
 
if (Debug)
{
Serial.println(replyBuffer);
Serial.print("Content-Length: ");
Serial.println(len(replyBuffer));
}
 
//send request
httpRequest();
}
//save the last connection state
lastConnected = client.connected();
}
 
//Function to send data
void httpRequest()
{
if (client.connect(server, 80))
{
if (Debug)
{
Serial.println("connecting...");
}
// send HTTP POST request:
client.println("POST http://narodmon.ru/post.php HTTP/1.0");
client.println("Host: narodmon.ru");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(len(replyBuffer));
client.println();
client.println(replyBuffer);
client.println();
 
lastConnectionTime = millis();
}
else
{
if (Debug)
{
Serial.println("connection failed");
Serial.println("disconnecting.");
}
client.stop();
}
}
 
//Number of DS18B20 thermal sensors on the bus
int DsCount()
{
int count=0;
bool thatsall = false ;
byte addr[8];
do
{
if ( !ds.search(addr))
{
ds.reset_search();
thatsall = true;
}
count++;
} while(!thatsall);
return (count-1);
}
 
//Function to determine the length of the string
int len ​​( char *buf )
{
int i=0;
do
{
i++;
} while (buf[i]!='\0');
return i;
}
 
//Line reversal function
void reverse(char s[])
{
int i, j;
char c;
 
for (i = 0, j = strlen(s)-1; i<j; i++, j--)
{
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
 
//Function to convert number to character array
void itoa ( int n, char s [])
{
int i, sign;
 
if (( sign = n ) < 0 ) /* write sign */ 
n = -n; /* make n a positive number */
i = 0;
do { /* generate numbers in reverse order */ 
s [ i++ ] = n % 10 + '0' ; /* get the next digit */
} while (( n /= 10 ) > 0 ) ; /* delete */ 
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

Comments

Comments