我的接收数据正确,但是无法保存在新变量中

时间:2019-12-26 19:05:11

标签: c++ arduino-uno nodemcu

为什么d值不等于我得到的数据?

我尝试让nodemcu获得NTP时间并将其发送到Arduino UNO。 Arduino从nodemcu接收数据,然后将它们排列为char数据。到目前为止一切顺利。

但是,当我设置新字符以保存数据时,我打印的值始终显示snark标记。

我该如何解决?

这是Arduino UNO代码

#include <SoftwareSerial.h>
SoftwareSerial s(5,6);

void setup() {
  // Initialize Serial port
  Serial.begin(9600);
  s.begin(9600);
  while (!Serial) continue;
}
int k=0;
void seting(char d){
  if(k==0){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set Y");
  }else if(k==1){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set M");
  }else if(k==2){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set D");
  }else if(k==3){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set WD");
  }else if(k==4){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set H");
  }else if(k==5){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set min");
  }else if(k==6){
    Serial.print("get = ");
    Serial.println(d);
    Serial.println("set sec");
  }else{
    k=0;
  }k++;
}

int i = 0;
char data[4];

void loop() {
  while(s.available()){

    char x = s.read();
    if(isdigit(x)){
      data[i]=x;
      i++;
    }
    else{
      Serial.print("data=");
      Serial.println(data);
      seting(data);

      memset(data,0,sizeof(data));
      i=0;
    }
  }
}

这是nodemcu代码

#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);

const char *ssid     = "dlink-0C00";
const char *password = "yjjri18598";

const long utcOffsetInSeconds = 28800;//TW+8,8*60*60
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

String formattedDate;
String dayStamp;
String timeStamp;

//Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);

void setup(){
  Serial.begin(9600);
  s.begin(9600);
  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}


void loop() {
  timeClient.update();


  formattedDate = timeClient.getFormattedDate();
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);
  Serial.print("DATE: ");
  Serial.print(dayStamp);
  s.print(dayStamp);
  s.print("/");
  Serial.print(", ");
  Serial.println(daysOfTheWeek[timeClient.getDay()]);
  s.print(timeClient.getDay());
  s.print("/");
  /*timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
  Serial.print("TIME:");
  Serial.println(timeStamp);*/
  int hour = timeClient.getHours();
  int minute = timeClient.getMinutes();
  int second = timeClient.getSeconds();
  Serial.print("TIME:");
  Serial.print(hour);
  s.print(hour);
  Serial.print(":");
  s.print(":");
  Serial.print(minute);
  s.print(minute);
  Serial.print(":");
  s.print(":");
  Serial.println(second);
  s.print(second);
  //s.write(timeStamp);
  s.write("/");
  delay(1000);
}

1 个答案:

答案 0 :(得分:0)

传递给函数seting()的参数是char的{​​{1}}数组指针,这可能就是您的输出不正确的原因。在Arduino代码中,尝试更改: char data[4];void seting(char d){ 因为传递给void seting(char *d){的数据是一个数组: seting()

还要修改您的char data[4];,以将println(d)用作输入变量kd的索引。如果可以解决问题,则可能需要将println(d[k])的大小调整为char data[4],以便可以使用char data[7]对其进行索引,该索引似乎从k更改为{{1 }}根据发布的Arduino代码。