I have an Arduino Uno connected to a NodeMCU ESP01 module, communicating via serial pins. The NodeMCU is powered from the Arduino's 3.3V pin. My idea is that the ESP01 receives MQTT messages, sends them to the Arduino via serial and wait for the Arduino to reply and then publish MQTT messages back.
If I power the Arduino from my computer's USB, everything works fine.
If I use an external power supply 12V/1A, the ESP01 can publish and receive "standard" MQTT messages (meaning, it does not send messages to the Arduino and does not expect a reply). It just receives and directly replies (no Arduino involved).
The problem occurs if the ESP needs to query the Arduino for a result.
The Arduino is able to read and write (I see the correct text in the console) to the serial port, but the NodeMCU ESP01 has problems publishing MQTT messages. Sometimes it does not publish anything (the ESP probably is unable to read what Arduino sends over serial), sometimes it publishes destroyed characters (random text).
It looks like the current needed by the ESP module is not high enough, but it does not make sense for me, since the external power supply should be able to provide more current than USB. Any ideas?
This is my schematic:
Code:
In my ESP01 I have the following MQTT onMessage
listener, If the topic is a query for the Arduino (topic is validated by the handleQuery
function), then the topic is sent by UART to the Arduino, which listens for specific strings in the loop
function. When the Arduino receives a string, it does some logic and writes a reply back to the ESP via UART (Serial.print()
).
After the ESP sends a message to the Arduino, it sets listeners for strings, that end with character %. If the reply from the Arduino is received, then it publishes an MQTT message with the result.
function onMessage(client, topic, message)
if handleQuery(topic.."+"..message) then
uart.write(0,topic.."+"..message)
uart.on("data","%", function(data)
if data=="quit%" then
uart.on("data") -- unregister event
end
if data ~= "quit%" then
client:publish(topic.."/response","REPLY:"..data,0,0)
end
end,0)
else
client:publish(topic.."/response","REPLY: Invalid query",0,0)
end
end
The reply from the Arduino has the following message format:
REPLY:<value>%
quit%
This solution works if the Arduino is connected to my desktop computer's USB. It does not work if it is powered from an external power supply. It seems like the ESP is not able to read what the Arduino wrote to UART with Serial.println()
if powered from an external power source.
I have no idea why, because power supply should be able to deliver more current.
Edit:
I use the following Arduino clone: eBay link
I tried to connect the ESP01 to the Arduino's 5V output with a voltage regulator (5V should deliver more current), and again, if the USB is connected, the ESP reads UART messages with no problem. If the Arduino is powered from an external power source, the ESP reads damaged characters. The only difference is that those damaged characters seems to be always the same. So perhaps some coding problems(?) or something like that.
So far it looks like a low current value is not a problem.
Edit 2
I removed from my code print()
statement from the ESP/LUA code that I used for "debbuging". It printed to serial some messages for me. It seems that it cleaned traffic on Serial line a little bit.
Now I get most of the time correct characters, but the first character is missing, so instead of:
LOW% I get OW%
HIGH% I get IGH%
29.53% I get 9.53%
And from time to time I get String =]�quit%.
Perhaps some input buffer has a problem(?).