Weather Station Web Server (Part 9)

The software sketch shown below turns the weather station into a basic web server. This allows you to connect to the station using a web browser of your choice. Using the static IP address assigned to EtherTen board we enter this as this url in the browser. In this case we enter http://192.168.1.45 into the browser. If all is good we should get html returned to our browser that shows the current value for each of the sensors in the weather station.This will only work on your internal network. You cannot access it via the internet as it uses a private IP address range. To enable access to the weather station from the Internet we would need to setup port forwarding on the router that connects the internal network to the Internet.

Web Server Flow

Software

With the sketch we are going to use some of the functionality we used in previous weather station sketches. Instead of outputing the data to the serial console we are going to listen for browser clients connecting to the weather station via the network cable. We setup a webserver to listen on port 80 for ip address 192.168.1.45.

In the software sketch we need to create a server and setup listening on port 80. To do this we need to specify a MAC and IP address. This is defined in lines 42 to 45. The MAC addess that was used, was one we got off an old modem that is no longer in use. Any item of network equipment would normally have a MAC address on a label attached to the gear. The internal network we use has the private IP address range of 192.168.1.X. We assigned host address of 45 to the weather station to get the IP address of 192.168.1.45

In the Setup function we initalise the sensors, timer and start the ethernet server. The timer we use is set to trigger every 0.5 seconds. We use this to get the 2.5 second sample period for the wind speed calcualtion. This is all done through the interrupt handler routines.

The main loop is where all of the action happens. We read the DS18B20 and BME280 sensors if required. From line 96 to 110 we update the min and max temp values. We also check if any rain has fallen and update the totals if needed.

From line 112 to 178 we listen for any browser clients connecting to the weather station web server. If a client is detected then we return the html code that displays the values from each of the sensors. Prior to sending the html data we turn on the TX LED (line 126). Once we have sent the data we turn off the TX LED (line 161).

In line 135 we have inserted some inline styling to make the text larger on the web page. This is for visual appearance only.

Software Sketch

Arduino Weather Station Basic Web Server Sketch(Download)

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
#include <SPI.h> 
#include <Ethernet.h> 

#include “TimerOne.h” 
#include <math.h> 

#include “cactus_io_DS18B20.h” 
#include “cactus_io_BME280_I2C.h” 

#define Bucket_Size 0.01 // bucket size to trigger tip count 
#define RG11_Pin 3 // digital pin RG11 connected to 
#define TX_Pin 8 // used to indicate web data tx 
#define DS18B20_Pin 9 // DS18B20 Signal pin on digital 9 

#define WindSensor_Pin (2) // digital pin for wind speed sensor 
#define WindVane_Pin (A2) // analog pin for wind direction sensor 
#define VaneOffset 0 // define the offset for caclulating wind direction 

volatile unsigned long tipCount; // rain bucket tip counter used in interrupt routine 
volatile unsigned long contactTime; // timer to manage any rain contact bounce in interrupt routine

volatile unsigned int timerCount; // used to count ticks for 2.5sec timer count 
volatile unsigned long rotations; // cup rotation counter for wind speed calcs 
volatile unsigned long contactBounceTime; // timer to avoid contact bounce in wind speed sensor 

long lastTipcount; // keep track of bucket tips 
float totalRainfall; // total amount of rainfall detected 

volatile float windSpeed; 
int vaneValue; // raw analog value from wind vane 
int vaneDirection; // translated 0 – 360 wind direction 
int calDirection; // calibrated direction after offset applied 
int lastDirValue; // last recorded direction value 

float minTemp; // keep track of minimum recorded temp 
float maxTemp; // keep track of maximum recorded temp 

// Create DS18B20, BME280 object 
DS18B20 ds(DS18B20_Pin); // on digital pin 9 
BME280_I2C bme; // I2C using address 0x77 

// Here we setup the web server. We are using a static ip address and a mac address 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
IPAddress ip(192, 168, 1, 45); 
EthernetServer server(80); // create a server listing on 192.168.1.45 port 80 

void setup() { 

// setup rain sensor values 
lastTipcount = 0; 
tipCount = 0; 
totalRainfall = 0; 

// setup anemometer values 
lastDirValue = 0; 
rotations = 0; 

// setup timer values 
timerCount = 0; 

ds.readSensor(); 
minTemp = ds.getTemperature_C(); 
maxTemp = ds.getTemperature_C(); 

// disable the SD card by switching pin 4 High 
pinMode(4, OUTPUT); 
digitalWrite(4, HIGH); 

// start the Ethernet connection and server 
Ethernet.begin(mac, ip); 
server.begin(); 

if (!bme.begin()) { 
// Serial.println(“Could not find BME280 sensor, check wiring!”); 
while (1); 


pinMode(TX_Pin, OUTPUT); 
pinMode(RG11_Pin, INPUT); 
pinMode(WindSensor_Pin, INPUT); 
attachInterrupt(digitalPinToInterrupt(RG11_Pin), isr_rg, FALLING); 
attachInterrupt(digitalPinToInterrupt(WindSensor_Pin), isr_rotation, FALLING); 

// setup the timer for 0.5 second 
Timer1.initialize(500000); 
Timer1.attachInterrupt(isr_timer); 

sei();// Enable Interrupts 


void loop() { 

ds.readSensor(); 
bme.readSensor(); 

// update min and max temp values 
if(ds.getTemperature_C() < minTemp) { 
minTemp = ds.getTemperature_C(); 

if(ds.getTemperature_C() > maxTemp) { 
maxTemp = ds.getTemperature_C(); 


// update rainfall total if required 
if(tipCount != lastTipcount) { 
cli(); // disable interrupts 
lastTipcount = tipCount; 
totalRainfall = tipCount * Bucket_Size; 
sei(); // enable interrupts 


// listen for incoming clients 
EthernetClient client = server.available(); 
if (client) { 
// an http request ends with a blank line 
boolean currentLineIsBlank = true; 
while (client.connected()) { 
if (client.available()) { 
char c = client.read(); 
Serial.write(c); 
// if you’ve gotten to the end of the line (received a newline 
// character) and the line is blank, the http request has ended, 
// so you can send a reply 
if (c == ‘\n’ && currentLineIsBlank) { 
// send a standard http response header 
digitalWrite(TX_Pin,HIGH); 
client.println(“HTTP/1.1 200 OK”); 
client.println(“Content-Type: text/html”); 
client.println(“Connection: close”); // connection closed completion of response 
client.println(“Refresh: 10”); // refresh the page automatically every 5 sec 
client.println(); 
client.println(“<!DOCTYPE HTML>”); 
client.println(“<html><body>”); 
digitalWrite(TX_Pin,HIGH); // Turn the TX LED on 
client.print(“<span style=\”font-size: 26px\”;><br>  Temperature is “); 
client.print(ds.getTemperature_C()); 
client.println(” °C<br>”); 
client.print(“%<br>  Humidity is “); 
client.print(bme.getHumidity()); 
client.println(” %<br>”); 
client.print(“%<br>  Pressure is “); 
client.print(bme.getPressure_MB()); 
client.println(” mb%<br>”); 
client.print(“%<br>  Wind Speed is “); 
client.print(windSpeed); 
client.println(” mph<br>”); 
getWindDirection(); 
client.print(“%<br>  Direction is “); 
client.print(calDirection); 
client.println(” °<br>”); 
client.print(“%<br>  Rainfall is “); 
client.print(totalRainfall); 
client.println(” mm<br>”); 
client.print(“%<br>  Minimum Temp “); 
client.print(minTemp); 
client.println(” °C<br>”); 
client.print(“%<br>  Maximum Temp “); 
client.print(maxTemp); 
client.println(” °C</span>”); 
client.println(“</body></html>”); 
digitalWrite(TX_Pin,LOW); // Turn the TX LED off 
break; 

if (c == ‘\n’) { 
// you’re starting a new line 
currentLineIsBlank = true; 
} else if (c != ‘\r’) { 
// you’ve gotten a character on the current line 
currentLineIsBlank = false; 





// give the web browser time to receive the data 
delay(1); 
// close the connection: 
client.stop(); 


// Interrupt handler routine for timer interrupt 
void isr_timer() { 

timerCount++; 

if(timerCount == 5) { 
// convert to mp/h using the formula V=P(2.25/T) 
// V = P(2.25/2.5) = P * 0.9 
windSpeed = rotations * 0.9; 
rotations = 0; 
timerCount = 0; 



// Interrupt handler routine that is triggered when the rg-11 detects rain 
void isr_rg() { 

if((millis() – contactTime) > 15 ) { // debounce of sensor signal 
tipCount++; 
totalRainfall = tipCount * Bucket_Size; 
contactTime = millis(); 



// Interrupt handler routine to increment the rotation count for wind speed 
void isr_rotation() { 

if((millis() – contactBounceTime) > 15 ) { // debounce the switch contact 
rotations++; 
contactBounceTime = millis(); 



// Get Wind Direction 
void getWindDirection() { 

vaneValue = analogRead(WindVane_Pin); 
vaneDirection = map(vaneValue, 0, 1023, 0, 360); 
calDirection = vaneDirection + VaneOffset; 

if(calDirection > 360) 
calDirection = calDirection – 360; 

if(calDirection > 360) 
calDirection = calDirection – 360; 
}

Web Results

Fire up your favourite browser and enter into the url http://192.1681.145 (or alternate ip to suite your local network). If all is well then you should see something like this appear in the browser.

AWS Basic web server sketch output

You can change what is sent from the weather station by modifying the html code in the sketch. If you want to display the wind speed in knots or km/hr you can create a function that does the conversion prior to sending to the browser.

Coming Up Next …

We create a more advanced web server that is accessible from the Internet.

Von Sirko