Latest Update
The code worked miraculously for some reason eventhough ESPdebug still outputs alien language! Apparently, this happened after I have executed this command: AT+UART_DEF=9600,8,1,0,0 in the serial monitor and change the baud rate of ESP to 9600 in the sketch.
Next step is to try the 4th solution in the link: ESP8266 wiring schemas - Yet Another Arduino Blog
I hope this topic is helpful to the Arduino newcomers who wants to try out ESP8266!
--
Hi Arduino peeps!
First off, I would like to apologize if this has been addressed a couple of times. I have tried most of the solutions given on the internet but none of them worked for me.
Situation
I am currently exploring on IoT platforms and came across an introductory project written on Thingsboard blog. Out of curiosity, I decided to try on this project: Temperature upload over MQTT using Arduino UNO, ESP8266 and DHT22 sensor | ThingsBoard Community Edition
Code
I copied the code exactly from the Thingsboard link above and made only four changes to it. They are:
- WiFi AP
- WiFi Password
- Device Access token
- Server
#include "DHT.h"
#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <ThingsBoard.h>
#define WIFI_AP "YOUR_WIFI_AP" //This is where I input my WIFI name
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD" //This is where I input my WIFI Pw
#define TOKEN "YOUR_ACCESS_TOKEN" //This is where I input the access token generated from demo.thingsboard.io
// DHT
#define DHTPIN 4
#define DHTTYPE DHT11 //I changed this from DHT22 to DHT11 since I am using DHT11
char thingsboardServer[] = "demo.thingsboard.io"; //I changed this to demo.thingsboard.io since I need to transmit temp and humidity data to the server
// Initialize the Ethernet client object
WiFiEspClient espClient;
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
ThingsBoard tb(espClient);
SoftwareSerial soft(2, 3); // RX, TX
int status = WL_IDLE_STATUS;
unsigned long lastSend;
void setup() {
// initialize serial for debugging
Serial.begin(9600);
dht.begin();
InitWiFi();
lastSend = 0;
}
void loop() {
status = WiFi.status();
if ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
delay(500);
}
Serial.println("Connected to AP");
}
if ( !tb.connected() ) {
reconnect();
}
if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
getAndSendTemperatureAndHumidityData();
lastSend = millis();
}
tb.loop();
}
void getAndSendTemperatureAndHumidityData()
{
Serial.println("Collecting temperature data.");
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Sending data to ThingsBoard:");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C ");
tb.sendTelemetryFloat("temperature", temperature);
tb.sendTelemetryFloat("humidity", humidity);
}
void InitWiFi()
{
// initialize serial for ESP module
soft.begin(9600);
// initialize ESP module
WiFi.init(&soft);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(WIFI_AP);
// Connect to WPA/WPA2 network
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
delay(500);
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
while (!tb.connected()) {
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( tb.connect(thingsboardServer, TOKEN) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED]" );
Serial.println( " : retrying in 5 seconds" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
Problem
Currently, my DHT11 and Arduino UNO worked flawlessly (I tried it separately using a simple DHT11 code). However, ESP8266 failed to connect and keeps generating the following results on the serial monitor:
[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present
To troubleshoot the ESP8266 on an isolated manner, I opened up the default ESPdebug sketch and run the code.
Code
// EspDebug - Test sketch for ESP8266 module
#include "Arduino.h"
// Emulate Serial1 on pins 7/6 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2, 3); // RX, TX
#endif
void setup()
{
Serial.begin(1200); // serial port used for debugging
Serial1.begin(115200); // your ESP's baud rate might be different
}
void loop()
{
if(Serial1.available()) // check if the ESP is sending a message
{
while(Serial1.available())
{
int c = Serial1.read(); // read the next character
Serial.write((char)c); // writes data to the serial monitor
}
}
if(Serial.available())
{
// wait to let all the input command in the serial buffer
delay(10);
// read the input command in a string
String cmd = "";
while(Serial.available())
{
cmd += (char)Serial.read();
}
// print the command and send it to the ESP
Serial.println();
Serial.print(">>>> ");
Serial.println(cmd);
// send the read character to the ESP
Serial1.print(cmd);
}
}
What I've Tried
- Playing with different baud rates:
According to some forums, setting serial monitor baud rate to 1200 delivers the best result. I tried and it's by far displaying the most "readable" results. There were random symbols and I noticed that the version number was inconsistent (sometimes it displayed 1.1.0.0; other times 0.1.0.0)
As for the ESP's baudrate, I tried inputting the two following commands:
AT+UART=9600,8,1,0,0 (This did not work, so I reversed the process by inserting AT+UART=115200,8,1,0,0)
AT+UART_DEF=9600,8,1,0,0 (This felt irreversible. Once I entered this command, my serial monitor started behaving abnormally, generating more random symbols and kept generating inversed question marks. I tried to undo it by inserting a AT+UART_DEF=115200,8,1,0,0 but the system did not seem to acknowledge the input)
-
Plugging in a 9V battery on top of the 3.3v from computer's USB port: Some said the 3.3v supplied by Arduino has a low current output and hence requiring additional external power source. Didn't work for my case.
-
Interchanged Rx and Tx: Didn't work as well.








