• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Micropython RTC

Q&A Forum › Category: ESP32 › Micropython RTC
0 Vote Up Vote Down
self.raymond asked 6 years ago

I want to be able to turn relays on and off by time.  Day Weeks and Hours. This is a plant watering system..  I wrote the code in Arduino now I want to write it in Micro-python.  It start by declaring integers for arrays.  The following is the Arduino Code Any comments would be appreciated

int OnMin1 [] = {17, 18, 19, 20};
int OnHour1 [] = {17, 18, 19, 20}; //= {2, 4, 8, 3, 6}; //[]= {17,18,19,20};//Array multi hour
int OnDay1 [] = {17, 18, 19, 20}; //OnHour1 = 3; //[]= {17,18,19,20};//Array multi hour
int OnMonth1 [] = {1, 5, 6, 6};
int OnYear1 [] = {2018.2019};
int OffMin1 [] = {17, 18, 19, 20};
int OffHour1 [] = {17, 18, 19, 20};
int OffDay1 [] = {8, 18, 19, 20};
int OffMonth1 [] = {1, 5, 6, 7};
int OffYear1 [] = {2018, 2019,};

The following Arduino Code turns relay on and off

delay(500);//Relay 0
for (int i = 0; i < sizeof(OnHour1) / sizeof(OnHour1[0]); i++)
for (int i = 0; i < sizeof(OnMin1) / sizeof(OnMin1[0]); i++)
for (int i = 0; i < sizeof(OnDay1) / sizeof(OnDay1[0]); i++)
for (int i = 0; i < sizeof(OnMonth1) / sizeof(OnMonth1[0]); i++) //This will turn one relay on and off
if ((now.hour() == OnHour1[i]) && (now.minute() == OnMin1[i]) && (now.day() == OnDay1[i]) && (now.month() == OnMonth1[i])) {
mcp.digitalWrite(0, LOW);
display1.setTextSize(2);
display1.setTextColor(WHITE);
display1.setCursor(0, 48);
display1.println("ON");
display1.display();
Serial.print("RELAY 1 ON");
}
else if ((now.hour() == OffHour1[i]) && (now.minute() == OffMin1[i]) && (now.day() == OffDay1[i]) && (now.month() == OffMonth1[i])) {
mcp.digitalWrite(0, HIGH);
display1.setTextSize(2);
display1.setTextColor(WHITE);
display1.setCursor(0, 48);
display1.println("OFF");
display1.display();
Serial.print("RELAY 1 OFF");
2 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Ray.

I’m sorry, but I can’t create custom code for you, I hope you understand because we receive hundreds of requests per week. I can give you some directions on how you can write your code in MicroPython.

I see that you’re controlling some relays and displaying the state on an oled display.

The latest version of our MicroPython eBook, shows how to use an OLED display on page 275.

Controlling a relay is as simple as controlling an output. You can see the “Blinking LED” Unit – page 84. When using relays, please make sure that you chose “safe to use” GPIOs (GPIOs that don’t output signals on boot, as I’ve told you here: https://rntlab.com/question/when-rebooting-turns-relays-on/

For the RTC, you can use the library you’ve told you here: https://rntlab.com/question/ds3231/

To deal with arrays with MicroPython, you can see this documentation: https://docs.micropython.org/en/latest/library/array.html

Alternatively, instead of arrays, you can use lists. This tutorial explains pretty well how to deal with lists: https://techtutorialsx.com/2017/08/12/esp32-esp8266-micropython-working-with-lists/

I hope this helps. Thank you for your interest in our work.

Regards,
Sara

0 Vote Up Vote Down
self.raymond answered 6 years ago

Thank You will check it out. 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.