Created
May 16, 2015 07:43
-
-
Save Elwell/35dea60bf32e02ed9fcf to your computer and use it in GitHub Desktop.
Arduino Ethernet to MQTT publisher
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
// Reed Switch to MQTT publisher for home | |
// Andrew Elwell, May 2015 | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
byte mac[] = { 0x00, 0x04, 0xA3, 0x2C, 0x1D, 0xF1 }; | |
byte server[] = { 10, 1, 1, 251 }; | |
//void callback(char* topic, byte* payload, unsigned int length); | |
EthernetClient ethClient; | |
PubSubClient client(server, 1883, 0, ethClient); | |
//int led = 13; // BLINKY THING - cant use if ethernet active | |
int gate= 2; // Blue -> Green pair | |
int gateState = 0; | |
int gatelast = 0; | |
int lhs = 3; | |
int lhsState = 0; | |
int lhslast = 0; | |
int rhs = 5; | |
int rhsState = 0; | |
int rhslast = 0; | |
int door = 6; | |
int doorState = 0; | |
int doorlast = 0; | |
int rear = 7; | |
int rearState = 0; | |
int rearlast = 0; | |
void setup() { | |
delay( 50 ); // allow some time (50 ms) after powerup and sketch start, for the Wiznet W5100 Reset IC to release and come out of reset. | |
if (Ethernet.begin(mac) == 0) { | |
// Serial.println("Failed to configure Ethernet using DHCP"); | |
return; | |
} | |
// pinMode(led, OUTPUT); | |
pinMode(gate, INPUT); // set pin to input | |
digitalWrite(gate, HIGH); // turn on pullup resistors | |
pinMode(lhs, INPUT); | |
digitalWrite(lhs, HIGH); | |
pinMode(rhs, INPUT); | |
digitalWrite(rhs, HIGH); | |
pinMode(door, INPUT); | |
digitalWrite(door, HIGH); | |
pinMode(rear, INPUT); | |
digitalWrite(rear, HIGH); | |
//Connect to MQTT | |
if (!client.connected()) | |
{ | |
client.connect("Etherten01"); | |
client.publish("etherten/status", "ONLINE"); | |
} | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
gateState = digitalRead(gate); | |
if (gateState != gatelast) { | |
if (gateState == HIGH) { | |
client.publish("etherten/gate", "open", strlen("open"), 1); | |
} else { | |
client.publish("etherten/gate", "closed", strlen("closed"), 1); | |
} | |
} | |
gatelast = gateState ; | |
lhsState = digitalRead(lhs); | |
if (lhsState != lhslast) { | |
if (lhsState == HIGH) { | |
client.publish("etherten/left_roller", "open", strlen("open"), 1); | |
} else { | |
client.publish("etherten/left_roller", "closed", strlen("closed"), 1); | |
} | |
} | |
lhslast = lhsState ; | |
rhsState = digitalRead(rhs); | |
if (rhsState != rhslast) { | |
if (rhsState == HIGH) { | |
client.publish("etherten/right_roller", "open", strlen("open"), 1); | |
} else { | |
client.publish("etherten/right_roller", "closed", strlen("closed"), 1); | |
} | |
} | |
rhslast = rhsState ; | |
doorState = digitalRead(door); | |
if (doorState != doorlast) { | |
if (doorState == HIGH) { | |
client.publish("etherten/garage_door", "open", strlen("open"), 1); | |
} else { | |
client.publish("etherten/garage_door", "closed", strlen("closed"), 1); | |
} | |
} | |
doorlast = doorState ; | |
rearState = digitalRead(rear); | |
if (rearState != rearlast) { | |
if (rearState == HIGH) { | |
client.publish("etherten/rear_roller", "open", strlen("open"), 1); | |
} else { | |
client.publish("etherten/rear_roller", "closed", strlen("closed"), 1); | |
} | |
} | |
rearlast = rearState ; | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment