Skip to content

Instantly share code, notes, and snippets.

@jonasbits
Created January 5, 2020 22:47
Show Gist options
  • Save jonasbits/ed108a58ea1dddd8a9de2e6d539f8d65 to your computer and use it in GitHub Desktop.
Save jonasbits/ed108a58ea1dddd8a9de2e6d539f8d65 to your computer and use it in GitHub Desktop.
/* final version 1.23.25
This is simulating a POP3 server
running on an Arduino
with a ESP-01 wifi module.
ESP-01 is not initilized because
the libraries is missing from Tinkercad Circuits.
We are emulating tx and rx from Serial Monitor.
It should be possible to include Dummy libraries
if there was #ifdef TINKERCAD made available
*/
/*
LiquidCrystal Library
This uses a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
Example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
#include <Arduino.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//check version of Arduino
#define XSTR(x) STR(x)
#define STR(x) #x
//#pragma message XSTR(ARDUINO)
#define BAUD 9600
#define MAX_INPUT 63
#define SPKR 13
//#ifdef TINKERCAD
#define BEEP_TIME 5
#define HELO "+OK POP3 server OK"
#define NO_USER 1
#define NO_PASS 2
#define LOGGED_IN 3
void setup(){
Serial.begin(BAUD);
Serial.println(HELO);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print(HELO);
delay(500);
//indicate start successfull
pinMode(SPKR, OUTPUT);
tone(SPKR, 523, BEEP_TIME);
}
//stub
void handleSerial(int num);
void processIncomingByte (const byte inByte, int num);
void process_data (const char * data);
int mainStart(const char * data);
unsigned int passCount = 0;
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
// // // //1234567890123456
lcd.print("Passwords: 00000");
if ( int bytesWaiting = Serial.available() ) {
handleSerial(bytesWaiting);
//handleSerial(0);
}
lcd.setCursor(15,1);
lcd.print(passCount);
delay(500);
}
void handleSerial(int num) {
// if serial data available, process it
while (Serial.available () > 0) {
processIncomingByte (Serial.read(), num);
}
}
void processIncomingByte (const byte inByte, int num)
{
static char input_line [MAX_INPUT];
static int input_pos = 0;
switch (inByte)
{
case '\n': // end of text, but not used in Tinkercad Circuits
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
//if (input_pos < (MAX_INPUT - 1))
if ( input_pos < (num - 1) ){
input_line [input_pos++] = inByte;
}else{
//workaround tinkercad
input_line [input_pos++] = inByte;
input_line [input_pos] = 0; // terminating null byte
input_pos = 0;
process_data(input_line);
}
break;
} // end of switch
} // end of processIncomingByte
#define LOCAL_ECHO 1
void process_data (const char * data)
{
//Serial.println(sizeof(data) );
#ifdef LOCAL_ECHO
Serial.println(data);
#endif
mainStart(data);
//Serial.println("-ERR Unknown command");
} // end of process_data
enum cmds
{ USER,
PASS,
STAT,
LIST,
RETR,
TOP,
DELE,
UIDL,
NOOP,
RSET,
CAPA,
QUIT
};
const char *cmdQuery[QUIT + 1] =
{
"USER","PASS", "STAT", "LIST", "RETR", "TOP", "DELE", "UIDL", "NOOP", "RSET", "CAPA", "QUIT"};
const char *cmdReply[QUIT + 1] =
{
"+OK accepted","+OK welcome","+OK 0 0 STAT", "+OK 0 messages (0 octets) LIST", "-ERR No such message RETR", "-ERR No such message TOP", "-ERR No such message TOP",
"-ERR No such message UIDL", "+OK NOOP", "+OK 0 messages (0 octets) RSET", "+OK Capability list follows.", "+OK goodbye QUIT"};
#define YES 1
#define NO 2
int mainStart(const char * data)
{
int valid_command = NO;
//char buff[] = "CAPA"; //testing early in dev
int activeCmd = QUIT;
for (int i = 0; i <= QUIT; i++)
{
if (strncasecmp(data, cmdQuery[i], 3) == 0)
{
valid_command = YES;
activeCmd = i;
}
}
if (valid_command == YES)
{
switch (activeCmd)
{
case USER:
Serial.println( cmdReply[activeCmd] ); break;
case PASS:
passCount = passCount + 1;
Serial.println( cmdReply[activeCmd] ); break;
case STAT:
Serial.println( cmdReply[activeCmd] ); break;
case LIST:
Serial.println( cmdReply[activeCmd] ); break;
case RETR:
Serial.println( cmdReply[activeCmd] ); break;
case TOP:
Serial.println( cmdReply[activeCmd] ); break;
case DELE:
Serial.println( cmdReply[activeCmd] ); break;
case UIDL:
Serial.println( cmdReply[activeCmd] ); break;
case NOOP:
Serial.println( cmdReply[activeCmd] ); break;
case RSET:
Serial.println( cmdReply[activeCmd] ); break;
case CAPA:
Serial.println( cmdReply[activeCmd] );
for (int i = 0; i <= QUIT; i++)
Serial.println( cmdQuery[i] );
Serial.println( "." );
break;
case QUIT:
Serial.println( cmdReply[activeCmd] ); break;
default:
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment