element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Help Please
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 4 replies
  • Subscribers 676 subscribers
  • Views 534 views
  • Users 0 members are here
  • help
  • pi
  • timer
  • raspberry_pi
  • raspberrypi
  • counter
Related

Help Please

giusepperamos
giusepperamos over 11 years ago

I used the raspberry pi to supervise a motel room status. My code is working fine, but I need to restart the count at specific time every day. please help me with that.

 

here is my script in Python.

 

 

 

# Librerias

 

 

import MySQLdb

import time

import datetime

import sys

import subprocess

import re

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)

 

 

# VARIABLES

input = GPIO.input(7)

rampa1 = 1

sucursal = ("Las Vegas")

ent = ("Entrada")

sal = ("Salida")

counts1 = 0

variable1 = 0

valorsalida = "*"

 

 

#Variables de Base de Datos

 

 

server = ("server")

DB = ("db")

password = ("password")

username = ("username")

 

 

# Prueba de coneccion a base de datos

 

 

# Ecuaciones

 

 

 

 

def emailerror():# Envio de Email en caso de error de coneccion

                import smtplib

                try:

                        SMTP_SERVER = 'smtp.gmail.com'

                        SMTP_PORT = 587

 

 

                        sender = 'pruebaraspberry@gmail.com'

                        recipient = 'aaguilar@agflosa.com, ramosgiuseppe@gmail.com'

                        subject = 'Error en conectar a la base de datos'

                        body = 'Revisar coneccion con el servidor de base de datos'

 

 

                        "Sends an e-mail to the specified recipient."

 

 

                        body = "" + body + ""

 

 

                        headers = ["From: " + sender,

                                          "Subject: " + subject,

                                           "To: " + recipient,

                                           "MIME-Version: 1.0",

                                           "Content-Type: text/html"]

                        headers = "\r\n".join(headers)

 

 

                        session = smtplib.SMTP("smtp.gmail.com",587)

 

 

                        session.ehlo()

                        session.starttls()

                        session.ehlo

                        session.login("pruebaraspberry@gmail.com","pepe2488")

 

 

                        session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)

                        session.quit()

                        print("Revisar coneccion con el servidor")

                except:

                        print ("revise coneccion a internet")

def coneccionentrada():# Coneccion de Entrada

  try:

 

 

                conn = MySQLdb.connect(server,DB,password,username)

                conn.cursor().execute("INSERT INTO contador (Camiones, fecha, Tiempo, Rampa, Sucursal, EOS) VALUES (%s, %s, %s, %s, %s,%s)",

  (counts1, dateentrada, timeentrada, rampa1, sucursal, ent))

                print ("Datos de Entrada Enviados")

  except:

                print ("los datos no pudieron enviarse")

def coneccionsalida():# Coneccion de Salida

 

 

        try:

                conn = MySQLdb.connect(server,DB,password,username)

                conn.cursor().execute("INSERT INTO contador (Camiones, fecha, Tiempo, Rampa, Sucursal, EOS) VALUES (%s, %s, %s, %s, %s, %s)",

  (valorsalida, datesalida, timesalida, rampa1, sucursal, sal))

                print ("Datos de Salida Enviados")

        except:

                print ("los datos no pudieron enviarse")

 

 

 

 

# Inicio del Programa

 

 

 

 

try:# Prueba de Conneccion

        print ("Conectandose a la Base de Datos")

        conn = MySQLdb.connect(server,DB,password,username)

        print ("Conectado con el Servidor")

except:

    emailerror()

 

 

# Inicio de Loop

while (True):

 

 

# Entrada de Auto Habitacion  1 #

 

 

   GPIO.wait_for_edge(7,GPIO.FALLING)

   time.sleep(180)

  

   if (input == GPIO.LOW) and (variable1==0):

 

 

        timeentrada = time.strftime("%H:%M:%S")

        dateentrada = time.strftime("%Y.%m.%d")

        counts1 += 1

        variable1 += 1

        print ("ENTRADA RAMPA 1")

        print (counts1)

 

 

   if(variable1==1):

        print ("Entrada Contada")

        coneccionentrada()

        variable1 += 1

 

 

# Salida de Auto Habitacion  1 #

 

 

   if (input == GPIO.HIGH) and (variable1==2):

        datesalida = time.strftime("%Y.%m.%d")

        timesalida = time.strftime("%H:%M:%S")

        coneccionsalida()

        variable1 = 0

  • Sign in to reply
  • Cancel
Parents
  • mconners
    0 mconners over 11 years ago

    This may seem obvious, but in your while loop compare the time and reset counts to zero if it falls within a range.

     

    What is the issue that you are having? Do you need help with the logic? Or the syntax?

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • mconners
    0 mconners over 11 years ago

    This may seem obvious, but in your while loop compare the time and reset counts to zero if it falls within a range.

     

    What is the issue that you are having? Do you need help with the logic? Or the syntax?

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • giusepperamos
    0 giusepperamos over 11 years ago in reply to mconners

    I need the variable counts1 returns to 0 at 7:00 am.

     

    I dot know here to put the time comparison .... because if the script is in the line "wait for an edge" it will never get back to 0 value.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • fvan
    0 fvan over 11 years ago in reply to giusepperamos

    Perhaps too brutal, but I'll propose it anyway:

     

    You could use cron to kill and restart the script at 7am every day. Won't matter in which state the script is.

     

    Frederick

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 11 years ago in reply to giusepperamos

    Maybe take a look at

     

    https://docs.python.org/2/library/signal.html#signal.setitimer

     

    Or google python timer interrupts.

     

    You should be able to accomplish what you need. Or as Frederick says, you could always reset it externally with cron.

     

    You wouldn't even need to kill it fully, you could kill it with a signal (like kill -1) and look for the signal in your program that way you could handle it gracefully.

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube