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