hi there, i am finishing a project now. playing a piano keyboard from my phone through my developed app, this app will send the note names (like C4, D4 etc.) to ESP8266 12F through UDP socket over wifi, and the ESP 8266 12F is supposed to send the forward the notes to mega 2560 through software serial.
now the ESP 8266 12F can receive the note names from my phone and sending correctly out (verified through my serial monitor in arduino IDE), the problem now is the mega 2560 can't receive the note names through serial. i paste my codes here as well as the connections, can anyone help me on this? thank you
here are my codes for ESP 8266 12F
/*
UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
adapted from Ethernet library examples
*/
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#ifndef STASSID
#define STASSID "MIWIFI_Td7J"
#define STAPSK "cTQhbxAp"
#endif
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
WiFiUDP Udp;
//D2, D3 pin
SoftwareSerial NodeMCU(4, 0);
void setup() {
Serial.begin(115200);
pinMode(4, INPUT);
pinMode(0, OUTPUT);
NodeMCU.begin(4800);
NodeMCU.print("setup from esp8266");
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("UDP server on port %d\n", localPort);
Udp.begin(localPort);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.printf("Received packet of size %d from %s:%d\n (to %s:%d, free heap = %d B)\n",
packetSize,
Udp.remoteIP().toString().c_str(), Udp.remotePort(),
Udp.destinationIP().toString().c_str(), Udp.localPort(),
ESP.getFreeHeap());
// read the packet into packetBufffer
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
packetBuffer[n] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println(NodeMCU.write(packetBuffer));
NodeMCU.print("ABC");
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
/*
test (shell/netcat):
--------------------
nc -u 192.168.esp.address 8888
*/
here is my code for mega 2560
/*
TAO PING 2022年12月28日
使用无源蜂鸣器播放《天空之城》, 同时在8*8的LED上显示note
*/
#include <SoftwareSerial.h>
SoftwareSerial arduinoSerial(3, 2);
/*LED相关常量*/
//LED pin numbers
const int ledRows[8] = {51, 32, 30, 45, 26, 47, 43, 41};
const int ledCols[8] = {28, 34, 24, 36, 49, 22, 53, 39};
const unsigned char NOTE_HIGH[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char NOTE_LOW[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1}
};
const unsigned char NOTE_MUTE[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char C[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char D[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char E[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char F[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char G[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char A[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char B[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
/*蜂鸣器相关常量*/
//低音区频率
const int LC = 262; //-C
const int LD = 294; //-D
const int LE = 330; //-E
const int LF = 349; //-F
const int LG = 392; //-G
const int LA = 440; //-A
const int LB = 494; //-B
//中音区频率
const int MU = 0; // MUTE
const int MC = 523; //C
const int MD = 587; //D
const int ME = 659; //E
const int MF = 698; //F
const int MG = 784; //G
const int MA = 880; //A
const int MB = 988; //B
//高音区频率
const int HC = 1046; //+C
const int HD = 1175; //+D
const int HE = 1318; //+E
const int HF = 1397; //+F
const int HG = 1568; //+G
const int HA = 1760; //+A
const int HB = 1976; //+B
/*曲谱相关常量*/
//定义节奏的时延,配合倍率调整速度
const float T = 1.5; //Tripple
const float W = 1; //WHOLE
const float H = 0.5; //HALF
const float Q = 0.25; //QUARTER
const float I = 0.125; //EIGHTH 不能用E,因为跟音符冲突了
const float S = 0.0625; //SIXTEENTH
//倍率:节奏1的速度,单位为开发板的执行速度
const int SPEED = 1000;
//天空之城频率谱
const int pieceTune[] = {
MU, MU, MU, MA, MB, HC, MB, HC, HE, MB, MB, MB, ME, ME, MA, MG, MA, HC, MG, MG, MG,
ME, MF, ME, MF, HC, ME, ME, MU, HC, HC, HC, MB, MF, MF, MB, MB, MB, MU, MA, MB, HC,
MB, HC, HE, MB, MB, MB, ME, ME, MA, MG, MA, HC, MG, MG, MG, MD, ME, MF, HC, MB, MB,
HC, HC, HD, HD, HE, HC, HC, HC, HC, MB, MA, MA, MB, MG, MA, MA, MA, HC, HD, HE, HD,
HE, HG, HD, HD, HD, MG, MG, HC, MB, HC, HE, HE, HE, HE, HE, MA, MB, HC, MB, HD, HD,
HC, MG, MG, MG, HF, HE, HD, HC, HE, HE, HE, HE, HA, HA, HG, HG, HE, HD, HC, HC, MU,
HC, HD, HC, HD, HD, HG, HE, HE, HE, HE, HA, HA, HG, HG, HE, HD, HC, HC, MU, HC, HD,
HC, HD, HD, MB, MA, MA, MA, MA, MB};
//天空之城显示谱
const String pieceNote[]= {
"MU", "MU", "MU", "MA", "MB", "HC", "MB", "HC", "HE", "MB", "MB", "MB", "ME", "ME",
"MA", "MG", "MA", "HC", "MG", "MG", "MG", "ME", "MF", "ME", "MF", "HC", "ME", "ME",
"MU", "HC", "HC", "HC", "MB", "MF", "MF", "MB", "MB", "MB", "MU", "MA", "MB", "HC",
"MB", "HC", "HE", "MB", "MB", "MB", "ME", "ME", "MA", "MG", "MA", "HC", "MG", "MG",
"MG", "MD", "ME", "MF", "HC", "MB", "MB", "HC", "HC", "HD", "HD", "HE", "HC", "HC",
"HC", "HC", "MB", "MA", "MA", "MB", "MG", "MA", "MA", "MA", "HC", "HD", "HE", "HD",
"HE", "HG", "HD", "HD", "HD", "MG", "MG", "HC", "MB", "HC", "HE", "HE", "HE", "HE",
"HE", "MA", "MB", "HC", "MB", "HD", "HD", "HC", "MG", "MG", "MG", "HF", "HE", "HD",
"HC", "HE", "HE", "HE", "HE", "HA", "HA", "HG", "HG", "HE", "HD", "HC", "HC", "MU",
"HC", "HD", "HC", "HD", "HD", "HG", "HE", "HE", "HE", "HE", "HA", "HA", "HG", "HG",
"HE", "HD", "HC", "HC", "MU", "HC", "HD", "HC", "HD", "HD", "MB", "MA", "MA", "MA",
"MA", "MB"};
//节奏,与谱一一对应
const float pieceDuration[] = {
W, W, W, H, H, T, H, W, W, W, W, W, H, H, T, H, W, W, W, W, W, W, T, H, W, W, W, W,
H, H, H, H, T, H, W, W, W, W, W, H, H, T, H, W, W, W, W, W, H, H, T, H, W, W, W, W,
W, H, H, W, H, Q, Q, Q, H, H, H, H, Q, H, W, H, H, H, H, W, W, W, W, W, H, H, T, H,
W, W, W, W, W, H, H, T, H, W, W, W, W, W, W, H, H, W, W, H, H, T, Q, H, W, W, W, W,
W, W, W, W, W, W, W, W, W, H, H, W, W, H, H, W, H, H, W, W, W, W, W, W, W, W, W, W,
H, H, W, W, H, H, W, H, Q, H, W, W, W, W, H, H};
const int tonePin = 8; //蜂鸣器的pin
int noteLen;//定义一个变量用来表示共有多少个音符
void setup() {
pinMode(3, INPUT);
pinMode(2, OUTPUT);
arduinoSerial.begin(4800);
pinMode(tonePin, OUTPUT); //设置蜂鸣器的pin为输出模式
noteLen = sizeof(pieceTune) / sizeof(pieceTune[0]); //这里用了一个sizeof函数,查出数组里有多少个音符
// initialize digital pin LED_BUILTIN as an output.
for(int i=0;i<8;i++){
pinMode(ledRows[i], OUTPUT);
pinMode(ledCols[i], OUTPUT);
}
Serial.begin(9600);
Serial.println("this is message from board");
}
void loop() {
// Serial.println(arduinoSerial.available());
while(arduinoSerial.available()>0){
Serial.println("got a message!");
Serial.println(arduinoSerial.readString());
}
for (int x = 0; x < noteLen; x++) { //循环音符的次数
tone(tonePin, pieceTune[x]); //依次播放pieceTune数组元素,即每个音符
//通过LED显示来控制节奏,不能使用delay,否则LED无法持续点亮
// Serial.println("from loop");
showNote(x);
// delay(int(SPEED * pieceDuration[x])); //每个音符持续的时间,即节拍pieceDuration,SPEED是调整时间的越大,曲子速度越慢,越小曲子速度越快
noTone(tonePin);//停止当前音符,进入下一音符
}
delay(2000);//等待2秒后,循环重新开始
}
//显示note
void showNote(int noteIndex) {
for (int i = 0; i < int(SPEED * pieceDuration[noteIndex]); i++) {
if (pieceNote[noteIndex].equals("MU"))
displayNote(NOTE_MUTE, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MC"))
displayNote(C, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MD"))
displayNote(D, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("ME"))
displayNote(E, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MF"))
displayNote(F, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MG"))
displayNote(G, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MA"))
displayNote(A, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("MB"))
displayNote(B, NOTE_MUTE);
else if (pieceNote[noteIndex].equals("HC"))
displayNote(C, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HD"))
displayNote(D, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HE"))
displayNote(E, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HF"))
displayNote(F, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HG"))
displayNote(G, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HA"))
displayNote(A, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("HB"))
displayNote(B, NOTE_HIGH);
else if (pieceNote[noteIndex].equals("LC"))
displayNote(C, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LD"))
displayNote(D, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LE"))
displayNote(E, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LF"))
displayNote(F, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LG"))
displayNote(G, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LA"))
displayNote(A, NOTE_LOW);
else if (pieceNote[noteIndex].equals("LB"))
displayNote(B, NOTE_LOW);
}
// time1 = millis();
// Serial.print(time1);
// Serial.print(", gap: ");
// Serial.println(time1 - time);
}
//展示音符,第一个参数是音,第二个参数表示是高中低音,通过对位求和展示高低音
void displayNote(unsigned char note[8][8], unsigned char key[8][8]) {
for (int r = 0; r < 8; r++) {
// take the row pin (anode) high:
digitalWrite(ledRows[r], HIGH);
// iterate over the ledCols (cathodes):
for (int c = 0; c < 8; c++) {
// get the state of the current pixel;
int thisPixel = (note[r][c] + key[r][c]) == 1 ? 0 : 1;
// when the row is HIGH and the col is LOW,
// the LED where they meet turns on:
digitalWrite(ledCols[c], thisPixel);
// turn the pixel off:
if (thisPixel == LOW) {
digitalWrite(ledCols[c], HIGH);
}
}
// take the row pin low to turn off the whole row:
digitalWrite(ledRows[r], LOW);
}
}