Advertisement
Azzar_budiyanto

program

Sep 17th, 2021 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "config.h"
  2.  
  3. char* str;
  4. String payload;
  5. uint32_t present;
  6. bool first_time;
  7. uint16_t  scrollDelay;
  8. #define  CHAR_SPACING  1
  9.  
  10. #define BUF_SIZE  75
  11. char curMessage[BUF_SIZE];
  12. char newMessage[BUF_SIZE];
  13. bool newMessageAvailable = false;
  14.  
  15. WiFiClient client;
  16.  
  17. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  18. Adafruit_MQTT_Subscribe minidisplay = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/display.minidisplay");
  19.  
  20. void MQTT_connect();
  21.  
  22. //MD_MAX72XX mx = MD_MAX72XX(CS_PIN, MAX_DEVICES);
  23. MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  24. uint8_t scrollDataSource(uint8_t dev, MD_MAX72XX::transformType_t t)
  25. {
  26.   static char       *p = curMessage;
  27.   static uint8_t    state = 0;
  28.   static uint8_t    curLen, showLen;
  29.   static uint8_t    cBuf[8];
  30.   uint8_t colData;
  31.   switch (state)
  32.   {
  33.     case 0: // Load the next character from the font table
  34.       showLen = mx.getChar(*p++, sizeof(cBuf) / sizeof(cBuf[0]), cBuf);
  35.       curLen = 0;
  36.       state++;
  37.       if (*p == '\0')
  38.       {
  39.         p = curMessage;
  40.         if (newMessageAvailable)
  41.         {
  42.           strcpy(curMessage, str);  // copy it in
  43.           newMessageAvailable = false;
  44.         }
  45.       }
  46.     case 1:
  47.       colData = cBuf[curLen++];
  48.       if (curLen == showLen)
  49.       {
  50.         showLen = CHAR_SPACING;
  51.         curLen = 0;
  52.         state = 2;
  53.       }
  54.       break;
  55.     case 2:
  56.       colData = 0;
  57.       curLen++;
  58.       if (curLen == showLen)
  59.         state = 0;
  60.       break;
  61.     default:
  62.       state = 0;
  63.   }
  64.   return (colData);
  65. }
  66.  
  67. void scrollText(void)
  68. {
  69.   static uint32_t   prevTime = 0;
  70.   if (millis() - prevTime >= scrollDelay)
  71.   {
  72.     mx.transform(MD_MAX72XX::TSL);
  73.     //mx.transform(MD_MAX72XX::TFLR);
  74.     prevTime = millis();
  75.   }
  76. }
  77. void  no_connection(void)
  78. {
  79.   newMessageAvailable = 1;
  80.   strcpy(curMessage, "No Internet! ");
  81.   scrollText();
  82. }
  83.  
  84. void wifi_connect() {
  85.   Serial.println(); Serial.println();
  86.   Serial.print("Connecting : ");
  87.   Serial.println(WLAN_SSID);
  88.  
  89.   WiFi.begin(WLAN_SSID, WLAN_PASS);
  90.   while (WiFi.status() != WL_CONNECTED) {
  91.     delay(500);
  92.     Serial.print(".");
  93.   }
  94.   Serial.println();
  95.  
  96.   Serial.println("WiFi connected");
  97.   Serial.println("IP address: ");
  98.   Serial.println(WiFi.localIP());
  99. }
  100.  
  101. void setup()
  102. {
  103.   Serial.begin(115200);
  104.   mx.begin();
  105.   mx.setShiftDataInCallback(scrollDataSource);
  106.  
  107.   scrollDelay = SCROLL_DELAY;
  108.   strcpy(curMessage, "---");
  109.   newMessage[0] = '\0';
  110.  
  111.  
  112.   Serial.print("\n[MD_MAX72XX Message Display]\nType a message for the scrolling display\nEnd message line with a newline");
  113.   Serial.println();
  114.   Serial.println();
  115.   Serial.println();
  116.  
  117.   for (uint8_t t = 4; t > 0; t--) {
  118.     Serial.printf("[SETUP] WAIT %d...\n", t);
  119.     Serial.flush();
  120.     delay(1000);
  121.   }
  122.  
  123.   for (uint8_t t = 4; t > 0; t--) {
  124.     Serial.printf("[SETUP] WAIT %d...\n", t);
  125.     Serial.flush();
  126.     delay(1000);
  127.   }
  128.   newMessageAvailable = 1;
  129.   present = millis();
  130.   first_time = 1;
  131.   mqtt.subscribe(&minidisplay);
  132.   str = "  waiting...  ";
  133. }
  134. void loop()
  135. {
  136.   unsigned long currentTime = millis();
  137.   while (WiFi.status() != WL_CONNECTED) {
  138.     Serial.println("WiFi not connected!");
  139.     delay(1000);
  140.   }
  141.   MQTT_connect();
  142.   Adafruit_MQTT_Subscribe *subscription;
  143.   while ((subscription = mqtt.readSubscription(1))) {
  144.     if (subscription == &minidisplay) {
  145.       payload = "";
  146.       Serial.print(F("Got: "));
  147.       Serial.println((char *)minidisplay.lastread);
  148.       str = (char*)minidisplay.lastread;
  149.       payload = (String) str;
  150.       payload += "     ";
  151.       str = &payload[0];
  152.       newMessageAvailable = 1;
  153.     }
  154.   }
  155.   scrollText();
  156.   if (currentTime - previousTime >= eventInterval) {
  157.     wifi_connect();
  158.     previousTime = currentTime;
  159.   }
  160. }
  161. void MQTT_connect() {
  162.   int8_t ret;
  163.   if (mqtt.connected()) {
  164.     return;
  165.   }
  166.   Serial.print("Connecting to MQTT... ");
  167.   uint8_t retries = 3;
  168.   while ((ret = mqtt.connect()) != 0) {
  169.     Serial.println(mqtt.connectErrorString(ret));
  170.     Serial.println("Retrying MQTT connection in 5 seconds...");
  171.     mqtt.disconnect();
  172.     delay(5000);
  173.     retries--;
  174.     if (retries == 0) {
  175.       while (1);
  176.     }
  177.   }
  178.   Serial.println("MQTT Connected!");
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement