#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "TimerOne.h"
#include <math.h>
#include "cactus_io_BME280_I2C.h"
#define TX_Pin 8 // used to indicate web data tx
// Data wire is plugged into digital pin 9 on the Arduino
#define ONE_WIRE_BUS 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 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
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
int deviceCount = 0; // how many DS18B20 we keep on the OneWire
float OutminTemp; // keep track of minimum recorded Outside temp
float OutmaxTemp; // keep track of maximum recorded Outside temp
float InminTemp; // keep track of minimum recorded Inside temp
float InmaxTemp; // keep track of maximum recorded Inside temp
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
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(8081); // create a server listing on 192.168.1.45 port 80
void setup() {
// setup anemometer values
lastDirValue = 0;
rotations = 0;
// setup timer values
timerCount = 0;
{
sensors.begin(); // Start up the library
Serial.begin(9600);
// locate devices on the bus
// Serial.print("Locating devices...");
// Serial.print("Found ");
// deviceCount = sensors.getDeviceCount();
// Serial.print(deviceCount, DEC);
// Serial.println(" devices.");
// Serial.println("");
sensors.requestTemperatures();
InminTemp = sensors.getTempCByIndex(0);
InmaxTemp = sensors.getTempCByIndex(0);
OutminTemp = sensors.getTempCByIndex(1);
OutmaxTemp = sensors.getTempCByIndex(1);
}
// 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(WindSensor_Pin), isr_rotation,
FALLING);
// setup the timer for 0.5 second
Timer1.initialize(500000);
Timer1.attachInterrupt(isr_timer);
sei(); // Enable Interrupts
}
void loop() {
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
bme.readSensor();
// update min and max temp values for Outside and Inside Temperatures
if (sensors.getTempCByIndex(1) < OutminTemp) {
OutminTemp = sensors.getTempCByIndex(1);
}
if (sensors.getTempCByIndex(1) > OutmaxTemp) {
OutmaxTemp = sensors.getTempCByIndex(1);
}
if (sensors.getTempCByIndex(0) < InminTemp) {
InminTemp = sensors.getTempCByIndex(0);
}
if (sensors.getTempCByIndex(0) > InmaxTemp) {
InmaxTemp = sensors.getTempCByIndex(0);
}
// 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 10 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.print(
"<HTML><HEAD><TITLE>RadioActive Weather Server</TITLE></HEAD>");
client.println(
"<font color='red'><h2>RadioActive Weather Server</font></h2>");
client.print(
"Agios Merkouris, Sifnos @ 36.987832, 24.719809<br><br>");
client.println("<html><body>");
digitalWrite(TX_Pin, HIGH); // Turn the TX LED on
client.print(
"<span style=\"font-size: 26px\";>Outside Temperature now ");
client.print(sensors.getTempCByIndex(1));
client.println("C<br>");
client.print("<br> Inside Temperature now ");
client.print(sensors.getTempCByIndex(0));
client.println("C<br>");
client.print("<br> Wind Speed now ");
client.print(windSpeed);
client.println(" mph<br>");
getWindDirection();
client.print("<br> Direction now ");
client.print(calDirection);
if ((calDirection) < 23) {
client.print(" North");
}
if ((calDirection > 22) && (calDirection < 68)) {
client.print(" NorthEast");
}
if ((calDirection > 67) && (calDirection < 113)) {
client.print(" East");
}
if ((calDirection > 112) && (calDirection < 158)) {
client.print(" SouthEast");
}
if ((calDirection > 157) && (calDirection < 203)) {
client.print(" South");
}
if ((calDirection > 202) && (calDirection < 247)) {
client.print(" SouthWest");
}
if ((calDirection > 246) && (calDirection < 292)) {
client.print(" West");
}
if ((calDirection > 291) && (calDirection < 337)) {
client.print(" NorthWest");
}
if ((calDirection > 336) && (calDirection <= 360)) {
client.print(" North");
}
client.println("<br>");
client.print("<br> Humidity now ");
client.print(bme.getHumidity());
client.println("%<br>");
client.print("<br> Pressure now ");
client.print(bme.getPressure_MB());
client.println("mb%<br>");
client.print("<br> Outside Min Temperature was ");
client.print(OutminTemp);
client.println("C<br>");
client.print("<br> Outside Max Temperature was ");
client.print(OutmaxTemp);
client.println("C<br>");
client.print("<br> Inside Min Temperature was ");
client.print(InminTemp);
client.println("C<br>");
client.print("<br> Inside Max Temperature was ");
client.print(InmaxTemp);
client.println("C");
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 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;
}
Weather radio station with Arduino UNO/MEGA Ethernet problem with EMailSender
Renzo, good morning and a good week to you my friend.
You 're asking me what kind of problem do I have, it doesn't work anymore!
On the UNO the code I will share with you works fine, it's a webserver Weather Station that produces an HTML page with all the different measures, on a static IP.
Now on the Mega, no page loads, no output whatsoever, nothing!
Try it yourself (if you can/want/have the time) and let me know.
Here's the code: