与NodeMCU的TX / RX通信

时间:2019-02-23 22:14:48

标签: wifi nodemcu arduino-esp8266

我正在尝试使用WiFi在两个NodeMCU(ESP-12e)之间建立一种收发器风格的关系。我将一个NodeMCU(“发送器”)配置为服务器,将另一个(“接收器”)配置为客户端。发射器读取一个模拟值并将其发送到接收器,接收器通过8个LED将其以数字格式输出。发射器说网络处于活动状态,但客户端无法连接,当我在便携式WiFi管理器中查看时,网络也不存在。有人可以告诉我我在做什么错吗?是我的代码中的东西,还是电路板的配置方式?

发送器/服务器代码:

#include <ESP8266WiFi.h>

#define PORT 8555
#define LED0   13
#define LED1   14
#define ADC     0

const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
WiFiServer  host(PORT);
WiFiClient  node;
IPAddress IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);

void setup() {
    pinMode(LED0, OUTPUT);
    pinMode(LED1, OUTPUT);
    pinMode(ADC,  INPUT);
    digitalWrite(LED0, LOW);
    digitalWrite(LED1, LOW);

    WiFi.disconnect();
    WiFi.mode(WIFI_AP);
    WiFi.softAP(NETWORK, PASSKEY);
    WiFi.softAPConfig(IP, gateway, subnet);
    delay(100);
    WiFi.begin(NETWORK, PASSKEY);
    delay(100);
    host.begin();
    delay(100);
    digitalWrite(LED1, HIGH);
}

void loop() {
    if (node.connected()) digitalWrite(LED0, HIGH);
    else digitalWrite(LED0, LOW);
    host.write(analogRead(ADC) / 4);
}

收件人/客户代码:

#include <ESP8266WiFi.h>

#define PORT 8555
#define DAC0   16
#define DAC1    5
#define DAC2    4
#define DAC3    2
#define DAC4   14
#define DAC5   12
#define DAC6   13
#define DAC7   15

const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
IPAddress   hostIP(192,168,4,1);
WiFiClient  node;

void setup() {
    pinMode(DAC0, OUTPUT);
    pinMode(DAC1, OUTPUT);
    pinMode(DAC2, OUTPUT);
    pinMode(DAC3, OUTPUT);
    pinMode(DAC4, OUTPUT);
    pinMode(DAC5, OUTPUT);
    pinMode(DAC6, OUTPUT);
    pinMode(DAC7, OUTPUT);
    digitalWrite(DAC7, HIGH);

    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    WiFi.begin(NETWORK, PASSKEY);
    while (WiFi.status() != WL_CONNECTED) {   // <-- gets stuck here
        digitalWrite(DAC7, LOW);
        delay(200);
        digitalWrite(DAC7, HIGH);
        delay(200);
    }
    if(node.connect(hostIP, PORT)) digitalWrite(DAC7, LOW);
}

void loop() {
    if (node.available()) adcWrite(node.read());
}

void adcWrite(byte level) {
    bool vals[8];
    for (int i = 0; i < 8; i++) {
        vals[i] = level % 2;
        level /= 2;
    }
    digitalWrite(DAC0, vals[0]);
    digitalWrite(DAC1, vals[1]);
    digitalWrite(DAC2, vals[2]);
    digitalWrite(DAC3, vals[3]);
    digitalWrite(DAC4, vals[4]);
    digitalWrite(DAC5, vals[5]);
    digitalWrite(DAC6, vals[6]);
    digitalWrite(DAC7, vals[7]);
}

TX上的端口LED0和LED1和RX上的DAC0-7端口连接到LED,TX上的ADC连接到10k电位器。当我运行代码时,LED0熄灭,LED1点亮,而DAC7无限闪烁。

0 个答案:

没有答案