Good day community.
I've got a software serial connection between ESP8266-E01 and an arduino Uno. Im trying to send sensor data to Thingspeak, but it looks like i have an issue with my code. I can't seem to figure it out
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(8,7);
String apiKey = "3HMMHUPRIDCED45G";
String ssid="Connectify-LTE"; // Wifi network SSID
String password ="megabone"; // Wifi network password
// defines pins numbers
const int trigPin = 13;
const int echoPin = 12;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
espSerial.begin(115200);
// espSerial.println("AT+CWMODE=3");
//delay(1000);
// espSerial.println("AT+RST");
delay(3000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");
delay(3000);
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(1000);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.017;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Send2Pachube();
delay(3000);
if (espSerial.available())
Serial.write(espSerial.read());
}
void Send2Pachube()
{
espSerial.println("AT");
delay(1000);
ShowSerialData();
espSerial.println("AT+CIFSR");
delay(1000);
ShowSerialData();
espSerial.println("AT+CIPSTART=\"TCP\",\"184.106.153.149\",\"80\"");
delay(6000);
ShowSerialData();
espSerial.println("AT+CIPSEND");
delay(6000);
ShowSerialData();
String str="GET /update?api_key=3HMMHUPRIDCED45G&field1=" + String(distance);
str += "\r\n\r\n";
espSerial.println(str);
delay(4000);
ShowSerialData();
espSerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
espSerial.println();
ShowSerialData();
espSerial.println("AT+CIPCLOSE");//close the connection
delay(100);
ShowSerialData();
delay(16000);
}
void ShowSerialData()
{
while(espSerial.available()!=0)
Serial.write(espSerial.read());
}