Cannot adapt Arduino code to ESP8266

Hi All - I am using this code on my Arduino, but I am no expert in this at all. I am trying to adapt this code to an ESP8266 board (NodeMCU or D1 Mini). No matter what I change, the code will not work properly on either of the board types. What I mean by not working properly is, the serial output is all jumbled up. This is just reading the output signal of a meter. I plug back into my Arduino UNO and everything is wonderful. This being an Arduino help forum, I am hoping that someone can tell me what is unique about this code that it will not output properly using another board. Thanks for any help someone can provide.

int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3

int i = 0;
int j = 0;
int k = 0;
int signCh = 8;
int sign = 0;
int decimal;
float dpp;
int units;
byte mydata[14];

String value_str;
long value_int; //was an int, could not measure over 32mm

float value;

void setup() {

Serial.begin(9600);

pinMode(req, OUTPUT);
pinMode(clk, INPUT_PULLUP);
pinMode(dat, INPUT_PULLUP);
digitalWrite(req,LOW); // set request at high
}

void loop() {
digitalWrite(req, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk) == LOW) {
} // hold until clock is high

while( digitalRead(clk) == HIGH) {

} // hold until clock is low
bitWrite(k, j, (digitalRead(dat) & 0x1));
}
mydata[i] = k;
}

sign = mydata[4];
value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
decimal = mydata[11];
units = mydata[12];
value_int = value_str.toInt();

if (decimal == 0) dpp = 1.0;
if (decimal == 1) dpp = 10.0;
if (decimal == 2) dpp = 100.0;
if (decimal == 3) dpp = 1000.0;
if (decimal == 4) dpp = 10000.0;
if (decimal == 5) dpp = 100000.0;

value = value_int / dpp;
if (sign == 0) {
Serial.println(value,decimal);
}
if (sign == 8) {
Serial.print("-"); Serial.println(value,decimal);
}
digitalWrite(req,LOW);
delay(1000);
}

My first step is always.... Load the "Blink without Delay" from File\examples\digital

Verify the hardware, connection and board manager choice are all good.

If only you had copied the content from the serial monitor instead of idle talk. As a shot in the dark I would guess that because of the wait your microcontroller is periodically rebooting. If I'm right just add yield() like this.

int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3

int i = 0;
int j = 0;
int k = 0;
int signCh = 8;
int sign = 0;
int decimal;
float dpp;
int units;
byte mydata[14];

String value_str;
long value_int; //was an int, could not measure over 32mm

float value;

void setup() {

Serial.begin(9600);

pinMode(req, OUTPUT);
pinMode(clk, INPUT_PULLUP);
pinMode(dat, INPUT_PULLUP);
digitalWrite(req,LOW); // set request at high
}

void loop() {
digitalWrite(req, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk) == LOW) {
  yield ();
} // hold until clock is high

while( digitalRead(clk) == HIGH) {
  yield ();

} // hold until clock is low
bitWrite(k, j, (digitalRead(dat) & 0x1));
}
mydata[i] = k;
}

sign = mydata[4];
value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
decimal = mydata[11];
units = mydata[12];
value_int = value_str.toInt();

if (decimal == 0) dpp = 1.0;
if (decimal == 1) dpp = 10.0;
if (decimal == 2) dpp = 100.0;
if (decimal == 3) dpp = 1000.0;
if (decimal == 4) dpp = 10000.0;
if (decimal == 5) dpp = 100000.0;

value = value_int / dpp;
if (sign == 0) {
Serial.println(value,decimal);
}
if (sign == 8) {
Serial.print("-"); Serial.println(value,decimal);
}
digitalWrite(req,LOW);
delay(1000);
}

Try this - see if the serial output is correct

/*
 * This ESP8266 NodeMCU code was developed by newbiely.com
 *
 * This ESP8266 NodeMCU code is made available for public use without any restriction
 *
 * For comprehensive instructions and wiring diagrams, please visit:
 * https://newbiely.com/tutorials/esp8266/esp8266-serial-monitor
 */

void setup() {
  Serial.begin(9600);  //why does NOBODY use a modern baud rate?
}

void loop() {
  Serial.println("ArduinoGetStarted.com");
  delay(1000);
}
1 Like

Thanks for this.... I tried but get this as an output:

10:25:51.656 -> HP�y4v�|��<l���^ 4�ihr�`

This makes me think that the pins on the Arduino do something different from those on the D1 Mini or NodeMCU.... But I really don't know.

This looks to me like an incorrectly selected serial monitor speed. Have you selected the same speed in the serial monitor as in the program?

1 Like

Yes, I have tried both 9600 & 115200 on both other boards.

NodeMCU or D1 Mini works fine with 115200 bauds. Just make sure that you have the same speed set in the Arduino IDE serial monitor, otherwise it can not communicate correctly with your ESP8266.

1 Like

This code doesnt use the pins - just the serial connection, and you cleaerly have not set the serial monitor correctly to communicate

.. sets the speed at 9600 baud, so you MUST set the serial monitor to work at that rate or you will get garbage.

Thanks for the info @johnerrington I have set the baud rates at 9600 and just finally got to try out the same setup on an Arduino Nano and I get no issues at all. But for reasons I do not understand, I cannot get the same results on the D1 Mini or NodeMCU.

Well, I'm running this exact code on a Lolin ESP8266 module and getting

ArduinoGetStarted.com

on the serial monitor, set to 9600 baud.

Programming: as NodeMCU 1.0

@nu2java

Hi

Have you updated to the latest esp8266 core in recent times?

I think the latest is 3.1.2 this can sometimes resolve all manner of odd behavior.

See here

Hi @nu2java.

The ROM bootloader on the ESP8266 microcontroller prints some information to Serial when the microcontroller starts. This information is printed at 74880 baud. For this reason, it is normal to see some garbage characters printed when you open Serial Monitor while it is set to a different baud rate. The information printed by the bootloader is only important for troubleshooting specific problems so, as long as the board is otherwise behaving as expected, you can simply ignore these garbage characters.

I think the garbage characters are acting as a "red herring" here. My hypothesis is that the reason you didn't see any thing else in Serial Monitor is because the conditions required to execute the Serial.print lines in your program were never satisfied.

In order to test this hypothesis, please upload to the ESP8266 board this simple test sketch (an equivalent of which was already provided by @johnerrington eight days ago, but apparently ignored by you):

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("hello");
  delay(1000);
}

After the upload finishes successfully, open Serial Monitor and set the baud rate menu to "*9600". Then add a reply here on the forum thread to tell us whether or not you saw the text "hello" printed repeatedly in the Serial Monitor.

Thanks all for the great suggestions on solving this issue. I am going to step through them and try out each one. This is really helpful information, so I appreciate your time and effort in assisting me.

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