Update: Point 1- I don't want to program ESP, I just want to use it as a wifi shiled. cause I'm not familiar with flashing or writing sketch for ESP, so still feel comfortable with arduino. beside my existing projects need to upgrade to wifi version. Not possible to turn everying into esp instead of arduino. Point 2- If anyone can provide me a complete working sketch, where arduino sending a value to ESP, ESP receiving it and then sending to some data server (even sparkfun type will do).

I was working on Arduino based project for last couple of months, where I had to read several sesor data (e.g. temp, humidity, light, sound, motion etc) and save these data to a remote mysql server. I was doing well with Arduino Uno and Ethernet shield, where I connect through RJ45 cable to make my Arduino UNO able using internet. here is the code for that:

#include <SPI.h>
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(1.1.10.);  // IP of the MySQL *server* here
char user[] = "root";              // MySQL user login username
char password[] = "007";        // MySQL user login password

// Sample query
char INSERT_DATA[] = "INSERT INTO database (message, sensor_num, value) VALUES ('%s',%d,%s)";
char query[128];
//char temperature[10];

int potPin = 4;
float temperature = 0;
long val=0;

EthernetClient client;
MySQL_Connection conn((Client *)&client);

void setup() {
  while (!Serial); // wait for serial port to connect
  if (conn.connect(server_addr, 3306, user, password)) {
    // Initiate the query class instance
    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
    // Save
    //dtostrf(50.125, 1, 1, temperature);
    sprintf(query, INSERT_DATA, "test sensor", 24, temperature);
    // Execute the query
    // Note: since there are no results, we do not need to read any data
    // Deleting the cursor also frees up memory used
    delete cur_mem;
    Serial.println("Data recorded.");
    Serial.println("Connection failed.");

void loop() {
  val = analogRead(potPin);
    temperature = (val*0.0048828125*1000);

Later, I found it's not convenient to use a whole bunch of cable around, so I was thinking to make it wireless. At beginning I was thinking of RF24OL but it also can't connect internet directly. Then I found ESP8266 module which can directly connect to wifi. Now, I bought some ESP8266 12e (not ESP-01), which got one USB port to connect with computer, which made things way more sipmle comparing previous versions of ESP. I also installed ESP supporting files on my Arduino IDE, tested and it's working fine. But, the problem is, whenever I upload the code only for ESP(like connectiong to wifi), it compile and upload proerply.

Like this code works:

void setup() {


void loop() {
  digitalWrite(BUILTIN_LED, LOW);



this also works fine,

 *  This sketch trys to Connect to the best AP based on a given list

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti wifiMulti;

void setup() {

    wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
    wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
    wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

    Serial.println("Connecting Wifi...");
    if(wifiMulti.run() == WL_CONNECTED) {
        Serial.println("WiFi connected");
        Serial.println("IP address: ");

void loop() {
    if(wifiMulti.run() != WL_CONNECTED) {
        Serial.println("WiFi not connected!");

That means, my Arduino IDE, ESP8266 12e all got no problems. But whenever I add something more, like DHT11 library, it shows error compiling.

this code doesnt compile:

#define SSID       "Itead_1(Public)"                //type your own SSID name
    #define PASSWORD   "27955416"                                //type your own WIFI password

#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

extern int chlID;        //client id(0-4)

// for yeelink api
#define APIKEY         "3a362e99d6f1daf974561163a8c99a85" // replace your yeelink api key here

//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0       15483 // replace your device ID
#define SENSORID0       26660 // replace your sensor ID

//replace the device ID and sensor ID for humidity sensor.
#define DEVICEID1       15483 // replace your device ID
#define SENSORID1       26661 // replace your sensor ID

char server[] = "api.yeelink.net";   // name address for yeelink API

unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 5*1000; // delay between 2 datapoints, 5s
String returnValue = ""; 
boolean ResponseBegin = false;

int DHT11PIN=25;                        //Connect D25 to data pin of DHT11

int humidity;
int temperature;

int post_number;

void setup()

  bool b = wifi.Initialize(STA, SSID, PASSWORD);
    DebugSerial.println("Init error");
  delay(8000);  //make sure the module can have enough time to get an IP address 
  String ipstring  = wifi.showIP();
  DebugSerial.println(ipstring);                //show the ip address of module

void loop()
  char message[400];
   // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if((millis() - lastConnectionTime > postingInterval)) {

  //read dht11
  int chk = dht11_read(DHT11PIN);
                post_number = 0;



  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:



// this method makes a HTTP connection to the server:
void sendData(int device_id,int sensor_id,int thisData) {
  // if there's a successful connection:
  if (wifi.ipConfig(TCP,server, 80)) {
    // send the HTTP PUT request:
    String cmd;
        cmd = "POST /v1.0/device/";
        cmd += String(device_id);
        cmd += "/sensor/";
        cmd += String(sensor_id);
        cmd += "/datapoints";
        cmd += " HTTP/1.1\r\n";
        cmd += "Host: api.yeelink.net\r\n";
        cmd += "Accept: *";
        cmd += "/";
        cmd += "*\r\n";
        cmd += "U-ApiKey: ";
        cmd += APIKEY;
        cmd += "\r\n";
        cmd += "Content-Length: ";
        int thisLength = 10 + getLength(thisData);
    cmd += String(thisLength);
        cmd += "\r\n";
        cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
        cmd += "Connection: close\r\n";
        cmd += "\r\n";
        cmd += "{"value":";
        cmd += String(thisData);
        cmd += "}\r\n";


    // note the time that the connection was made:
    lastConnectionTime = millis();
  else {
    // if you couldn't make a connection:
    DebugSerial.println("connection failed");

int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
  // return the number of digits:
  return digits;

int dht11_read(int pin)
        int bits[5];
        int cnt = 7;
        int idx = 0;

        // EMPTY BUFFER
        for (int i=0; i< 5; i++)
        {bits[i]= 0;}[/i]

[i]        // REQUEST SAMPLE[/i]
[i]        pinMode(pin, OUTPUT);[/i]
[i]        digitalWrite(pin, LOW);[/i]
[i]        delay(18);[/i]
[i]        digitalWrite(pin, HIGH);[/i]
[i]        delayMicroseconds(40);[/i]
[i]        pinMode(pin, INPUT);[/i]

[i]        // ACKNOWLEDGE or TIMEOUT[/i]
[i]        unsigned int loopCnt = 10000;[/i]
[i]        while(digitalRead(pin) == LOW)[/i]
[i]                if (loopCnt-- == 0) return -2;[/i]

[i]        loopCnt = 10000;[/i]
[i]        while(digitalRead(pin) == HIGH)[/i]
[i]                if (loopCnt-- == 0) return -2;[/i]

[i]        // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT[/i]
[i]        for (int i=0; i<40; i++)[/i]
[i]        {[/i]
[i]                loopCnt = 10000;[/i]
[i]                while(digitalRead(pin) == LOW)[/i]
[i]                        if (loopCnt-- == 0) return -2;[/i]

[i]                unsigned long t = micros();[/i]

[i]                loopCnt = 10000;[/i]
[i]                while(digitalRead(pin) == HIGH)[/i]
[i]                        if (loopCnt-- == 0) return -2;[/i]

[i]                if ((micros() - t) > 40) bits[idx] |= (1 << cnt);[/i]
[i]                if (cnt == 0)   // next byte?[/i]
[i]                {[/i]
[i]                        cnt = 7;    // restart at MSB[/i]
[i]                        idx++;      // next byte![/i]
[i]                }[/i]
[i]                else cnt--;[/i]
[i]        }[/i]

[i]        // WRITE TO RIGHT VARS[/i]
[i]        // as bits[1] and bits[3] are allways zero they are omitted in formulas.[/i]
[i]        humidity    = bits[0]; [/i]
[i]        temperature = bits[2]; [/i]

[i]        int sum = bits[0] + bits[2];  [/i]

[i]        if (bits[4] != sum) return -1;[/i]
[i]        return 0;[/i]

So, I decided to use ESP and arduino nano together. so that, ESP will work as wifi shiled to connect to internet, like ethernet shield was doing before, arduino will handle parts of sensors data reading and so on. I tried to connect to internet through connecting RX-TX and TX-RX between Arduino-ESP, but it doesn't work. I uploaded wifi connecting script to ESP and it connects to wifi properly, then i uploaded sensor data reading and saving to mysql script to arduino, then conneted arduino tx & rx to esp rx & tx. but arduino can't connect to internet. Now, my problem is, how I can use ESP8266 12e NodeMCU only as ethernet shield so that my arduino can connect to internet and upload data to my sql server? My ESP will only act as a wifi shield, i don't want to configure esp to read sensor data, cuase it got lack of GPIO pins, most importantly it doesn't work as smoothly as arduino does. So please don't suggest me to use only esp with sensors, because i also wanted to upgrade my existing projects to wifi which done with ethernet shields. Thanks in advance

  • If you are trying to send data to the server then I think that connecting tx-tx and rx-rx should maybe work. Or else you could write a code that reads Arduino's messages and sends it to the server. Commented Jun 12, 2017 at 8:50
  • I tried with both way, doesn't work. And don't know how to write code to esp that will read from arduino, or other way arond. do you have any working code sample? @ Sagnik Pradhan Commented Jun 12, 2017 at 8:54
  • I don't really have. Commented Jun 12, 2017 at 9:03
  • 1
    I know exactly what the ESP8266 is. If you look through the bundled examples in the ESP8266 core you'll find at least one written by me. To use the ESP8266 with an Arduino board you have to have firmware installed in the ESP8266 that communicates with the Arduino. Normally the ESP8266 comes preinstalled with this firmware, called the "AI Thinker AT Firmware". Or you can write your own firmware to communicate between the two boards. Installing some random firmware that scans WiFi access points and connects to one will not allow communication with the Arduino because you haven't...
    – Majenko
    Commented Jun 12, 2017 at 14:56
  • 1
    ...written ESP8266 firmware that will communicate with the Arduino.
    – Majenko
    Commented Jun 12, 2017 at 14:56

1 Answer 1


OK you have made it very clear that, you want to use the ESP as a serial to WIFI bridge. You have a Node-MCU ESP12 board, which is normally configured for programming using Luna.

  1. You need to have the correct firmware for this "AI Thinker AT Firmware" and you need to find the correct tool chain for loading this on your board. It would be easier to but an ESP7 (or lower) to be honest that already comes with the correct firmware.

  2. You want to communicate with the ESP from the Nano. To do this you will need to use Software Serial on the Nano and cross connect the software serial port to the ESPs serial port. You must not connect to the nano's serial port, you need this to program the nano.

  3. To configure the ESP to talk to a wireless network you will need to send it AT commands (from the nano) these will need to be part of the setup of you program. Technically this is programming the ESP, but I'm guessing you'll be OK with this.

  4. Now you can add in your DHT-11 sensor and library and it will work.


You would find it so much easier to download the board support package for the ESP, take one of the Web server/client examples and add the DHT-11 code to that. You'll find, since it is all written in the same dialect of C++ as the Arduino uses, very easy. In fact the biggest problem you will have is finding something to weight the USB cable down to stop it from rolling the Node-MCU over.

BTW your code doesn't compile because you have left the italic mark-up tags in when you copied it off the website. Try removing all the [i] and [/i] tags.

  • Gorila Thx a lot, I was exactly looking 4 somethng as specific as u mentioned. Let me search each steps as well try&see wht happened. Feels ur answer is more informative and very specific. beside everything let me answer few points based on ur steps: 1) I think the board got pre-installed firmware, and I used arduino IDE to program it instead of Luna. Tried ESploere to upload some .lua files, but for some unknown(to me) reasons failed. 2) Made sense. 3) my ESP working fine for conn. wifi or even the first code to send data to mysql server, just was not working when added dht11 library. Commented Jun 13, 2017 at 18:37
  • I was looking for sample and got two other threads whre which aimed to achieve similar goal: (1) arduino.stackexchange.com/questions/17787/… (2) arduino.stackexchange.com/questions/35908/… Commented Jun 13, 2017 at 19:52
  • UPDATE: If someone looking for easier solution: I got something better, where seems I don't need to use AT command everytime after flashing once: instructables.com/id/… Commented Jun 15, 2017 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.