# Cannot adapt Arduino code to ESP8266

**URL:** <https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013>\
**Category:** Programming\
**Created:** [April 28, 2024, 2:17am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013 "2024-04-28T02:17:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nu2java](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nu2java/32/977241_2.png) [@nu2java](https://forum.arduino.cc/u/nu2java)\
**Post date:** [April 28, 2024, 2:17am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/1 "2024-04-28T02:17:17Z")

</div>

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.

```arduino
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);
}

```

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [April 28, 2024, 3:11am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/2 "2024-04-28T03:11:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flashko](https://avatars.discourse-cdn.com/v4/letter/f/f04885/32.png) [@flashko](https://forum.arduino.cc/u/flashko)\
**Post date:** [April 28, 2024, 5:28am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/3 "2024-04-28T05:28:49Z")

</div>

> [@nu2java](#):
>
> 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.

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.

```arduino
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);
}

```

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [April 28, 2024, 5:47am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/4 "2024-04-28T05:47:50Z")

</div>

Try this - see if the serial output is correct

```arduino
/*
 * 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);
}

```

---

<div class="post-metadata">

**Author:** ![nu2java](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nu2java/32/977241_2.png) [@nu2java](https://forum.arduino.cc/u/nu2java)\
**Post date:** [April 28, 2024, 5:28pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/5 "2024-04-28T17:28:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flashko](https://avatars.discourse-cdn.com/v4/letter/f/f04885/32.png) [@flashko](https://forum.arduino.cc/u/flashko)\
**Post date:** [April 28, 2024, 6:15pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/6 "2024-04-28T18:15:48Z")

</div>

> [@nu2java](#):
>
> `10:25:51.656 -> HP�y4v�|��`\<l���^ 4�ihr�`

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?

---

<div class="post-metadata">

**Author:** ![nu2java](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nu2java/32/977241_2.png) [@nu2java](https://forum.arduino.cc/u/nu2java)\
**Post date:** [April 28, 2024, 6:37pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/7 "2024-04-28T18:37:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bojan\_jurca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bojan_jurca/32/1199504_2.png) [@bojan\_jurca](https://forum.arduino.cc/u/bojan_jurca)\
**Post date:** [April 29, 2024, 11:45am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/8 "2024-04-29T11:45:18Z")

</div>

> [@nu2java](#):
>
> NodeMCU or D1 Mini

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.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [May 3, 2024, 4:23pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/9 "2024-05-03T16:23:34Z")

</div>

> [@nu2java](#):
>
> 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 code doesnt use the pins - just the serial connection, and you cleaerly have not set the serial monitor correctly to communicate

> [@johnerrington](#):
>
> `Serial.begin(9600);`

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

---

<div class="post-metadata">

**Author:** ![nu2java](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nu2java/32/977241_2.png) [@nu2java](https://forum.arduino.cc/u/nu2java)\
**Post date:** [May 4, 2024, 1:25am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/10 "2024-05-04T01:25:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [May 4, 2024, 5:35am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/11 "2024-05-04T05:35:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![indev2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/indev2/32/1126779_2.png) [@indev2](https://forum.arduino.cc/u/indev2)\
**Post date:** [May 4, 2024, 6:01am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/12 "2024-05-04T06:01:16Z")

</div>

@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

> **[How to update the core of your Arduino Board](https://support.arduino.cc/hc/en-us/articles/4404691106066-How-to-update-the-core-of-your-Arduino-Board)**
>
> The Arduino IDE installation package comes with some cores by default. Periodically, updated versions of cores are released, with new features and bug fixes. Keeping your core updated will improve ...

---

<div class="post-metadata">

**Author:** ![ptillisch](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@ptillisch](https://forum.arduino.cc/u/ptillisch)\
**Post date:** [May 6, 2024, 7:09am UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/13 "2024-05-06T07:09:26Z")

</div>

Hi @nu2java.

> [@nu2java](#):
>
> I tried but get this as an output:
> 
> `10:25:51.656 -> HP�y4v�|��`\<l���^ 4�ihr�`

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](https://en.wikipedia.org/wiki/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](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/4), but apparently ignored by you):

```cpp
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.

---

<div class="post-metadata">

**Author:** ![nu2java](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nu2java/32/977241_2.png) [@nu2java](https://forum.arduino.cc/u/nu2java)\
**Post date:** [May 7, 2024, 1:50pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/14 "2024-05-07T13:50:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 3, 2024, 1:50pm UTC](https://forum.arduino.cc/t/cannot-adapt-arduino-code-to-esp8266/1253013/15 "2024-11-03T13:50:45Z")

</div>

Closed on November 3, 2024, 1:50pm UTC

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