Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_PCD8544.h>
- #include <WiFiEsp.h>
- #include <SD.h>
- #include <Wire.h>
- #include <RTClib.h>
- Adafruit_PCD8544 display = Adafruit_PCD8544(32, 30, 28, 26, 24);
- RTC_DS1307 RTC;
- int errorCode = 0;
- #define SDCardError 1
- #define WiFiErrorInit 2
- #define WiFiErrorConnect 3
- #define FTPServerError 4
- #define FileError 5
- const int POT = 0;
- int val = 0;
- const int ButtonBACK = 23;
- const int ButtonOK = 25;
- const int ButtonLANG = 27;
- const int ButtonOPTIONS = 29;
- const int RTC_POW_PIN = A6;
- const int RTC_GND_PIN = A5;
- boolean lastBACK = LOW;
- boolean lastOK = LOW;
- boolean lastLANG = LOW;
- boolean lastOPTIONS = LOW;
- boolean currentBACK = LOW;
- boolean currentOK = LOW;
- boolean currentLANG = LOW;
- boolean currentOPTIONS = LOW;
- int pos = 0;
- #define ENG 0
- #define RUS 1
- #define SYM 2
- String symbols[3] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ_", "", "_.,?!1234567890-+=:;()/"};
- const int symLen[3] = {26, 32, 22};
- int lang = RUS;
- static const unsigned char PROGMEM menu_icons[][128] = {
- { 0x00, 0x00, 0x00, 0x00,
- 0x1F, 0xFF, 0xFF, 0xFF,
- 0x18, 0x00, 0x00, 0x03,
- 0x14, 0x00, 0x00, 0x05,
- 0x12, 0x00, 0x00, 0x09,
- 0x11, 0x00, 0x00, 0x11,
- 0x10, 0x80, 0x00, 0x21,
- 0x10, 0x40, 0x00, 0x41,
- 0x10, 0x20, 0x00, 0x81,
- 0x10, 0x10, 0x01, 0x01,
- 0x10, 0x08, 0x02, 0x01,
- 0x10, 0x04, 0x04, 0x01,
- 0x10, 0x02, 0x08, 0x01,
- 0x10, 0x01, 0x10, 0x01,
- 0x10, 0x00, 0xE4, 0x01,
- 0x10, 0x00, 0x0C, 0x01,
- 0x10, 0x00, 0x1F, 0xF9,
- 0x10, 0x00, 0x3F, 0xF9,
- 0x10, 0x00, 0x7F, 0xF9,
- 0xF0, 0x00, 0x3F, 0xF9,
- 0x10, 0x00, 0x1F, 0xF9,
- 0x10, 0x00, 0x0C, 0x01,
- 0xF0, 0x00, 0x04, 0x41,
- 0x10, 0x00, 0x00, 0x61,
- 0x10, 0x00, 0x3F, 0xF1,
- 0xF0, 0x00, 0x3F, 0xF9,
- 0x10, 0x00, 0x3F, 0xFD,
- 0x10, 0x00, 0x3F, 0xF9,
- 0xF0, 0x00, 0x3F, 0xF1,
- 0x10, 0x00, 0x00, 0x61,
- 0x1F, 0xFF, 0xFF, 0x4F,
- 0x00, 0x00, 0x00, 0x00,
- },
- { 0x00, 0x00, 0x00, 0x00,
- 0x0F, 0xFF, 0xFF, 0xF0,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x09, 0xFF, 0xFF, 0x90,
- 0x08, 0x00, 0x00, 0x10,
- 0x08, 0x00, 0x00, 0x10,
- 0x0F, 0xFF, 0xFF, 0xF0,
- 0x00, 0x00, 0x00, 0x00,
- },
- { 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x40, 0x00, 0x00,
- 0x00, 0xE0, 0x00, 0x00,
- 0x01, 0xF0, 0x00, 0x00,
- 0x03, 0xF8, 0x00, 0x00,
- 0x07, 0xFC, 0x00, 0x10,
- 0x0F, 0xF8, 0x00, 0x20,
- 0x1F, 0xF0, 0x00, 0x40,
- 0x3F, 0xE0, 0x00, 0x80,
- 0x7F, 0xC0, 0x01, 0x00,
- 0x3F, 0xE0, 0x02, 0x00,
- 0x1F, 0x30, 0x04, 0x00,
- 0x0E, 0x18, 0x08, 0x00,
- 0x04, 0x0C, 0x10, 0x00,
- 0x00, 0x06, 0x20, 0x00,
- 0x00, 0x03, 0x40, 0x00,
- 0x00, 0x01, 0x80, 0x00,
- 0x00, 0x01, 0xC0, 0x00,
- 0x00, 0x0E, 0x60, 0x00,
- 0x00, 0x1E, 0x30, 0x00,
- 0x00, 0x3E, 0x18, 0x00,
- 0x00, 0x7C, 0x0C, 0x00,
- 0x00, 0xF8, 0x06, 0x00,
- 0x01, 0xF0, 0x03, 0x00,
- 0x03, 0xE0, 0x01, 0x80,
- 0x07, 0xC0, 0x00, 0xC0,
- 0x07, 0x80, 0x00, 0x60,
- 0x07, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00,
- }
- };
- String menu_strings[3];
- static const unsigned char PROGMEM newMsgIcon[] = {0xFF,
- 0xC3,
- 0xA5,
- 0x99,
- 0x81,
- 0x81,
- 0x81,
- 0xFF,
- };
- WiFiEspClient client;
- WiFiEspClient dclient;
- int status = WL_IDLE_STATUS;
- IPAddress server( 192, 168, 8, 102 );
- char outBuf[128];
- char outCount;
- String senderName;
- boolean debounce(const int ButtonPIN, boolean last) {
- boolean current = digitalRead(ButtonPIN);
- if (last != current)
- {
- delay(5);
- current = digitalRead(ButtonPIN);
- return current;
- }
- return last;
- }
- void buttonsRead() {
- currentBACK = debounce(ButtonBACK, lastBACK);
- currentOK = debounce(ButtonOK, lastOK);
- currentLANG = debounce(ButtonLANG, lastLANG);
- currentOPTIONS = debounce(ButtonOPTIONS, lastOPTIONS);
- if (lastBACK != currentBACK || lastOK != currentOK || lastLANG != currentLANG || lastOPTIONS != currentOPTIONS) {
- delay(200);
- }
- }
- String stringCut(String source) {
- String target;
- if (source.length() > 1) {
- for (int i = 0; i < source.length() - 1; i++)
- {
- target += source[i];
- }
- return target;
- }
- else {
- return "";
- }
- }
- String encrypt(String source) {
- int key = 38522863;
- String target;
- String currentResult;
- for (int i = 0; i < source.length(); i++)
- {
- currentResult += source[i] * key;
- for (int j = 0; j < currentResult.length(); j++)
- {
- target += atoi(currentResult[j]);
- }
- }
- return target;
- }
- String encrypt(String source, int key) {
- String target;
- String currentResult;
- for (int i = 0; i < source.length(); i++)
- {
- currentResult += source[i] * key;
- for (int j = 0; j < currentResult.length(); j++)
- {
- target += atoi(currentResult[j]);
- }
- }
- return target;
- }
- boolean connectToSSID() {
- File SSIDs = SD.open("SSIDS.TXT", FILE_READ);
- if (!SSIDs) {
- errorCode = WiFiErrorConnect;
- return false;
- }
- int len;
- while (SSIDs.available()) {
- SSIDs.read();
- len++;
- }
- SSIDs.close();
- SSIDs = SD.open("SSIDS.TXT", FILE_READ);
- if (!SSIDs) {
- errorCode = WiFiErrorConnect;
- return false;
- }
- char* ssid[len];
- char* password[len];
- char buf;
- int counter = 0;
- boolean isName = true;
- while (SSIDs.available()) {
- buf = SSIDs.read();
- if (buf == '@') {
- isName = !isName;
- if (isName) counter++;
- }
- else if (isName && buf != '\n' && buf != '\r') {
- ssid[counter] += buf;
- }
- else if (buf != '\n' && buf != '\r') {
- password[counter] += buf;
- }
- }
- SSIDs.close();
- if (status != WL_CONNECTED) {
- for (int i = 0; i < sizeof(ssid); i++)
- {
- /*char ssidChar[ssid[i].length()];
- char passwordChar[password[i].length()];
- ssidChar = ssid + '\n';
- passwordChar = password + '\n';*/
- status = WiFi.begin(ssid[i], password[i]);
- if (status == WL_CONNECTED) {
- return true;
- }
- }
- }
- return false;
- }
- void setup() {
- symbols[1] = utf8rus("АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ_");
- menu_strings[0] = utf8rus("Сообщения");
- menu_strings[1] = utf8rus("Информация");
- menu_strings[2] = utf8rus("Настройки");
- display.begin(); // Инициализация дисплея
- display.setContrast(60); // Устанавливаем контраст
- display.setTextColor(BLACK); // Устанавливаем цвет текста
- display.setTextSize(1); // Устанавливаем размер текста
- display.clearDisplay(); // Очищаем дисплей
- display.setCursor(display.width() / 2 - 10 * 2.8, display.height() / 2 + 8);
- display.print("Loading...");
- display.display();
- Serial.begin(115200);
- Serial1.begin(115200);
- pinMode(RTC_POW_PIN, OUTPUT);
- pinMode(RTC_GND_PIN, OUTPUT);
- digitalWrite(RTC_POW_PIN, HIGH);
- digitalWrite(RTC_GND_PIN, LOW);
- Serial.println("RTC initializing started");
- RTC.begin();
- if (!RTC.isrunning()) {
- RTC.adjust(DateTime(__DATE__, __TIME__));
- }
- Serial.println("SD initializing started");
- if (!SD.begin(10)) {
- errorCode = SDCardError;
- Serial.println("SD initializing error");
- return;
- }
- Serial.println("SD initialized");
- Serial.println("WiFi initializing started");
- WiFi.init(&Serial1);
- if (WiFi.status() == WL_NO_SHIELD) {
- errorCode = WiFiErrorInit;
- Serial.println("WiFi initializing error");
- return;
- }
- Serial.println("WiFi initialized");
- delay(3000);
- if (status != WL_CONNECTED)
- {
- if (!connectToSSID()) {
- errorCode = WiFiErrorConnect;
- Serial.println("Error connecting to SSID");
- }
- }
- Serial.println("FTP connecting started");
- if (!client.connect(server, 21)) {
- errorCode = FTPServerError;
- Serial.println("FTP connecting error");
- return;
- }
- if(!eRcv()) return;
- client.println(F("USER Anonymous"));
- if(!eRcv()) return;
- /*client.println(F("PASS 123"));
- if(!eRcv()) return;*/
- client.println(F("SYST"));
- if(!eRcv()) return;
- client.println(F("Type I"));
- if(!eRcv()) return;
- client.println(F("PASV"));
- if(!eRcv()) return;
- char *tStr = strtok(outBuf,"(,");
- int array_pasv[6];
- for ( int i = 0; i < 6; i++) {
- tStr = strtok(NULL,"(,");
- array_pasv[i] = atoi(tStr);
- if(tStr == NULL)
- {
- Serial.println(F("Bad PASV Answer"));
- }
- }
- unsigned int hiPort,loPort;
- hiPort = array_pasv[4] << 8;
- loPort = array_pasv[5] & 255;
- Serial.println(F("Data port: "));
- hiPort = hiPort | loPort;
- Serial.println(hiPort);
- if (dclient.connect(server,hiPort)) {
- Serial.println(F("Data connected"));
- }
- else {
- Serial.println(F("Data connection failed"));
- client.stop();
- return;
- }
- }
- void MsgToContact(String contactName) {
- }
- String inputText(boolean NO_BACK = false) {
- String inputString;
- while (true) {
- buttonsRead();
- if (lastBACK == LOW && currentBACK == HIGH)
- {
- inputString = stringCut(inputString);
- }
- if (lastOK == LOW && currentOK == HIGH)
- {
- inputString += symbols[lang][val];
- }
- if (lastLANG == LOW && currentLANG == HIGH)
- {
- lang++;
- if (lang > 2) lang = 0;
- else if (lang < 0) lang = 2;
- }
- if (lastOPTIONS == LOW && currentOPTIONS == HIGH)
- {
- int result;
- if (!NO_BACK) {
- String params[] = {utf8rus("Ввод"), utf8rus("Отменить")};
- result = optionsList(params, 2);
- }
- else {
- String params[] = {utf8rus("Ввод"), };
- result = optionsList(params, 1);
- }
- switch (result) {
- case 0:
- return inputString;
- case 1:
- return "";
- }
- }
- lastBACK = currentBACK;
- lastOK = currentOK;
- lastLANG = currentLANG;
- lastOPTIONS = currentOPTIONS;
- display.clearDisplay();
- display.setCursor(0, 1);
- if (inputString.length() <= 42)
- {
- for (int i = 0; i <= inputString.length(); i++)
- {
- display.print(inputString[i]);
- }
- }
- else
- {
- for (int i = inputString.length() - 42; i <= inputString.length(); i++)
- {
- display.print(inputString[i]);
- }
- }
- display.drawFastHLine(0, 32, display.width() - 1, BLACK);
- display.setCursor(0, 34);
- val = analogRead(POT);
- val = map(val, 1, 1015, symLen[lang], 0);
- val = constrain(val, 0, symLen[lang]);
- pos = 0;
- if (val > pos + 12) pos = val - 12;
- else if (val < pos) pos = val;
- for (int i = pos; i < val; i++)
- {
- display.print(symbols[lang][i]);
- }
- display.setTextSize(2);
- display.print(symbols[lang][val]);
- display.setTextSize(1);
- for (int i = val + 1; i <= pos + 12; i++)
- {
- display.print(symbols[lang][i]);
- }
- display.display();
- }
- return inputString;
- }
- void Messages() {
- File contacts = SD.open("CONTACTS.TXT", FILE_READ);
- if (!contacts) {
- errorCode = FileError;
- return;
- }
- String* contactList;
- char buf;
- int count = 0;
- while (contacts.available()) {
- buf = contacts.read();
- if (buf == '@') count++;
- else if (buf != '\n' && buf != '\r') contactList[count] += buf;
- }
- contacts.close();
- Serial.println("Contacts were read from SD");
- while (true) {
- if (!checkForErrors()) return;
- display.clearDisplay();
- statusBar();
- buttonsRead();
- if (lastOK == LOW && currentOK == HIGH) {
- MsgToContact(contactList[val]);
- }
- else if (lastBACK == LOW && currentBACK == HIGH) {
- return;
- }
- else if (lastOPTIONS == LOW && currentOPTIONS == HIGH) {
- String params[] = {utf8rus("Удалить"), utf8rus("Добавить")};
- int result = optionsList(params, 2);
- if (result == 0) {
- String* newContactList;
- for (int i = 0; i < val; i++) newContactList[i] = contactList[i];
- for (int i = val + 1;i < count; i++) newContactList[i - 1] = contactList[i];
- contactList = newContactList;
- count--;
- SD.remove("CONTACTS.TXT");
- contacts = SD.open("CONTACTS.TXT", FILE_WRITE);
- for (int i = 0; i < count; i++)
- {
- contacts.println(contactList[i] + '@');
- }
- }
- else if (result == 1) {
- String newName = inputText(true);
- if (newName.length() > 8) {
- display.clearDisplay();
- statusBar();
- display.setCursor(0, 8);
- display.print(utf8rus("Ошибка: длина не должна превышать 8"));
- display.display();
- delay(3000);
- goto a;
- }
- String* newContactList;
- for (int i = 0; i <= val; i++) newContactList[i] = contactList[i];
- newContactList[val] = newName;
- for (int i = val + 1; i < count; i++) newContactList[i + 1] = contactList[i];
- contactList = newContactList;
- count++;
- SD.remove("CONTACTS.TXT");
- contacts = SD.open("CONTACTS.TXT", FILE_WRITE);
- for (int i = 0; i < count; i++)
- {
- contacts.println(contactList[i] + '@');
- }
- a: count = count;
- }
- }
- val = analogRead(POT);
- val = map(val, 100, 1000, 2, 0);
- val = constrain(val, 0, count - 1);
- }
- }
- void Status() {
- while (true) {
- }
- }
- void Settings() {
- while (true) {
- }
- }
- int optionsList(String options[], int len) {
- while (true) {
- if (!checkForErrors) return -1;
- buttonsRead();
- if (lastBACK == LOW && currentBACK == HIGH) return -1;
- else if (lastOK == LOW && currentOK == HIGH) return val;
- display.clearDisplay();
- statusBar();
- val = analogRead(POT);
- val = map(val, 1, 1015, len, 0);
- val = constrain(val, 0, len);
- pos = 0;
- if (val > pos + 5) pos = val - 5;
- else if (val < pos) pos = val;
- for (int i = pos; i < val; i++)
- {
- for (int j = 0; j < options[i].length(); j++)
- {
- display.print(options[i][j]);
- }
- display.display();
- display.println();
- }
- }
- }
- boolean checkForErrors() {
- switch (errorCode)
- {
- case 1:
- SDCardErrorFunc();
- case 2:
- WiFiErrorInitFunc();
- case 3:
- WiFiErrorConnectFunc();
- case 4:
- FTPServerErrorFunc();
- case 5:
- FileErrorFunc();
- }
- if (errorCode && errorCode != WiFiErrorConnect) return false;
- return true;
- }
- boolean checkForNewMessages() {
- if (status != WL_CONNECTED) {
- Serial.println("WiFi is not connected");
- return false;
- }
- }
- void statusBar() {
- display.drawFastHLine(0, 1, display.width() - 1, BLACK);
- display.setCursor(display.width() - 10, 0);
- DateTime dateTime = RTC.now();
- display.print(String(dateTime.hour(), DEC) + ":" + String(dateTime.minute(), DEC));
- display.display();
- }
- void loop() {
- if (!checkForErrors) return;
- buttonsRead();
- if (lastOK == LOW && currentOK == HIGH)
- {
- switch (val)
- {
- case 0:
- Messages();
- case 1:
- Status();
- case 2:
- Settings();
- }
- }
- lastBACK = currentBACK;
- lastOK = currentOK;
- lastLANG = currentLANG;
- lastOPTIONS = currentOPTIONS;
- display.clearDisplay();
- val = analogRead(POT);
- val = map(val, 1, 800, 2, 0);
- val = constrain(val, 0, 2);
- statusBar();
- display.drawBitmap(display.width() / 2 - 16, display.height() / 2 - 21, menu_icons[val], 32, 32, 1);
- display.setCursor(display.width() / 2 - menu_strings[val].length() * 2.8, display.height() / 2 + 11);
- display.print(menu_strings[val]);
- display.display();
- }
- boolean SDCardErrorFunc() {
- if (!SD.begin(10))
- {
- display.clearDisplay();
- display.setCursor(display.width() / 2 - 13 * 2.8, display.height() / 2 + 8);
- display.print("SD Card ERROR");
- display.display();
- Serial.println("SD error");
- delay(5000);
- return false;
- }
- errorCode = 0;
- return true;
- }
- boolean WiFiErrorInitFunc() {
- WiFi.init(&Serial1);
- if (WiFi.status() == WL_NO_SHIELD)
- {
- display.clearDisplay();
- display.setCursor(display.width() / 2 - 15 * 2.8, display.height() / 2 + 8);
- display.print("WiFi Init ERROR");
- display.display();
- Serial.println("WiFi error init");
- delay(5000);
- return false;
- }
- errorCode = 0;
- return true;
- }
- boolean WiFiErrorConnectFunc() {
- if (!connectToSSID) {
- display.clearDisplay();
- display.setCursor(0, display.height() / 2 + 8);
- display.print("No connection with SSID");
- display.display();
- Serial.println("WiFi error connect");
- delay(5000);
- return false;
- }
- errorCode = 0;
- return true;
- }
- boolean FTPServerErrorFunc() {
- Serial.println("FTP server error");
- return false;
- }
- boolean FileErrorFunc() {
- display.clearDisplay();
- display.setCursor(display.width() / 2 - 10 * 2.8, display.height() / 2 + 8);
- display.print("File ERROR");
- display.display();
- Serial.println("File error");
- delay(5000);
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement