switched to mqtt, added class valve
This commit is contained in:
+64
-98
@@ -1,121 +1,87 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <shiftregister.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "pump.h"
|
||||
#include "valve.h"
|
||||
|
||||
#define COUNT_HTTP_METHOD 3
|
||||
#define METHOD_LENGTH 6
|
||||
#define BUFFER_SIZE 128
|
||||
|
||||
byte mac[] = { 0xC3, 0xF9, 0x7C, 0xB1, 0x8C, 0xF3 };
|
||||
|
||||
IPAddress ip(192, 168, 13, 0);
|
||||
IPAddress gateway(192, 168, 0, 1);
|
||||
IPAddress subnet(255, 255, 128, 0);
|
||||
// IPAddress ip(192, 168, 13, 0);
|
||||
// IPAddress gateway(192, 168, 0, 1);
|
||||
// IPAddress subnet(255, 255, 128, 0);
|
||||
|
||||
EthernetServer server(80);
|
||||
IPAddress ip(192, 168, 178, 202);
|
||||
IPAddress gateway(192, 168, 178, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
IPAddress dns(192, 168, 178, 1);
|
||||
IPAddress server(192, 168, 178, 35);
|
||||
// IPAddress server(91, 121, 93, 94);
|
||||
|
||||
const byte const httpMethos[COUNT_HTTP_METHOD][METHOD_LENGTH] = { 'GET ', 'PATCH ', 'DELETE' };
|
||||
ShiftRegister reg(RCLK_PIN, SRCLK_PIN, SRSER_PIN, 2);
|
||||
Pump pump(SSR_PIN, PRESSURE_PIN, INT_HALL_PIN, STATUS_LED_1_PIN);
|
||||
Valve valves;
|
||||
EthernetClient ethClient;
|
||||
PubSubClient client(ethClient);
|
||||
|
||||
// ShiftRegister reg()
|
||||
|
||||
void listenForEthernetClients(void);
|
||||
void reconnect();
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Arduino Bewaesserung");
|
||||
Serial.println("Arduino Irrigation");
|
||||
|
||||
Ethernet.begin(mac, ip);
|
||||
server.begin();
|
||||
valves.addShiftRegister(®);
|
||||
for (uint8_t i = 0; i < VALVE_COUNT; i++)
|
||||
valves.add(valvePins[i], true);
|
||||
valves.closeAll();
|
||||
|
||||
client.setServer(server, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
Ethernet.begin(mac, ip, dns, gateway, subnet);
|
||||
|
||||
// Allow the hardware to sort itself out
|
||||
delay(1500);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
listenForEthernetClients();
|
||||
if (!client.connected())
|
||||
reconnect();
|
||||
client.loop();
|
||||
pump.loop();
|
||||
}
|
||||
|
||||
void listenForEthernetClients() {
|
||||
EthernetClient client = server.available();
|
||||
if (!client) return;
|
||||
|
||||
Serial.println("Got a client");
|
||||
byte buffer[256];
|
||||
|
||||
uint8_t method = -1;
|
||||
|
||||
uint8_t i = 0;
|
||||
uint8_t countChars = client.readBytesUntil(' ', buffer, METHOD_LENGTH);
|
||||
for (countChars--; countChars < METHOD_LENGTH; countChars++ )
|
||||
buffer[countChars] = ' ';
|
||||
for (uint8_t i = 0; i < COUNT_HTTP_METHOD; i++)
|
||||
if (memcmp(buffer, httpMethos[i], METHOD_LENGTH)) {
|
||||
method = i;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint16_t len = client.available();
|
||||
if (len > 0)
|
||||
client.read(buffer, len);
|
||||
Serial.write(buffer, len);
|
||||
|
||||
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 />");
|
||||
|
||||
client.stop();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// // an http request ends with a blank line
|
||||
// boolean currentLineIsBlank = true;
|
||||
// while (client.connected()) {
|
||||
// if (client.available()) {
|
||||
// char c = client.read();
|
||||
// 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;
|
||||
// }
|
||||
// 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
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
for (unsigned int i=0;i<length;i++) {
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
delay(1);
|
||||
// client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
if (client.connect("arduinoClient")) {
|
||||
Serial.println("connected");
|
||||
client.publish("outTopic","reconnect");
|
||||
client.subscribe("inTopic");
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user