#include <SPI.h>
#include <WiFi.h>
#include <Wire.h>
#include <U8g2lib.h>
#define LCDWidth u8g2.getDisplayWidth()
#define ALIGN_CENTER(t) ((LCDWidth - (u8g2.getUTF8Width(t))) / 2)
#define ALIGN_RIGHT(t) (LCDWidth - u8g2.getUTF8Width(t))
#define ALIGN_LEFT 0
#define CONNECTION_TIMEOUT 10 // Seconds
char* wifiMode = "";
int btn = 12;
int btnTrigger = 19;
int mode = 0;
unsigned long previousTime = 0;
static const unsigned char image_drop_bits[] U8X8_PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x7f, 0x00, 0x00, 0xff, 0x00, 0x80, 0xff, 0x01, 0x80, 0xff, 0x01, 0xc0, 0xff, 0x01, 0xc0, 0xff, 0x03, 0x40, 0xff, 0x03, 0x40, 0xff, 0x03, 0x40, 0xfe, 0x03, 0x80, 0xfe, 0x01, 0x80, 0xfc, 0x01, 0x00, 0xf3, 0x00, 0x70, 0x7e, 0x0e, 0xce, 0x81, 0x73, 0xe7, 0x00, 0xe7, 0x87, 0xff, 0xe1, 0x3c, 0x00, 0x3c, 0xe0, 0xff, 0x07};
static const unsigned char image_menu_help_sign_black_bits[] U8X8_PROGMEM = {0xe0, 0x03, 0x18, 0x0c, 0xc4, 0x11, 0xe2, 0x23, 0xf2, 0x27, 0x31, 0x47, 0x01, 0x47, 0x81, 0x43, 0xc1, 0x41, 0xc1, 0x41, 0x02, 0x20, 0xc2, 0x21, 0xc4, 0x11, 0x18, 0x0c, 0xe0, 0x03, 0x00, 0x00};
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
void setup() {
Serial.begin(115200);
pinMode(btn, INPUT_PULLUP);
pinMode(btnTrigger, INPUT_PULLUP);
u8g2.begin();
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 57, "Machine");
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(34, 10, "Loading...");
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 38, "Drop");
u8g2.drawXBM(81, 17, 24, 24, image_drop_bits);
} while (u8g2.nextPage());
delay(1000);
int timeout_counter = 0;
wl_status_t state;
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 57, "Machine");
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(1, 10, "Connecting to Wifi...");
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 38, "Drop");
u8g2.drawXBM(81, 17, 24, 24, image_drop_bits);
} while (u8g2.nextPage());
delay(250);
timeout_counter++;
if (timeout_counter >= CONNECTION_TIMEOUT * 5) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 57, "Machine");
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(15, 10, "Connection failed");
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 38, "Drop");
u8g2.drawXBM(81, 17, 24, 24, image_drop_bits);
} while (u8g2.nextPage());
delay(2000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 57, "Machine");
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(18, 10, "Starting as AP");
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 38, "Drop");
u8g2.drawXBM(81, 17, 24, 24, image_drop_bits);
} while (u8g2.nextPage());
// Start AP
delay(2000);
}
}
wifiMode = "STA";
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 57, "Machine");
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(13, 10, "Connected to Wifi");
u8g2.setFont(u8g2_font_profont22_tr);
u8g2.drawStr(23, 38, "Drop");
u8g2.drawXBM(81, 17, 24, 24, image_drop_bits);
} while (u8g2.nextPage());
delay(2000);
}
void printwords(int xloc, int yloc /*bottom*/, const char *msg) {
int dspwidth = u8g2.getDisplayWidth(); // display width in pixels
int strwidth = 0; // string width in pixels
char glyph[2]; glyph[1] = 0;
for (const char *ptr = msg, *lastblank = NULL; *ptr; ++ptr) {
while (xloc == 0 && (*msg == ' ' || *msg == '\n'))
if (ptr == msg++) ++ptr; // skip blanks and newlines at the left edge
glyph[0] = *ptr;
strwidth += u8g2.getStrWidth(glyph); // accumulate the pixel width
if (*ptr == ' ') lastblank = ptr; // remember where the last blank was
else ++strwidth; // non-blanks will be separated by one additional pixel
if (*ptr == '\n' || // if we found a newline character,
xloc + strwidth > dspwidth) { // or if we ran past the right edge of the display
int starting_xloc = xloc;
// print to just before the last blank, or to just before where we got to
while (msg < (lastblank ? lastblank : ptr)) {
glyph[0] = *msg++;
xloc += u8g2.drawStr(xloc, yloc, glyph);
}
strwidth -= xloc - starting_xloc; // account for what we printed
yloc += u8g2.getMaxCharHeight() + 1; // advance to the next line
xloc = 0; lastblank = NULL;
}
}
while (*msg) { // print any characters left over
glyph[0] = *msg++;
xloc += u8g2.drawStr(xloc, yloc, glyph);
}
}
void loop() {
unsigned long currentTime = millis();
if (digitalRead(btn) == LOW)
{
if (mode == 4)
{
mode = 0;
}
else {
mode += 1;
}
Serial.println(mode);
delay(250);
previousTime = currentTime;
}
if (currentTime - previousTime < 3000) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_siji_t_6x10);
u8g2.drawGlyph(0, 10, 0xE21A);
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(20, 10, wifiMode);
//u8g2.drawXBM(97, 37, 15, 16, image_menu_help_sign_black_bits);
u8g2.drawStr(80, 10, String("Mode: " + String(mode)).c_str());
switch (mode) {
case 0:
printwords(0, 26, "Water release for 2 seconds.");
break;
case 1:
printwords(0, 26, "Single drop with camera trigger at the start.");
break;
case 2:
printwords(0, 26, "Double drop with camera trigger at the start.");
break;
case 3:
printwords(0, 26, "Single drop with camera trigger at the end.");
break;
case 4:
printwords(0, 26, "Double drop with camera trigger at the end.");
break;
}
} while ( u8g2.nextPage() );
//delay(3000);
} else {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.drawStr(0, 26, "IP: 192.168.1.4");
u8g2.drawStr(0, 38, "Valve open: 90 ms");
u8g2.drawStr(0, 50, "Drop delay: 50 ms");
u8g2.drawStr(0, 62, "Flash delay: 290 ms");
u8g2.drawStr(20, 10, wifiMode);
u8g2.drawStr(80, 10, String("Mode: " + String(mode)).c_str());
u8g2.setFont(u8g2_font_siji_t_6x10);
u8g2.drawGlyph(0, 10, 0xE21A);
} while ( u8g2.nextPage() );
}
if (digitalRead(btnTrigger) == LOW)
{
// Start Animation
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(49, 40, "03");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(56, 48, "sec");
} while ( u8g2.nextPage() );
delay(1000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(49, 40, "02");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(56, 48, "sec");
} while ( u8g2.nextPage() );
delay(1000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(49, 40, "01");
u8g2.setFont(u8g2_font_profont12_tr);
u8g2.drawStr(56, 48, "sec");
} while ( u8g2.nextPage() );
delay(1000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(49, 40, "GO");
} while ( u8g2.nextPage() );
// Dropping
delay(1000);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_profont29_tr);
u8g2.drawStr(32, 40, "DONE");
} while ( u8g2.nextPage() );
delay(1000);
}
}