Прошивка(прошивка кривая и написана чисто для проверки, при нажатии на кнопку происходит синхронизация с NTP и рисует IP):
Спойлер
Код: Выделить всё
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <SPI.h>
#include <Wire.h>
#include <uRTCLib.h>
#include <String.h>
#ifndef STASSID
#define STASSID "WIFI_SSID"
#define STAPSK "PASS"
#endif
#define GMT 5
const char* ssid = STASSID;
const char* password = STAPSK;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
uRTCLib rtc(0x68);
byte brightness = 0b111;
int dispBuf[] = {' ', ' ', ' ', '0', '0', '-', '0', '0', '-', '0', '0', ' ', ' '};
void SendByte(byte data){
digitalWrite(16, LOW);
SPI.transfer(data);
digitalWrite(16, HIGH);
delayMicroseconds(100);
}
void updateDisplay(){
int symb[512];
symb[' '] = 0;
symb['-'] = 0b100;
symb['0'] = 0b111000001011;
symb['1'] = 0b1010;
symb['2'] = 0b110000000111;
symb['3'] = 0b100000001111;
symb['4'] = 0b1000001110;
symb['5'] = 0b101000001101;
symb['6'] = 0b111000001101;
symb['7'] = 0b1011;
symb['8'] = 0b111000001111;
symb['9'] = 0b101000001111;
symb['.'] = 0b10000;
symb['•'] = 0b100000;
symb['0'+256] = symb['0'] | symb['.'];
symb['1'+256] = symb['1'] | symb['.'];
symb['2'+256] = symb['2'] | symb['.'];
symb['3'+256] = symb['3'] | symb['.'];
symb['4'+256] = symb['4'] | symb['.'];
symb['5'+256] = symb['5'] | symb['.'];
symb['6'+256] = symb['6'] | symb['.'];
symb['7'+256] = symb['7'] | symb['.'];
symb['8'+256] = symb['8'] | symb['.'];
symb['9'+256] = symb['9'] | symb['.'];
SendByte(0b01000000); //cmd2
digitalWrite(16, LOW);
SPI.transfer(0b11000000); //cmd3
for(int i = 0; i < 13; i++){
SPI.transfer(byte(symb[dispBuf[i]]));
SPI.transfer(byte(symb[dispBuf[i]]>>8));
SPI.transfer(0b00000000);
}
digitalWrite(16, HIGH);
}
void setup() {
//ota
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//my code
pinMode(15, INPUT);
pinMode(2, OUTPUT);
pinMode(16, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(16, HIGH);
SPI.begin();
SPI.setBitOrder(LSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.setDataMode(SPI_MODE3);
SendByte(0b01000000); //cmd2
digitalWrite(16, LOW);
SPI.transfer(0b11000000); //cmd3
for(int i = 0; i < 13; i++){
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
}
digitalWrite(16, HIGH);
delayMicroseconds(100);
SendByte(0b00001100); //cmd1
SendByte(0b10001111); //cmd4
Wire.begin(4, 5);
timeClient.begin();
timeClient.update();
//RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
rtc.set(timeClient.getSeconds(), timeClient.getMinutes(), timeClient.getHours()+GMT, 1, 20, 4, 20);
}
void loop() {
ArduinoOTA.handle();
updateDisplay();
rtc.refresh();
dispBuf[3] = '0' + rtc.hour()/10;
dispBuf[4] = '0' + rtc.hour()%10;
dispBuf[6] = '0' + rtc.minute()/10;
dispBuf[7] = '0' + rtc.minute()%10;
dispBuf[9] = '0' + rtc.second()/10;
dispBuf[10] = '0' + rtc.second()%10;
if(rtc.hour() > 21 || rtc.hour() < 8 && brightness == 0b111){
SendByte(0b10001000);
brightness = 0b000;
}
if(rtc.hour() >= 8 && rtc.hour() <= 21 && brightness == 0b000){
SendByte(0b10001111);
brightness = 0b111;
}
if(digitalRead(15)){
timeClient.update();
rtc.set(timeClient.getSeconds(), timeClient.getMinutes(), timeClient.getHours()+GMT, 1, 20, 4, 20);
for(int i = 1; i < 13; i++) dispBuf[i] = ' ';
IPAddress kk = WiFi.localIP();
String k = kk.toString();
int l = 1;
for(int i = 0; i < k.length(); i++){
if(k[i] != '.')
if(i != k.length() - 2 && k[i+1] == '.') dispBuf[l++] = k[i] + 256; else dispBuf[l++] = k[i];
}
updateDisplay();
while(digitalRead(15))delay(50);
for(int i = 1; i < 13; i++) dispBuf[i] = ' ';
dispBuf[3] = '0' + rtc.hour()/10;
dispBuf[4] = '0' + rtc.hour()%10;
dispBuf[5] = '-';
dispBuf[6] = '0' + rtc.minute()/10;
dispBuf[7] = '0' + rtc.minute()%10;
dispBuf[8] = '-';
dispBuf[9] = '0' + rtc.second()/10;
dispBuf[10] = '0' + rtc.second()%10;
}
delay(50);
}
