Last active
July 14, 2021 09:09
-
-
Save anarkistix/d0d454419a126badee62897ee5eb0276 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include <MQTT.h> | |
int led = D7; | |
int drippBryter = D1; | |
int sprederBryter = D2; | |
String ipString; | |
SYSTEM_MODE(MANUAL) | |
STARTUP(WiFi.selectAntenna(ANT_INTERNAL)) | |
void callback(char* topic, byte* payload, unsigned int length); | |
/** | |
* if want to use IP address, | |
* byte server[] = { XXX,XXX,XXX,XXX }; | |
* MQTT client(server, 1883, callback); | |
* want to use domain name, | |
* exp) iot.eclipse.org is Eclipse Open MQTT Broker: https://iot.eclipse.org/getting-started | |
* MQTT client("iot.eclipse.org", 1883, callback); | |
**/ | |
MQTT client("XXX.XXX.XX.XX", 1883, callback); | |
// recieve message | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char p[length + 1]; | |
memcpy(p, payload, length); | |
p[length] = NULL; | |
String topicStr = topic; | |
//Kode for dryppvanning | |
if (topicStr == "your topic string 2") | |
{ | |
Particle.publish("DRYPP"); | |
//turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message | |
if(payload[0] == 49){ | |
//Skru på vannet | |
Particle.publish("Skrur på vannet"); | |
turnWaterOn(); | |
} | |
//turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message | |
else if (payload[0] == 48){ | |
Particle.publish("Skrur av vannet"); | |
turnWaterOff(); | |
} | |
} | |
//Kode for sprederen | |
if (topicStr == "your topic string 1") | |
{ | |
Particle.publish("SPREDER"); | |
//turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message | |
if(payload[0] == 49){ | |
//Skru på vannet | |
turnSprederWaterOn(); | |
} | |
//turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message | |
else if (payload[0] == 48){ | |
turnSprederWaterOff(); | |
} | |
} | |
} | |
void turnWaterOn(){ | |
digitalWrite(led, HIGH); | |
digitalWrite(drippBryter, HIGH); | |
client.publish("your topic string 2", "1"); //Setter status på bryteren i HA | |
} | |
void turnWaterOff(){ | |
digitalWrite(led, LOW); | |
digitalWrite(drippBryter, LOW); | |
Particle.publish("Skrur AV bryter"); | |
client.publish("your topic string 2", "0"); //Setter status på bryteren i HA | |
} | |
void turnSprederWaterOn(){ | |
digitalWrite(led, HIGH); | |
digitalWrite(sprederBryter, HIGH); | |
client.publish("your topic string 1", "1"); //Setter status på bryteren i HA | |
} | |
void turnSprederWaterOff(){ | |
digitalWrite(led, LOW); | |
digitalWrite(sprederBryter, LOW); | |
Particle.publish("Skrur AV spreder"); | |
client.publish("your topic string 1", "0"); //Setter status på bryteren i HA | |
} | |
void setup() { | |
WiFi.on(); | |
WiFi.useDynamicIP(); | |
WiFi.connect(); | |
waitUntil(WiFi.ready); | |
Particle.connect(); | |
pinMode(led, OUTPUT); | |
pinMode(drippBryter, OUTPUT); | |
pinMode(sprederBryter, OUTPUT); | |
// connect to the server | |
client.connect("sparkclient"); | |
// publish/subscribe | |
if (client.isConnected()) { | |
client.publish("water_system_mqtt_alive","Water system setting up. MQTT ALIVE."); | |
client.subscribe("your topic string 1"); | |
client.subscribe("your topic string 2"); | |
} | |
} | |
void loop() { | |
Particle.process(); | |
if (client.isConnected()) { | |
Serial.println("mqtt client loop"); | |
client.loop(); | |
} else { | |
Serial.println("mqtt connect loop"); | |
client.connect("sparkclient"); | |
if (client.isConnected()) { | |
client.subscribe("your topic string"); | |
client.subscribe("your topic string"); | |
} | |
} | |
delay(1000); | |
client.publish("water_system_mqtt_alive",String(Time.second())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment