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);
}
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);
}
/*
* 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);
}
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.
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.
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):
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.