Notifications
Clear all

ESP32 - Wifi hotspots - and RTC modules

77 Posts
5 Users
22 Likes
6,024 Views
(@ajpatrick)
Member
Joined: 2 years ago
Posts: 16
Topic starter  

Hi guys,

new member here, and whilst it feels disingenuous to post my intro then follow it up only a few hours later with a cry for help, i guess that is kind of how forums like this work anyway. Hook them in with promises of questions answered, keep them in because they can't escape the rabbit hole of learning they have stumbled down. 🙂

 

My problem is this;

I recently 'made' an Out Of Class Pass Machine, to save me having to be interrupted multiple times a lesson to write out passes by hand. I originally made it using a thermal printer, an arduino uno and a ds3231 rtc.

It worked wonderfully, except when it didn't. The RTC would sometimes lose the time and start spewing out nonsense. So i thought maybe this is more about my dodgy soldering or bad code, so i got an arduino nano, and soldered everything directly. I also combed through the code to see if i had done something wrong there, but couldn't find a cuplrit.

In the meantime another teacher asked me to make one for them. Now i don't feel comfortable making something that i know may well break regularly and giving it as a gift, as that seems more like a curse, so i started thinking up ways to make it more reliable, and if possible, make the setting of the RTC an easy process that doesn't require uploading and reuploading code.

This is where the ESP32 comes in. I was able to bumble my way through getting it to work on the ESP32 in place of the arduino, so no wifi or bluetooth functions, just using it as a controller. The next step was to come up with a way to connect to it when needed and to have it update the rtc time with the right time.

The solution i came up with was to have it access my phone hotspot, and if connected, grab the time from an ntp server, update the rtc, then disconnect.

This works! sort of. I have done something where it will work perfectly IF it can connect to the wifi and grab the time, but it won't work at all if the network isn't there.

I feel like i am missing something obvious. I have messed around with if then else while in different ways. I have had it in the setup() but have now moved the wifi and rtc update routine into a button function so it only happens if i press the button. But the other button, which prints the ticket, wont work unless the wifi thing has happened. Im rather confused and need fresh eyes with more knowledge to offer some help.

So below is the code, there are possibly bit of redundant stuff in there as i have changed and rechanged things, i am not particularly fluent in any of this stuff, and i really feel there is something super simple and obvious i either don't know about or can't see. at the end of the day you dont know what you dont know, and that makes it hard to find solutions, so time to cry for help. 🙂

#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#include "Wire.h"
#include "time.h"
#include 
#include "RTClib.h";
#include ;
#include 

#define TX_PIN 1 // DFRobot GREEN WIRE labeled RX on printer
#define RX_PIN 2 // DFRobot YELLOW WIRE labeled TX on printer
#define btnPin1 4
#define btnPin2 5

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "us.pool.ntp.org", 10*3600);

RTC_DS3231 rtc;

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor

const char* ssid = "$$$$$$";
const char* password = "$$$$$$";

void RTC_Update(){
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
rtc.adjust(epochTime);
}

void setup() {
//button setup
pinMode(btnPin1,INPUT_PULLUP);
pinMode(btnPin2,INPUT_PULLUP);
// Printer data TO 27
pinMode(27, OUTPUT); digitalWrite(27, LOW);
//printer setup
mySerial.begin(9600);
printer.begin();
printer.sleep(); // Tell printer to sleep
delay(3000L); // Sleep for 3 seconds
printer.wake(); // MUST wake() before printing again, even if reset
printer.setDefault(); // Restore printer to default




}
void loop() {
//button for wifi update of time

int Push_button_state2 = digitalRead(btnPin2);
if ( Push_button_state2 == LOW ){
WiFi.begin(ssid, password);
delay(5000);
timeClient.begin();
delay(2000);
timeClient.update(); rtc.begin();
RTC_Update();
delay(2000);
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}

int Push_button_state = digitalRead(btnPin1);
if ( Push_button_state == LOW ){
DateTime now = rtc.now();
printer.println(F("------------------------------"));
printer.println(F(""));
printer.setSize('M');
printer.justify('C');
printer.println(F("OUT-OF-CLASS-PASS"));
printer.setSize('L');
printer.print(F("Date: "));
printer.print(now.day(), DEC);
printer.print("/");
printer.print(now.month(), DEC);
printer.println(F(""));
printer.setSize('M');
printer.println(F("This student has permission to"));
printer.println(F("leave the classroom."));
printer.justify('C');
char buffer [25];
uint8_t sec, min, hour;
sec = now.second();
min = now.minute();
hour = now.hour();
sprintf (buffer, "Left class at: %02u:%02u\n", hour, min);
printer.print (buffer);
printer.println(F("Pass valid for 5 minutes."));
printer.justify('L');
printer.println(F("Thank you,"));
printer.justify('C');
printer.doubleHeightOn();
printer.println(F(" MR. PATRICK "));
printer.println(F(""));
printer.doubleHeightOff();
printer.println(F(""));
printer.println(F("------------------------------"));
}


}

if this massive wordy post isn't clear i apologise, when you don't know a topic well its hard to speak about it clearly and concisely, so your patience is appreciated.

