Last active
January 9, 2017 22:56
-
-
Save djneo92nl/88cc523eed20b284acbeab2ac8763bc3 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
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
#include <NewRemoteTransmitter.h> | |
NewRemoteTransmitter transmitter(153, 3, 260, 3); | |
IPAddress server(146, 185, 147, 98); // Update these with values suitable for your network. | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
//Ethernet Mac Adres | |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; | |
long lastReconnectAttempt = 0; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
char* message = payload; | |
// Memory pool for JSON object tree. | |
// | |
// Inside the brackets, 200 is the size of the pool in bytes, | |
// If the JSON object is more complex, you need to increase that value. | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(message); | |
// Test if parsing succeeds. | |
if (!root.success()) { | |
Serial.println("parseObject() failed"); | |
return; | |
} | |
String command = root["command"]; | |
int device = root["device"]; | |
String value = root["value"]; | |
Serial.println(command); | |
if(command == "off"){ | |
transmitter.sendUnit(root["device"], false); | |
} | |
if(command == "on"){ | |
Serial.println("ok"); | |
transmitter.sendUnit(device, true); | |
} | |
if(command == "dim"){ | |
transmitter.sendDim(root["device"], root["value"]); | |
} | |
} | |
boolean reconnect() { | |
if (client.connect("arduinoClient")) { | |
Serial.println("Connected"); | |
// Once connected, publish an announcement... | |
client.publish("device/003/status","{active}"); | |
// ... and resubscribe | |
client.subscribe("device/003/command"); | |
} | |
return client.connected(); | |
} | |
void setup() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
// this check is only needed on the Leonardo: | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
// start the Ethernet connection: | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
// no point in carrying on, so do nothing forevermore: | |
for (;;) | |
; | |
} | |
// print your local IP address: | |
printIPAddress(); | |
client.setServer(server, 1883); | |
client.setCallback(callback); | |
} | |
void loop() { | |
if (!client.connected()) { | |
long now = millis(); | |
if (now - lastReconnectAttempt > 5000) { | |
lastReconnectAttempt = now; | |
// Attempt to reconnect | |
if (reconnect()) { | |
lastReconnectAttempt = 0; | |
} | |
} | |
} else { | |
// Client connected | |
client.loop(); | |
} | |
} | |
void printIPAddress() | |
{ | |
Serial.print("My IP address: "); | |
for (byte thisByte = 0; thisByte < 4; thisByte++) { | |
// print the value of each byte of the IP address: | |
Serial.print(Ethernet.localIP()[thisByte], DEC); | |
Serial.print("."); | |
} | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment