I have a requirement to get RFID data on Arduido UNO and send it to ESP8266 to be sent to ThingSpeak via the Wifi module. However, ESP8266 is also programmed to send Temperature and humidity data to ThingSpeak and turn on a motor when temperature exceeds certain threshold.

I have followed this logic on ESP8266 loop

if(millis() > time_now + period)
    read temperature, humidity from DHT sensor
    Send only temp,humidity to ThingSpeak
  // Outside millis()
  while (Serial.available())
    Serial.readString() - to retrieve the User name sent by Arduino UNO
    Send only temp,humidity and UserId=1 to ThingSpeak

However the whileloop is not executed as expected and is random. Since millis() is unblocked, my understanding is that while it is waiting and if there is data available on Serial, While loop is to be executed. However, the readString() is not executed everytime data is available on Serial. Full code on ESP8266 is below. Any help is to identify the issue is much appreciated.

#include <DHT.h>
#include <DHT_U.h>
#include <Stepper.h>

#define DHTPIN 3  // what pin we're connected to D4
#define DHTTYPE DHT11  // DHT 11  (AM2302)
ULN2003 --- ESP-12F(WeMos D1 mini)
  ========   =======================
  IN1 >> GPIO 14 (D5)
  IN2 >> GPIO 12 (D6)
  IN3 >> GPIO 13 (D7)
  IN4 >> GPIO 15 (D8)
  VCC(+) >> ... 5V Prefer to use external 5V Source
  Gnd(-) >> GND (G)
#define IN1  14 // GPIO 14
#define IN2  12 // GPIO 12
#define IN3  13 // GPIO 13
#define IN4  15 // GPIO 15

#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>

String apiKey = "API_KEY";     //  Enter your Write API key from ThingSpeak
const char *ssid =  "WIFI_SSID";     // replace with your wifi ssid and wpa2 key
const char *pass =  "WIFI_PWD";
const char* server = "api.thingspeak.com";
int uid =1;
//Variables to store temperature and humidity
float hum = 70.2;  //Stores humidity value
float temp = 20.0; //Stores temperature value
//Variable to store User Name
String name =" ";
int period = 1000; // delay period
unsigned long time_now = 0;

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
WiFiClient client;
const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution
// for your motor
Stepper myStepper(stepsPerRevolution, 14, 13, 12, 15); // initialize the stepper library on pins 8 through 11:
SoftwareSerial s(D3,D2);

void setup() {
  // set the speed at 60 rpm:
  // initialize the serial port:
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 

  Serial.println("Connecting to ");
  WiFi.begin(ssid, pass);

      while (WiFi.status() != WL_CONNECTED) 
      Serial.println("***WiFi connected***");


void loop() {
  // Read temperature, humidity values from DHT sensor at constant intervals
  if(millis() > time_now + period){
        time_now = millis();
        //Read temp reading
        displayData( hum, temp);
        // Turn on motor to ON the fan when temperature exceeds 23
      if(temp > 23)
      Serial.println("Sending only temp and hum data");
      //Send temp, humudity data to ThingSpeak
      String paramater2 = apiKey+"&field1="+String(temp)+"&field2="+String(hum)+"\r\n\r\n\r\n";
   } //End of delay 

   //Check if serial input available which is the User Id string sent by Arduino UNO on SoftwareSerial port
    Serial.println("In ESP sketch.. Serial Available. Reading.....");
    //Read UID And store into String name
    name = s.readString(); //This works
    //Print name onto Serial monitor
    Serial.println(name); //This works
    //if UID available, send Temp, Humidity and Uid=1 to thingspeak
    String parameter1 = apiKey+"&field1="+String(temp)+"&field2="+String(hum)+"&field3="+String(uid)+"\r\n\r\n\r\n"; 
    Serial.println("Sending temp, hum and uid data");


void displayData(float humidity, float temperature)   
    //print to serial monitor
    Serial.print("Humidity: ");
    Serial.print(" %, Temp: ");
    Serial.println(" Celsius");        

  void sendToThingSpeak(String sendData)
    if (client.connect(server,80))   //   "" or api.thingspeak.com
       Serial.println("Inside Send to thingspeak - Data Received");

       client.print("POST /update HTTP/1.1\n");
       client.print("Host: api.thingspeak.com\n");
       client.print("Connection: close\n");
       client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
       client.print("Content-Type: application/x-www-form-urlencoded\n");
       client.print("Content-Length: ");

       Serial.print("Temperature: ");
       Serial.print("deg C. Humidity: ");
       Serial.println(".. Connecting to Thingspeak.");
  • please use the {} function to make your code readable – Jaromanda X Feb 6 '19 at 4:06
  • Thank you Jarmanda. I have formatted the code now. – Tinasen Feb 6 '19 at 4:13
  • what about the I have followed this logic – Jaromanda X Feb 6 '19 at 6:22
  • what is D2 and D3? to which io pin does it map? on NodeMcu D3 is io 0 and has external pull-up. if could disturb SoftwareSerial. which version of the esp8266 core you use? 2.4.2 or 2.5 beta? – Juraj Feb 6 '19 at 12:50
  • D3=GPIO0, D2=GPIO4 and they are connected to pin 2,3 of Arduino UNO. I am using esp8266 2.4.1 – Tinasen Feb 6 '19 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.