I just want to make a simple alarm which will send message on whatsapp on the time.
But I don't know why its giving me a error called "ISO C++ forbids comparison between pointer and integer [-fpermissive]" Please help me with this code.
Sorry if I chose the wrong category.
Thank you for your time and consideration.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <UrlEncode.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
//Week Days
String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//Month names
String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const char* ssid = "....";
const char* password = "....";
String phoneNumber = "...";
String apiKey = "...";
void sendMessage(String message){
// Data to send with HTTP POST
String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
WiFiClient client;
HTTPClient http;
http.begin(client, url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
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);
}
// Free resources
http.end();
}
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());
// Send Message to WhatsAPP
//sendMessage("Start");
timeClient.begin();
timeClient.setTimeOffset(19800);
}
void loop() {
//-----------------------------------------------Debug----------------------------------------------//
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
//Serial.print("Epoch Time: ");
//Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
//Serial.print("Formatted Time: ");
//Serial.println(formattedTime);
long currentHour = timeClient.getHours();
//Serial.print("Hour: ");
//Serial.println(currentHour);
long currentMinute = timeClient.getMinutes();
//Serial.print("Minutes: ");
//Serial.println(currentMinute);
long currentSecond = timeClient.getSeconds();
//Serial.print("Seconds: ");
//Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
//Serial.print("Week Day: ");
//Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
//Serial.print("Month day: ");
//Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
//Serial.print("Month: ");
//Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
//Serial.print("Month name: ");
//Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
//Serial.print("Year: ");
//Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
//Serial.print("Current date: ");
//Serial.println(currentDate);
//Serial.println("");
//delay(2000);
if((currentHour) == "18" && (currentMinute) == "35"){
sendMessage("Alarm!");
delay(65000);
}
//------------------------------------------------------------------------------------------------------------//
}
The error message provides a lot more information than that. Copy and paste the entire thing, not your paraphased version of it.
horace
October 31, 2023, 1:50pm
3
currentHour is a long int and "18" is a character string
did you intend
if((currentHour) == 18 && (currentMinute) == 35)
or
if ( (currentHour == "18") && (currentMinute == "35") )
Sorry It was int but I tried to change to long is it ok?
horace
October 31, 2023, 2:24pm
6
hours range is 0 to 23 and minutes/seconds 0 to 59 so int was OK (even byte would probably work)
your problem was comparing a numeric value (long int) with a character string ( a char* pointer) hence the error message "ISO C++ forbids comparison between pointer and integer"
F:\Users\Admin\Documents\Arduino\Medicine_3\Medicine_3.ino: In function 'void loop()':
Medicine_3:123:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if((currentHour) == "18" && (currentMinute) == "35"){
^
Medicine_3:123:49: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if((currentHour) == "18" && (currentMinute) == "35"){
^
Multiple libraries were found for "NTPClient.h"
Used: F:\Users\Admin\Documents\Arduino\libraries\NTPClient
Not used: F:\Users\Admin\Documents\Arduino\libraries\ESP8266_Weather_Station
exit status 1
ISO C++ forbids comparison between pointer and integer [-fpermissive]
This is the whole error
horace
October 31, 2023, 2:53pm
9
if you wish to compare using character strings make the int values String() e.g.
if(String(currentHour) == "18" && String(currentMinute) == "35"){
Its not working
void loop() {
//-----------------------------------------------Debug----------------------------------------------//
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
//Serial.print("Epoch Time: ");
//Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
//Serial.print("Formatted Time: ");
//Serial.println(formattedTime);
long currentHour = timeClient.getHours();
//Serial.print("Hour: ");
//Serial.println(currentHour);
long currentMinute = timeClient.getMinutes();
//Serial.print("Minutes: ");
//Serial.println(currentMinute);
long currentSecond = timeClient.getSeconds();
//Serial.print("Seconds: ");
//Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
//Serial.print("Week Day: ");
//Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
//Serial.print("Month day: ");
//Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
//Serial.print("Month: ");
//Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
//Serial.print("Month name: ");
//Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
//Serial.print("Year: ");
//Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
//Serial.print("Current date: ");
//Serial.println(currentDate);
//Serial.println("");
//delay(2000);
if(String(currentHour) == "20" && String(currentMinute) == "50"){
sendMessage("Alarm!");
delay(65000);
}
//------------------------------------------------------------------------------------------------------------//
}
horace
October 31, 2023, 3:24pm
11
I_Am_Arceus:
Its not working
is rather vauge!
does it compile? if not what are the error messages?
if it runs what happens? what does it do? what should it do? upload the serial monitor output (as text not a screen image)
Yes! it compiles it runs but nothing happens it should send me a message on my whatsapp on the desired time.
horace
October 31, 2023, 3:31pm
13
I assume it should send an alarm at 20:50 - try adding a serial println in case it is a whatsapp problem
e.g. also avoid String() if possible using integer values
if(currentHour == 20 && currentMinute == 50){
Serial.println("20:50 Alarm!");
sendMessage("Alarm!");
delay(65000);
}
what does the serial monitor show?
Hey!
if(currentHour == 20 && currentMinute == 50){
}
this statement works perfectly fine and was working perfectly.
The problem was that the callmebot server in down so the api key's aren't working.
Sorry for the inconvenience caused I apologize for my mistake.
Thank you everyone for your time.
I will close this topic as soon as the server start and the code work
Thank you again.
Special thank to @horace & @DrDiettrich
horace
October 31, 2023, 4:20pm
15
that is one reason why when you transmit data over a communications system it is a good idea to print a local copy - you can always comment out the print when it is all working
The code is working fine thank you all of you for your help!
Hey @horace I want a alarm to go on that time but the problem is that it should not send me message more than 5 times can you help me with that.
Thank you for your time!
horace
November 1, 2023, 11:31am
18
have a counter - increment it on each transmission - when it reaches 5 stop transmitting
e.g.
static int transmitCounter=0;
if(transmitCounter < 5 && currentHour== 20 && currentMinute == 50){
transmitCounter++;
sendMessage("Alarm!");
delay(65000);
}
transmitCounter is static so it retains it's last value on sucessive calls to loop()
you need some means of resettting transmitCounter to 0
alto777
November 1, 2023, 2:07pm
19
horace:
delay(65000);
Doesn't that mean that currentMinute will have advanced beyond 50 next time through the loop?
Initialize transmitCounter to more than 5, then
if (currentHour == 20 && currentMinute == 50) {
transmitCounter == 0;
}
if (tranmsitCounter < 5) {
sendMessage("Alarm!");
delay(65000);
transmitCounter++;
}
Of course the 65000 millisecond delay shoukd be replaced by millis ()-based mechanism to send the Alarm ! message every N milliseconds for five times.
Nearly resets itself, too, I just noticed.
a7
system
Closed
April 29, 2024, 2:07pm
20
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.