Hi All,
I know this question must have been asked many times and it should be very basic but for some reason, I could not solve the issue, hence the post.
General info:
I have an Arduino Uno and I would like to create serial communication between the Arduino and ESP8266 board. I am using an ESP-01 adapter which takes care of the voltage shifts and allows me to use the 5v instead of the 3v required.
The ESP board seems to work without any issue, I have uploaded a very simple sketch which uploads some data to ThingSpeak servers. here is the code for it.
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(9600);
Serial.println();
WiFi.begin("AP Name", "Pass");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
randomSeed(50);
}
void loop() {
const char* host = "api.thingspeak.com";
String tmpr ;
String humid ;
while(Serial.available())
{
if(Serial.find("Data"))
{
WiFiClient client;
if (client.connect(host, 80))
{
//Serial string should be at this format "Data:Temp:Humid"
String incommingStr = Serial.readString();
incommingStr.trim();
Serial.println("String is : " + incommingStr);
int inx = incommingStr.indexOf(":");
Serial.println("inx = " + String(inx));
int inx2 = 0;
if(inx>=0)
{
inx2 = incommingStr.lastIndexOf(":");
Serial.println("inx2 = " + String(inx2));
tmpr = incommingStr.substring(inx+1,inx2);
humid = incommingStr.substring(inx2+1);
}
Serial.println(String(tmpr) + " : Temp");
Serial.println(String(humid) + " : Humid");
String postStr="update?api_key=XXXXX&field1=" + String(tmpr) + "&field2=" + String(humid);
String apiKey= "XXXXX";
Serial.println("We are connected to the host ...");// we are connected to the host!
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: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println("Response");
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop();
Serial.println("\n[Disconnected]");
}
else
{
Serial.println("Connection to the host failed...");// connection failure
client.stop();
}
}
}
}
The Problem:
This code works without any issue as long as I am connecting the ESP board to a USB port on my computer using a USB adapter and sending serial string by a serial monitor application. If I connect the board to the Arduino board, I can see the ESP output messages in the serial monitor that monitors the serial output of the Arduino. (the massages are showing it is connected to my WiFi network successfully) But I can not send any thing either using the serial monitor or in the code from Arduino to the ESP.
Here is the code that I have on the Arduino Side which uses SoftwareSerial library to communicate.
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ESPserial.begin(9600);
ESPserial.println("Data:24:45"); //this is just a test but nothing seems to be sent to the ESP board
Serial.println("Started....");
}
void loop() {
// put your main code here, to run repeatedly:
while(ESPserial.available())
{
Serial.write( ESPserial.read()) ;
}
while(Serial.available())
{
ESPserial.write( Serial.read() ); // THIS LINE DOES NOT SEEM TO WORK
}
}
I am very new to C++ programing and Arduino and would appreciate any suggestion.
Regards,