SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <Max72xxPanel.h>
  4. #include <ESP8266WiFi.h>
  5. #include <PubSubClient.h>
  6.  
  7. // Belegung
  8. //DOT Matrix:       ESP8266:
  9. //VCC               5V (VUSB)
  10. //GND               GND
  11. //DIN               D7 (GPIO13)
  12. //CS                D8 (GPIO15)
  13. //CLK               D5 (GPIO14)
  14.  
  15. int pinCS = 15;                           //Für den PIN Select habe ich GPIO8 gewählt
  16. int numberOfHorizontalDisplays = 12;     //Anzahl der Module Horizontal
  17. int numberOfVerticalDisplays = 1;        //Anzahl der Module Vertikal
  18.  
  19. const char* ssid = "test";
  20. const char* password = "7148400962358541";
  21. const char* mqtt_server = "192.168.1.3";
  22.  
  23. WiFiClient espClient;
  24. PubSubClient client(espClient);
  25.  
  26. Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
  27.  
  28. int ScrollWait = 70;                  // Zeit in ms für Scroll Geschwindigkeit
  29. int helligkeit = 3;             // Helligkeit des DisplaysDefault Helligkeit 0 bis 15
  30. int spacer = 1;                 // Länge eines Leerzeichens
  31. int width = 5 + spacer;         // Schriftgröße
  32. String MatrixText = "...";
  33.  
  34. void setup_wifi() {
  35.   delay(10);
  36.   // We start by connecting to a WiFi network
  37.   Serial.println();
  38.   Serial.print("Connecting to ");
  39.   Serial.println(ssid);
  40.  
  41.   WiFi.begin(ssid, password);
  42.  
  43.   while (WiFi.status() != WL_CONNECTED) {
  44.     delay(500);
  45.     Serial.print(".");
  46.   }
  47.  
  48.   randomSeed(micros());
  49.  
  50.   Serial.println("");
  51.   Serial.println("WiFi connected");
  52.   Serial.println("IP address: ");
  53.   Serial.println(WiFi.localIP());
  54. }
  55.  
  56. void MQTTCallback(char* topic, byte* payload, unsigned int length) {
  57.   String PayloadString = "";
  58.   for (int i = 0; i < length; i++) { PayloadString = PayloadString + (char)payload[i]; }
  59.  
  60.   Serial.println("New message arrived");  
  61.   Serial.println(topic);  
  62.   Serial.println(PayloadString);  
  63.  
  64.   if(strcmp(topic, "MatrixDisplay2/text") == 0) {  
  65.     Serial.println("set new Text");      
  66.     MatrixText = PayloadString;
  67.   }
  68.  
  69.   if(strcmp(topic, "MatrixDisplay2/intensity") == 0) {
  70.     Serial.println("set new Intensity");      
  71.     helligkeit = PayloadString.toInt();
  72.     matrix.setIntensity(helligkeit);
  73.   }  
  74.  
  75.   if(strcmp(topic, "MatrixDisplay2/scrollwait") == 0) {
  76.     Serial.println("set new ScrollWait");      
  77.     ScrollWait = PayloadString.toInt();
  78.   }  
  79. }
  80.  
  81. void reconnect() {
  82.   while (!client.connected()) {
  83.     // Create a random client ID
  84.     String clientId = "ESP8266Client-";
  85.     clientId += String(random(0xffff), HEX);
  86.  
  87.     // Attempt to connect
  88.     if (client.connect(clientId.c_str())) {
  89.       Serial.println("subscribe objects");
  90.       client.subscribe("MatrixDisplay2/text");  
  91.       client.subscribe("MatrixDisplay2/intensity");  
  92.       client.subscribe("MatrixDisplay2/scrollwait");      
  93.     }
  94.     else
  95.     {
  96.       Serial.print("failed, rc=");
  97.       Serial.print(client.state());
  98.       Serial.println(" try again in 5 seconds");
  99.      
  100.       delay(5000);
  101.     }
  102.   }
  103. }
  104.  
  105. void setup() {
  106.   //delay(1000);
  107.  
  108.   matrix.setIntensity(helligkeit);
  109.   for (int matrixIndex=0 ; matrixIndex < numberOfHorizontalDisplays ; matrixIndex++ )
  110.   {
  111.     matrix.setRotation(matrixIndex, 1);        //Erste DOT Matrix Drehen
  112.   }
  113.  
  114.   Serial.begin(115200);
  115.   Serial.println("Boot display...");
  116.  
  117.   setup_wifi();
  118.   client.setServer(mqtt_server, 1888);
  119.   client.setCallback(MQTTCallback);  
  120. }
  121.  
  122. void printMatrix() {
  123.   for ( int i = 0 ; i < width * MatrixText.length() + matrix.width() - 1 - spacer; i++ ) {
  124.  
  125.     matrix.fillScreen(LOW);
  126.  
  127.     int letter = i / width;
  128.     int x = (matrix.width() - 1) - i % width;
  129.     int y = (matrix.height() - 8) / 2; //Zentrieren des Textes Vertikal
  130.  
  131.     while ( x + width - spacer >= 0 && letter >= 0 ) {
  132.       if ( letter < MatrixText.length() ) {
  133.         matrix.drawChar(x, y, MatrixText[letter], HIGH, LOW, 1);
  134.       }
  135.       letter--;
  136.       x -= width;
  137.  
  138.       client.loop();
  139.     }
  140.  
  141.     matrix.write();
  142.     delay(ScrollWait);
  143.   }
  144. }
  145.  
  146. void loop() {
  147.   if (!client.connected())  {
  148.     reconnect();
  149.   }
  150.  
  151.   client.loop();    
  152.   delay(500);
  153.    
  154.   printMatrix();      
  155. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top