Skip to content

Instantly share code, notes, and snippets.

@kRHYME7
Last active March 7, 2024 07:13
Show Gist options
  • Save kRHYME7/bb293af23e292db0be4ffdffbdbc8cd7 to your computer and use it in GitHub Desktop.
Save kRHYME7/bb293af23e292db0be4ffdffbdbc8cd7 to your computer and use it in GitHub Desktop.
Team OA
give me a code that will make the three servo motors turn on at 7am, 12pm, and 3pm
import time
from machine import Pin, PWM
import esp32
# ESP32 PWM pins
servo1_pin = 18
servo2_pin = 19
servo3_pin = 21
# Create PWM channels
pwm1 = PWM(Pin(servo1_pin))
pwm2 = PWM(Pin(servo2_pin))
pwm3 = PWM(Pin(servo3_pin))
# Feed times
feed_times = [7, 12, 15]
while True:
current_hour = time.localtime().tm_hour
current_min = time.localtime().tm_min
if current_hour in feed_times:
if current_min == 0:
# Turn servos on
pwm1.duty(90)
pwm2.duty(90)
pwm3.duty(90)
# Wait 5 seconds
time.sleep(5)
# Turn servos off
pwm1.duty(0)
pwm2.duty(0)
pwm3.duty(0)
time.sleep(60)
please create a code that has three hx711 load cells that tracts weight in grams, the weight should be abound 100 to 110 grams, and sends notification to the owner about the weight of that the hx711 load cell has detected. please make this code compatible with esp32 board
import esp32
import hx711
from twilio.rest import Client
# Load cells
lc1 = hx711.HX711(dout=4, pd_sck=18)
lc2 = hx711.HX711(dout=5, pd_sck=19)
lc3 = hx711.HX711(dout=26, pd_sck=27)
# Calibrate load cells
lc1.set_scale_ratio(2280)
lc2.set_scale_ratio(1930)
lc3.set_scale_ratio(1790)
# Twilio client
client = Client(...)
# Phone number
to_number = '+1234567890'
while True:
# Get individual weights
w1 = lc1.get_weight_grams()
w2 = lc2.get_weight_grams()
w3 = lc3.get_weight_grams()
# Print individual weights
print(w1)
print(w2)
print(w3)
# Check if any below threshold
if w1 < 100 or w2 < 100 or w3 < 100:
# Send message with all weights
message = f"w1: {w1} g\nw2: {w2} g\nw3: {w3} g"
client.messages.create(to=to_number,
from_='+1987654321',
body=message)
please give me a code about the water sensor detects that the water level is low and will send signal to the water pump to pump water for 15 seconds. please make this code compatible with esp32
import esp32
import time
from machine import Pin, PWM
# Water level sensor on GPIO 34
water_sensor = Pin(34, Pin.IN)
# Water pump on GPIO 23
pump = PWM(Pin(23))
while True:
# Check if water level is low
if water_sensor.value() == 0:
print("Water level low, turning pump on")
# Turn pump on for 15 seconds
pump.duty(512)
time.sleep(15)
# Turn pump off
pump.duty(0)
print("Pump off")
time.sleep(60)
please make a code that switches to the back up battery. whenever it detects that the main power is out. please make this code compatible to esp32
import esp32
from machine import Pin
import time
# Backup battery enable pin
backup_enable = Pin(14, Pin.OUT)
# Main power status pin
power_status = Pin(12, Pin.IN)
while True:
# Check main power status
if power_status.value() == 0:
print("Main power outage detected!")
# Turn backup battery on
backup_enable.value(1)
print("Backup battery switched on")
else:
print("Main power restored")
# Turn off backup
backup_enable.value(0)
time.sleep(5)
Integrated
import time
from machine import Pin, PWM
import esp32
import hx711
from twilio.rest import Client
# ESP32 PWM pins
servo1_pin = 18
servo2_pin = 19
servo3_pin = 21
# Create PWM channels
pwm1 = PWM(Pin(servo1_pin))
pwm2 = PWM(Pin(servo2_pin))
pwm3 = PWM(Pin(servo3_pin))
# Feed times
feed_times = [7, 12, 15]
# Load cells
lc1 = hx711.HX711(dout=4, pd_sck=18)
lc2 = hx711.HX711(dout=5, pd_sck=19)
lc3 = hx711.HX711(dout=26, pd_sck=27)
# Calibrate load cells
lc1.set_scale_ratio(2280)
lc2.set_scale_ratio(1930)
lc3.set_scale_ratio(1790)
# Twilio client
client = Client(...)
# Phone number
to_number = '+1234567890'
# Water level sensor on GPIO 34
water_sensor = Pin(34, Pin.IN)
# Water pump on GPIO 23
pump = PWM(Pin(23))
# Backup battery enable pin
backup_enable = Pin(14, Pin.OUT)
# Main power status pin
power_status = Pin(12, Pin.IN)
while True:
# Check if water level is low
if water_sensor.value() == 0:
print("Water level low, turning pump on")
# Turn pump on for 15 seconds
pump.duty(512)
time.sleep(15)
# Turn pump off
pump.duty(0)
print("Pump off")
# Get individual weights
w1 = lc1.get_weight_grams()
w2 = lc2.get_weight_grams()
w3 = lc3.get_weight_grams()
# Print individual weights
print(w1)
print(w2)
print(w3)
# Check if any below threshold
if w1 < 100 or w2 < 100 or w3 < 100:
# Send message with all weights
message = f"w1: {w1} g\nw2: {w2} g\nw3: {w3} g"
client.messages.create(to=to_number,
from_='+1987654321',
body=message)
current_hour = time.localtime().tm_hour
current_min = time.localtime().tm_min
if current_hour in feed_times:
if current_min == 0:
# Turn servos on
pwm1.duty(90)
pwm2.duty(90)
pwm3.duty(90)
# Wait 5 seconds
time.sleep(5)
# Turn servos off
pwm1.duty(0)
pwm2.duty(0)
pwm3.duty(0)
# Check main power status
if power_status.value() == 0:
print("Main power outage detected!")
# Turn backup battery on
backup_enable.value(1)
print("Backup battery switched on")
else:
print("Main power restored")
# Turn off backup
backup_enable.value(0)
time.sleep(5)
/*
MIT License
Copyright (c) 2021 Felix Biego
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <ESP32Time.h>
ESP32Time rtc;
ESP32Time rtc1(-3600); // offset GMT-1
ESP32Time rtc2(7200); // offset GMT+2
void setup() {
Serial.begin(115200);
rtc.setTime(0, 00, 00, 01, 01, 2024); // 17th Jan 2021 15:24:30
//rtc1.setTime(1609459200); // 1st Jan 2021 00:00:00
// time can be set on one instance
// no need for rtc1.setTime() or rtc2.setTime()
}
void remind_me() {
// Serial.println(rtc.getTime()); // (String) 15:24:38
// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021
// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021
// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38
// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
//
// Serial.println(rtc.getMicros()); // (unsigned long) 723546
// Serial.println(rtc.getMillis()); // (unsigned long) 723
// Serial.println(rtc.getEpoch()); // (unsigned long) 1609459200
Serial.println(rtc.getSecond()); // (int) 38 (0-59)
Serial.println(rtc.getMinute()); // (int) 24 (0-59)
// Serial.println(rtc.getHour()); // (int) 3 (0-12)
Serial.println(rtc.getHour(true)); // (int) 15 (0-23)
// Serial.println(rtc.getAmPm()); // (String) pm
// Serial.println(rtc.getAmPm(true)); // (String) PM
Serial.println(rtc.getDay()); // (int) 17 (1-31)
// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6)
// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365)
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
// Serial.println(rtc.getYear()); // (int) 2021
Serial.println(rtc.getTime("RTC0: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
Serial.println(rtc1.getTime("RTC1: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
Serial.println(rtc2.getTime("RTC2: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
// formating options http://www.cplusplus.com/reference/ctime/strftime/
Serial.println(rtc.getEpoch()); // (unsigned long)
Serial.println(rtc1.getEpoch()); // (unsigned long)
Serial.println(rtc2.getEpoch()); // (unsigned long)
Serial.println(rtc.getLocalEpoch()); // (unsigned long) epoch without offset, same for all instances
delay(1000);
}
void is_alarm() {
// Example alarm time: 7:00 AM
int alarmHour = 7;
int alarmMinute = 0;
// Get current time
int currentSeconds = rtc.getSecond(); // (int) 38 (0-59)
int currentHour = rtc.getHour();
int currentMinute = rtc.getMinute();
// Check if it's time for the alarm
// if (currentHour == alarmHour && currentMinute == alarmMinute) {
// // Execute alarm action
// Serial.println("Alarm triggered!");
// // Here you can add code to perform an action, like turning on a light
// }
if (currentSeconds == 10 ) {
Serial.println("Ay okay");
}
else if ( currentSeconds == 20 ) {
Serial.println("Ay okay 2.0");
}
else if ( currentSeconds == 30 ) {
Serial.println("ohhhhhhhhhhhhh yeah 3.0");
}
else {
Serial.println(rtc.getSecond()); // (int) 38 (0-59)
}
}
void loop() {
// remind_me();
is_alarm();
// Serial.println("...");
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
delay(1000); // Check every minute
}
+ https://www.youtube.com/watch?v=qKlYvjXJlIs
#TODO
+ Use RTC and make an alarm system that runs in the background (Services/ Daemons.
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment