Issue with running code on NodeMCU 1.0 (ESP-12E Module)

Hi everyone,

I am trying to develop a simple one way communication between my ESP12E module and my phone by using the callmebot via signal app. I found the code on the web which is originally made for ESP12.

When attempting to run the code an error message appears which I cant figure out why or how to fix it.

**Using library HttpClient-2.2.0 at version 2.2.0 in folder: C:\Users\ ROG\Documents\Arduino\libraries\HttpClient-2.2.0 **

**Using library ESP8266WiFi at version 1.0 in folder: C:\Users\ ROG\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266WiFi **

**Using library WiFi at version 1.2.7 in folder: C:\Program Files (x86)\Arduino\libraries\WiFi **

**Using library SPI at version 1.0 in folder: C:\Users\AROG\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\SPI **

exit status 1

'url' was not declared in this scope

Would appreciate any help.

The code can be seen below:

#include <b64.h>
#include <HTTPClient.h>


#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <WiFi.h>         //Including wifi.h library it will take care of all wifi related task

const char* ssid     = "ssid";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "password";     // The password of the Wi-Fi network

String apiKey = "apiKey";              //Add your Token number that bot has sent you on signal messenger
String phone_number = "+Phonenumber"; //Add your signal app registered phone number (same number that bot send you in url)

void setup() {
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');
  
  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
  
  // use message_to_signal function to send your own message
  message_to_signal("Test from ESP12E");  // 
}

void loop()
{
  //loop will do nothing for this example
}

void  message_to_signal(String message)       // user define function to send meassage to Signal app
{
  //adding all number, your api key, your message into one complete url
  url = "https://api.callmebot.com/signal/send.php?phone=" + phone_number + "&apikey=" + apiKey + "&text=" + urlencode(message);
  postData(); // calling postData to run the above-generated url once so that you will receive a message.
}

void postData()     //userDefine function used to call api(POST data)
{
  int httpCode;     // variable used to get the responce http code after calling api
  HTTPClient http;  // Declare object of class HTTPClient
  http.begin(url);  // begin the HTTPClient object with generated url
  httpCode = http.POST(url); // Finaly Post the URL with this function and it will store the http code
  if (httpCode == 200)      // Check if the responce http code is 200
  {
    Serial.println("Sent ok."); // print message sent ok message
  }
  else                      // if response HTTP code is not 200 it means there is some error.
  {
    Serial.println("Error."); // print error message.
  }
  http.end();          // After calling API end the HTTP client object.
}

String urlencode(String str)  // Function used for encoding the url
{
    String encodedString="";
    char c;
    char code0;
    char code1;
    char code2;
    for (int i =0; i < str.length(); i++){
      c=str.charAt(i);
      if (c == ' '){
        encodedString+= '+';
      } else if (isalnum(c)){
        encodedString+=c;
      } else{
        code1=(c & 0xf)+'0';
        if ((c & 0xf) >9){
            code1=(c & 0xf) - 10 + 'A';
        }
        c=(c>>4)&0xf;
        code0=c+'0';
        if (c > 9){
            code0=c - 10 + 'A';
        }
        code2='\0';
        encodedString+='%';
        encodedString+=code0;
        encodedString+=code1;
        //encodedString+=code2;
      }
      yield();
    }
    return encodedString;
}

You forgot to create this global variable before you tried to assign it a value.

Appreciate your replay John. I'm not quite sure I understood what you meant. could please describe/show the fix? What I can see is already added if I'm not wrong :sweat:

like this you meant right? which is ready in the code.

void message_to_signal(String message) // user define function to send meassage to Signal app
{
//adding all number, your api key, your message into one complete url
url = "https://api.callmebot.com/signal/send.php?phone=" + phone_number + "&apikey=" + apiKey + "&text=" + urlencode(message);
postData(); // calling postData to run the above-generated url once so that you will receive a message.
}

In C language each variable should be declared before usage. Your url does not declared - hence the error.

declare the url . Because this variable is used in more than one code block ( function) , I think it should be global

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.