Serial Communication with Esp

Could you help me to solve this compiling error?

#include <SoftwareSerial.h>
#include <stdlib.h>

int ledPin = 13;

#define trigPin A1
#define echoPin A0

String apiKey = "2TNSKZAUY0FUD7QY";

SoftwareSerial ser(2,3); // RX, TX

void setup() {                
  
  pinMode(ledPin, OUTPUT);    
  
  Serial.begin(9600); 
  ser.begin(9600);

  pinMode(trigPin,OUTPUT);  //Trigger
  pinMode(echoPin,INPUT);   //Echo
  
  // reset ESP8266
  ser.println("AT+RST");
}

void loop() {

   long duration, distance;
  
  digitalWrite(ledPin, HIGH);   
   delay(200);               
  digitalWrite(ledPin, LOW);

  
    digitalWrite(trigPin,LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin,HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin,LOW);
    
    duration = pulseIn(echoPin,HIGH);
    distance = (duration/2)/29.1;
    Serial.print(distance);
    Serial.println("cm//");
    delay(2000); 
    
  }

 
  Serial.println(distance);
 
  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com
  cmd += "\",80";
  ser.println(cmd);
   
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
  }
  
  // prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(distance);
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ser.println(cmd);

  if(ser.find(">")){
    ser.print(getStr);
  }
  else{
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
  }
    
  // thingspeak needs 15 sec delay between updates
  delay(16000);  
}

esp_distance:51: error: 'Serial' does not name a type
esp_distance:55: error: 'cmd' does not name a type
esp_distance:56: error: 'cmd' does not name a type
esp_distance:57: error: 'ser' does not name a type
esp_distance:59: error: expected unqualified-id before 'if'
esp_distance:66: error: 'getStr' does not name a type
esp_distance:67: error: 'getStr' does not name a type
esp_distance:68: error: 'getStr' does not name a type
esp_distance:69: error: 'getStr' does not name a type
esp_distance:72: error: 'cmd' does not name a type
esp_distance:73: error: 'cmd' does not name a type
esp_distance:74: error: 'ser' does not name a type
esp_distance:76: error: expected unqualified-id before 'if'
esp_distance:79: error: expected unqualified-id before 'else'
esp_distance:86: error: expected constructor, destructor, or type conversion before '(' token
esp_distance:87: error: expected declaration before '}' token
'Serial' does not name a type

What folder do you have these libraries in?

#include <SoftwareSerial.h>
#include <stdlib.h>

CrossRoads:
What folder do you have these libraries in?

I really have no idea about libraries on programming.I just include them at the beginning of codes. I've to say that I just copied this code from somewhere on web and a little change it to use according to my desires to establish my project.If I don't change anything and directly upload to sketch then there is no compiling error and runs fine.

Where should exactly look for the find out what folder do have these libraries in?

If you want to take a look here is the original code

// esp8266_test.ino
//
// Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi 
// module.
//
// Author: Mahesh Venkitachalam
// Website: electronut.in

#include <SoftwareSerial.h>
#include <stdlib.h>

// LED 
int ledPin = 13;
// LM35 analog input
int lm35Pin = 0;

// replace with your channel's thingspeak API key
String apiKey = "2TNSKZAUY0FUD7QY";

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(2,3); // RX, TX

// this runs once
void setup() {                
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);    

  // enable debug serial
  Serial.begin(9600); 
  // enable software serial
  ser.begin(9600);
  
  // reset ESP8266
  ser.println("AT+RST");
}


// the loop 
void loop() {
  
  // blink LED on board
  digitalWrite(ledPin, HIGH);   
  delay(200);               
  digitalWrite(ledPin, LOW);

  // read the value from LM35.
  // read 10 values for averaging.
  int val = 0;
  for(int i = 0; i < 10; i++) {
      val += analogRead(lm35Pin);   
      delay(500);
  }

  // convert to temp:
  // temp value is in 0-1023 range
  // LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
  // So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
  float temp = val*50.0f/1023.0f;

  // convert to string
  char buf[16];
  String strTemp = dtostrf(temp, 4, 1, buf);
  
  Serial.println(strTemp);
  
  // TCP connection
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149"; // api.thingspeak.com
  cmd += "\",80";
  ser.println(cmd);
   
  if(ser.find("Error")){
    Serial.println("AT+CIPSTART error");
    return;
  }
  
  // prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strTemp);
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  ser.println(cmd);

  if(ser.find(">")){
    ser.print(getStr);
  }
  else{
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");
  }
    
  // thingspeak needs 15 sec delay between updates
  delay(16000);  
}

What board are you trying to compile the program for?

Is it for a standard Arduino board (if so which one) or is it for some version of ESP8266 ?

...R

What board are you trying to compile the program for?

It's Arduino UNO.

I'm using Arduino 2,3 pins to serial communication with ESP8266-01. I can send AT commands manually from serial monitor. You may want to look this post or just this video

Your problem is that you have some code in no-mans-land. Try removing the } from line 48

...R

Try removing the } from line 48

thank you this single bracket was the reason of whole compiling error.

It seems code in no-mans-land working fine enough :slight_smile:

And the result : video

Now I've to write down codes for other 3 sensors. And maybe I should reduce the delay times between each measure.I guess hcsr04,esp and thingspeak can handle it.

ElCezeri:
It seems code in no-mans-land working fine enough :slight_smile:

If you removed the } the code is no longer out-in-the-cold

Do you understand what I meant by it being "in no-mans-land" ?

...R

Do you understand what I meant by it being "in no-mans-land" ?

At first, I just thought you are making fun with me because, I'm taking someone's code and trying to fit in mine desires with basic programming knowledge.

However, now I can somehow understand what you meant a little. I was defining or calling a variable that it is in reality not exist right there! or something similar. Anyway I appreciate your help.

No, I was not trying to make fun of you, though I was trying for a little humour. The effect of your stray bracket was to maroon some of the code outside of any function - hence my use of the term "no man's land"

...R