I couldn't find any solutions on the net. I am stuck with this problem from the last week . Any one can help ?
I am using arduino IDE .Here is my code :
ESP code:
/*this code :
1- connects to the network. Then it waits unitl arduino mega sends "hello esp!" . When it recievs it sends "hello arduino " to the arduino
2- It recieves data throgh RX0 and sends data throgh TX1 .
3- It prints the ip address only.But I need to know the port too.
*/
#include "ESP8266WiFi.h"
String readString;
char c ;
int recieving_counter = 0;
// WiFi parameters to be configured
const char* ssid = "bt";
const char* password = "braintools" ;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print a new line, then print WiFi connected and the IP address
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
recieve_data();
if (recieving_counter>0){
recieving_counter=0;
send_data();
}
// always check to see if the esp is connected to the network
if (WiFi.status() == WL_CONNECTED){
}
else{//reconnect
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
}
void send_data(){
Serial1.println("Hello Arduino! ");
delay(3000);
}
void recieve_data(){
while (!Serial.available()){
// wait until the arduino sends the data
delay(1000);
Serial.println("- ");
}
while (Serial.available()){
Serial.print("r");
c = Serial.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
recieving_counter++;
}
Serial.print(readString);
readString = ' ';
delay(2000);
}
Arduino code:
String readString;
char c ;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
send_data();
recive_data();
}
void send_data(){
//Serial.println("Hello Esp!");
Serial1.println("Hello Esp!");
delay(3000);
}
void recive_data(){
while (!Serial1.available()){
// wait until the esp sends the data
delay(1000);
Serial.println("* ");
}
while (Serial1.available()){
Serial.print("r");
c = Serial1.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
Serial.print(readString);
readString = ' ';
//delay (500);
delay (2000);
}