2022-6-3 11:58:40 [显示全部楼层]
2814浏览
查看: 2814|回复: 0

[ESP8266/ESP32] 用esp32通过wifi和processing的通信

[复制链接]
拿到了Beetle ESP32-C3的试用,准备做关于一个感应人体播放视频的项目
为了这个项目先做了一些预研。
有了这个小项目
给ESP32烧写程序
  1. /*
  2. *  This sketch sends a message to a TCP server
  3. *
  4. */
  5. #include <WiFi.h>
  6. #include <WiFiMulti.h>
  7. WiFiMulti WiFiMulti;
  8. int k=1;
  9. void setup()
  10. {
  11.     Serial.begin(115200);
  12.     delay(10);
  13.     pinMode(0, INPUT);
  14.     // We start by connecting to a WiFi network
  15.     WiFiMulti.addAP("wifi", "password");
  16.     Serial.println();
  17.     Serial.println();
  18.     Serial.print("Waiting for WiFi... ");
  19.     while(WiFiMulti.run() != WL_CONNECTED) {
  20.         Serial.print(".");
  21.         delay(500);
  22.     }
  23.     Serial.println("");
  24.     Serial.println("WiFi connected");
  25.     Serial.println("IP address: ");
  26.     Serial.println(WiFi.localIP());
  27.     delay(500);
  28. }
  29. void loop()
  30. {
  31. //    const uint16_t port = 80;
  32. //    const char * host = "192.168.1.1"; // ip or dns
  33.     const uint16_t port =10002;
  34.     const char * host = "192.168.1.102"; // ip or dns
  35.     Serial.print("Connecting to ");
  36.     Serial.println(host);
  37.     // Use WiFiClient class to create TCP connections
  38.     WiFiClient client;
  39.     if (!client.connect(host, port)) {
  40.         Serial.println("Connection failed.");
  41.         Serial.println("Waiting 5 seconds before retrying...");
  42.         delay(5000);
  43.         return;
  44.     }
  45.     // This will send a request to the server
  46.     //uncomment this line to send an arbitrary string to the server
  47.     //client.print("Send this data to the server");
  48.     //uncomment this line to send a basic document request to the server
  49.     //client.print("GET /index.html HTTP/1.1\n\n");
  50.    
  51.       client.write(k);
  52.     delay(5000);
  53.     k++;
  54.   int maxloops = 0;
  55.   //wait for the server's reply to become available
  56.   while (!client.available() && maxloops < 1000)
  57.   {
  58.     maxloops++;
  59.     delay(1); //delay 1 msec
  60.   }
  61.   if (client.available() > 0)
  62.   {
  63.     //read back one line from the server
  64.     String line = client.readStringUntil('\r');
  65.     Serial.println(line);
  66.   }
  67.   else
  68.   {
  69.     Serial.println("client.available() timed out ");
  70.   }
  71.    // Serial.println("Closing connection.");
  72.    // client.stop();
  73.     //Serial.println("Waiting 5 seconds before restarting...");
  74.     //delay(5000);
  75. }
复制代码
然后用processing写一个服务器程序

  1. import processing.net.*;
  2. int port = 10002;      
  3. Server myServer;        
  4. void setup()
  5. {
  6.   size(400, 400);
  7.   background(0);
  8.   myServer = new Server(this, port);
  9. }
  10. void draw()
  11. {
  12.   // Get the next available client
  13.   Client thisClient = myServer.available();
  14.   // If the client is not null, and says something, display what it said
  15.   if (thisClient !=null) {
  16.     int whatClientSaid = thisClient.read();
  17.     //if (whatClientSaid != null) {
  18.       println(whatClientSaid);
  19.     //}
  20.   }
  21. }
复制代码
来读取esp32传过来的数据。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail