0

I am sending some data from ESP8266 01 module to Arduino. But the data received by Arduino is not complete.

Below are the images of the output from ESP8266 01 module and Arduino.

ESP8266 01 output Data received by Arduino

Here is the ESP8266 01 Module code:-

#include <ESP8266WiFi.h>
#include<String.h>

char c,e;                       //stores the bytes coming from the server
int DEBUG = 0;

int co=0;                       //control the sending of Alert

const char* ssid     = "ssid";            //your SSID of WIFI
const char* password = "password";       // password of Wifi
//--------------------------------------------

const char* host = "api.thingspeak.com";

void setup() {
  //dht.begin();
    Serial.begin(115200);
    delay(10);
  // We start by connecting to a WiFi network

    if (DEBUG){
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
  }

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (DEBUG){
      Serial.print(".");
    }
  }

    if (DEBUG){
      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
}

int value = 0;

void loop() {
  delay(1000);
  ++value;

  if (DEBUG){
    Serial.print("connecting to ");
    Serial.println(host);
  }

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  if (DEBUG){
    Serial.print("Requesting URL: ");
  }

//============================================================================================================
//                                Sending Control info to arduino Serially
//============================================================================================================
  // This will send the request to the server
  client.print(String("GET ")  + "/channels/******/feeds.json?api_key=*****************&results=2 HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");         // get information about device control
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);                         // received by arduino serially
  }
 //Serial.println("closing connection");
}

Here is the Arduino code:-

    //This sketch in the implementation of home automation 
#include <SoftwareSerial.h>// import the serial library
#include<String.h>
//-----------------------------------------
//    pin definations
//-----------------------------------------
#define Light 2              // pin 2 to control light
#define fan 3                 // pin 3 to control fan
//------------------------------------------
//       variables
//------------------------------------------
char e;
int p=0,q=0;        //these variables prevent switching ON/OFF of already turned ON/OFF devices
int DEBUG = 1;

SoftwareSerial home_monitor(10, 11); // RX, TX

char control_data; // the data transmitted over Bluetooth

void setup() {
  // put your setup code here, to run once:
  home_monitor.begin(115200);
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (home_monitor.available()){
     control_data=home_monitor.read();   // getting control information from ESP8266 serially
     e=control_data;
     if (DEBUG){
      Serial.print(control_data);
     }
     //Serial.print(e);
 //============================================================================================================
 //                                        Device Control
 //============================================================================================================

 // Performing required operations based on the value of channel
    //-------------------------------------------------------------
    //                        LIGHT
    //-------------------------------------------------------------

    if(e=='0'&&p==0)         //Light ON            
    {
      if (DEBUG){
        Serial.println("Light ON");
      }
      digitalWrite(Light,LOW);
      p=1;
    }
     if(e=='1'&&p==1)        //Light off
    {
      if (DEBUG){
        Serial.println("Light OFF");
      }
      digitalWrite(Light,HIGH);
      p=0;
    }

    //-------------------------------------------------------------
    //                        Heater
    //-------------------------------------------------------------

    if(e=='2'&&q==0)
    {
      if (DEBUG){
        Serial.println("Fan ON");
      }
      digitalWrite(fan,LOW);
      q=1;
    }
    if(e=='3'&&q==1)
    {
      if (DEBUG){
        Serial.println("Fan OFF");
      }
      digitalWrite(fan,HIGH);
      q=0;
    }
  }
}
  • 1
    Serial.begin(115200); on ESP8266 and Serial.begin(9600); on Arduino. Try using the equal value of the bitrate. – AltAir Mar 10 '19 at 19:28
0

You are using client.print() and the Arduino uses home_monitor.read() to receive the message (if I understood correctly). What I did in my sketch was change the home_monitor.read() to home_monitor.readStringUntil('\n').

Also before your ESP sends a message via client.print() it is recommended to run the client.flush() method so it waits until all outgoing characters in the buffer have been sent, before sending the next.

When using the ESP you should be aware of the useful yield() method (call it at the end of the loop). This let's the ESP get his background duties finished before going on (not calling this method may result in some issues.

|improve this answer|||||

Your Answer

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

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