If anyone has the time and expertise to have a glance over the code and let me know if there is some obvious error, i would really appreciate it. Once i get it running i will then post a bit of info on the whole project for others to use and modify if they see a use for it. At the end of the day i have made a wireless printer with a set thing to print on command, with a clock. 🙂 not exactly ground breaking but wow i am learning alot. i want to do a circuit board with rechargable battery and stuff too to make it super helpful to anyone who asks for one, but that is still a way down the road as i have no idea how to approach those things.

 

Anyway, this is probably an overly long post so i will sign off now, but thanks for reading if you made it all the way through!

 


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@ajpatrick

I haven't used BLE or the ESP32, but if you have a smart phone with BLE, perhaps you could just request the time directly from the phone instead of requiring a network connection.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@ajpatrick)
Member
Joined: 2 years ago
Posts: 16
Topic starter  

@will Thanks for the suggestion. I will do some reading now to see if i can learn more about that approach in case it proves easier than trying to get this wifi solution working.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@ajpatrick Maybe I am misremembering but isn't the entire point of a RTC to remember the time as long as it has power? You set it once and as long as the battery in it is good the clock is accurate. I think the batteries are good for a couple years. That means setting the clock should be done once in the setup.

I will look at giving this a try tomorrow.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@ajpatrick)
Member
Joined: 2 years ago
Posts: 16
Topic starter  

@zander Hi Ron,

You are right, and that was the original design. no wifi or bluetooth, just the RTC set when the program was uploaded. But if i am giving it to someone else, who may leave the school or move away etc. and the battery fails they have no way of setting the time without getting the source code, and uploading it again. if it was just for me, i don't actually need to change it, but because i will be giving it to someone i don't want it to be difficult to use if the is an issue, like the time being wrong, because it is the main component of the whole device, without an accurate time the tickets are useless. so i am just being careful really.  I don't want it to check the ntp unless there is wifi connection avaiable is what i was thinking. so the teacher sees the time is wrong, presses a button which connects it to their phone and grabs the time, stores it in the rtc, then the device is good to go for months hopefully. does that make sense? i might not be being very clear sorry.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@ajpatrick My understanding is the time only has to be set once then as long as the battery is good it remains accurate. That means putting the code in the setup.

I will be happy to look at it but was up early, had a liquid lunch and too out of it now to concentrate.

Maybe if you can show us what is wrong, a screen shot is always best otherwise it's a bit of a guessing game.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@ajpatrick)
Member
Joined: 2 years ago
Posts: 16
Topic starter  

@zander certainly no rush to reply, i am greatful for any help, it doesn't have to be immediate. Perhaps if i rephrase the whole thing like this;

I currently have a device which consists of a thermal printer, an esp32, a ds3231 rtc, a button and a battery pack of 6x1.5v AA batteries.

When powered on, the esp32 responds to the button being pressed and prints out a ticket with some fixed info plus the current time and date.

this works well. when the battery fails i can replace it, re-upload the original code with the new time and it will be good for a year or two apart from replacing the AA batteries that run the printer and esp32.

a friend has asked me to make them one, and i am reluctant to do so unless i can work out a way to make resetting the time on the rtc after battery failure much easier. If they replace the battery the time will be wrong, and they aren't comfortable getting code and uploading it etc. so i am looking for a way that makes resetting the RTC painless. that is perhaps a more succinct way of putting it. My current attempt sees me adding a button which when pressed connects to my phone and grabs the time, but this isnt working properly and causing other issues. below is my working original code without any wifi stuff.

given it is on an esp32, i assume the solution would be either via wifi or bluetooth but i have no knowledge of either, and little enough of any coding.

i hope this makes the issue more clear and removes the confusion of my failed attempts and the subsequent issues.

#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#include "Wire.h"
#include "RTClib.h"

#define TX_PIN 1 // DFRobot GREEN WIRE labeled RX on printer
#define RX_PIN 2 // DFRobot YELLOW WIRE labeled TX on printer
#define btnPin1 4
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor





RTC_DS3231 rtc;

