mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
Spritz code
This commit is contained in:
parent
9d2bc15180
commit
0690b9d177
@ -1,30 +1,42 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "ArduinoJson.h"
|
||||
const char* ssid = "Xiaomi_Router";
|
||||
const char* password = "UYQQMTHF";
|
||||
|
||||
WiFiClient espClient;
|
||||
char message_buff[100];
|
||||
|
||||
int relayOne = 5; //D1 => Left Valve
|
||||
int relayTwo = 4; //D2 => Right Valve
|
||||
int relayThree = 0; // D3 => Center Valve
|
||||
int relayFour = 13; //D7 => Center Pump
|
||||
int relayFive = 14; //D5 => Right Pump
|
||||
int relaySix = 12; // D6 -> Left Pump
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
int leftValve = 5; //D1 => Left Valve //relayOne
|
||||
int rightValve = 4; //D2 => Right Valve //relayTwo
|
||||
int centerValve = 0; // D3 => Center Valve //relayThree
|
||||
int centerPump = 13; //D7 => Center Pump //relayFour
|
||||
int rightPump = 14; //D5 => Right Pump //relayFive
|
||||
int leftPump = 12; // D6 -> Left Pump //relaySix
|
||||
|
||||
// MQTT Infos
|
||||
const char* subscribeSpritzTopic = "DIY/Spritz/set";
|
||||
const char* getSpritzTopic = "DIY/Spritz/get";
|
||||
const int mqtt_port = 1883;
|
||||
const char* mqtt_server = "192.168.31.140";
|
||||
const char* mqtt_client = "WemosD1MiniClient-Spritz";
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
pinMode(relayOne, OUTPUT);
|
||||
digitalWrite(relayOne, LOW);
|
||||
pinMode(relayTwo, OUTPUT);
|
||||
digitalWrite(relayTwo, LOW);
|
||||
pinMode(relayThree, OUTPUT);
|
||||
digitalWrite(relayThree, LOW);
|
||||
pinMode(relayFour, OUTPUT);
|
||||
digitalWrite(relayFour, LOW);
|
||||
pinMode(relayFive, OUTPUT);
|
||||
digitalWrite(relayFive, LOW);
|
||||
pinMode(relaySix, OUTPUT);
|
||||
digitalWrite(relaySix, LOW);
|
||||
pinMode(leftValve, OUTPUT);
|
||||
digitalWrite(leftValve, LOW);
|
||||
pinMode(rightValve, OUTPUT);
|
||||
digitalWrite(rightValve, LOW);
|
||||
pinMode(centerValve, OUTPUT);
|
||||
digitalWrite(centerValve, LOW);
|
||||
pinMode(centerPump, OUTPUT);
|
||||
digitalWrite(centerPump, LOW);
|
||||
pinMode(rightPump, OUTPUT);
|
||||
digitalWrite(rightPump, LOW);
|
||||
pinMode(leftPump, OUTPUT);
|
||||
digitalWrite(leftPump, LOW);
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println("Spritz test");
|
||||
@ -42,6 +54,9 @@ void setup() {
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
client.setServer(mqtt_server, mqtt_port);
|
||||
client.setCallback(callback);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@ -55,46 +70,101 @@ void loop() {
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.println("Connected !");
|
||||
} else {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
}
|
||||
}
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
String payloadString;
|
||||
for (int i = 0; i < length; i++) {
|
||||
Serial.print((char)payload[i]);
|
||||
payloadString += (char)payload[i];
|
||||
}
|
||||
|
||||
/*digitalWrite(relayOne, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relayOne, LOW);
|
||||
delay(2000);
|
||||
digitalWrite(relayTwo, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relayTwo, LOW);
|
||||
delay(2000);
|
||||
digitalWrite(relayThree, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relayThree, LOW);
|
||||
delay(2000);
|
||||
digitalWrite(relayFour, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relayFour, LOW);
|
||||
delay(2000);
|
||||
digitalWrite(relayFive, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relayFive, LOW);
|
||||
delay(2000);
|
||||
digitalWrite(relaySix, HIGH);
|
||||
delay(2000);
|
||||
digitalWrite(relaySix, LOW);
|
||||
delay(2000);*/
|
||||
DynamicJsonDocument result(1024);
|
||||
deserializeJson(result, payloadString);
|
||||
|
||||
Serial.println("-- Start flow");
|
||||
Serial.println("On ouvre l'arrivee");
|
||||
digitalWrite(relayFour, HIGH);
|
||||
delay(500);
|
||||
Serial.println("Pompe go");
|
||||
digitalWrite(relayThree, HIGH);
|
||||
Serial.println("Versage");
|
||||
delay(4000);
|
||||
Serial.println("On coupe la pompe");
|
||||
digitalWrite(relayThree, LOW);
|
||||
delay(500);
|
||||
Serial.println("On coupe l'arrivee");
|
||||
digitalWrite(relayFour, LOW);
|
||||
delay(3000);
|
||||
Serial.println("-- End flow");
|
||||
if(strcmp(topic, subscribeSpritzTopic) == 0) {
|
||||
int secondsAperol = result["aperol"];
|
||||
int secondsProsecco = result["prosecco"];
|
||||
int secondsWater = result["water"];
|
||||
spritzLogic(secondsAperol, secondsProsecco, secondsWater);
|
||||
}
|
||||
}
|
||||
|
||||
void spritzLogic(int secondsAperol, int secondsProsecco, int secondsWater) {
|
||||
// APEROL
|
||||
Serial.println("-- Start flow -- APEROL");
|
||||
Serial.println("Pompe en avant !"); // Start pump
|
||||
digitalWrite(centerPump, HIGH);
|
||||
delay(500);
|
||||
Serial.println("Valve ouverte");
|
||||
digitalWrite(centerValve, HIGH);
|
||||
Serial.println("Versage");
|
||||
delay(secondsAperol);
|
||||
Serial.println("Valve fermée");
|
||||
digitalWrite(centerValve, LOW);
|
||||
delay(500);
|
||||
Serial.println("Pompe fermée");
|
||||
digitalWrite(centerPump, LOW);
|
||||
Serial.println("-- End flow -- APEROL");
|
||||
|
||||
Serial.println("-- Start flow -- PROSECCO");
|
||||
Serial.println("Pompe en avant !"); // Start pump
|
||||
digitalWrite(rightPump, HIGH);
|
||||
delay(500);
|
||||
Serial.println("Valve ouverte");
|
||||
digitalWrite(rightValve, HIGH);
|
||||
Serial.println("Versage");
|
||||
delay(secondsProsecco);
|
||||
Serial.println("Valve fermée");
|
||||
digitalWrite(rightValve, LOW);
|
||||
delay(500);
|
||||
Serial.println("Pompe fermée");
|
||||
digitalWrite(rightPump, LOW);
|
||||
Serial.println("-- End flow -- PROSECCO");
|
||||
|
||||
Serial.println("-- Start flow -- WATER");
|
||||
Serial.println("Pompe en avant !"); // Start pump
|
||||
digitalWrite(leftPump, HIGH);
|
||||
delay(500);
|
||||
Serial.println("Valve ouverte");
|
||||
digitalWrite(leftValve, HIGH);
|
||||
Serial.println("Versage");
|
||||
delay(secondsWater);
|
||||
Serial.println("Valve fermée");
|
||||
digitalWrite(leftValve, LOW);
|
||||
delay(500);
|
||||
Serial.println("Pompe fermée");
|
||||
digitalWrite(leftPump, LOW);
|
||||
Serial.println("-- End flow -- WATER");
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
// Attempt to connect
|
||||
if (client.connect(mqtt_client)) {
|
||||
Serial.println("connected");
|
||||
// Everything ok, it's connected to mqtt broker
|
||||
client.subscribe(subscribeSpritzTopic);
|
||||
String ipString = "{\"Name\":\"SpritzDealer\",\"IpAddress\":\""+ WiFi.localIP().toString()+"\"}";
|
||||
ipString.toCharArray(message_buff, ipString.length()+1);
|
||||
client.publish("IpAddress",message_buff);
|
||||
} else {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user