new code base
This commit is contained in:
+12
-41
@@ -1,43 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
// Ethernet
|
#define PRESSURE_PIN A7
|
||||||
#define IP_ADDRESS "192.168.0.55"
|
#define BUTTON_PIN A6
|
||||||
#define IP_PORT 8888
|
#define SROE_PIN A3
|
||||||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
#define STATUS_LED_1_PIN A0
|
||||||
|
#define DISPLAY_ACTIVE_PIN 9
|
||||||
// Pins
|
#define STATUS_LED_2_PIN 8
|
||||||
#define ETHERNET_INT 3
|
#define SRSER_PIN 7
|
||||||
#define ETHERNET_CS 10
|
#define SRCLK_PIN 6
|
||||||
#define ETHERNET_RESET A1
|
#define RCLK_PIN 5
|
||||||
|
#define SSR_PIN 4
|
||||||
#define HALL_INT 2
|
#define INT_ETH_PIN 3
|
||||||
#define PRESSURE_READ A7
|
#define INT_HALL_PIN 2
|
||||||
#define BUTTON_READ A6
|
|
||||||
#define DISPLAY_ACTIVE_READ 9
|
|
||||||
|
|
||||||
#define STATUS_LED_1 8
|
|
||||||
#define STATUS_LED_2 A0
|
|
||||||
|
|
||||||
#define SR_LATCH 5
|
|
||||||
#define SR_CLOCK 6
|
|
||||||
#define SR_DATA 7
|
|
||||||
#define SR_OUTPUT_ENABLE A3
|
|
||||||
#define SR_CLEAR A2
|
|
||||||
|
|
||||||
// Shift register output pins
|
|
||||||
#define VALVE_01 1
|
|
||||||
#define VALVE_02 2
|
|
||||||
#define VALVE_03 3
|
|
||||||
#define VALVE_04 4
|
|
||||||
#define VALVE_05 5
|
|
||||||
#define VALVE_06 6
|
|
||||||
#define VALVE_07 7
|
|
||||||
#define VALVE_08 8
|
|
||||||
#define VALVE_09 9
|
|
||||||
#define VALVE_10 10
|
|
||||||
#define VALVE_11 11
|
|
||||||
#define VALVE_12 12
|
|
||||||
|
|
||||||
#define ERROR_LAMP 15
|
|
||||||
#define PUMP_ON_LAMP 16
|
|
||||||
+62
-71
@@ -1,97 +1,88 @@
|
|||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
|
|
||||||
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin) {
|
Pump::Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin) {
|
||||||
this->pumpPin = pumpPin;
|
this->pumpPin = pumpPin;
|
||||||
this->pressurePin = pressurePin;
|
this->pressurePin = pressurePin;
|
||||||
this->flowPin = flowPin;
|
this->flowPin = flowPin;
|
||||||
|
this->errorLedPin = errorLedPin;
|
||||||
|
|
||||||
pinMode(this->pumpPin, OUTPUT);
|
pinMode(this->pumpPin, OUTPUT);
|
||||||
|
pinMode(this->errorLedPin, OUTPUT);
|
||||||
pinMode(this->pressurePin, INPUT);
|
pinMode(this->pressurePin, INPUT);
|
||||||
|
// Hall flow sensor?
|
||||||
|
|
||||||
digitalWrite(this->pumpPin, LOW);
|
digitalWrite(this->pumpPin, LOW);
|
||||||
|
digitalWrite(this->errorLedPin, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Pump::loop() {
|
void Pump::loop() {
|
||||||
if (millis() - lastMillisLoop < this->delayLoopMs)
|
switch (this->state) {
|
||||||
return;
|
case State::Off :
|
||||||
this->lastMillisLoop = millis();
|
|
||||||
|
|
||||||
this->calculatePressure();
|
|
||||||
this->calculateFlow();
|
|
||||||
this->checkErrors();
|
|
||||||
this->controlPump();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Pump::switchPump(bool state) {
|
|
||||||
if (this->currentStatus == Status::Failure)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (state && this->currentStatus == Status::Standby)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (state)
|
|
||||||
this->currentStatus = Status::On;
|
|
||||||
else
|
|
||||||
this->currentStatus = Status::Off;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t Pump::getPumpRunningTime() {
|
|
||||||
if (this->currentStatus == Status::On)
|
|
||||||
return millis() - this->pumpRunningStart;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pump::calculatePressure() {
|
|
||||||
uint16_t rawValue = analogRead(this->pressurePin);
|
|
||||||
if (rawValue < 104)
|
|
||||||
this->pressure = 0;
|
|
||||||
else if (rawValue > 922)
|
|
||||||
this->pressure = 550;
|
|
||||||
else
|
|
||||||
this->pressure = map(rawValue, 104, 922, 0, 550);
|
|
||||||
|
|
||||||
//TODO: Error checking
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pump::calculateFlow() {
|
|
||||||
//TODO: Error checking
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pump::checkErrors() {
|
|
||||||
if (this->error == Error::None)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (this->error )
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Pump::controlPump() {
|
|
||||||
if (this->lastStatus == this->currentStatus)
|
|
||||||
return;
|
|
||||||
this->lastStatus = this->currentStatus;
|
|
||||||
|
|
||||||
switch (this->currentStatus) {
|
|
||||||
case Status::Off :
|
|
||||||
digitalWrite(this->pumpPin, LOW);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Status::On :
|
case State::On :
|
||||||
digitalWrite(this->pumpPin, HIGH);
|
|
||||||
this->pumpRunningStart = millis();
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Status::Standby :
|
case State::Timer :
|
||||||
;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Status::Failure :
|
case State::Standby :
|
||||||
digitalWrite(this->pumpPin, LOW);
|
|
||||||
if (this->errorFunction)
|
break;
|
||||||
this->errorFunction(this->error);
|
|
||||||
|
case State::Error :
|
||||||
|
if (millis() - this->startTime > Pump::blinkTime) {
|
||||||
|
this->startTime = millis();
|
||||||
|
if (this->ledState) {
|
||||||
|
this->ledState = false;
|
||||||
|
digitalWrite(this->errorLedPin, LOW);
|
||||||
|
} else {
|
||||||
|
this->ledState = true;
|
||||||
|
digitalWrite(this->errorLedPin, HIGH);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Pump::activate(uint32_t duration = 0) {
|
||||||
|
this->startTime = millis();
|
||||||
|
if (duration) {
|
||||||
|
this->state = State::Timer;
|
||||||
|
this->duration = duration;
|
||||||
|
} else {
|
||||||
|
this->state = State::On;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Pump::stop() {
|
||||||
|
if (this->state != State::Error)
|
||||||
|
this->state = State::Off;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Pump::clearError() {
|
||||||
|
if (this->state == State::Error)
|
||||||
|
this->state = State::Off;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Pump::getPressure() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Pump::getRunningTime() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Pump::getFlow() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Pump::checkSensors() {
|
||||||
|
|
||||||
|
}
|
||||||
+19
-37
@@ -2,60 +2,42 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
class Pump {
|
class Pump {
|
||||||
public:
|
public:
|
||||||
enum Error : uint8_t {
|
static const uint16_t blinkTime = 1000;
|
||||||
None,
|
enum State {
|
||||||
HighPressure,
|
|
||||||
LowFlow,
|
|
||||||
Both
|
|
||||||
};
|
|
||||||
|
|
||||||
enum Status : uint8_t {
|
|
||||||
Off,
|
Off,
|
||||||
On,
|
On,
|
||||||
|
Timer,
|
||||||
Standby,
|
Standby,
|
||||||
Failure
|
Error
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void (*ErrorFunction)(Error);
|
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin, uint8_t errorLedPin);
|
||||||
|
|
||||||
Pump(uint8_t pumpPin, uint8_t pressurePin, uint8_t flowPin);
|
|
||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
void clearError() { this->error = Error::None; }
|
|
||||||
bool switchPump(bool state);
|
|
||||||
void incrementFlowCounter() { this->flowCounter++; };
|
|
||||||
|
|
||||||
uint8_t getFlowPin() { return this->flowPin; }
|
void activate(uint32_t duration = 0);
|
||||||
uint32_t getPumpRunningTime();
|
void stop();
|
||||||
|
void clearError();
|
||||||
|
|
||||||
void setErrorFunction(ErrorFunction ptr) { this->errorFunction = ptr; }
|
uint16_t getPressure();
|
||||||
|
uint16_t getRunningTime();
|
||||||
|
uint16_t getFlow();
|
||||||
|
|
||||||
|
State getState() { return this->state; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void calculatePressure();
|
void checkSensors();
|
||||||
void calculateFlow();
|
State state = State::Off;
|
||||||
void checkErrors();
|
|
||||||
void controlPump();
|
|
||||||
|
|
||||||
void setStatus(Status status);
|
bool ledState = false;
|
||||||
|
|
||||||
Status lastStatus = Status::Off;
|
|
||||||
Status currentStatus = Status::Off;
|
|
||||||
Error error = Error::None;
|
|
||||||
ErrorFunction errorFunction = nullptr;
|
|
||||||
|
|
||||||
uint8_t pumpPin;
|
uint8_t pumpPin;
|
||||||
uint8_t pressurePin;
|
uint8_t pressurePin;
|
||||||
uint8_t flowPin;
|
uint8_t flowPin;
|
||||||
uint8_t delayLoopMs = 20;
|
uint8_t errorLedPin;
|
||||||
|
|
||||||
uint16_t flowCounter = 0;
|
uint32_t startTime;
|
||||||
uint16_t pressure = 0;
|
uint32_t duration;
|
||||||
|
|
||||||
uint32_t lastMillisLoop = 0;
|
|
||||||
uint32_t lastPressureCalculation = 0;
|
|
||||||
uint32_t lastFlowCalculation = 0;
|
|
||||||
uint32_t pumpRunningStart = 0;
|
|
||||||
};
|
};
|
||||||
+61
-44
@@ -1,61 +1,78 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet2.h>
|
||||||
#include <EthernetUdp.h>
|
#include <SPI.h>
|
||||||
|
#include <shiftregister.h>
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
|
byte mac[] = { 0xC3, 0xF9, 0x7C, 0xB1, 0x8C, 0xF3 };
|
||||||
char ReplyBuffer[UDP_TX_PACKET_MAX_SIZE];
|
|
||||||
|
|
||||||
EthernetUDP Udp;
|
IPAddress ip(192, 168, 13, 0);
|
||||||
|
IPAddress gateway(192, 168, 0, 1);
|
||||||
|
IPAddress subnet(255, 255, 128, 0);
|
||||||
|
|
||||||
|
EthernetServer server(80);
|
||||||
|
|
||||||
|
ShiftRegister reg()
|
||||||
|
|
||||||
|
void listenForEthernetClients(void);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.println("Irrigation Arduino");
|
Serial.println("Arduino Bewaesserung");
|
||||||
|
|
||||||
Ethernet.init(ETHERNET_CS);
|
|
||||||
IPAddress ip;
|
|
||||||
ip.fromString(IP_ADDRESS);
|
|
||||||
Ethernet.begin(mac, ip);
|
Ethernet.begin(mac, ip);
|
||||||
|
server.begin();
|
||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
|
||||||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
|
||||||
while (true)
|
|
||||||
delay(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Ethernet.linkStatus() == LinkOFF)
|
|
||||||
Serial.println("Ethernet cable is not connected.");
|
|
||||||
|
|
||||||
Udp.begin(IP_PORT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// if there's data available, read a packet
|
listenForEthernetClients();
|
||||||
int packetSize = Udp.parsePacket();
|
|
||||||
if (packetSize) {
|
|
||||||
Serial.print("Received packet of size ");
|
|
||||||
Serial.println(packetSize);
|
|
||||||
Serial.print("From ");
|
|
||||||
IPAddress remote = Udp.remoteIP();
|
|
||||||
for (int i=0; i < 4; i++) {
|
|
||||||
Serial.print(remote[i], DEC);
|
|
||||||
if (i < 3) {
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Serial.print(", port ");
|
|
||||||
Serial.println(Udp.remotePort());
|
|
||||||
|
|
||||||
// read the packet into packetBuffer
|
void listenForEthernetClients() {
|
||||||
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
EthernetClient client = server.available();
|
||||||
Serial.println("Contents:");
|
|
||||||
Serial.println(packetBuffer);
|
|
||||||
|
|
||||||
// send a reply to the IP address and port that sent us the packet we received
|
if (!client) return;
|
||||||
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
|
|
||||||
Udp.write(ReplyBuffer);
|
Serial.println("Got a client");
|
||||||
Udp.endPacket();
|
// an http request ends with a blank line
|
||||||
|
boolean currentLineIsBlank = true;
|
||||||
|
while (client.connected()) {
|
||||||
|
if (client.available()) {
|
||||||
|
char c = client.read();
|
||||||
|
|
||||||
|
char buffer[7];
|
||||||
|
client.readBytesUntil(' ', buffer, 7);
|
||||||
|
Serial.print(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
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-Type: text/html");
|
||||||
|
client.println();
|
||||||
|
// print the current readings, in HTML format:
|
||||||
|
client.print("Temperature: ");
|
||||||
|
client.print("temperature");
|
||||||
|
client.print(" degrees C");
|
||||||
|
client.println("<br />");
|
||||||
|
client.print("Pressure: lUl");
|
||||||
|
client.print(" Pa");
|
||||||
|
client.println("<br />");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
delay(10);
|
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
|
||||||
|
Serial.println();
|
||||||
|
delay(1);
|
||||||
|
client.stop();
|
||||||
}
|
}
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
|
|
||||||
This directory is intended for PlatformIO Test Runner and project tests.
|
|
||||||
|
|
||||||
Unit Testing is a software testing method by which individual units of
|
|
||||||
source code, sets of one or more MCU program modules together with associated
|
|
||||||
control data, usage procedures, and operating procedures, are tested to
|
|
||||||
determine whether they are fit for use. Unit testing finds problems early
|
|
||||||
in the development cycle.
|
|
||||||
|
|
||||||
More information about PlatformIO Unit Testing:
|
|
||||||
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
|
|
||||||
Reference in New Issue
Block a user