void setup() {
pinMode(btnPin1,INPUT_PULLUP);
// DFRobot BLUE WIRE TO 27
pinMode(27, OUTPUT); digitalWrite(27, LOW);

mySerial.begin(9600);


printer.begin(); // Init printer (same regardless of serial type)
printer.println(F("------------------------------"));

printer.sleep(); // Tell printer to sleep
delay(3000L); // Sleep for 3 seconds
printer.wake(); // MUST wake() before printing again, even if reset
printer.setDefault(); // Restore printer to default


// SETUP RTC MODULE
if (! rtc.begin()) {
printer.println("Couldn't find RTC");
while (1);
}
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop() {
DateTime now = rtc.now();
int Push_button_state = digitalRead(btnPin1);
if ( Push_button_state == LOW ){
printer.println(F("------------------------------"));
printer.println(F(""));
printer.setSize('M');
printer.justify('C');
printer.println(F("OUT-OF-CLASS-PASS"));
printer.setSize('L');
printer.print(F("Date: "));
printer.print(now.day(), DEC);
printer.print("/");
printer.print(now.month(), DEC);
printer.println(F(""));
printer.setSize('M');
printer.println(F("This student has permission to"));
printer.println(F("leave the classroom."));
printer.justify('C');
char buffer [25];
uint8_t sec, min, hour;
sec = now.second();
min = now.minute();
hour = now.hour();
sprintf (buffer, "Left class at: %02u:%02u\n", hour, min);
printer.print (buffer);
printer.println(F("Pass valid for 5 minutes."));
printer.justify('L');
printer.println(F("Thank you,"));
printer.justify('C');
printer.doubleHeightOn();
printer.println(F(" MR. PATRICK "));
printer.println(F(""));
printer.doubleHeightOff();
printer.println(F(""));
printer.println(F("------------------------------"));
}
}

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@ajpatrick 

You'll still have a problem with WIFI unless you and your friend use the same ID and password.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
darup
(@darup)
Member
Joined: 2 years ago
Posts: 63
 

The RTC I acquire from HiLegto on amazon seem to be stable and operate rather reliably in the use I have for them (RPi B, B+, 4; not have had a need for the Pico yet).

Two pieces that have seemed to have gone unmentioned:  "https://www.nist.gov" and Cron in what I sense to be your quest for an accurate time source.

Since you have covered all the other thoughts I might have had what about the two I mentioned.

NIST offers time for free 24/7/365.  Maybe like me you have COTS clocks sitting around doing little or nothing more than collecting dust.  If you do, you may have an instant resource to use as your accurate time source.  Most of the "Atomic" clocks have an extremely sensitive receiver and a very accurate time piece.  My recollection is the internet has many examples how one might access the innards of the "Atomic" clock to collect the time you want IF you are into hardware.

Cron is an extremely useful piece of software if you might be using a chip that has a linux flavor O/S.  It can be optioned to start at power up; it can be left running in the background; and it can launch other software when properly setup and optioned.

I think the RTC is the better of the choices if the verion being used is correctly setUp and properly optioned along with a fresh powerCell.

NTP would be my second choice though it does require a network connection to be of any value.

If you are into hardware and have available an "Atomic" clock that uses the NIST time stardard (the Navy has another one that is a whole lot more interesting that includes the air almanac) I would be inclined to give it a try since the writeUp suggest something stationary without "largness" restrictions.

Finally, I know nothing about an ESP-32.  I am learning Python and the RPi Pico.  I prefer 'c', assembler, Java ... !  So whatever has been written by me is at best something to be taken under advisement with maybe a grain of salt!

Possibly, this might in someway jiggle a thought?

吉姆 | 짐 | ジム | Джим | ဂျင်မ် ਜਿੰਮ | Pīšlis | জিম | រមមមមុយ


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@will Will is 100% correct, you @ajpatrick think you have a code or perhaps an implementation issue,, but in fact you have a design issue.

If you want to base the design on local time of day you need a way to enter a time when the CR2032 dies after a couple of years.

OR

If a 'rough' time will suffice, say +- a minute then forget WiFi or any radio method of getting the time and just enter it by keyboard. Yes you need to connect a keyboard every couple years so that is the 'con' of that method.

Maybe there is a photographic method,

Maybe an RFID method,

others?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@ajpatrick)
Member
Joined: 2 years ago
Posts: 16
Topic starter  

@will i can set this up for them initially, but of course the problem will still be there if they ever change their hotspot details. i am thinking a webserver in softap mode might be a way around that eventually so when it turns on if there are no ssid and pass details saved it will create a soft ap, then they could login to it, set the wifi details, restart and it will connect to their phone and update. but that is well above my head right now.

 

edit; this would also be a way of setting the time, but i have no idea how to take entries in a web server and use them to set the rtc. there are several possible solutions all of which i currently lack the knowledge for, but that is of course the whole point of the process.

In terms of design issue, oh yeah, this is very true. one of the things i love about getting others involved is the different ways to solve a problem that become apparent. Its a fantastic process i think, and as a teacher, i love getting those 'aha!' moments myself just as much as i love seeing kids have them in class. 🙂


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @ajpatrick

@will i can set this up for them initially, but of course the problem will still be there if they ever change their hotspot details.

They may not be so eager to share passwords 🙂

Anything seems possible when you don't know what you're talking about.


   
AJPatrick reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@zander FYI @ajpatrick

Ron, have you ever used BLE ? Could it connect to a smart phone and request the time from it ? Everybody has a cell phone now, and they always reset their internal time regularly, so it seems like it may provide a potential cure for resetting the time.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6895
 

@will No BLE experience. I am thinking WiFiMgr, that way no need to know creds up front. MNy WiFi is super flaky right now but check Bill's video on how to add WiFiMgr to any existing sketch. NOTE, ALL the wifi code needs to be in the setup section NOT the loop. Once the RTC is init it is good for a couple years. See 23:59

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
AJPatrick reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@zander FYI @ajpatrick

Wouldn't that require all of the people wanting to use the sketch having to upload their info to keep it all private ?

And, unless it becomes officially cancelled (let us hope), there's still Daylight Saving Time to consider requiring a time change twice a year, or else that needs to be built into the sketch.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Page 1 / 6