The Serial.print doesn't work WELL for ESP8266 and

Hi,
the below sketch got few questions:

  1. Serial.print doesn't works well, only got output of the Serial.prints inside loop, nothing output in setup;
  2. 'A' changes by MIT APP, even not sensitive as excepted, 'B' never changed;
  3. the LED2 was signed at 2, I changed it to 5, it still works on 2, why?
    Thanks for help.
    Adam
#include <ThingSpeak.h>               // add librery
#include <ESP8266WiFi.h>

WiFiClient  client;
unsigned long counterChannelNumber = 1750380;                
const char * myCounterReadAPIKey = "LQADPT608NR624AF";     
const int FieldNumber1 = 1;                                 // The field you wish to read
const int FieldNumber2 = 2;                                 // The field you wish to read
const int LED1 = 4;  // GPIO 0
const int LED2 = 5;  // GPIO 2

void setup()
{


  delay(2000);  // 
  Serial.begin(115200);

 //Serial.begin(74880);
  delay(2000);

  Serial.print("File   : "), Serial.println(__FILE__);
  const char compile_date[] = __DATE__ " " __TIME__;
  Serial.print("Compile timestamp: ");
  Serial.println(compile_date);

  Serial.println();

  WiFi.begin("XXXX", "YYYYYY");                

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  ThingSpeak.begin(client);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

}

void loop()
{
  int A = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
  Serial.print("A = ");
  Serial.println(A);
  digitalWrite(LED1, A);
  int B = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
  Serial.print("B = ");
  Serial.println(B);
  digitalWrite(LED2, B);

}

I'm not sure this line is valid. Try with this commented out.
And your code doesn't show where FILE is from.

Don't have any help for your LEDs except write a simple sketch that only controls the LEDs and see if you can fine where things are going wrong. My guess would be LED pin identification but can't say why your code isn't correct.

1 Like

__FILE__ is a built-in predeifned macro generated at compile time. I do not know if this applies to the ESP8266, it certainly applies to avr-gcc the Arduino AVR core.

//Edit
It does not seem to be avr-gcc where it comes from.

1 Like

It is a standard cpp pre-defined macro:

--- bill

2 Likes

Serial.print does surely work in setup()
This is my bare-minimum Testcode for ESP8266


int myCounter = 0;
 
void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer = 0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 2;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
}


void loop() {
  BlinkHeartBeatLED(OnBoard_LED,100);

  if ( TimePeriodIsOver(MyTestTimer,1000) ) {
    Serial.print("Hello World ");
    myCounter++;
    Serial.println(myCounter);
  }  
}

I guess

if you replace the comma with a semicolon it will work
Me personal I don't like it when two function-calls are in one line

one possible reason is:
uploading the new code did not happen
best regards Stefan

1 Like

Hi everybody,

for the below sketch I have a few questions:

The function Serial.print doesn't works well, I got only output from the Serial.prints inside loop, I don't see any characters from the Serial.prints in function setup

what?

the constant LED2 was assigned the value 2
I changed the value to 5

I still have the LED physically connected to IO-pin 2 and the LED is still switching on/off why?

alternatively
I changed the code from

const int LED2 = 2;

to

const int LED2 = 5;
1 Like

Thank you StefanL38 for all your hard work on this.
you’re amazing and I learned a lot.

Thanks I learned something. i.e. the FILE macro.

My original thought was the "," separating the two "lines" of code. However I was confused why if the syntax was incorrect the compiler did not flag it. So I learned that as well.

:slight_smile:

1 Like

For what its worth, I'm at best a fair programmer. However learned to divide and conquer.

If I was concerned the Serial.print was not working I would immediately run this:

void setup(){
   Serial.begin(115200);
   delay(1000);

   Serial.println(" anything you wish")'
}
void loop() {}

Now I would know if it was the processor type or my code is at fault.

I also use the "blink without delay" example to verify the board is still working (or not) when I get into a situation where I think the board may be at fault.

1 Like

Thank you JohnRob, that print file and date pattern I learned from here originally by jremington I believe.

Thanks for the answers of the first question.
How about the others please?
2. 'A' changes by MIT APP, even not sensitive as excepted, 'B' never changed;
3. the LED2 was signed at 2, I changed it to 5, it still works on 2, why?

For the LEDs I would use the "blink without delay" and test the function of one LED at a time by changing the sketch pinout to what you are using. What you describe is hard to grasp knowing how the processor works, so there must be more to it. I think the above will show the issue and get you going.

You will find the above is my approach to how I solve problems with my own code. It is the best way I know of to sort out issues.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.