controller.ino
bf524f6f
 
 #include <SPI.h>
 #include <EthernetENC.h>
 #include <PubSubClient.h>
 
 // Update these with values suitable for your network.
 byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
 IPAddress ip(10, 13, 37, 165); 
 byte server[] = {10, 13, 37, 101};
 
 void callback(char* topic, byte* payload, unsigned int length) {
   // handle message arrived
 }
 
 EthernetClient ethClient;
 PubSubClient client(server, 1883, callback, ethClient);
 
 void setup()
 {
   Serial.begin(9600);
   Ethernet.begin(mac, ip);
   client.setBufferSize(255);
  
 }
 
 void reconnect() {
   // Loop until we're reconnected
   while (!client.connected()) {
     Serial.print("Attempting MQTT connection...");
     // Attempt to connect
     if (client.connect("connectionName", "[[USER]]", "[[PASS]]")) {
       Serial.println("connected");
       // Once connected, publish an announcement...
       client.publish("outTopic","hello world");
       // ... and resubscribe
       client.subscribe("inTopic");
     } else {
       Serial.print("failed, rc=");
       Serial.print(client.state());
       Serial.println(" try again in 5 seconds");
       // Wait 5 seconds before retrying
       delay(5000);
     }
   }
 }
 
 void loop()
 {
   if (!client.connected()) {
     reconnect();
   }
   client.loop();
 }