首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SerialTransfer库接收从nodemcu发送到Arduino UNO的有效载荷中的所有零

使用SerialTransfer库接收从nodemcu发送到Arduino UNO的有效载荷中的所有零
EN

Stack Overflow用户
提问于 2021-08-01 07:46:51
回答 1查看 201关注 0票数 0

我有一个nodemcu主流传感器值到Arduino Uno从UART使用SerialTransfer.h。我已经在Arduino数字引脚2,3上设置了一个额外的串口,用于使用SoftwareSerial.h的Rx,Tx。我已经将Tx在nodemcu上连接到了Uno上,Rx在nodemcu上转到Tx上了。我有一个电平移位器来调整3.3V和5伏Arduino.我已确保提供一个共同点。

我从nodemcu发送带有传感器值的结构(bool和int类型,硬编码用于演示),但在Arduino只接收零值,如串行监视器所见。我的密码在下面。如有任何意见,我将不胜感激。

我试过以下几种方法,但没有区别。

在Uno上使用SoftwareSerial.h

  • Reversing创建的
  1. ,在nodemcu Tx和Arduino Uno Rx

上设置有和不带电平移位器的从

这是nodemcu主人的代码。

代码语言:javascript
复制
#include <Wire.h>
#include <SerialTransfer.h>

SerialTransfer masterMCU;

struct PAYMASTER {
  /*
  water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
  fan: instruction to control fan speed - LO, MED, HIGH. Note PC fan requires an int between 0 and 255.
  led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
  */
  bool water;
  int fan; 
  int led;
} instructions = {
  true,
  201,
  60
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(999);
  masterMCU.begin(Serial);
  delay(999);
}

void debug() {
  Serial.print("MASTER: ");
  Serial.print(millis());
  Serial.print("   Water: ");
  Serial.print(instructions.water);
  Serial.print(", Fan: ");
  Serial.print(instructions.fan);
  Serial.print(", LED: ");
  Serial.println(instructions.led);
}

void loop() {
  // put your main code here, to run repeatedly:
  masterMCU.txObj(instructions, sizeof(instructions));
  masterMCU.sendData(sizeof(instructions));
  debug();
  delay(999);

}

这是Arduino Uno奴隶的代码。

代码语言:javascript
复制
#include <Wire.h>
#include <SerialTransfer.h>
#include <SoftwareSerial.h>

SerialTransfer slaveMCU;
SoftwareSerial extra(2, 3); // Rx 2, Tx 3

struct PAYMASTER {
  /*
  water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
  fan: instruction to control fan speed - LO, MED, HIGH. Note PC fan requires an int between 0 and 255.
  led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
  */
  bool water;
  int fan; 
  int led;
} instructions;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(201);
  extra.begin(9600);
  delay(201);
  slaveMCU.begin(extra);
  delay(201);
}

void debug() {
  Serial.print("SLAVE: ");
  Serial.print(millis());
  Serial.print("   Water: ");
  Serial.print((bool)instructions.water);
  Serial.print(", Fan: ");
  Serial.print(instructions.fan);
  Serial.print(", LED: ");
  Serial.println(instructions.led);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (slaveMCU.available()) {
    slaveMCU.rxObj(instructions, sizeof(instructions));
    debug();
  } else if (slaveMCU.status < 0) {
    Serial.print("ERROR: ");

    if(slaveMCU.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(slaveMCU.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(slaveMCU.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
  delay(999);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-01 19:45:31

我做了一些修改,现在收到的数据都是正确的。

  1. I在master中用millis()代替delay()
  2. 在master中用SerialTransfer::sendDatum()代替SerialTransfer::sendData()。前者用于流多个对象,而后者用于流单个对象。
  3. i将结构中的int类型替换为uint8_t,该结构由主程序和从机发送。

这些值现在在Arduino Uno正确地接收到。直到上面的3点,所有的改变都没有什么区别。我保留了其他的变化,因为它们似乎对结果也很重要。下面是最终代码,用于正确地传输和接收从nodemcu主到Arduino Uno从服务器的对象。

nodemcu主

代码语言:javascript
复制
#include <Wire.h>
#include <SerialTransfer.h>

SerialTransfer masterMCU;
unsigned long tic = millis();
unsigned long toc = tic;

#define DELTA 1000

struct PAYMASTER {
  /*
  water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
  fan: instruction to control fan speed - LO, MED, HIGH. Note PC fan requires an int between 0 and 255.
  led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
  */
  bool water;
  uint8_t fan; 
  uint8_t led;
} instructions = {
  true,
  201,
  60
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(999);
  masterMCU.begin(Serial);
  delay(999);
}

void debug() {
  Serial.print("MASTER: ");
  Serial.print(millis());
  Serial.print("   Water: ");
  Serial.print(instructions.water);
  Serial.print(", Fan: ");
  Serial.print(instructions.fan);
  Serial.print(", LED: ");
  Serial.println(instructions.led);
}

void loop() {
  // put your main code here, to run repeatedly:
  toc = millis();
  if ((toc - tic) > DELTA) {
    masterMCU.txObj(instructions, sizeof(instructions));
    masterMCU.sendDatum(instructions), sizeof(instructions);
    debug();
    tic = toc;
  }

}

Arduino Uno从

代码语言:javascript
复制
#include <Wire.h>
#include <SerialTransfer.h>
#include <SoftwareSerial.h>

SerialTransfer slaveMCU;
SoftwareSerial Extra(2, 3); // Rx: 2, Tx: 3
unsigned long tic = millis();
unsigned long toc = tic;

struct PAYMASTER {
  /*
  water: instruction to switch pump on or off. Note the float sensor in pump's circuit will prevent overflow.
  fan: instruction to control fan speed - LO, MED, HIGH. Note PC fan requires an int between 0 and 255.
  led: instruction to control LED brightness. Note that the FastLED library requires an int between 0 and 255.
  */
  bool water;
  uint8_t fan; 
  uint8_t led;
} instructions;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(201);
  Extra.begin(9600);
  delay(201);
  slaveMCU.begin(Extra);
  delay(201);
}

void debug() {
  Serial.print("SLAVE: ");
  Serial.print(millis());
  Serial.print("   Water: ");
  Serial.print((bool)instructions.water);
  Serial.print(", Fan: ");
  Serial.print(instructions.fan);
  Serial.print(", LED: ");
  Serial.println(instructions.led);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (slaveMCU.available()) {
    slaveMCU.rxObj(instructions);
    debug();
  } else if (slaveMCU.status < 0) {
    Serial.print("ERROR: ");

    if(slaveMCU.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(slaveMCU.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(slaveMCU.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68608433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档