Using String function

Attached is a sketch from Random Nerd Tutorials to send a Whatsapp message using the ESP8266.
It all works perfectly but I would like to know if, and more importantly how, I can send a "String" such as :
("%02d:%02d:%02d" , time_hour , time_min , time_sec)

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>

const char* ssid = "XXXXXXXXXX";
const char* password = "XXXXXXXXXXX";
String phoneNumber = "XXXXXXXXXXXX";
String apiKey = "XXXXXXX";

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  sendMessage("Hello from ESP8266!");
  delay(10000);
}

void sendMessage(String message) {
  String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
  WiFiClient client;
  HTTPClient http;
  http.begin(client, url);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpResponseCode = http.POST(url);
  if (httpResponseCode == 200) {
    Serial.print("Message sent successfully");
  }
  else {
    Serial.println("Error sending the message");
    Serial.print("HTTP response code: ");
    Serial.println(httpResponseCode);
  }
  http.end();
}

That looks very much like it is a format string and data items for use with one of the C string formatting functions

You put "String" in quotes as if it is not really a String that you want so what exactly are you trying to do ?

Basically I can send text or values (temperature in my case) as separate Whatsapp messages but I would like to send a single message such as :
"New all time low = 1.2 degrees at 10:21:45"

Does the message test have to be a String or can it be a C style string ?

If it must be a String then why can't you just concatenate the values together just vas the sketch that you posted does in this line ?

  String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);

If it can be a C style string then use sprintf() to build it as alluded to in your original question

I think it could be either method it's just where to put it in the original sketch?
If I try to add anything to the 'urlEncode(message)' I get errors.
If I try to change the message in the void loop I get errors

post the complete sketch.

writing "add anything to urlEncode(message)"

is unclear and does not show how you have written the code.

You call the sendMessage() function with the message to be sent. Build your required message just befoer the function call and use it as the parameter sent to sendMessage()

The complete sketch is massive.
I am currently using the sketch I previously posted within my main sketch and I am sending separate messages such as:
"Minimum temperature reached "
(temperature value)
"At "
(time_hour, time_min, time_sec)

You can posts sketches big as 120000 characters as code-sections.

If you want to play a game of posting only a small part of your code which then later turns out that the bug is somewhere in that part of the code you have not posted go ahead distracting finding the solution.

otherwise poste a the complete sketch and post where in your code the error occurs

My problem is not with my main sketch.
If I cant find a way to send the required string in the original posted sketch then I am not going to attempt to add it to my main sketch

1 Like

as you are told build a String directly or use sprintf() to build a null terminated c style char buffer (c-string)

void setup() {
  Serial.begin(115200); Serial.println();
  uint8_t time_hour = 8, time_min = 22, time_sec = 33;
  float temperature = 22.57;
  String message = "Minimum temperature of ";
  message += String(temperature, 1); // rounding with  just 1 digit after decimal point
  message += "°C reached at ";
  message += String(time_hour);
  message += "h";
  message += String(time_min);
  message += " and ";
  message += String(time_sec);
  message += " second";
  if (time_sec > 1) message += "s";
  // message now holds "Minimum temperature of 22.6°C reached at 8h22 and 33 seconds"
  Serial.println(message);
}

void loop() {}

test it here

This is starting to work but If I try to send a message from within the void loop I get an error message that "message is not declared in this scope"

The "sendMessage()" function takes a String so you can pass anything that can be converted to a String. That can be a character array, character pointer, or String.

void loop() {
  char time[10];
  sprintf(time, "%02d:%02d:%02d" , time_hour , time_min , time_sec);
  sendMessage(time);
  delay(10000);
}

I HAVE IT :slight_smile:

I had used Message not message in one of the lines !!!

Thank you all for your help

It's amazing what you can fix if you take note of the error message

:wink: :slightly_smiling_face: :grinning: :smiley: :smile: :grin: :laughing: so true

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