ESP-01 receive data serially from arduino

Hi,

I am working on project where I have connected three sensors(MQ2,MQ7,MQ135) to mega2560 which reads the sensors output and prints them serially. this is the code that I use in arduino mega2560

float MQ2 = 0;
float MQ135 = 1;
float MQ7 = 2;

void setup() 
{
  Serial.begin(9600);
 
 //Serial.println("Welcome");
  delay(1000);
}

void loop() 
{
  //Serial.print("MQ2 :");
  Serial.println(analogRead(0));
 
  delay(1000);
  //Serial.print("MQ135 :");
  Serial.println(analogRead(1));
  
  delay(1000);
  //Serial.print("MQ7 :");
  Serial.println(analogRead(2));
  
  delay(500);
 }

I have connected ESP-01 to mega via Tx and Rx so that ESP-01 can read the data from mega serially.This is the code that I have used in ESP-01. Output of ESP is also serially monitored

#include <ESP8266WiFi.h>

// WiFi credentials
char ssid[] = "hotspot";      //  your network SSID (name)
char pass[] = "12345678";   // your network password
int status = WL_IDLE_STATUS;

// Initialize the Wifi client library
WiFiClient client;

char * thingName = "mymkr1000";

// Server
char host[] = "dweet.io";
boolean stringComplete;
String inputString;

int value = 0;
int sendGet = 0;
int value1 = 0;
int value2 = 0;
int value3 = 0;
int shubi = 0;

void setup()
{
  Serial.begin(9600);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

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

void loop() 
{

  serialEvent();

  if (stringComplete) 
  {
    Serial.print(inputString);
    value = inputString.toInt();
    inputString = "";
    stringComplete = false;
    sendGet = 1;
   }
   
  if(sendGet == 1)
  {
    sendGet = 0;
  
    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;
    }
  
    Serial.println(F("Connected"));


    String request = "GET /dweet/for/";
    request += String(thingName);
    request += "?value1=" + String(value1);
    request += "&value2=" + String(value2);
    request += "&value3=" + String(value3);
    request += " HTTP/1.1";
    
    // Send the HTTP request:
    client.println(request);
    client.println("Host: dweet.io");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();


    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);
    }
    
    Serial.println();
    Serial.println("closing connection");
  }

}



//------------------------------------------- SERIAL EVENT


void serialEvent() 
{
  while (Serial.available()) 
  {
    char inChar = (char)Serial.read();
    //Serial.println(inChar);
    if(inChar == 126)
    {  //~ erases string
      inputString = "";
    }
    if(inChar != 13 && inChar != 126)
    {      // add it to the inputString:
      inputString += inChar;
    }
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == 13) 
    { //13 is ASCI carriage return
      stringComplete = true;
      shubi ++;
    }
  }
}

I want this data to be uploaded to dweet.io,
My problem is that I get same readout rather than three different readouts in serial monitor for ESP-01. Even if the variable value has the successive readouts from the mega they are not reflected in output of ESP. what changes should be made in which code any help is appreciated. Thanks in advance

So, on the ESP device, are you using Serial to talk to the Mega OR to talk to the PC? Both is the wrong answer. So is yes.

Thanks PaulS,

On ESP,I am using Serial to read data from mega and serial write to get it on serial monitor.

I can See the output of ESP on serial monitor and not having any trouble in getting it

I don't think serialEvent() is supposed to be called from loop(). It's already in main().

Why do you use an int for sendGet and a boolean for stringComplete when they are both logical flags?

What is the undocumented variable "shubi"?

Please post a sample of the program output, in code tags.

Connecting to hotspot
.............
WiFi connected
IP address: 
192.168.43.36
301
321
343
302
323
341
306
328
connecting to dweet.io
Connected
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json
Content-Length: 214
Date: Sun, 20 Aug 2017 12:41:07 GMT
Connection: close

{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"mymkr1000","created":"2017-08-20T12:41:07.947Z","content":{"value1":0,"value2":0,"value3":0},"transaction":"225d630b-ab07-4d33-a0e3-6e5ee521aad9"}}
closing connection
347
316
339
356
326
348

This is the output that I get.

What is the undocumented variable "shubi"?

this does not have work its just a residual of my trials

It is sending different numbers each time. That is what it is supposed to do, isn't it? What do you expect to see?

Do you understand that you are calling serialEvent() twice each loop()?

Thanks aarg

It is sending different numbers each time. That is what it is supposed to do, isn't it? What do you expect to see?

If you see the curly brackets after content in the output it gives "value1":0, "value2":0, "value3":0.
Instead of this I want the data received from mega to be displayed there for example like "value1":301,"value2":321,"value3":343 they are the sensors readouts received from mega over serial, you can see those readouts in the output between my IP add and 'connecting to dweet.io'

Do you understand that you are calling serialEvent() twice each loop()?

Sorry, I am not able to figure that out, actually since working on this code from last three days have made my eyes go clumsy and weird. Could you just help me to figure that out by point it,please

On ESP,I am using Serial to read data from mega and serial write to get it on serial monitor.

Didn't I say that "both" is the WRONG answer?

Count:
Sorry, I am not able to figure that out, actually since working on this code from last three days have made my eyes go clumsy and weird. Could you just help me to figure that out by point it,please

Look at the SerialEvent tutorial. You will observe that SerialEvent is not called inside the loop() function. The reason why is explained there.

Thanks aarg, Icommented the line in loop() which calls SerialEvent but then it does no show anything after the IP address is printed. here's how the output looks like now.

Connecting to hotspot
.............
WiFi connected
IP address: 
192.168.43.36

Didn't I say that "both" is the WRONG answer?

PaulS I am not getting what you are trying to bring in to notice. But I have made connections the same way like I said and it shows output.

You never assign any values to value1, value2, or value3. Small wonder they print as 0.

How do I do that so that the first serially received value gets stored in variable value1, the second value in variable value2 and third value in variable value3

Count:
How do I do that so that the first serially received value gets stored in variable value1, the second value in variable value2 and third value in variable value3

It would have saved a lot of time to ask that in your first post.

That topic is well covered here:
http://forum.arduino.cc/index.php?topic=396450

Thanks aarg, and sorry for writing back late. I went through all of the codes from the tutorial you suggested. Its really amazing. At beginning I was confused with which code is suitable in my case and later found it out, example no.5 worked for me. Thanks for great help aarg.