Advertisement
NittyGritty

ESP8266_Easy_Matrix_Demo1_Scrolling_Message_Wifi_2.ino

Dec 21st, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.65 KB | None | 0 0
  1. /*
  2. Project: Wifi controlled LED matrix display
  3. NodeMCU pins    -> EasyMatrix pins
  4. MOSI-D7-GPIO13  -> DIN
  5. CLK-D5-GPIO14   -> Clk
  6. GPIO0-D3        -> LOAD
  7.  
  8. Pin Name  GPIO #  HSPI Function
  9. MTDI      GPIO12  MISO (DIN)
  10. MTCK      GPIO13  MOSI (DOUT)
  11. MTMS      GPIO14  CLOCK
  12. MTDO      GPIO15  CS / SS
  13.  
  14. GPIO0 = 3
  15. GPIO1 = 10
  16. GPIO2 = 4
  17. GPIO3 = 9
  18. GPIO4 = 2
  19. GPIO5 = 1
  20. GPIO9 = 11
  21. GPIO10 = 12
  22. GPIO12 = 6
  23. GPIO13 = 7
  24. GPIO14 = 5
  25. GPIO15 = 8
  26. GPIO16 = 0
  27. */
  28.  
  29. #include <ESP8266WiFi.h>
  30. #include <ESP8266WebServer.h>
  31. #include <SPI.h>
  32. #include <Adafruit_GFX.h>
  33. #include <Max72xxPanel.h>
  34.  
  35. #include <ArduinoOTA.h>
  36. #include <ESP8266mDNS.h>
  37. #include <DNSServer.h>
  38. #include <ESP8266WebServer.h>
  39.  
  40. // Helps with connecting to internet
  41. #include <WiFiManager.h>
  42.  
  43. // HOSTNAME for OTA update
  44. #define HOSTNAME "ESP8266-OTA-"
  45.  
  46. void configModeCallback (WiFiManager *myWiFiManager);
  47.  
  48. // ******************* String form to sent to the client-browser ************************************
  49. String form =
  50.   "<p>"
  51.   "<center>"
  52.   "<h1>ESP8266 Web Server</h1>"
  53.   "<form action='msg'><p>Type your message <input type='text' name='msg' size=100 autofocus> <input type='submit' value='Submit'></form>"
  54.   "</center>";
  55.  
  56. ESP8266WebServer server(80);                             // HTTP server will listen at port 80
  57. long period;
  58. int offset=1,refresh=0;
  59. int pinCS = 0; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI )
  60. int numberOfHorizontalDisplays = 8;
  61. int numberOfVerticalDisplays = 1;
  62. String decodedMsg;
  63. Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
  64.  
  65. String tape = "Arduino";
  66. int wait = 80; // In milliseconds
  67.  
  68. int spacer = 1;
  69. int width = 5 + spacer; // The font width is 5 pixels
  70.  
  71. /*
  72.   handles the messages coming from the webbrowser, restores a few special characters and
  73.   constructs the strings that can be sent to the oled display
  74. */
  75. void handle_msg() {
  76.                        
  77.   matrix.fillScreen(LOW);
  78.   server.send(200, "text/html", form);    // Send same page so they can send another msg
  79.   refresh=1;
  80.   // Display msg on Oled
  81.   String msg = server.arg("msg");
  82.   Serial.println(msg);
  83.   decodedMsg = msg;
  84.   // Restore special characters that are misformed to %char by the client browser
  85.  
  86.   decodedMsg.replace(0xc4, 0x8e);
  87.  
  88.   decodedMsg.replace(0xd6, 0x99);
  89.   decodedMsg.replace(0xDC, 0x9A);
  90.  
  91.   decodedMsg.replace(0xE4, 0x84);
  92.   decodedMsg.replace(0xF6, 0x94);
  93.   decodedMsg.replace(0xFC, 0x81);
  94.  
  95.   decodedMsg.replace(0xDF, 0xE0);
  96.   decodedMsg.replace(0xA7, 0x15);  
  97.  
  98.   decodedMsg.replace("+", " ");      
  99.   decodedMsg.replace("%21", "!");  
  100.   decodedMsg.replace("%22", "");  
  101.   decodedMsg.replace("%23", "#");
  102.   decodedMsg.replace("%24", "$");
  103.   decodedMsg.replace("%25", "%");  
  104.   decodedMsg.replace("%26", "&");
  105.   decodedMsg.replace("%27", "'");  
  106.   decodedMsg.replace("%28", "(");
  107.   decodedMsg.replace("%29", ")");
  108.   decodedMsg.replace("%2A", "*");
  109.   decodedMsg.replace("%2B", "+");  
  110.   decodedMsg.replace("%2C", ",");  
  111.   decodedMsg.replace("%2F", "/");  
  112.   decodedMsg.replace("%3A", ":");    
  113.   decodedMsg.replace("%3B", ";");  
  114.   decodedMsg.replace("%3C", "<");  
  115.   decodedMsg.replace("%3D", "=");  
  116.   decodedMsg.replace("%3E", ">");
  117.   decodedMsg.replace("%3F", "?");  
  118.   decodedMsg.replace("%40", "@");
  119.  
  120.   Serial.println(decodedMsg);                   // print original string to monitor
  121.   Serial.println(' ');                          // new line in monitor
  122. }
  123.  
  124. // ----------------------------------------------------------------------------------------
  125.  
  126. void setup(void) {
  127. matrix.setIntensity(3); // Use a value between 0 and 15 for brightness
  128.  
  129. // Adjust to your own needs
  130. //  matrix.setPosition(0, 1, 0); // The first display is at <0, 0>
  131. //  matrix.setPosition(1, 0, 0); // The second display is at <1, 0>
  132.  
  133. /*
  134. // Adjust to your own needs
  135.   matrix.setPosition(0, 7, 0); // The first display is at <0, 7>
  136.   matrix.setPosition(1, 6, 0); // The second display is at <1, 0>
  137.   matrix.setPosition(2, 5, 0); // The third display is at <2, 0>
  138.   matrix.setPosition(3, 4, 0); // And the last display is at <3, 0>
  139.   matrix.setPosition(4, 3, 0); // The first display is at <0, 0>
  140.   matrix.setPosition(5, 2, 0); // The second display is at <1, 0>
  141.   matrix.setPosition(6, 1, 0); // The third display is at <2, 0>
  142.   matrix.setPosition(7, 0, 0); // And the last display is at <3, 0>
  143. */
  144.  
  145.   // начальные координаты матриц 8*8
  146.   matrix.setRotation(0, 1);        // 1 матрица
  147.   matrix.setRotation(1, 1);        // 2 матрица
  148.   matrix.setRotation(2, 1);        // 3 матрица
  149.   matrix.setRotation(3, 1);        // 4 матрица
  150.   matrix.setRotation(4, 1);        // 4 матрица
  151.   matrix.setRotation(5, 1);        // 4 матрица
  152.   matrix.setRotation(6, 1);        // 4 матрица
  153.   matrix.setRotation(7, 1);        // 4 матрица
  154.  
  155.   matrix.fillScreen(LOW); // clear display
  156.   matrix.write(); // Send bitmap to display
  157.  
  158. //ESP.wdtDisable();                               // used to debug, disable wachdog timer,
  159.   Serial.begin(115200);                           // full speed to monitor
  160.                                
  161.  
  162.     //WiFiManager
  163.   //Local intialization. Once its business is done, there is no need to keep it around
  164.   WiFiManager wifiManager;
  165.   // Uncomment for testing wifi manager
  166.   //wifiManager.resetSettings();
  167.   wifiManager.setAPCallback(configModeCallback);
  168.  
  169.   //or use this for auto generated name ESP + ChipID
  170.   wifiManager.autoConnect();
  171.  
  172.   // OTA Setup
  173.   String hostname(HOSTNAME);
  174.   hostname += String(ESP.getChipId(), HEX);
  175.   WiFi.hostname(hostname);
  176.   ArduinoOTA.setHostname((const char *)hostname.c_str());
  177.   ArduinoOTA.begin();
  178.  
  179.   while (WiFi.status() != WL_CONNECTED) {         // Wait for connection
  180.     delay(500);
  181.     Serial.print(".");
  182.   }
  183.   // Set up the endpoints for HTTP server,  Endpoints can be written as inline functions:
  184.   server.on("/", []() {
  185.     server.send(200, "text/html", form);
  186.   });
  187.   server.on("/msg", handle_msg);                  // And as regular external functions:
  188.   server.begin();                                 // Start the server
  189.  
  190.   char result[255];
  191.   sprintf(result, "WebServer ready! Send Message using http://%1d.%1d.%1d.%1d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
  192.   Serial.println();
  193.   Serial.println(result);
  194.   decodedMsg = result;
  195.   Serial.println("WebServer ready!   ");
  196.  
  197.   Serial.println(WiFi.localIP());                 // Serial monitor prints localIP
  198.   // Serial.print(analogRead(A0));
  199.  
  200. }
  201.  
  202. // ----------------------------------------------------------------------------------------
  203.  
  204. void loop(void) {
  205.  
  206.   for ( int i = 0 ; i < width * decodedMsg.length() + matrix.width() - 1 - spacer; i++ ) {
  207.     server.handleClient();                        // checks for incoming messages
  208.     if (refresh==1) i=0;
  209.     refresh=0;
  210.     matrix.fillScreen(LOW);
  211.  
  212.     int letter = i / width;
  213.     int x = (matrix.width() - 1) - i % width;
  214.     int y = (matrix.height() - 8) / 2; // center the text vertically
  215.  
  216.     while ( x + width - spacer >= 0 && letter >= 0 ) {
  217.       if ( letter < decodedMsg.length() ) {
  218.         matrix.drawChar(x, y, decodedMsg[letter], HIGH, LOW, 1);
  219.       }
  220.  
  221.       letter--;
  222.       x -= width;
  223.     }
  224.  
  225.     matrix.write(); // Send bitmap to display
  226.  
  227.     delay(wait);
  228.   }
  229. }
  230.  
  231. // Called if WiFi has not been configured yet
  232. void configModeCallback (WiFiManager *myWiFiManager) {
  233.   Serial.println("Wifi Manager");
  234.   Serial.println("Please connect to AP");
  235.   Serial.println(myWiFiManager->getConfigPortalSSID());
  236.   Serial.println("To setup Wifi Configuration");
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement