Complete Weather Station with WebSockets connection
Weather Station with, wind direction, wind speed, rain, temperature, humidity and presssure. Use webSocket to send all data to a client.
Components and supplies
1
Resistor 10k ohm
1
Ethernet Cable, Cat5e
1
Solderless Breadboard Half Size
1
Jumper wires (generic)
1
Arduino MKR1000
Project description
Code
Weather Station with websocket example
arduino
1#include <WeatherStationLib.h> 2#include <WiFiConnect.h> 3#include <WebSocketServer.h> 4 5#define WINDSPEEDPIN 1 6#define DIRPIN 15 7#define RAINPIN 4 8 9// I need to Call Adafruit library from the this sketch , because when I tried to call the method 10// .begin() the MKR100 hangs. So include the Adafruit library in the WeatherStationLib library 11// but I called that method from setup() and I read the BME280 from my Library. Very weird 12extern int bmeError; 13extern Adafruit_BME280 bme; 14 15String ssid = "XXXXXX"; 16String passwd = "YYYYYYY"; 17int webSocketPort = 51000; 18WiFiServer server(webSocketPort); 19WebSocketServer webSocketServer; 20WiFiConnect wifi(ssid, passwd); 21WeatherStation weather(RAINPIN, DIRPIN, WINDSPEEDPIN, 1); 22 23//Print status on serial 24void printStatus() { 25 26 if (weather.available()) { 27 28 Serial.print("Speed: "); 29 Serial.print(weather.windSpeed()); 30 31 Serial.print(" Gust: "); 32 Serial.print(weather.windGust()); 33 34 Serial.print(" Direction: "); 35 Serial.print(weather.windDirection()); 36 37 Serial.print(" Rain: "); 38 Serial.print(weather.actualRain()); 39 40 Serial.print(" Last Rain: "); 41 Serial.print(weather.lastRain()); 42 43 Serial.print(" Acum. Rain: "); 44 Serial.print(weather.accumulatedRain()); 45 46 Serial.print(" Last Day Rain: "); 47 Serial.print(weather.lastDayRain()); 48 49 Serial.print("Temp: "); 50 Serial.print(weather.temperature()); 51 52 Serial.print(" Hum: "); 53 Serial.print(weather.humidity()); 54 55 Serial.print(" Press: "); 56 Serial.println(weather.pressure()); 57 58 Serial.println(); 59 } 60 61} 62 63 64// the setup function runs once when you press reset or power the board 65void setup() { 66 Serial.begin(9600); 67 while (!Serial) {} 68 69 int i; 70 // Try 3 times to connect with the wifi 71 for (i = 0; i < 3; i++) { 72 if (wifi.connect() ) { 73 break; 74 } 75 } 76 77 if (!wifi.connected) { 78 Serial.println("Connection Fail!"); 79 while (true) {} 80 } 81 else { 82 Serial.println("Connected!"); 83 } 84 85 if (bme.begin()) 86 bmeError = false; 87 else 88 bmeError = true; 89 90 // start the server: 91 server.begin(); 92 93 Serial.println(); 94 Serial.print("Waiting for Client Connection on IP: "); Serial.print(wifi.ip()); 95 Serial.print(":"); Serial.println(webSocketPort); 96} 97 98// the loop function runs over and over again forever 99void loop() { 100 String data; 101 int i = 0, startTime, sendTime = 10 * 1000; 102 103 //looks if somebody connect 104 WiFiClient client = server.available(); 105 106 if (client.connected() && webSocketServer.handshake(client)) { 107 108 while (client.connected()) { 109 data = webSocketServer.getData(); 110 111 if (data.length() > 0) { 112 Serial.println("Received: " + data); 113 } 114 115 //printStatus(); 116 if (weather.available()) { 117 data = "MSGOK," + (String)weather.windSpeed() + "," + (String)weather.windGust() + "," + (String)weather.windDirection() + "," + 118 (String)weather.actualRain() + "," + (String)weather.lastDayRain() + "," + (String)weather.accumulatedRain() + "," + 119 (String)weather.temperature() + "," + (String)weather.humidity() + "," + (String)weather.pressure(); 120 121 //we can not use a delay() because conflict with data retrieval 122 if (millis() - startTime > sendTime) { 123 webSocketServer.sendData(data); 124 Serial.print("Sending Data: "); Serial.println(data); 125 startTime = millis(); 126 } 127 } 128 } 129 } 130 131} 132
Weather Station with websocket example
arduino
1#include <WeatherStationLib.h> 2#include <WiFiConnect.h> 3#include <WebSocketServer.h> 4 5#define WINDSPEEDPIN 1 6#define DIRPIN 15 7#define RAINPIN 4 8 9// I need to Call Adafruit library from the this sketch , because when I tried to call the method 10// .begin() the MKR100 hangs. So include the Adafruit library in the WeatherStationLib library 11// but I called that method from setup() and I read the BME280 from my Library. Very weird 12extern int bmeError; 13extern Adafruit_BME280 bme; 14 15String ssid = "XXXXXX"; 16String passwd = "YYYYYYY"; 17int webSocketPort = 51000; 18WiFiServer server(webSocketPort); 19WebSocketServer webSocketServer; 20WiFiConnect wifi(ssid, passwd); 21WeatherStation weather(RAINPIN, DIRPIN, WINDSPEEDPIN, 1); 22 23//Print status on serial 24void printStatus() { 25 26 if (weather.available()) { 27 28 Serial.print("Speed: "); 29 Serial.print(weather.windSpeed()); 30 31 Serial.print(" Gust: "); 32 Serial.print(weather.windGust()); 33 34 Serial.print(" Direction: "); 35 Serial.print(weather.windDirection()); 36 37 Serial.print(" Rain: "); 38 Serial.print(weather.actualRain()); 39 40 Serial.print(" Last Rain: "); 41 Serial.print(weather.lastRain()); 42 43 Serial.print(" Acum. Rain: "); 44 Serial.print(weather.accumulatedRain()); 45 46 Serial.print(" Last Day Rain: "); 47 Serial.print(weather.lastDayRain()); 48 49 Serial.print("Temp: "); 50 Serial.print(weather.temperature()); 51 52 Serial.print(" Hum: "); 53 Serial.print(weather.humidity()); 54 55 Serial.print(" Press: "); 56 Serial.println(weather.pressure()); 57 58 Serial.println(); 59 } 60 61} 62 63 64// the setup function runs once when you press reset or power the board 65void setup() { 66 Serial.begin(9600); 67 while (!Serial) {} 68 69 int i; 70 // Try 3 times to connect with the wifi 71 for (i = 0; i < 3; i++) { 72 if (wifi.connect() ) { 73 break; 74 } 75 } 76 77 if (!wifi.connected) { 78 Serial.println("Connection Fail!"); 79 while (true) {} 80 } 81 else { 82 Serial.println("Connected!"); 83 } 84 85 if (bme.begin()) 86 bmeError = false; 87 else 88 bmeError = true; 89 90 // start the server: 91 server.begin(); 92 93 Serial.println(); 94 Serial.print("Waiting for Client Connection on IP: "); Serial.print(wifi.ip()); 95 Serial.print(":"); Serial.println(webSocketPort); 96} 97 98// the loop function runs over and over again forever 99void loop() { 100 String data; 101 int i = 0, startTime, sendTime = 10 * 1000; 102 103 //looks if somebody connect 104 WiFiClient client = server.available(); 105 106 if (client.connected() && webSocketServer.handshake(client)) { 107 108 while (client.connected()) { 109 data = webSocketServer.getData(); 110 111 if (data.length() > 0) { 112 Serial.println("Received: " + data); 113 } 114 115 //printStatus(); 116 if (weather.available()) { 117 data = "MSGOK," + (String)weather.windSpeed() + "," + (String)weather.windGust() + "," + (String)weather.windDirection() + "," + 118 (String)weather.actualRain() + "," + (String)weather.lastDayRain() + "," + (String)weather.accumulatedRain() + "," + 119 (String)weather.temperature() + "," + (String)weather.humidity() + "," + (String)weather.pressure(); 120 121 //we can not use a delay() because conflict with data retrieval 122 if (millis() - startTime > sendTime) { 123 webSocketServer.sendData(data); 124 Serial.print("Sending Data: "); Serial.println(data); 125 startTime = millis(); 126 } 127 } 128 } 129 } 130 131} 132
Downloadable files
WeatherStation_Arduino_Connections
WeatherStation_Arduino_Connections

Connections ARduino BME280
Connections ARduino BME280

WeatherStation_Arduino_Connections
WeatherStation_Arduino_Connections

Comments
Only logged in users can leave comments