Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Use the MD_MAX72XX library to scroll text on the display
- // received through the ESP8266 WiFi interface.
- //
- // Demonstrates the use of the callback function to control what
- // is scrolled on the display text. User can enter text through
- // a web browser and this will display as a scrolling message on
- // the display.
- //
- // IP address for the ESP8266 is displayed on the scrolling display
- // after startup initialization and connected to the WiFi network.
- //
- // Connections for ESP8266 hardware SPI are:
- // Vcc 3v3 LED matrices seem to work at 3.3V
- // GND GND GND
- // DIN D7 HSPID or HMOSI
- // CS or LD D8 HSPICS or HCS
- // CLK D5 CLK or HCLK
- //
- #include <ESP8266WiFi.h>
- #include <MD_MAX72xx.h>
- #include <SPI.h>
- #include <time.h>
- #include <TimeLib.h>
- #include <WiFiUdp.h>
- // NTP Servers:
- static const char ntpServerName[] = "sg.pool.ntp.org";
- //static const char ntpServerName[] = "time.nist.gov";
- //static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
- //static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
- //static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
- const int timeZone = +8; // Central European Time
- //const int timeZone = -5; // Eastern Standard Time (USA)
- //const int timeZone = -4; // Eastern Daylight Time (USA)
- //const int timeZone = -8; // Pacific Standard Time (USA)
- //const int timeZone = -7; // Pacific Daylight Time (USA)
- WiFiUDP Udp;
- unsigned int localPort = 8888; // local port to listen for UDP packets
- time_t getNtpTime();
- void digitalClockDisplay();
- void printDigits(int digits);
- void sendNTPpacket(IPAddress &address);
- #define PRINT_CALLBACK 0
- #define DEBUG 0
- #define LED_HEARTBEAT 0
- #if DEBUG
- #define PRINT(s, v) { Serial.print(F(s)); Serial.print(v); }
- #define PRINTS(s) { Serial.print(F(s)); }
- #else
- #define PRINT(s, v)
- #define PRINTS(s)
- #endif
- #if LED_HEARTBEAT
- #define HB_LED D2
- #define HB_LED_TIME 800 // in milliseconds
- #endif
- // Define the number of devices we have in the chain and the hardware interface
- // NOTE: These pin numbers will probably not work with your hardware and may
- // need to be adapted
- #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
- #define MAX_DEVICES 8
- #define CLK_PIN D5 // or SCK
- #define DATA_PIN D7 // or MOSI
- #define CS_PIN D8 // or SS
- // SPI hardware interface
- MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
- // Arbitrary pins
- //MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
- // WiFi login parameters - network name and password
- const char* ssid = "pv6";
- const char* password = "amenhype";
- // WiFi Server object and parameters
- WiFiServer server(80);
- // Global message buffers shared by Wifi and Scrolling functions
- const uint8_t MESG_SIZE = 255;
- const uint8_t CHAR_SPACING = 1;
- const uint8_t SCROLL_DELAY = 100;
- char curMessage[MESG_SIZE];
- char newMessage[MESG_SIZE];
- bool newMessageAvailable = false;
- char WebResponse[] = "HTTP/1.1 200 OKnContent-Type: text/htmlnn";
- char WebPage[] =
- "<!DOCTYPE html>"
- "<html>"
- "<head>"
- "<title>Wifi Display</title>"
- "<script>"
- "strLine = "";"
- "function SendText()"
- "{"
- " nocache = "/&nocache=" + Math.random() * 1000000;"
- " var request = new XMLHttpRequest();"
- " strLine = "&MSG=" + document.getElementById("txt_form").Message.value;"
- " request.open("GET", strLine + nocache, false);"
- " request.send(null);"
- "}"
- "</script>"
- "</head>"
- "<body>"
- "<Center>"
- "<h1>Smart Notice Board </h1>"
- "<form id="txt_form" name="frmText">"
- "<label>Type Your Message:<input type="text" name="Message" size="100" maxlength="255"></label><br><br>"
- "</form>"
- "<br>"
- "<input type="submit" value="Send Message" onclick="SendText()">"
- "<Center>"
- "</body>"
- "</html>";
- char *err2Str(wl_status_t code)
- {
- switch (code)
- {
- case WL_IDLE_STATUS: return("IDLE"); break; // WiFi is in process of changing between statuses
- case WL_NO_SSID_AVAIL: return("NO_SSID_AVAIL"); break; // case configured SSID cannot be reached
- case WL_CONNECTED: return("CONNECTED"); break; // successful connection is established
- case WL_CONNECT_FAILED: return("CONNECT_FAILED"); break; // password is incorrect
- case WL_DISCONNECTED: return("CONNECT_FAILED"); break; // module is not configured in station mode
- default: return("??");
- }
- }
- uint8_t htoi(char c)
- {
- c = toupper(c);
- if ((c >= '0') && (c <= '9')) return(c - '0');
- if ((c >= 'A') && (c <= 'F')) return(c - 'A' + 0xa);
- return(0);
- }
- boolean getText(char *szMesg, char *psz, uint8_t len)
- {
- boolean isValid = false; // text received flag
- char *pStart, *pEnd; // pointer to start and end of text
- // get pointer to the beginning of the text
- pStart = strstr(szMesg, "/&MSG=");
- if (pStart != NULL)
- {
- pStart += 6; // skip to start of data
- pEnd = strstr(pStart, "/&");
- if (pEnd != NULL)
- {
- while (pStart != pEnd)
- {
- if ((*pStart == '%') && isdigit(*(pStart+1)))
- {
- // replace %xx hex code with the ASCII character
- char c = 0;
- pStart++;
- c += (htoi(*pStart++) << 4);
- c += htoi(*pStart++);
- *psz++ = c;
- }
- else
- *psz++ = *pStart++;
- }
- *psz = '