Software and Final Steps

Start 

Let's upload some code and attached the rain gauge.

Code 

With the base unlatched we can easily upload code. Upload the following code. It's the same one from Weather Station 3 but the magnet socket is changed from D5 to D2. You'll see why later. Remember to add in your network details as well as your Wunderground ID and key.

Download file Copy to clipboard
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
#include "WiFiEsp.h"
#include <Wire.h>
#include "Seeed_BME280.h"
#include <math.h>
#include "Arduino.h"
#include "SI114X.h"
#include "SoftwareSerial.h"
#include <OneButton.h>

#define magSocket 2

//Defines for Wunderground variables
#define WID "WUNDERGROUND_ID"
#define WKEY "WUNDERGOUND_KEY"
#define getPayload "/weatherstation/updateweatherstation.php?ID="  WID  "&PASSWORD="  WKEY  "&dateutc=now&tempf=%s&humidity=%s&baromin=%s&UV=%s&dailyrainin=%s&action=updateraw"


//Create serial communication object
SoftwareSerial Serial1(8, 9); // RX, TX

//Wifi settings
char ssid[] = "YOURWIFINETOWRK";       // your network SSID (name)
char pass[] = "YOURPASSWORD";      // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

//Server URL for later
char server[] = "weatherstation.wunderground.com";

//Storage for getPayload
char buf[200];


// Initialize the Ethernet client object
WiFiEspClient client;

// Construct sensor objects
SI114X sunlightSensor;
BME280 bme280;

//Attach magnet switch to one button
OneButton magSwitch(magSocket, true);

// Variables for weather measurements
float temperature;
float humidity;
float pressure;
float uv;
float rain;

// Letter version of the variables from above
char cTemp[8];
char cHum[8];
char cPress[8];
char cUv[8];
char cRain[8];

// Variable to hold time
unsigned long previousMillis = 0;
unsigned long newDayMillis = 0;

// Milliseconds to wait between updates.
const long interval = 2 * 60 * 1000L;
const long dayMillis = 86400000L;

void setup() {
  // Initialize serial for debugging
  Serial.begin(9600);
  // Initialize serial for ESP module
  Serial1.begin(9600);
  // Initialize ESP module
  WiFi.init(&Serial1);

  // Initialize sunlight sensor
  while (!sunlightSensor.Begin()) {
    Serial.println(F("Sunlight Error"));
    delay(1000);
  }

  // Initialize BME280 sensor
  if (!bme280.init()) {
    Serial.println(F("BME Error"));
  }

  // Attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // You're connected now, so print out the data
  Serial.println(F("You're connected to the network"));

  // Print out all info about wifi connection
  printWifiStatus();

  // Run updateWeather function
  updateWeather();
  // Run sendData function
  sendData();

  //Create magnet switch function
  magSwitch.attachClick(bucketMove);
}

void loop() {
  // Store the current time
  unsigned long currentMillis = millis();

  // Check to see if enough time has passed
  if (currentMillis - previousMillis >= interval) {
    // Save the last time data was sent
    previousMillis = currentMillis;

    //Check to see if a day has passed
    if (currentMillis - newDayMillis >= dayMillis ){
      //Set rain measured to 0
      rain = 0;
      //Reset day counter
      newDayMillis = currentMillis;
    }

    // Run updateWeather function
    updateWeather();
    // Run sendData function
    sendData();
  }

  //Listen for rain events
  magSwitch.tick();

  // While there is a connection to the server
  while (client.available()) {
    // Store the response
    char c = client.read();
    // And print it to the serial console
    Serial.write(c);
  }

  // If the server's disconnected, stop the client:
  if (!client.connected()) {
    // Prints debugging messages
    //Serial.println();
    //Serial.println("disconnecting from server.");
    // Disconnect the server
    client.flush();
    client.stop();
  }
}

void updateWeather() {
  // Read temp and convert to F
  temperature = bme280.getTemperature();
  temperature = temperature * 1.8 + 32;

  // Read humidity
  humidity = bme280.getHumidity();

  // Read Pressure and convert to inches of water
  pressure = bme280.getPressure() * 0.00040146;

  // Read UV and convert to UV Index range
  uv = (float)sunlightSensor.ReadUV() / 100;

  // Calculate rain in inches. See tutorial on why it's .09
  rain = rain * .09;
}

void sendData() {
  // Convert float numbers to char arrays
  dtostrf(temperature, 4, 2, cTemp);
  dtostrf(humidity, 4, 2, cHum);
  dtostrf(pressure, 4, 2, cPress);
  dtostrf(uv, 4, 2, cUv);
  dtostrf(rain, 4, 2, cRain);

  // Try and send the data
  if (client.connect(server, 80)) {
    // Make a HTTP request:
    sprintf(buf, getPayload, cTemp, cHum, cPress, cRain, cUv);
    client.println(String("GET ") + buf + String(" HTTP/1.1"));
    //Serial.println(String("GET ") + buf + String(" HTTP/1.1"));
    client.println(F("Host: weatherstation.wunderground.com"));
    client.println(F("Connection: close"));
    client.println();
  }

}
void printWifiStatus()
{
  // Print the SSID of the network you're attached to
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());

  // Print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print(F("Signal strength (RSSI):"));
  Serial.print(rssi);
  Serial.println(F(" dBm"));
}

void bucketMove() {
  //Increment the rain detected
  rain++;
}

Battery Powered 

I'm going to put a battery in here to use it fully wirelessly. I did have to bend the cable a bit. The side with the barrel jack. It's better to do this when it isn't inside the Arduino.

USB out
USB out
Battery in
A good place for the battery
Back in the enclosure
Twisted shut

Rain Gauge 

There is one opening in the enclosure and that is for the rain gauge from Weather Station 3. Using the long cable, plug one side in the magnet switch socket. The other through the hole in the bottom and into D2. The base can be latched and now go get some weather data!

Base and gauge
Base and gauge
Here is the hole
Plug into the switch
Through the hole
Pull a bit
Into D2
Into D2
All done!

There is a more to this kit 

We added the other Arduino so you could have one outside with the Weather Station and the other would be inside with the LCD Display relaying the data. We'd like you to build this version first to make sure everything is working. The next section will release a week after this one shipped. It will just be a software update and another project using parts you have. Get hyped!