// Code 2
// By MagicBit
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <esp_now.h>
#include <WiFi.h>
esp_now_peer_info_t peerInfo;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Use the MAC of the other board below
uint8_t broadcastAddress[] = {0x78, 0x21, 0x84, 0x8D, 0xFF, 0x50};
int choose = 0;
int inputLine_y = 36;
int inputSpace_x = 7;
int R_count = 0;
int potValue;
char Msg[27]={' '};
char got[18];
char sent[18];
char elements[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&got, incomingData, sizeof(got));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Message: ");
Serial.println(got);
for (int i=0; i<18;++i) {
display.fillRect(i*7,18, 7, 9, BLACK);
}
display.setCursor(1,19); // input
display.setTextColor(WHITE);
display.println(got);
}
void setup() {
Serial.begin(115200);
//===============================
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
esp_now_register_recv_cb(OnDataRecv);
//===============================
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.drawRect(0, 0, 128, 15, 1);
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(20,4);
display.print("MAGIC MESSENGER");
display.drawLine(0,54,127,54,WHITE);
display.display();
display.drawLine(0,32 , 127, inputLine_y-4, WHITE);
pinMode(34,INPUT);
pinMode(35,INPUT);
pinMode(39,INPUT);
}
void loop() {
Serial.println(Msg);
potValue = analogRead(39);
int Lbutton = digitalRead(35);
int Rbutton = digitalRead(34);
if ((Rbutton==LOW)&&(choose==0)) { // For Choose
delay(300);
Msg[R_count] = elements[map(potValue)];
++R_count;
}
if ((Rbutton==LOW)&&(choose==1)) { // Delete all
delay(300);
R_count = 0;
//strcpy(Msg,"");
char Msg_tmp[27]={' '};
strcpy(Msg,Msg_tmp);
for (int i=0; i<18;++i) {
display.fillRect(inputSpace_x * i, inputLine_y - 1, 7, 9, BLACK);
}
}
if ((Rbutton==LOW)&&(choose==2)) {
delay(300);
strcpy(sent, Msg);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &sent, sizeof(sent));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
//==============================
if (Lbutton == LOW){
delay(300);
choose++;
if (choose > 2) {
choose = 0;
}
}
menu(choose);
//==============================
display.fillRect(inputSpace_x * R_count, inputLine_y, 5, 7, BLACK);
display.fillRect(inputSpace_x * R_count, inputLine_y - 1, 7, 9, WHITE);
display.setCursor(inputSpace_x * R_count + 1, inputLine_y);
display.setTextColor(BLACK);
display.println(elements[map(potValue)]);
for (int i=0; i<R_count;++i) {
display.fillRect(inputSpace_x * i, inputLine_y - 1, 7, 9, BLACK);
display.setCursor(inputSpace_x*i +1, inputLine_y); // input
display.setTextColor(WHITE);
display.println(Msg[i]);
}
display.display();
}
void menu(int pos) {
switch (pos) {
case 0 :{
display.fillRect(48, 55, 45, 9, BLACK); //delete
display.fillRect(98, 55, 30, 9, BLACK); //SEND
display.fillRect(0, 55, 41, 9, WHITE);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(3, 56);
display.println("Choose");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(51, 56);
display.println("Delete");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(101, 56);
display.println("Send");
display.display();
}
break;
case 1 :{
display.fillRect(0, 55, 41, 9, BLACK); //choose
display.fillRect(98, 55, 30, 9, BLACK); //SEND
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(3, 56);
display.println("Choose");
display.fillRect(48, 55, 41, 9, WHITE);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(51, 56);
display.println("Delete");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(101, 56);
display.println("Send");
display.display();
}
break;
case 2 :
{
display.fillRect(0, 55, 41, 9, BLACK); //choose
display.fillRect(48, 55, 45, 9, BLACK); //delete
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(3, 56);
display.println("Choose");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(51, 56);
display.println("Delete");
display.fillRect(98, 55, 30, 9, WHITE);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(101, 56);
display.println("Send");
display.display();
}
break;
}
}
int map(int potValue) {
if (potValue == 4095) {
return 36;
}
else
{
return int((potValue*37)/4095);
}
}
Comments