完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我想使用 ESP8266 通过 Wifi(使用路由器)在两个 Teensy3.2 之间发送数据。我正在测试以下代码,但它不起作用,你能告诉我这里有什么问题吗?
服务器 代码:全选#include // ESP8266 Class ESP8266_TCP wifi; // Target Access Point #define ssid "AirCard-3D52" #define pass "52836427" // Connect this pin to CH_PD pin on ESP8266 #define PIN_RESET 8 void setup() { delay(3000); // We use Serial1 to interface with ESP8266 // and use Serial to debugging Serial.begin(9600); Serial1.begin(115200); wifi.begin(&Serial1, &Serial, PIN_RESET); // Check that ESP8266 is available if(wifi.test()) { // Connect to target Access Point String ip = connectAP(); // Open TCP Server on port 2000 and 30 seconds for connection timeout (Max 2880) wifi.openTCPServer(2000, 30); } else { // ESP8266 isn't available Serial.println("Check module connection and restart to try again..."); while(true); } } void loop() { // Check for any data has coming to ESP8266 int dataState = wifi.isNewDataComing(WIFI_SERVER); if(dataState != WIFI_NEW_NONE) { if(dataState == WIFI_NEW_CONNECTED) { // Connected with TCP Client Side Serial.println("Status : Connected"); } else if(dataState == WIFI_NEW_DISCONNECTED) { // Disconnected from TCP Client Side Serial.println("Status : Disconnected"); } else if(dataState == WIFI_NEW_MESSAGE) { // Got a message from TCP Client Side digitalWrite(13,HIGH); Serial.println("ID : " + String(wifi.getId())); Serial.println("Message : " + wifi.getMessage()); wifi.closeTCPConnection(0); } else if(dataState == WIFI_NEW_SEND_OK) { // Message transfer has successful Serial.println("SENT!!!!"); } } } // Access Point Connection Function that you can loop connect to Access Point until successful String connectAP() { String ip = "0.0.0.0"; while(ip.equals("0.0.0.0")) { ip = wifi.connectAccessPoint(ssid, pass); if(!ip.equals("0.0.0.0")) { break; } } return ip; } 客户端 代码:全选#include // ESP8266 Class ESP8266_TCP wifi; // Target Access Point #define ssid "AirCard-3D52" #define pass "52836427" // TCP Server IP and port #define serverIP "192.168.1.3" #define serverPort 2000 // Connect this pin to CH_PD pin on ESP8266 #define PIN_RESET 6 // Pin that connected to button to send any message #define PIN_SEND 22 void setup() { delay(3000); // Set pin for send command to input mode pinMode(PIN_SEND, INPUT); // We use Serial1 to interface with ESP8266 // and use Serial to debugging Serial.begin(9600); Serial1.begin(115200); wifi.begin(&Serial1, &Serial, PIN_RESET); // Check that ESP8266 is available if(wifi.test()) { // Connect to target Access Point String ip = connectAP(); delay(1000); } else { // ESP8266 isn't available Serial.println("Check module connection and restart to try again..."); while(true); } } void loop() { // Check for any data has coming to ESP8266 int dataState = wifi.isNewDataComing(WIFI_CLIENT); if(dataState != WIFI_NEW_NONE) { Serial.println("Inside If"); Serial.println(dataState); if(dataState == WIFI_NEW_CONNECTED) { // Connected with TCP Server Side // Send a message to TCP Server Side Serial.println("Sending"); wifi.send("12345678"); } else if(dataState == WIFI_NEW_DISCONNECTED) { // Disconnected from TCP Server Side Serial.println("Disconnected"); } else if(dataState == WIFI_NEW_SEND_OK) { // Message transfer has successfully Serial.println("SENT!!!!"); } else if(dataState == WIFI_NEW_ALREADY_CONNECT) { // Already connected with TCP Server Side Serial.println("Already Connect!!"); } } // When button for connect to TCP Server Side was pressed //if(digitalRead(PIN_SEND)>500) { // Send message to TCP Server Side and waiting for 1 sec Serial.println("Connect!!"); wifi.connectTCP(serverIP, serverPort); delay(1000); //} delay(50); } // Access Point Connection Function that you can loop connect to Access Point until successful String connectAP() { String ip = "0.0.0.0"; while(ip.equals("0.0.0.0")) { ip = wifi.connectAccessPoint(ssid, pass); if(!ip.equals("0.0.0.0")) { break; } } return ip; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
920 浏览 1 评论
552浏览 6评论
461浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
446浏览 5评论
447浏览 4评论
417浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 21:43 , Processed in 0.767490 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号