mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 17:51:20 +00:00
Spritz code
This commit is contained in:
parent
9d2bc15180
commit
0690b9d177
@ -1,30 +1,42 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include "ArduinoJson.h"
|
||||||
const char* ssid = "Xiaomi_Router";
|
const char* ssid = "Xiaomi_Router";
|
||||||
const char* password = "UYQQMTHF";
|
const char* password = "UYQQMTHF";
|
||||||
|
|
||||||
WiFiClient espClient;
|
char message_buff[100];
|
||||||
|
|
||||||
int relayOne = 5; //D1 => Left Valve
|
WiFiClient espClient;
|
||||||
int relayTwo = 4; //D2 => Right Valve
|
PubSubClient client(espClient);
|
||||||
int relayThree = 0; // D3 => Center Valve
|
|
||||||
int relayFour = 13; //D7 => Center Pump
|
int leftValve = 5; //D1 => Left Valve //relayOne
|
||||||
int relayFive = 14; //D5 => Right Pump
|
int rightValve = 4; //D2 => Right Valve //relayTwo
|
||||||
int relaySix = 12; // D6 -> Left Pump
|
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() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
pinMode(relayOne, OUTPUT);
|
pinMode(leftValve, OUTPUT);
|
||||||
digitalWrite(relayOne, LOW);
|
digitalWrite(leftValve, LOW);
|
||||||
pinMode(relayTwo, OUTPUT);
|
pinMode(rightValve, OUTPUT);
|
||||||
digitalWrite(relayTwo, LOW);
|
digitalWrite(rightValve, LOW);
|
||||||
pinMode(relayThree, OUTPUT);
|
pinMode(centerValve, OUTPUT);
|
||||||
digitalWrite(relayThree, LOW);
|
digitalWrite(centerValve, LOW);
|
||||||
pinMode(relayFour, OUTPUT);
|
pinMode(centerPump, OUTPUT);
|
||||||
digitalWrite(relayFour, LOW);
|
digitalWrite(centerPump, LOW);
|
||||||
pinMode(relayFive, OUTPUT);
|
pinMode(rightPump, OUTPUT);
|
||||||
digitalWrite(relayFive, LOW);
|
digitalWrite(rightPump, LOW);
|
||||||
pinMode(relaySix, OUTPUT);
|
pinMode(leftPump, OUTPUT);
|
||||||
digitalWrite(relaySix, LOW);
|
digitalWrite(leftPump, LOW);
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("Spritz test");
|
Serial.println("Spritz test");
|
||||||
@ -42,6 +54,9 @@ void setup() {
|
|||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
client.setServer(mqtt_server, mqtt_port);
|
||||||
|
client.setCallback(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -55,46 +70,101 @@ void loop() {
|
|||||||
}
|
}
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("Connected !");
|
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);
|
DynamicJsonDocument result(1024);
|
||||||
delay(2000);
|
deserializeJson(result, payloadString);
|
||||||
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);*/
|
|
||||||
|
|
||||||
Serial.println("-- Start flow");
|
if(strcmp(topic, subscribeSpritzTopic) == 0) {
|
||||||
Serial.println("On ouvre l'arrivee");
|
int secondsAperol = result["aperol"];
|
||||||
digitalWrite(relayFour, HIGH);
|
int secondsProsecco = result["prosecco"];
|
||||||
delay(500);
|
int secondsWater = result["water"];
|
||||||
Serial.println("Pompe go");
|
spritzLogic(secondsAperol, secondsProsecco, secondsWater);
|
||||||
digitalWrite(relayThree, HIGH);
|
}
|
||||||
Serial.println("Versage");
|
}
|
||||||
delay(4000);
|
|
||||||
Serial.println("On coupe la pompe");
|
void spritzLogic(int secondsAperol, int secondsProsecco, int secondsWater) {
|
||||||
digitalWrite(relayThree, LOW);
|
// APEROL
|
||||||
delay(500);
|
Serial.println("-- Start flow -- APEROL");
|
||||||
Serial.println("On coupe l'arrivee");
|
Serial.println("Pompe en avant !"); // Start pump
|
||||||
digitalWrite(relayFour, LOW);
|
digitalWrite(centerPump, HIGH);
|
||||||
delay(3000);
|
delay(500);
|
||||||
Serial.println("-- End flow");
|
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