|
本例中编写一个M5Stack程序用于与PC端进行UDP通讯,这样让PC(Python)执行诸如深度学习之类的繁重操作,可以将操作结果发送到M5Stack。能够做这样的事情似乎很有趣,所以我在Python的发送端编写了源代码。在本文中,我将介绍接收方(Arduino)和发送方(Python)的源代码。在此过程中需要使用家用路由器或智能手机作为接入点建立网络,并与UDP通信。M5Stack应用程序源代码-UDP数据接收
- #include <M5Stack.h>
- #include <WiFi.h>
- #include <WiFiUdp.h>
-
- #define N 1024
-
- const char* ssid = "yourssid";
- const char* password = "yourpassword";
- const int port = ****;
-
- // The udp library class
- WiFiUDP udp;
-
- void print_wifi_state(){
- M5.Lcd.clear(BLACK); // clear LCD
- M5.Lcd.setTextColor(YELLOW);
- M5.Lcd.setCursor(3, 3);
- M5.Lcd.println("");
- M5.Lcd.println("WiFi connected.");
- M5.Lcd.print("IP address: ");
- M5.Lcd.println(WiFi.localIP());
- M5.Lcd.print("Port: ");
- M5.Lcd.println(port);
- }
-
- void setup_wifi(){
- M5.Lcd.setTextColor(RED);
- M5.Lcd.setTextSize(2);
- M5.Lcd.setCursor(3, 10);
- M5.Lcd.print("Connecting to ");
- M5.Lcd.println(ssid);
-
- // setup wifi
- WiFi.mode(WIFI_STA); // WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF
- WiFi.begin(ssid, password);
- // WiFi.begin();
-
- // Connecting ..
- while (WiFi.status() != WL_CONNECTED) {
- delay(100);
- M5.Lcd.print(".");
- }
-
- // print state
- print_wifi_state();
-
- udp.begin(port);
- }
-
- void setup() {
- M5.begin();
-
- // setup wifi
- setup_wifi();
- }
-
- void loop() {
- char packetBuffer[N];
- int packetSize = udp.parsePacket();
-
- // get packet
- if (packetSize){
- int len = udp.read(packetBuffer, packetSize);
- if (len > 0){
- packetBuffer[len] = '\0'; // end
- }
-
- // print param
- M5.Lcd.clear(BLACK);
- M5.Lcd.setCursor(3, 3);
- M5.Lcd.setTextColor(GREEN);
- M5.Lcd.println(packetBuffer);
- }
- }
复制代码
Python源代码-UDP数据传输
- from socket import socket, AF_INET, SOCK_DGRAM
-
- ADDRESS = "192.168.2.3" # M5Stack address
- PORT = 5555
-
- s = socket(AF_INET, SOCK_DGRAM)
-
- while True:
- msg = input("> ")
- s.sendto(msg.encode(), (ADDRESS, PORT))
-
- s.close()
复制代码 M5Stack和PC之间的UDP通信测试-制作聊天社应用程序(UDP版本)
- #include <M5Stack.h>
- #include <AquesTalkTTS.h>
- #include <Avatar.h>
- #include <tasks/LipSync.h>
- #include <WiFi.h>
- #include <WiFiUdp.h>
-
- #define N 30
-
- using namespace m5avatar;
-
- // AquesTalk License Key
- // NULL or wrong value is just ignored
- const char* AQUESTALK_KEY = "XXXX-XXXX-XXXX-XXXX";
-
- // for wifi setup
- const char* ssid = "yourssid";
- const char* password = "yourpassword";
- const int port = ****;
-
- Avatar avatar;
- WiFiUDP udp;
-
- void setup_wifi(){
- M5.Lcd.setTextColor(RED);
- M5.Lcd.setTextSize(2);
- M5.Lcd.setCursor(3, 10);
- M5.Lcd.print("Connecting to ");
- M5.Lcd.println(ssid);
-
- // wifi setup
- WiFi.mode(WIFI_STA); // WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF
- WiFi.begin(ssid, password);
- // WiFi.begin();
-
- // Connecting ..
- while (WiFi.status() != WL_CONNECTED) {
- delay(100);
- M5.Lcd.print(".");
- }
-
- udp.begin(port);
- }
-
- void setup() {
- int iret;
- M5.begin();
-
- // setup wifi
- setup_wifi();
-
- // For Kanji-to-speech mode (requires dictionary file saved on microSD)
- // See http://blog-yama.a-quest.com/?eid=970195
- // iret = TTS.createK(AQUESTALK_KEY);
- iret = TTS.create(AQUESTALK_KEY);
- M5.Lcd.setBrightness(30);
- M5.Lcd.clear();
- avatar.init();
- avatar.addTask(lipSync, "lipSync");
- }
-
- void loop() {
- char packetBuffer[N];
- int packetSize = udp.parsePacket();
-
- // get packet
- if (packetSize){
- int len = udp.read(packetBuffer, packetSize);
- if (len > 0){
- packetBuffer[len] = '\0'; // end
- }
- TTS.play(packetBuffer, 80);
- avatar.setSpeechText(packetBuffer);
- delay(1000);
- // clear "c"
- while (len >= 0){
- len--;
- packetBuffer[len] = ' ';
- }
- }
- }
复制代码
|
|