Hi All,
I am struggling to understand how to create my own library for the random amounts of code that I have on one tab. I have researched it and tried several times to figure it out and I am still stuck after almost 3, possibly 4 months of rewriting and testing and still cannot figure it out.
I'm also stuck trying to figure out some other issues but they can wait.
this is my original code (the edited version has got so messy that i struggle to see what i have done). i just need help figuring out which parts go where.
this is a WEMOS D1 MINI, seven segment led clock with temperature and humidity sensors, future webpage for updating, time/date changes and sensor display via web. (HTML not working, its the newest part i am stuck on so ignore that)
all the parts i have "emphasised" are the parts that control the Seven Segment display, which is the parts i would like to put into a library, just so that it is out of view while i work on other areas.
any help would be greatly appreciated, and please dumb it down lol
#include <FS.h>
*#include <TimeLib.h>*
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
*#include "DHT.h"*
#include <ESP8266WebServer.h>
*#define DHTPIN D5*
*#define DHTTYPE DHT11*
//#define ntpServerName "au.pool.ntp.org"
//#define timeZone "AEST-10AEDT,M10.0,M4.1.0/3"
*DHT dht(DHTPIN, DHTTYPE); //DHT_Unified*
ESP8266WebServer server(80);
const char ssid[] = "SSID"; // your network SSID (name)
const char pass[] = "PASS"; // your network password
// NTP Servers:
static const char ntpServerName[] = "au.pool.ntp.org";
const int timeZone = +10;
WiFiUDP Udp;
unsigned int localPort = 8888;
time_t getNtpTime();
void digitalClockDisplay();
void printDigits(int digits);
void sendNTPpacket(IPAddress &address);
*const int g_pinData = D3; //D1*
*const int g_pinCommLatch = D2; //D2*
*const int g_pinClock = D1; //D3*
*const int dot1 = D7;*
*const int dot2 = D8;*
*int temp = 0;*
*int humid = 0;*
*int timeH = 0;*
*int timeM = 0;*
*int datetime = 0;*
*byte g_digits [10]; // Definitions of the 7-bit values for displaying digits*
*byte g_digits2 [10];*
*int g_numberToDisplay = 0; // default number being displayed, 0 *
*const int g_registers = 4; // Number of shift registers in use, 4*
*byte g_registerArray [g_registers]; // Array of numbers to pass to shift registers*
void setup()
{
Serial.begin(115200);
while (!Serial) ; // Needed for Leonardo only
delay(250);
Serial.println("Warehouse Clock");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
//dht.begin();
//configTime(timeZone, ntpServerName);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP number assigned by DHCP is ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
setSyncInterval(300);
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
// when the digital clock was displayed
*pinMode (g_pinData, OUTPUT); //define 74595 pin as output*
* pinMode (g_pinCommLatch, OUTPUT); //define 74595 pin as output*
* pinMode (g_pinClock, OUTPUT); //define 74595 pin as output*
* pinMode (dot1, OUTPUT);*
* pinMode (dot2, OUTPUT);*
*int a = 1, b = 2, c = 4, d = 8, e = 16, f = 32, g = 64;*
* g_digits [0] = 8 + 4 + 2 + 64 + 32 + 1; //1 + 2 + 4 + 8 + 16 + 32;*
* g_digits [1] = 4 + 2; //2 + 4;*
* g_digits [2] = 8 + 4 + 16 + 32 + 64; //1 + 2 + 64 + 16 + 8;*
* g_digits [3] = 8 + 4 + 16 + 2 + 64; //1 + 2 + 4 + 8 + 64;*
* g_digits [4] = 1 + 16 + 4 + 2; //32 + 64 + 2 + 4;*
* g_digits [5] = 8 + 1 + 16 + 2 + 64; //1 + 32 + 64 + 4 + 8;*
* g_digits [6] = 8 + 1 + 16 + 2 + 64 + 32; //1 + 32 + 16 + 8 + 4 + 64;*
* g_digits [7] = 8 + 4 + 2; //1 + 2 + 4;*
* g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64; //1 + 2 + 4 + 8 + 16 + 32 + 64;*
* g_digits [9] = 1 + 8 + 4 + 16 + 2 + 64; //8 + 4 + 2 + 1 + 32 + 64;*
* g_digits2 [0] = 1 + 8 + 4 + 16; //1 + 2 + 64 + 32; // Degree dot*
* g_digits2 [1] = 8 + 1 + 32 + 64; //1 + 8 + 16 + 32; // Capital C*
* g_digits2 [2] = 32 + 16; //64 + 16; // r, 80*
* g_digits2 [3] = 1 + 32 + 16 + 2; //4 + 16 + 32 + 64; // h, 116*
* g_digits [99] = 0;*
for (int i=0;i<5;i++) {
g_registerArray [0] = g_digits [8];
g_registerArray [1] = g_digits [8];
g_registerArray [2] = g_digits [8];
g_registerArray [3] = g_digits [8];
sendSerialData (g_registers, g_registerArray);
delay(200);
g_registerArray [0] = g_digits [99];
g_registerArray [1] = g_digits [99];
g_registerArray [2] = g_digits [99];
g_registerArray [3] = g_digits [99];
sendSerialData (g_registers, g_registerArray);
delay(200);
}
}
*time_t prevDisplay = 0;*
*void sendSerialData (byte registerCount, byte *pValueArray) {*
* // Signal to the 595s to listen for data*
* digitalWrite (g_pinCommLatch, LOW);*
* for (byte reg = registerCount; reg > 0; reg--)*
* {*
* byte value = pValueArray [reg - 1];*
* for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)*
* {*
* digitalWrite (g_pinClock, LOW);*
* digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);*
* digitalWrite (g_pinClock, HIGH);*
* }*
* }*
* // Signal to the 595s that I'm done sending*
* digitalWrite (g_pinCommLatch, HIGH);*
*}*
void loop(){
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
if ((second() == 00) || (second() == 15) ||(second() == 30) ||(second() == 45)){
digitalTempDisplay();
delay (100);
digitalHumidDisplay();
delay (200);
}else{
digitalClockDisplay();
}
}
}
}
*void digitalClockDisplay()*
*{*
* Serial.print(hour());*
* Serial.print(':');*
* char buf[2];*
* sprintf(buf,"%2d",minute());*
* Serial.print(buf);*
* Serial.print(':');*
* Serial.print(second());*
* Serial.println();*
* *
* // digital clock display of the time*
* g_registerArray [0] = g_digits [hourFormat12() / 10];*
* g_registerArray [1] = g_digits [hourFormat12() % 10];*
* g_registerArray [2] = g_digits [minute() / 10];*
* g_registerArray [3] = g_digits [minute() % 10];*
* sendSerialData (g_registers, g_registerArray);*
* digitalWrite(dot1, HIGH);*
* digitalWrite(dot2, HIGH);*
* delay(500);*
* digitalWrite(dot1, LOW);*
* digitalWrite(dot2, LOW);*
* delay(500);*
*}*
*void digitalDateDisplay(){*
* Serial.print(day());*
* Serial.print(':');*
* Serial.print(month());*
* Serial.print(':');*
* Serial.print(year());*
* Serial.println();*
* g_registerArray [0] = g_digits [day() / 10];*
* g_registerArray [1] = g_digits [day() % 10];*
* g_registerArray [2] = g_digits [month() / 10];*
* g_registerArray [3] = g_digits [month() % 10];*
* sendSerialData (g_registers, g_registerArray);*
* digitalWrite(dot1, HIGH);*
* digitalWrite(dot2, HIGH);*
* delay(500);*
* digitalWrite(dot1, LOW);*
* digitalWrite(dot2, LOW);*
* delay(500);*
*}*
*void digitalYearDisplay(){*
* *
* g_registerArray [0] = g_digits [year() / 1000];*
* g_registerArray [1] = g_digits [(year() % 1000) / 100];*
* g_registerArray [2] = g_digits [(year() % 100) / 10];*
* g_registerArray [3] = g_digits [year() % 10];*
* sendSerialData (g_registers, g_registerArray);*
* digitalWrite(dot1, HIGH);*
* digitalWrite(dot2, HIGH);*
* delay(500);*
* digitalWrite(dot1, LOW);*
* digitalWrite(dot2, LOW);*
* delay(500);*
*}*
*void digitalTempDisplay(){*
* *
* Serial.print(temp);*
* Serial.println();*
* *
* temp = dht.readTemperature();*
* *
* g_registerArray [0] = g_digits [temp / 10];*
* g_registerArray [1] = g_digits [temp % 10];*
* g_registerArray [2] = g_digits2 [0];*
* g_registerArray [3] = g_digits2 [1];*
* sendSerialData (g_registers, g_registerArray);*
* digitalWrite(dot1, HIGH);*
* digitalWrite(dot2, HIGH);*
* delay(500);*
* digitalWrite(dot1, LOW);*
* digitalWrite(dot2, LOW);*
* delay(500);*
*}*
*void digitalHumidDisplay(){*
* *
* Serial.print(humid);*
* Serial.println();*
* humid = dht.readHumidity();*
* *
* g_registerArray [0] = g_digits [humid / 10];*
* g_registerArray [1] = g_digits [humid % 10];*
* g_registerArray [2] = g_digits2 [2];*
* g_registerArray [3] = g_digits2 [3];*
* sendSerialData (g_registers, g_registerArray);*
* digitalWrite(dot1, HIGH);*
* digitalWrite(dot2, HIGH);*
* delay(500);*
* digitalWrite(dot1, LOW);*
* digitalWrite(dot2, LOW);*
* delay(500);*
*}*
void printDigits(int digits)
{
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
void handle_OnConnect() {
struct tm timeinfo;
temp = temp,DEC; // Gets the values of the temperature
humid = dht.readHumidity(); // Gets the values of the humidity
timeH = hour();
timeM = minute();
datetime = timeStatus(),DEC;
server.send(200, "text/html", SendHTML(temp,humid,timeH,timeM));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(float tempstat,float humidstat,float timeHstat,float timeMstat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>ESP32 Weather Report</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<h1>Warehouse Clock</h1>\n";
ptr +="<h6>Built By Sean Wilson</h6>\n";
ptr +="<p>Temperature: ";
ptr +=(int)tempstat;
ptr +=char(176);
ptr +="C</p>";
ptr +="<p>Humidity: ";
ptr +=(int)humidstat;
ptr +="%</p>";
ptr +="<p>Time: ";
ptr +=(int)timeHstat;
ptr +=":";
ptr +=(int)timeMstat;
ptr +="</p>";
ptr +="</div>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
sterretje: code tags changed to cpp