/* Fade for c-info wall at c-base with MQTT */ #include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> // Apparently the preprocessor variables B0 and B1 are already taken. // Therefore we use the prefix CBASE #define CBASER0 PM_3 #define CBASEG0 PD_1 #define CBASEB0 PD_0 #define CBASER1 PL_4 #define CBASEG1 PL_5 #define CBASEB1 PM_1 #define CBASER2 PM_7 #define CBASEG2 PA_7 #define CBASEB2 PM_6 int brightness = 50; // how bright the LED is int fadeAmount = 1; // how many points to fade the LED by int fadeCounter = 0; // MQTTServer to use char server[] = "c-beam.cbrp3.c-base.org"; void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Received message for topic "); Serial.print(topic); Serial.print("with length "); Serial.println(length); Serial.println("Message:"); Serial.write(payload, length); Serial.println(); fadeCounter = 20; } EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient); void setup() { pinMode(CBASER0, OUTPUT); pinMode(CBASEG0, OUTPUT); pinMode(CBASEB0, OUTPUT); pinMode(CBASER1, OUTPUT); pinMode(CBASEG1, OUTPUT); pinMode(CBASEB1, OUTPUT); pinMode(CBASER2, OUTPUT); pinMode(CBASEG2, OUTPUT); pinMode(CBASEB2, OUTPUT); Serial.begin(115200); Serial.print("Ethernet starting."); // Start Ethernet with the build in MAC Address Ethernet.begin(0); Serial.print("My IP address: "); Serial.println(Ethernet.localIP()); } void doFade() { while(true) { if (brightness < 50) { fadeAmount = 1; brightness = 50; return; } if (brightness >= 200) { fadeAmount = -1; } // There was some problem with the upper-most LED stripe. // That is why we just set this one to full brightness forever. if(fadeCounter > 0) { analogWrite(CBASER0, 0); analogWrite(CBASEG0, 0); analogWrite(CBASEB0, 255); } else { analogWrite(CBASER0, 255); analogWrite(CBASEG0, 0); analogWrite(CBASEB0, 0); } // The rest of the wall is pulsing. analogWrite(CBASER1, brightness); analogWrite(CBASEG1, brightness); analogWrite(CBASEB1, brightness); analogWrite(CBASER2, brightness); analogWrite(CBASEG2, brightness); analogWrite(CBASEB2, brightness); delay(10); brightness = brightness + fadeAmount; } } void loop() { if (!client.connected()) { Serial.println("Disconnected. Reconnecting...."); if(!client.connect("c-info")) { Serial.println("Connection failed"); } else { Serial.println("Connection success"); if(client.subscribe("user/boarding")) { Serial.println("Subscription 2 successfull"); } } } client.loop(); Serial.print("fadeCounter: "); Serial.println(fadeCounter); doFade(); if (fadeCounter > 0) { fadeCounter--; } delay(800); }