Im trying to write (modify) a code, where a rtc module is used. Im using the esp32 and as it has wifi, im trying to use it as a NTP Client in order to get the correct time without the rtc module. So far it is working, i get the correct time, but i only get it like this "HH:MM:SS", but I need to get it as variables. So for example the hours as the variable "timeH", the minutes as "timeM" and the seconds as "timeS". Could someone help me?
Could someone help me?
No, because you failed to post your code.
Yea, because Im a beginner with coding with the arduino ide and I just tried to imploment a simple time code, wich I found on the internet into another code, but the second code needs the time as variables (hours, minutes and seconds)....
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char* ssid = "346256256562456";
const char* password = "245624564545624";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}
void loop() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
// Serial.println(formattedDate); //
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Serial.print("DATE: "); //
// Serial.println(dayStamp); //
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: "); //
Serial.println(timeStamp); //
delay(1000);
}
The library you are using has these methods:
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
Why can't you just use them?
I tried using that
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
, but then I got this error
non-member function 'int getDay()' cannot have cv-qualifier
I tried using that
You make it EXTREMELY difficult to help you. Read the stickies at the top of the forum. Again, if necessary. This IS the programming section of the forum. You are expected to POST YOUR CODE!
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "23523523235";
const char* password = "235235235";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 19; // Set hours to wake (military time)
int setMin = 47; // Set minute to wake
int uled = 21; // Set pinout with with PWM
//PH time service
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
void setup() {
pinMode(uled, OUTPUT);
Serial.begin(115200); // Initialize Serial Monitor
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}
//class NTPClient{
//...
//public:
//getFullFormattedTime;
//};
//String NTPClient//::getFullFormattedTime()
//{
// time_t rawtime = this->getEpochTime()
// struct tm * ti;
// ti = localtime (&rawtime);
// uint16_t year = ti->tm_year + 1900;
// String yearStr = String(year);
// uint8_t month = ti->tm_mon + 1;
// String monthStr = month < 10 ? "0" + String(month) : String(month);
// uint8_t day = ti->tm_mday;
// String dayStr = day < 10 ? "0" + String(day) : String(day);
// uint8_t hours = ti->tm_hour;
// String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
// uint8_t minutes = ti->tm_min;
// String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
// uint8_t seconds = ti->tm_sec;
// String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
// return yearStr + "-" + monthStr + "-" + dayStr + " " +
// hoursStr + ":" + minuteStr + ":" + secondStr;
//{
void loop()
{
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
// Serial.println(formattedDate); //
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Serial.print("DATE: "); //
// Serial.println(dayStamp); //
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: "); //
Serial.println(timeStamp); //
delay(1000);
//t = timeStamp
if (getHours() == setHour && getMinutes() == setMin) // Check if it's time to wake up!
{
start();
}
// Wait one second before repeating
delay (1000);
}
void start()
{
// Fix for SB LED
analogWrite(uled, 1);
delay((fadeTime * 60000)/50);
analogWrite(uled, 2);
delay((fadeTime * 60000)/50);
analogWrite(uled, 3);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 5);
delay((fadeTime * 60000)/50);
// Fade script
for (int i = 6 ; i <= 255; i++)
{
analogWrite(uled, i);
delay(((fadeTime * 60000)/306));
Serial.print(" mil sec ");
Serial.print(((fadeTime * 60000)/306));
Serial.print(" PWM " );
Serial.print(i);
}
delay(20000); // Stay Bright
analogWrite(uled, 0); // Turn off
}
//PH time service
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
Why are you declaring these functions?
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
int currentHour = timeClient.getHours();
int currentMin = timeClient.getMinutes();
int currentSec = timeClient.getSeconds();
Ahhh, now I understand and now it works, but theres (hopefully) just one more thing
void start()
{
// Fix for SB LED
analogWrite(uled, 1);
delay((fadeTime * 60000)/50);
analogWrite(uled, 2);
delay((fadeTime * 60000)/50);
analogWrite(uled, 3);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 5);
delay((fadeTime * 60000)/50);
'analogWrite' was not declared in this scope
just one more thing
There must be something wrong with where that snippet lives. Calling a moving company.
Is the necessity to post ALL of your code that difficult to comprehend?
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "wsesaeraser";
const char* password = "fgsertwefgefg";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 19; // Set hours to wake (military time)
int setMin = 47; // Set minute to wake
int uled = 21; // Set pinout with with PWM
//PH time service
int currentHour = timeClient.getHours();
int currentMin = timeClient.getMinutes();
int currentSec = timeClient.getSeconds();;
void setup() {
pinMode(uled, OUTPUT);
Serial.begin(115200); // Initialize Serial Monitor
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}
//class NTPClient{
//...
//public:
//getFullFormattedTime;
//};
//String NTPClient//::getFullFormattedTime()
//{
// time_t rawtime = this->getEpochTime()
// struct tm * ti;
// ti = localtime (&rawtime);
// uint16_t year = ti->tm_year + 1900;
// String yearStr = String(year);
// uint8_t month = ti->tm_mon + 1;
// String monthStr = month < 10 ? "0" + String(month) : String(month);
// uint8_t day = ti->tm_mday;
// String dayStr = day < 10 ? "0" + String(day) : String(day);
// uint8_t hours = ti->tm_hour;
// String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
// uint8_t minutes = ti->tm_min;
// String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
// uint8_t seconds = ti->tm_sec;
// String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
// return yearStr + "-" + monthStr + "-" + dayStr + " " +
// hoursStr + ":" + minuteStr + ":" + secondStr;
//{
void loop()
{
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
// Serial.println(formattedDate); //
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Serial.print("DATE: "); //
// Serial.println(dayStamp); //
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: "); //
Serial.println(timeStamp); //
delay(1000);
//t = timeStamp
if (currentHour == setHour && currentMin == setMin) // Check if it's time to wake up!
{
start();
}
// Wait one second before repeating
delay (1000);
}
void start()
{
// Fix for SB LED
analogWrite(uled, 1);
delay((fadeTime * 60000)/50);
analogWrite(uled, 2);
delay((fadeTime * 60000)/50);
analogWrite(uled, 3);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 5);
delay((fadeTime * 60000)/50);
// Fade script
for (int i = 6 ; i <= 255; i++)
{
[b]analogWrite(uled, i);
delay(((fadeTime * 60000)/306));
Serial.print(" mil sec ");
Serial.print(((fadeTime * 60000)/306));
Serial.print(" PWM " );
Serial.print(i);
}
delay(20000); // Stay Bright
[b]analogWrite(uled, 0); // Turn off
}
AnalogWrite cant be used with the Esp32, but is it anyhow possible to use something different (without changing too much)
Philipp_Gretscher:
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "wsesaeraser";
const char* password = "fgsertwefgefg";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
// Changable Vars
int fadeTime = 1; // How long the light will fade to max
int setHour = 19; // Set hours to wake (military time)
int setMin = 47; // Set minute to wake
int uled = 21; // Set pinout with with PWM
//PH time service
int currentHour = timeClient.getHours();
int currentMin = timeClient.getMinutes();
int currentSec = timeClient.getSeconds();;
void setup() {
pinMode(uled, OUTPUT);
Serial.begin(115200); // Initialize Serial Monitor
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
}
//class NTPClient{
//...
//public:
//getFullFormattedTime;
//};
//String NTPClient//::getFullFormattedTime()
//{
// time_t rawtime = this->getEpochTime()
// struct tm * ti;
// ti = localtime (&rawtime);
// uint16_t year = ti->tm_year + 1900;
// String yearStr = String(year);
// uint8_t month = ti->tm_mon + 1;
// String monthStr = month < 10 ? "0" + String(month) : String(month);
// uint8_t day = ti->tm_mday;
// String dayStr = day < 10 ? "0" + String(day) : String(day);
// uint8_t hours = ti->tm_hour;
// String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
// uint8_t minutes = ti->tm_min;
// String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
// uint8_t seconds = ti->tm_sec;
// String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
// return yearStr + "-" + monthStr + "-" + dayStr + " " +
// hoursStr + ":" + minuteStr + ":" + secondStr;
//{
void loop()
{
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
// Serial.println(formattedDate); //
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
// Serial.print("DATE: "); //
// Serial.println(dayStamp); //
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: "); //
Serial.println(timeStamp); //
delay(1000);
//t = timeStamp
if (currentHour == setHour && currentMin == setMin) // Check if it's time to wake up!
{
start();
}
// Wait one second before repeating
delay (1000);
}
void start()
{
// Fix for SB LED
analogWrite(uled, 1);
delay((fadeTime * 60000)/50);
analogWrite(uled, 2);
delay((fadeTime * 60000)/50);
analogWrite(uled, 3);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 4);
delay((fadeTime * 60000)/50);
analogWrite(uled, 5);
delay((fadeTime * 60000)/50);
// Fade script
for (int i = 6 ; i <= 255; i++)
{
[b]analogWrite(uled, i);
delay(((fadeTime * 60000)/306));
Serial.print(" mil sec ");
Serial.print(((fadeTime * 60000)/306));
Serial.print(" PWM " );
Serial.print(i);
}
delay(20000); // Stay Bright
[b]analogWrite(uled, 0); // Turn off
}
is it anyhow possible to use something different
Maybe. If you understand what analogWrite() does, and the ESP32 doesn't have the hardware that implements that functionality, then you can't use analogWrite(), and you have to resort to bit-banging PWM using timers.
without changing too much
Just change the hardware.
Philipp_Gretscher:
AnalogWrite cant be used with the Esp32, but is it anyhow possible to use something different (without changing too much)
A quick Google search showed some workarounds. Start with the reported issue on GitHub: analogWrite() not implemented · Issue #4 · espressif/arduino-esp32 · GitHub