单片机之神 发表于 2023-12-6 21:53:23

ESP32C3在arduino串口监视中无法打印数据

如题。串口波特率已经选择921600,求各位大佬解答。

void setup() {
// put your setup code here, to run once:
Serial.begin(921600);
Serial.printf("hello world");
WiFi.begin(id,psw);
while (WiFi.status()!=WL_CONNECTED) {
    delay(500);
    Serial.println("正在连接...");
}
Serial.println("连接成功!");
client.setServer(mqttBroke,port);
client.setCallback(callback);
while (!client.connected()) {
    String client_id = "esp32-client-";
    client_id += String(WiFi.macAddress());
    Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
    if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("Public emqx mqtt broker connected");
    } else {
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000);
    }
}
client.subscribe(topic);
client.publish(topic,"esp32 conected to mqtt!");
pinMode(12,OUTPUT);
    // pwmTimerConfig->speed_mode = LEDC_LOW_SPEED_MODE;
    // pwmTimerConfig->timer_num = LEDC_TIMER_0;
    // pwmTimerConfig->freq_hz = FREQ;
    // pwmTimerConfig->clk_cfg = LEDC_USE_APB_CLK;
    // pwmTimerConfig->duty_resolution = LEDC_TIMER_8_BIT;
    // ledc_timer_config(pwmTimerConfig);
    // pwmChannelConfig->gpio_num = 12;
    // pwmChannelConfig->speed_mode = LEDC_LOW_SPEED_MODE;
    // pwmChannelConfig->channel = LEDC_CHANNEL_0;
    // pwmChannelConfig->intr_type = LEDC_INTR_DISABLE;
    // pwmChannelConfig->timer_sel = LEDC_TIMER_0;
    // pwmChannelConfig->duty = 0;
    // pwmChannelConfig->hpoint = 0;
    // ledc_channel_config(pwmChannelConfig);

}

void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message:");
    for (int i = 0; i < length; i++) {
      Serial.print((char) payload);
    }
    Serial.println();
    Serial.println("-----------------------");
    switch(payload[0])
    {
      case '0':
      digitalWrite(12,0);
      break;
      case '1':
      digitalWrite(12,1);
      break;
      default:
      Serial.println("???");
      break;
    }   
}

void reconnect() {
while (!client.connected()) {
    Serial.println("Reconnecting to MQTT broker...");
    String client_id = "esp8266-client-";
    client_id += String(WiFi.macAddress());
    if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("Reconnected to MQTT broker.");
      client.subscribe(topic);
    } else {
      Serial.print("Failed to reconnect to MQTT broker, rc=");
      Serial.print(client.state());
      Serial.println("Retrying in 5 seconds.");
      delay(5000);
    }
}
}

void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
    reconnect();
}
client.loop();

// for (uint8_t i = 0; i < pow(RESOLUTION,2)-1; i++)
// {
//   ledc_set_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0,i);
//   ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
//   delay(1000);
// }

// for (uint8_t i = pow(RESOLUTION,2)-1; i > 0 ; i--)
// {
//   ledc_set_duty(LEDC_LOW_SPEED_MODE,LEDC_CHANNEL_0,i);
//   ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);
//   delay(1000);
// }

}





DFEDU 发表于 2023-12-7 17:06:13

单独测试串口打印是否可以?

单片机之神 发表于 2023-12-7 19:20:40

DFEDU 发表于 2023-12-7 17:06
单独测试串口打印是否可以?

还是不行
页: [1]
查看完整版本: ESP32C3在arduino串口监视中无法打印数据