2 years, 2 months ago.

Currently unable to display any of printf statements for code

I am trying to display serial messages for wifi code but instead getting very odd symbols as shown on coolterm, I have tested for windows and mac for this. I have attached code as welll


  1. include "mbed.h"
  2. include <stdio.h>
  3. include <string.h>

Serial serial(USBTX, USBRX); Serial esp(P4_28, P4_29); tx, rx DigitalOut reset(P0_4); DigitalOut blueLed(P2_3); DigitalOut greenLed(P2_4); DigitalOut redLed(P2_5); DigitalOut vibr(P2_12);

Timer t;

int count,ended,timeout;

int sample_ctemp; int sample_humid; int toggle = 0;

char buf[4096]; char snd[255]; char rcv[255]; char sendStr[11];

char ssid[32] = "*"; enter WiFi router ssid inside the quotes char pwd [32] = "**"; enter WiFi router password inside the quotes

void SendCMD(); void getReply();

int main() { Make sure all feedback mechanisms are turned off blueLed=0; greenLed=0; redLed=0; vibr=0;

set buad rates for comms esp.baud(115200); serial.baud(115200);

Setup a serial interrupt function to capture received data esp.attach(&getReply, Serial::RxIrq);

Device doesn't appear to need a reset upon waking up so the device is not reset (i.e. reset = 1); reset=1;

Give the user some to setup the virtual terminal wait(10);

serial.printf("\n-- Clear Buffer --\r\n"); Simple way to clear the buffer strcpy(snd, "AT+GMR\r\n"); View version info timeout=1; SendCMD(); getReply(); serial.printf(buf);

/* Make sure interrupt has a few seconds to clear the RX buffer wait(0.5);

Print the buffer pc.printf(buf);

  • /

/******************** The "AT+CWMODE_DEF" command sets the mode of the radio. 1 = "Station" mode (a.k.a. client,) 2 = "AP" mode, 3 = "Soft AP" mode. Software AP mode so that the device is both a Station and AP, each with its own MAC and IP address. Mode 1 is the default. *********************/

/* serial.printf("\n-- Set Radio Mode --\r\n"); strcpy(snd, "AT+CWMODE_DEF=1\r\n"); default wifi mode station = 1 timeout=2; SendCMD(); getReply(); serial.printf(buf); Prints out response, should be "OK" */

/********************* Connect to the AP listed above with the SSID and password in the string definitions for "ssid[]" and "pwd[]" Note that the "_DEF" appended to the CWJAP command means that these credentials are stored on the radio so that when power is lost, the device will reconnect to the network as soon as power is restored. ********************/

/*serial.printf("\n-- Connecting to AP --\r\n"); serial.printf("ssid = %s pwd = %s\r\n",ssid,pwd); strcpy(snd, "AT+CWJAP_DEF=\""); connect to AP, set as default strcat(snd, ssid); strcat(snd, "\",\""); strcat(snd, pwd); strcat(snd, "\"\r\n"); timeout=20; SendCMD(); getReply();*/

/* pc.printf(buf); */

Check to see that the connection has been established /*pc.printf("\n-- Connection Information --\r\n"); strcpy(snd, "AT+CIFSR\r\n"); get local IP address timeout=5; SendCMD(); getReply(); pc.printf(buf); */

Just to let the user know that the config process has ended geenLed = 1; wait(1); genLed = 0;

set CIPMUX to 0=Single,1=Multi /*pc.printf("\n-- Setting Connection Mode --\r\n"); strcpy(snd, "AT+CIPMUX=1\r\n"); SendCMD(); timeout=5; getReply(); pc.printf(buf);*/


/*pc.printf("\n-- Create UDP transmission --\r\n"); strcpy(snd, "AT+CIPSTART=4\r\n"); SendCMD(); timeout=5; getReply(); pc.printf(buf);*/

while(1) {

serial.printf("\n-- Send data --\r\n"); strcpy(snd, "AT+CIPSEND=4"); SendCMD(); timeout=5; getReply(); serial.printf(buf);

/*pc.printf("\n-- Receive data --\r\n"); strcpy(rcv, "+IPD,%d\r\n", sizeof(buf)+15); ReceiveCMD(); timeout=5; getReply(); pc.printf(buf);*/

} main loop does nothing }

This function sends the command string to the ESP8266 void SendCMD() { esp.printf("%s", snd); }

This function sends the command string to the ESP8266 /*void ReceiveCMD() { esp.printf("%s", rcv); }*/

This function establishes the space for the received string, starts a timer so that if things get stuck, the app won't hang. void getReply() {

memset(buf, '\0', sizeof(buf)); t.start(); ended=0; count=0; while(!ended) { if(esp.readable()) { buf[count] = esp.getc(); count++; } if( > timeout) { ended = 1; t.stop(); t.reset(); } }

/* This function is for interrupt for gesture sensor */


Are you sure you're using the right baud rate in your terminal monitor?

posted by Jan Jongboom 03 Mar 2017

I have been experimenting with 9600, 14400 and 19200 and got similar results as well

posted by Seb Creighton 03 Mar 2017
Comment on this question

1 Answer

2 years, 2 months ago.

With <<code>> and <</code>> it will be actually readable.

However you say you tried 9600, 14400 and 19200 baudrate. However from your code:


So consider trying 115200 baud.

To post an answer, please log in.