Hello there,
I'm using Arduino since a few weeks, I am familiar with the C# environment and have coded a lot of programms for microcontrollers from Velleman to automize my greenhouse, but now these days my new computer doesn't work with these older cards, so I have chosen for an Arduino based solution, I'm surching already a few days to shorten the code but I'm stuck here, I always get errors, so can anyone help me out here?
I made a sketch where I want to make the changes, you can load it simply in an ESP32 WROOM DEV kit without tft or so, the code I use will be uploaded and changed by the interaction on the tft, but there I didn't had a problem to write and use the coding. Only fill in the wifi connection and your password. So here's the modified code, because I already have much more code.
// Using ESP32 Dev Module ESP32 WROOM 32 from AZ delivery https://www.az-delivery.de/en
/* Include libraries */
#include <FS.h> // File system library
#include <SPI.h> // ESP32 Arduino core library
#include <SD.h> // SD card library
#include "time.h" // C:\Users\User\AppData\Local\Arduino..\packages\esp32\hardware\esp32\2.0.9\tools\sdk\esp32\include\newlib\platform_include\*/
#include <WiFi.h> // This library allows an Arduino board to connect to the internet
#include <Wire.h> // Wiring library
/*
* Connect the µSD card module to the following pins:
*
* µSD Card module | ESP32
*
* CS SD D0/GPIO2
* MOSI MOSI/GPIO23
* VSS/GND GND
* VDD/VCC 5V
* CLK/SCK SCK/GPIO18
* MISO MISO/GPIO19
*
* TFT SPI | Resistor | ESP32
*
* VCC - 5V
* GND - GND
* CS 1KΩ SS/GPIO5
* RESET/RST 1KΩ GPIO22 OR 3.3V
* D/C 1KΩ GPIO4
* SDI/MOSI 1KΩ MOSI/GPIO23
* SCK 1KΩ SCK/GPIO18
* LED - 3.3V or GPIO15 (if used to save screen)
* SDO/MISO 1KΩ MISO/GPIO19
*
* SD CARD (if used) | ESP32
* 9 D2 -
* 1 D3 GPIO2
* 2 CMD MOSI/GPIO23
* 3 VSS GND
* 4 VDD 3.3V
* 5 CLK SCK/GPIO18
* 6 VSS GND
* 7 DO MISO/GPIO19
* 8 D1 -
*
*/
/*
Connect following pins from the DHT to the ESP32
* DHT Resistor ESP32
* 1 - 3.3V
* 2 10KΩ 3.3V
* 2 - 12
* 3 - -
* 4 - GND
*/
// Disconnect GPIO22 and wire the tft pin RESET to 3.3V
// BMP 280 connection ESP32
/* VCC 3.3V
* GND GND
* SCL 21
* SDA 22
*/
// Identification and password needed to connect to your router
const char *ssid = "your ssid";
const char *password = "password";
const char *ntpServerName = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
struct tm timeInfo;
// We need a number to switch between pages
int Page = 0;
char prog0[13] = { "111000009001" }; // For example set the output on monday, tuesday, wednesday high at 09:00, the last digit 1 = pinMode high
char prog1[13] = { "111111109010" }; // Every day we set the output off at 09:01, the last digit = 0, so pinMode low
char prog2[13] = { "100000018101" }; // Monday set output on at 18:10
char prog3[13] = { "100000018150" }; // Monday set output off at 18:15
int selProg = 0;
int selIO = 1;
File myFile;
char fileName[] = "/programm.txt";
const int chipSelect = 2;
char charRead;
char inputString[13]; //string to hold read string
int c50 = 0;
void setup() {
Serial.begin(115200);
// connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
int i = 0;
while (i < 15 && WiFi.status() != WL_CONNECTED) {
i++;
Serial.print(".");
if (i == 15) {
// disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
Serial.println("WIFI disconnected");
break;
}
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(" CONNECTED");
}
// init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServerName);
Serial.println("Initializing System")
/* Setup the sd-card and test the type, send the information to the serial monitor */
if (!SD.begin(2)) { // Select chip, use SD_CS GPIO 2
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD Card Size: %lluMB\n", cardSize);
Serial.println("initialisation done.");
writeToFile();
delay(2000);
readFromFile();
printFile();
}
void loop() {
loadProgramm();
delay(20000);
}
void writeToFile() {
myFile = SD.open("/programm.txt", FILE_WRITE);
if (myFile) // it opened OK
{
Serial.println("Writing to programm.txt");
myFile.println(prog0);
myFile.println(prog1);
myFile.println(prog2);
myFile.println(prog3);
// close the file:
myFile.close();
Serial.println("Done");
} else
Serial.println("Error opening programm.txt"); // programm.txt does not exist
// re-open the file for reading:
myFile = SD.open("/programm.txt");
if (myFile) {
Serial.println("Writing to programm.txt");
myFile.println(prog0);
myFile.println(prog1);
myFile.println(prog2);
myFile.println(prog3);
Serial.println("programm.txt created");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.println("Programm text: ");
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening the text file");
}
}
void readFromFile() {
int d = 0;
byte i = 0; //counter
//now read it back and show on Serial monitor
// Check to see if the file exists:
if (!SD.exists("/programm.txt"))
Serial.println("programm.txt doesn't exist.");
Serial.println("Reading from programm.txt:");
myFile = SD.open("/programm.txt");
while (myFile.available()) {
char inputChar = myFile.read(); // Gets one byte from serial buffer
if (inputChar == '\n') //end of line (or 10)
{
inputString[i] = 0; //terminate the string correctly
Serial.println(inputString);
if (d == 0) {
substituteChars(inputString, prog0, 0, 11);
}
if (d == 1) {
substituteChars(inputString, prog1, 0, 11);
}
if (d == 2) {
substituteChars(inputString, prog2, 0, 11);
}
if (d == 3) {
substituteChars(inputString, prog3, 0, 11);
}
d++;
i = 0;
} else {
inputString[i] = inputChar; // Store it
i++;
}
if (i > sizeof(inputString)) {
Serial.println("Incoming string longer than array allows");
Serial.println(sizeof(inputString));
while (1)
;
}
}
// close the file:
myFile.close();
Serial.println("Done reading file");
printFile();
}
void printFile() {
Serial.print("programma 0 = ");
Serial.println(prog0);
Serial.print("programma 1 = ");
Serial.println(prog1);
Serial.print("programma 2 = ");
Serial.println(prog2);
Serial.print("programma 3 = ");
Serial.println(prog3);
}
void substituteChars(char source[], char destination[], byte startAt, byte length) {
for (int pos = 0; pos < length; pos++) {
destination[startAt + pos] = source[pos];
}
}
// So far everything goes fine
void loadProgramm() {
int dayNow;
int daySet;
/*
hour = timeinfo.tm_hour;
min = timeinfo.tm_min;
sec = timeinfo.tm_sec;
day = timeinfo.tm_mday;
month = timeinfo.tm_mon + 1;
year = timeinfo.tm_year +1900;
wday = int from Sunday 0-6
tm_isdst = int Daylight Saving Time
see also: https://cplusplus.com/reference/ctime/tm/
*/
int hourNow = timeInfo.tm_hour;
String setMin = String(prog0[9]);
if (setMin != "0") {
setMin += String(prog0[10]);
} else {
setMin = String(prog0[10]);
}
String setHour = String(prog0[7]);
if (setHour != "0") {
setHour += String(prog0[8]);
} else {
setHour = String(prog0[8]);
}
int minNow = timeInfo.tm_min;
char onOff = prog0[11];
int i = timeInfo.tm_wday;
String weekday = String(i);
if (i == 0) { // Sunday
i = 7;
}
// if (prog0 != "000000000000" || prog0 != "100000000000") {
// Programm at I/O1 != empty
if (String(prog0[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
if (String(onOff) == "1") {
// output high
Serial.println("Output on");
} else {
// output low
Serial.println("Output off");
}
}
/*} else {
Serial.println("Programm empty");
}*/
setMin = String(prog1[9]);
if (setMin != "0") {
setMin += String(prog1[10]);
} else {
setMin = String(prog1[10]);
}
setHour = String(prog1[7]);
if (setHour != "0") {
setHour += String(prog1[8]);
} else {
setHour = String(prog1[8]);
}
minNow = timeInfo.tm_min;
onOff = prog1[11];
if (String(prog1[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
if (String(onOff) == "1") {
// output high
Serial.println("Output on");
} else {
// output low
Serial.println("Output off");
}
}
setMin = String(prog2[9]);
if (setMin != "0") {
setMin += String(prog2[10]);
} else {
setMin = String(prog2[10]);
}
setHour = String(prog2[7]);
if (setHour != "0") {
setHour += String(prog2[8]);
} else {
setHour = String(prog2[8]);
}
onOff = prog2[11];
if (String(prog2[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
if (String(onOff) == "1") {
// output high
Serial.println("Output on");
} else {
// output low
Serial.println("Output off");
}
}
setMin = String(prog3[9]);
if (setMin != "0") {
setMin += String(prog3[10]);
} else {
setMin = String(prog3[10]);
}
setHour = String(prog3[7]);
if (setHour != "0") {
setHour += String(prog3[8]);
} else {
setHour = String(prog3[8]);
}
onOff = prog3[11];
if (String(prog3[i - 1]) == "1" && String(hourNow) == String(setHour) && String(minNow) == String(setMin)) {
if (String(onOff) == "1") {
// output high
Serial.println("Output on");
} else {
// output low
Serial.println("Output off");
}
}
}
// How can I make the loadProgramm, readFromFile and writeToFile more complex but with less code??
// Probably with a class or a function, but I don't know how, I only wrote classes a long time ago into JavaScript and Flash ActionScript
// I also wrote in C#
