#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Beginner need help with something relatively simple but can't find what I need online


    Hi everyone, I live in France and I'm sorry for all the French in the code, etc. I could really use some help, I'm a complete beginner and have spent weeks looking for solutions. I've completed my project but it's just so primitive, I'd like to do it properly.
    I have a school project that consists of receiving data sent by a cell phone, processing ('translating') it and sending it to a website.
    The data comes from a device measuring data from a beehive 5 times a day.

    I've broken it down into two main parts each consisting of three sub-parts for you. 'What I need to do' and 'How I do it'.

    1- What I need do:

    a. Receiving:
    I collect the data with a GSM module connected to the computer and I use HyperTerminal to send AT commands to the module. The data is then stored in a .txt file.

    b. Processing:
    The data for one text message looks like this:
    OK
    at+cmgd=at+cmgf=1

    OK
    at+cmgr=1
    +CMGL: 1,"REC READ","+4915110451969",,"13/01/20,21:06:26+04"
    12,6V
    05/00R+21C
    001,5Kg41%+21C
    13/00R+21C
    001,4Kg42%+21C
    15/00R+21C
    001,4Kg43%+21C
    19/00R+22C
    001,5Kg41%+22C
    21/00R+22C
    001,5Kg40%+22C!
    E06,32
    A second text would just look the same (without the AT commands) added at the end starting with:
    +CMGL: 2,"REC READ","+4915110451969",,"13/01/20,21:06:26+04"
    It then has to look something like this:
    20/01/13
    SMS N° 5


    Mesure effectuée à 05 h

    Poids : 001,5Kg
    41% d'humidité
    Température à l'intérieur de la ruche : +21C
    Température extérieure : +21C
    00 mm de pluie


    Mesure effectuée à 13 h

    Poids : 001,4Kg
    42% d'humidité
    Température à l'intérieur de la ruche : +21C
    Température extérieure : +21C
    00 mm de pluie


    Mesure effectuée à 15 h

    Poids : 001,4Kg
    43% d'humidité
    Température à l'intérieur de la ruche : +21C
    Température extérieure : +21C
    00 mm de pluie


    Mesure effectuée à 19 h

    Poids : 001,5Kg
    41% d'humidité
    Température à l'intérieur de la ruche : +22C
    Température extérieure : +22C
    00 mm de pluie


    Mesure effectuée à 21 h

    Poids : 001,5Kg
    40% d'humidité
    Température à l'intérieur de la ruche : +22C
    Température extérieure : +22C
    00 mm de pluie


    Charge de batterie restante : 12,6V

    Crédit restant : 06,32 €
    Sending:
    I then send all the Processed data to a wordpress.com blog and a picture of graphs that other people in my group are working on.

    How I do it:

    Receiving:
    The solution I found relies on a program called Super Macro that opens and types the AT command in HyperTerminal for the GSM module it then opens my mess of a program that looks like this:
    (I start at the end incase there are multiple text messages and then work my way up)
    Processing:
    Code:
    import os
    
    #opens a reads CAPTURE.TXT (where the data from the text message is stored)
    fichier_capture = open("CAPTURE.TXT", 'r')
    ligne = fichier_capture.readlines()
    
    #Checks the first character from line -3 (to see if there's the data from the text message)
    ligneCondition = ligne[-3]
    caractereCondition = ligneCondition[0]
    
    #Finds the text message number
    ligneMultiSms = ligne[-15]
    multiSMS = ligneMultiSms[7:9]
    
    #If the 1st character from line -3 != to E... 
    if caractereCondition != "E":
         print("Pas de nouveaux messages")
    
    elif multiSMS != "1,":
         #Date data   
         date = ligne[-15]
         anneeDate = date[39:41]
         moisDate = date[42:44]
         jourDate = date[45:47]
         #Data from 5 o' clock
         mesure05h_1 = ligne[-13]
         mesure05h_2 = ligne[-12]
         heure_mesure05h = mesure05h_1[0:2]
         pluie_mersure05h = mesure05h_1[3:5]
         temperature_ext05h = mesure05h_1[6:10]
         temperature_int05h = mesure05h_2[10:14]
         poids05h = mesure05h_2[0:7]
         humidite05h = mesure05h_2[7:10]
         #data from 1PM 
         mesure13h_1 = ligne[-11]
         mesure13h_2 = ligne[-10]
         heure_mesure13h = mesure13h_1[0:2]
         pluie_mersure13h = mesure13h_1[3:5]
         temperature_ext13h = mesure13h_1[6:10]
         temperature_int13h = mesure13h_2[10:14]
         poids13h = mesure13h_2[0:7]
         humidite13h = mesure13h_2[7:10]
         #Data from 3PM
         mesure15h_1 = ligne[-9]
         mesure15h_2 = ligne[-8]
         heure_mesure15h = mesure15h_1[0:2]
         pluie_mersure15h = mesure15h_1[3:5]
         temperature_ext15h = mesure15h_1[6:10]
         temperature_int15h = mesure15h_2[10:14]
         poids15h = mesure15h_2[0:7]
         humidite15h = mesure15h_2[7:10]
         #Data from 7PM
         mesure19h_1 = ligne[-7]
         mesure19h_2 = ligne[-6]
         heure_mesure19h = mesure19h_1[0:2]
         pluie_mersure19h = mesure19h_1[3:5]
         temperature_ext19h = mesure19h_1[6:10]
         temperature_int19h = mesure19h_2[10:14]
         poids19h = mesure19h_2[0:7]
         humidite19h = mesure19h_2[7:10]
         #Data from 9PM
         mesure21h_1 = ligne[-5]
         mesure21h_2 = ligne[-4]
         heure_mesure21h = mesure21h_1[0:2]
         pluie_mersure21h = mesure21h_1[3:5]
         temperature_ext21h = mesure21h_1[6:10]
         temperature_int21h = mesure21h_2[10:14]
         poids21h = mesure21h_2[0:7]
         humidite21h = mesure21h_2[7:10]
         #Remaining battery charge
         euros = ligne[-3]
         sousEuros = euros[1:6]
         batterie = ligne[-14]
         #prints tranlsated data for the date
         print(jourDate,"/",moisDate,"/",anneeDate)
         print("SMS N°", multiSMS,'\n','\n')
         #Prints tranlsated data from 5AM
         print("Mesure effectuée à", heure_mesure05h, "h",'\n')
         print("Poids :", poids05h)
         print(humidite05h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int05h)
         print("Température extérieure :", temperature_ext05h)
         print(mesure05h_1[3:5], "mm de pluie"'\n','\n') 
         #Prints tranlsated data from 1PM
         print("Mesure effectuée à", heure_mesure13h, "h", '\n')
         print("Poids :", poids13h)
         print(humidite13h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int13h)
         print("Température extérieure :", temperature_ext13h)
         print(mesure13h_1[3:5], mm de pluie"'\n','\n')
         #Prints tranlsated data from 3PM
         print("Mesure effectuée à", heure_mesure15h, "h", '\n')
         print("Poids :", poids15h)
         print(humidite15h, "d'humidité")[/highlight]
         print("Température à l'intérieur de la ruche :", temperature_int15h)
         print("Température extérieure :", temperature_ext15h)
         print(mesure15h_1[3:5], "mm de pluie"'\n','\n')
         #Print tranlsateds data from 7PM
         print("Mesure effectuée à", heure_mesure19h, "h", '\n')
         print("Poids :", poids19h)
         print(humidite19h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int19h)
         print("Température extérieure :", temperature_ext19h)
         print(mesure19h_1[3:5], "mm de pluie"'\n','\n')
         #Prints tranlsated data from 9PM
         print("Mesure effectuée à", heure_mesure21h, "h", '\n')
         print("Poids :", poids21h)
         print(humidite21h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int21h)
         print("Température extérieure :", temperature_ext21h)
         #Prints processed data for the remaining battery charge a phone credits
         print(mesure21h_1[3:5], "mm de pluie"'\n','\n')
         print("Charge de batterie restante :", batterie)
         print("Crédit restant :", sousEuros, "€",'\n','\n')
         print("--------------------------------------------------------------------------", '\n')
         
    #Ferme de fichier CAPTURE.TXT
    fichier_capture.close()
    #--------------------------------------------------------------------------
    #--------------------------------------------------------------------------
    
    #Opens file.txt and writes all the raw data from CAPTURE.TXT except the last text message
    sms2w = open("file.txt",'w')
    sms2w.writelines([item for item in ligne[:-15]])
    sms2w.close()
    #--------------------------------------------------------------------------
    #--------------------------------------------------------------------------
    
    #Opens file.txt and does the same operation as before, collecting and processing
    sms2r = open("file.txt",'r')
    ligneSms2 = sms2r.readlines()
    
    if multiSMS != "1,":
    
         ligneMultiSms = ligneSms2[-13]
         multiSMS = ligneMultiSms[7:9]
                   
         date = ligneSms2[-13]
         anneeDate = date[39:41]
         moisDate = date[42:44]
         jourDate = date[45:47]
    
         mesure05h_1 = ligneSms2[-11]
         mesure05h_2 = ligneSms2[-10]
         heure_mesure05h = mesure05h_1[0:2]
         pluie_mersure05h = mesure05h_1[3:5]
         temperature_ext05h = mesure05h_1[6:10]
         temperature_int05h = mesure05h_2[10:14]
         poids05h = mesure05h_2[0:7]
         humidite05h = mesure05h_2[7:10]
    
         mesure13h_1 = ligneSms2[-9]
         mesure13h_2 = ligneSms2[-8]
         heure_mesure13h = mesure13h_1[0:2]
         pluie_mersure13h = mesure13h_1[3:5]
         temperature_ext13h = mesure13h_1[6:10]
         temperature_int13h = mesure13h_2[10:14]
         poids13h = mesure13h_2[0:7]
         humidite13h = mesure13h_2[7:10]
    
         mesure15h_1 = ligneSms2[-7]
         mesure15h_2 = ligneSms2[-6]
         heure_mesure15h = mesure15h_1[0:2]
         pluie_mersure15h = mesure15h_1[3:5]
         temperature_ext15h = mesure15h_1[6:10]
         temperature_int15h = mesure15h_2[10:14]
         poids15h = mesure15h_2[0:7]
         humidite15h = mesure15h_2[7:10]
    
         mesure19h_1 = ligneSms2[-5]
         mesure19h_2 = ligneSms2[-4]
         heure_mesure19h = mesure19h_1[0:2]
         pluie_mersure19h = mesure19h_1[3:5]
         temperature_ext19h = mesure19h_1[6:10]
         temperature_int19h = mesure19h_2[10:14]
         poids19h = mesure19h_2[0:7]
         humidite19h = mesure19h_2[7:10]
    
         mesure21h_1 = ligneSms2[-3]
         mesure21h_2 = ligneSms2[-2]
         heure_mesure21h = mesure21h_1[0:2]
         pluie_mersure21h = mesure21h_1[3:5]
         temperature_ext21h = mesure21h_1[6:10]
         temperature_int21h = mesure21h_2[10:14]
         poids21h = mesure21h_2[0:7]
         humidite21h = mesure21h_2[7:10]
    
         euros = ligneSms2[-1]
         sousEuros = euros[1:6]
         batterie = ligneSms2[-12]
    
         print(jourDate,"/",moisDate,"/",anneeDate)
         print("SMS N°", multiSMS,'\n','\n')
         print("Mesure effectuée à", heure_mesure05h, "h",'\n')
         print("Poids :", poids05h)
         print(humidite05h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int05h)
         print("Température extérieure :", temperature_ext05h)
         print(mesure05h_1[3:5], "mm de pluie"'\n','\n') 
             
         print("Mesure effectuée à", heure_mesure13h, "h", '\n')
         print("Poids :", poids13h)
         print(humidite13h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int13h)
         print("Température extérieure :", temperature_ext13h)
         print(mesure13h_1[3:5], "mm de pluie"'\n','\n')
    
         print("Mesure effectuée à", heure_mesure15h, "h", '\n')
         print("Poids :", poids15h)
         print(humidite15h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int15h)
         print("Température extérieure :", temperature_ext15h)
         print(mesure15h_1[3:5], "mm de pluie"'\n','\n')
             
         print("Mesure effectuée à", heure_mesure19h, "h", '\n')
         print("Poids :", poids19h)
         print(humidite19h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int19h)
         print("Température extérieure :", temperature_ext19h)
         print(mesure19h_1[3:5], "mm de pluie"'\n','\n')
             
         print("Mesure effectuée à", heure_mesure21h, "h", '\n')
         print("Poids :", poids21h)
         print(humidite21h, "d'humidité")
         print("Température à l'intérieur de la ruche :", temperature_int21h)
         print("Température extérieure :", temperature_ext21h)
         print(mesure21h_1[3:5], "mm de pluie"'\n','\n')
         print("Charge de batterie restante :", batterie)
         print("Crédit restant :", sousEuros, "€",'\n','\n')
         print("--------------------------------------------------------------------------", '\n')
    sms2r.close()
    #--------------------------------------------------------------------------
    #--------------------------------------------------------------------------
    
    #As long as the program doesn't reache the 1st text message, keep going
    while multiSMS !="1,":
         #Opens file2.txt in write mode and writes file.txt except the las text message
         sms3w = open("file2.txt",'w')
         sms3w.writelines([item for item in ligneSms2[:-13]])
         sms3w.close()
         #--------------------------------------------------------------------------
         #--------------------------------------------------------------------------
    
         #Opens file2.txt and does the same collecting and processing operation
         sms3r = open("file2.txt",'r')
         ligneSms = sms3r.readlines()
    
         if multiSMS != "1,":
    
              ligneMultiSms = ligneSms[-13]
              multiSMS = ligneMultiSms[7:9]
                        
              date = ligneSms[-13]
              anneeDate = date[39:41]
              moisDate = date[42:44]
              jourDate = date[45:47]
    
              mesure05h_1 = ligneSms[-11]
              mesure05h_2 = ligneSms[-10]
              heure_mesure05h = mesure05h_1[0:2]
              pluie_mersure05h = mesure05h_1[3:5]
              temperature_ext05h = mesure05h_1[6:10]
              temperature_int05h = mesure05h_2[10:14]
              poids05h = mesure05h_2[0:7]
              humidite05h = mesure05h_2[7:10]
    
              mesure13h_1 = ligneSms[-9]
              mesure13h_2 = ligneSms[-8]
              heure_mesure13h = mesure13h_1[0:2]
              pluie_mersure13h = mesure13h_1[3:5]
              temperature_ext13h = mesure13h_1[6:10]
              temperature_int13h = mesure13h_2[10:14]
              poids13h = mesure13h_2[0:7]
              humidite13h = mesure13h_2[7:10]
    
              mesure15h_1 = ligneSms[-7]
              mesure15h_2 = ligneSms[-6]
              heure_mesure15h = mesure15h_1[0:2]
              pluie_mersure15h = mesure15h_1[3:5]
              temperature_ext15h = mesure15h_1[6:10]
              temperature_int15h = mesure15h_2[10:14]
              poids15h = mesure15h_2[0:7]
              humidite15h = mesure15h_2[7:10]
    
              mesure19h_1 = ligneSms[-5]
              mesure19h_2 = ligneSms[-4]
              heure_mesure19h = mesure19h_1[0:2]
              pluie_mersure19h = mesure19h_1[3:5]
              temperature_ext19h = mesure19h_1[6:10]
              temperature_int19h = mesure19h_2[10:14]
              poids19h = mesure19h_2[0:7]
              humidite19h = mesure19h_2[7:10]
    
              mesure21h_1 = ligneSms[-3]
              mesure21h_2 = ligneSms[-2]
              heure_mesure21h = mesure21h_1[0:2]
              pluie_mersure21h = mesure21h_1[3:5]
              temperature_ext21h = mesure21h_1[6:10]
              temperature_int21h = mesure21h_2[10:14]
              poids21h = mesure21h_2[0:7]
              humidite21h = mesure21h_2[7:10]
    
              euros = ligneSms[-1]
              sousEuros = euros[1:6]
              batterie = ligneSms[-12]
    
              print(jourDate,"/",moisDate,"/",anneeDate)
              print("SMS N°", multiSMS,'\n','\n')
              print("Mesure effectuée à", heure_mesure05h, "h",'\n')
              print("Poids :", poids05h)
              print(humidite05h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int05h)
              print("Température extérieure :", temperature_ext05h)
              print(mesure05h_1[3:5], "mm de pluie"'\n','\n') 
                  
              print("Mesure effectuée à", heure_mesure13h, "h", '\n')
              print("Poids :", poids13h)
              print(humidite13h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int13h)
              print("Température extérieure :", temperature_ext13h)
              print(mesure13h_1[3:5], "mm de pluie"'\n','\n')
    
              print("Mesure effectuée à", heure_mesure15h, "h", '\n')
              print("Poids :", poids15h)
              print(humidite15h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int15h)
              print("Température extérieure :", temperature_ext15h)
              print(mesure15h_1[3:5], "mm de pluie"'\n','\n')
                  
    
              print("Mesure effectuée à", heure_mesure19h, "h", '\n')
              print("Poids :", poids19h)
              print(humidite19h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int19h)
              print("Température extérieure :", temperature_ext19h)
              print(mesure19h_1[3:5], "mm de pluie"'\n','\n')
                  
              print("Mesure effectuée à", heure_mesure21h, "h", '\n')
              print("Poids :", poids21h)
              print(humidite21h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int21h)
              print("Température extérieure :", temperature_ext21h)
              print(mesure21h_1[3:5], "mm de pluie"'\n','\n')
              print("Charge de batterie restante :", batterie)
              print("Crédit restant :", sousEuros, "€",'\n','\n')
              print("--------------------------------------------------------------------------", '\n')
         sms3r.close()
    #---------------------------------------------------------------------------
    #--------------------------------------------------------------------------
    
         #Opens file.txt in write mode and writes file2.txt except the last text message
         sms4w = open("file.txt",'w')
         sms4w.writelines([item for item in ligneSms[:-13]])
         sms4w.close()
    #---------------------------------------------------------------------------
    #--------------------------------------------------------------------------
    
         #Opens file.txt and collects and processes
         sms4r = open("file.txt",'r')
         ligneSms2 = sms4r.readlines()
    
         if multiSMS != "1,":
    
              ligneMultiSms = ligneSms2[-13]
              multiSMS = ligneMultiSms[7:9]
                        
              date = ligneSms2[-13]
              anneeDate = date[39:41]
              moisDate = date[42:44]
              jourDate = date[45:47]
    
              mesure05h_1 = ligneSms2[-11]
              mesure05h_2 = ligneSms2[-10]
              heure_mesure05h = mesure05h_1[0:2]
              pluie_mersure05h = mesure05h_1[3:5]
              temperature_ext05h = mesure05h_1[6:10]
              temperature_int05h = mesure05h_2[10:14]
              poids05h = mesure05h_2[0:7]
              humidite05h = mesure05h_2[7:10]
    
              mesure13h_1 = ligneSms2[-9]
              mesure13h_2 = ligneSms2[-8]
              heure_mesure13h = mesure13h_1[0:2]
              pluie_mersure13h = mesure13h_1[3:5]
              temperature_ext13h = mesure13h_1[6:10]
              temperature_int13h = mesure13h_2[10:14]
              poids13h = mesure13h_2[0:7]
              humidite13h = mesure13h_2[7:10]
    
              mesure15h_1 = ligneSms2[-7]
              mesure15h_2 = ligneSms2[-6]
              heure_mesure15h = mesure15h_1[0:2]
              pluie_mersure15h = mesure15h_1[3:5]
              temperature_ext15h = mesure15h_1[6:10]
              temperature_int15h = mesure15h_2[10:14]
              poids15h = mesure15h_2[0:7]
              humidite15h = mesure15h_2[7:10]
    
              mesure19h_1 = ligneSms2[-5]
              mesure19h_2 = ligneSms2[-4]
              heure_mesure19h = mesure19h_1[0:2]
              pluie_mersure19h = mesure19h_1[3:5]
              temperature_ext19h = mesure19h_1[6:10]
              temperature_int19h = mesure19h_2[10:14]
              poids19h = mesure19h_2[0:7]
              humidite19h = mesure19h_2[7:10]
    
              mesure21h_1 = ligneSms2[-3]
              mesure21h_2 = ligneSms2[-2]
              heure_mesure21h = mesure21h_1[0:2]
              pluie_mersure21h = mesure21h_1[3:5]
              temperature_ext21h = mesure21h_1[6:10]
              temperature_int21h = mesure21h_2[10:14]
              poids21h = mesure21h_2[0:7]
              humidite21h = mesure21h_2[7:10]
    
              euros = ligneSms2[-1]
              sousEuros = euros[1:6]
              batterie = ligneSms2[-12]
    
              print(jourDate,"/",moisDate,"/",anneeDate)
              print("SMS N°", multiSMS,'\n','\n')
              print("Mesure effectuée à", heure_mesure05h, "h",'\n')
              print("Poids :", poids05h)
              print(humidite05h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int05h)
              print("Température extérieure :", temperature_ext05h)
              print(mesure05h_1[3:5], "mm de pluie"'\n','\n') 
                  
              print("Mesure effectuée à", heure_mesure13h, "h", '\n')
              print("Poids :", poids13h)
              print(humidite13h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int13h)
              print("Température extérieure :", temperature_ext13h)
              print(mesure13h_1[3:5], "mm de pluie"'\n','\n')
    
              print("Mesure effectuée à", heure_mesure15h, "h", '\n')
              print("Poids :", poids15h)
              print(humidite15h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int15h)
              print("Température extérieure :", temperature_ext15h)
              print(mesure15h_1[3:5], "mm de pluie"'\n','\n')
                  
              print("Mesure effectuée à", heure_mesure19h, "h", '\n')
              print("Poids :", poids19h)
              print(humidite19h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int19h)
              print("Température extérieure :", temperature_ext19h)
              print(mesure19h_1[3:5], "mm de pluie"'\n','\n')
                  
              print("Mesure effectuée à", heure_mesure21h, "h", '\n')
              print("Poids :", poids21h)
              print(humidite21h, "d'humidité")
              print("Température à l'intérieur de la ruche :", temperature_int21h)
              print("Température extérieure :", temperature_ext21h)
              print(mesure21h_1[3:5], "mm de pluie"'\n','\n')
              print("Crédit restant :", sousEuros, "€",'\n','\n')
              print("--------------------------------------------------------------------------", '\n')
    
    sms4r.close()
    I basically go:
    -line by line starting from the end and find the pieces of data I need
    -process the data and print
    -write the raw data back to a 2nd file without the last text
    -start a loop as long as there's raw data left
    -open the 2nd file collect and process, print
    -write to a 3rd file
    -open the 3rd file collect and process, print
    -write back to the 2nd file
    -end loop.

    As you can see the code is really long and confusing for something so simple. I'm looking for a way to simplify the whole process and to make the code neat.

    Sending:
    Then to send all the processed data to the blog I use Super Macro again to copy the data printed to the shell (!) and paste it to Windows Live Writer and publish.

    Using Super Macro is very slow, imprecise and ugly as we can see windows opening and closing, the program running in the shell, text being selected and pasted etc.
    I'd like to find a way to streamline the entire process by writing the AT commands in HyperTerminal (or manage to receive the data from the GSM module), processing the data and sending it to wordpress using only python and adding a simple GUI (hopefully I'll by able to figure out the GUI by myself), no more Super Macro.
    The ideal end-goal would be to make the program available for beekeepers using the measuring device on their beehive and a GSM module.

    Any help would be greatly appreciated. If anything isn't clear, please let me know.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    510
    Rep Power
    33
    Python has wordpress interfaces, like this one for example but I have never used wordpress so can't help there.

    All of the data extraction is the same and it appears that the print statements are also so that code could be moved into a common function.
    Code:
    ##----- 2 partial code examples ----------
    elif multiSMS != "1,":
         #Date data   
         date = ligne[-15]
         anneeDate = date[39:41]
         moisDate = date[42:44]
         jourDate = date[45:47]
         #Data from 5 o' clock
         mesure05h_1 = ligne[-13]
         mesure05h_2 = ligne[-12]
         heure_mesure05h = mesure05h_1[0:2]
         pluie_mersure05h = mesure05h_1[3:5]
         temperature_ext05h = mesure05h_1[6:10]
         temperature_int05h = mesure05h_2[10:14]
         poids05h = mesure05h_2[0:7]
         humidite05h = mesure05h_2[7:10]
    #
    #----- the following does the same thing
    if multiSMS != "1,":
    
         ligneMultiSms = ligneSms2[-13]
         multiSMS = ligneMultiSms[7:9]
                   
         date = ligneSms2[-13]
         anneeDate = date[39:41]
         moisDate = date[42:44]
         jourDate = date[45:47]
    
         mesure05h_1 = ligneSms2[-11]
         mesure05h_2 = ligneSms2[-10]
         heure_mesure05h = mesure05h_1[0:2]
         pluie_mersure05h = mesure05h_1[3:5]
         temperature_ext05h = mesure05h_1[6:10]
         temperature_int05h = mesure05h_2[10:14]
         poids05h = mesure05h_2[0:7]
         humidite05h = mesure05h_2[7:10]
    #
    #  ----- use instead ------------------------------------
    ##        note that you could also send mesure05h_1, et al and extract those as well
    def extract_date(string_in):
         anneeDate = string_in[39:41]
         moisDate = string_in[42:44]
         jourDate = string_in[45:47]
    
        return anneeDate, moisDate, jourDate
    ...
    ...
    elif multiSMS != "1,":
        #Date data   
        date = ligne[-15]
        anneeDate, moisDate, jourDate   = extract_date(date)
    ...
    ...
    if multiSMS != "1,":
    
        ligneMultiSms = ligneSms2[-13]
        multiSMS = ligneMultiSms[7:9]
                   
        date = ligneSms2[-13]
        anneeDate, moisDate, jourDate   = extract_date(date)
    Last edited by dwblas; February 15th, 2013 at 10:01 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    You have so much duplicate code that when you misspelled your pluie_mersure variable you had to rewrite the extraction in your print statement. Dictionaries and functions can help somewhat.

    Try to understand this rewrite of your first 115 lines of code. I used only three measurement groups.
    Code:
    import os
    
    heure_mesure = dict()
    pluie_mesure = dict()
    temperature_ext = dict()
    temperature_int = dict()
    poids = dict()
    humidite = dict()
    
    def extract_hourly_measurements(key, mesure1, mesure2):
        heure_mesure[key] = mesure1[0:2]
        pluie_mesure[key] = mesure1[3:5]
        temperature_ext[key] = mesure1[6:10]
        temperature_int[key] = mesure2[10:14]
        poids[key] = mesure2[0:7]
        humidite[key] = mesure2[7:10]
    
    def display(key):
        print("Mesure effectuée à", heure_mesure[key], "h",'\n')
        print("Poids :", poids[key])
        print(humidite[key], "d'humidité")
        print("Température à l'intérieur de la ruche :", temperature_int[key])
        print("Température extérieure :", temperature_ext[key])
        print(pluie_mesure[key], "mm de pluie"'\n','\n') 
    
    #opens a reads CAPTURE.TXT (where the data from the text message is stored)
    with open("CAPTURE.TXT", 'r') as fichier_capture:
        ligne = fichier_capture.readlines()
    
    #Checks the first character from line -3 (to see if there's the data from the text message)
    ligneCondition = ligne[-3]
    caractereCondition = ligneCondition[0]
    
    #Finds the text message number
    ligneMultiSms = ligne[-15]
    multiSMS = ligneMultiSms[7:9]
    
    #If the 1st character from line -3 != to E... 
    if caractereCondition != "E":
         print("Pas de nouveaux messages")
    
    elif multiSMS != "1,":
         #Date data   
         date = ligne[-15]
         anneeDate = date[39:41]
         moisDate = date[42:44]
         jourDate = date[45:47]
         extract_hourly_measurements('05h',ligne[-13],ligne[-12],)
         extract_hourly_measurements('13h',ligne[-11],ligne[-10],)
         extract_hourly_measurements('15h',ligne[ -9],ligne[ -8],)
         #...
         #Remaining battery charge
         euros = ligne[-3]
         sousEuros = euros[1:6]
         batterie = ligne[-14]
         #prints tranlsated data for the date
         print(jourDate,"/",moisDate,"/",anneeDate)
         print("SMS N°", multiSMS,'\n','\n')
         #Prints tranlsated data from 5AM
    
         for key in '05h 13h 15h'.split():    # print all groups of temperature and humidity data
             display(key)
         
         #Prints processed data for the remaining battery charge a phone credits
         print(mesure21h_1[3:5], "mm de pluie"'\n','\n')
         print("Charge de batterie restante :", batterie)
         print("Crédit restant :", sousEuros, "€",'\n','\n')
         print("--------------------------------------------------------------------------", '\n')
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Thanks a lot for helping me with the code!
    I tried your suggestion b49P23TIvg and I've managed to cut down the number of lines by more than half !
    And thanks for pointing out my mistake with the variable.

    dwblas, I tried using wordpresslib_xmlrpc but couldn't get it to work. Before going with Windows Live Writer, I tried sending it by email with python. I put all the processed data in a variable then put that in the body of the message as
    Code:
    body = str(variable)
    but it printed it out with all the syntax (stuff like [ and '\n' etc), it was ugly. French accents were also a problem.
    I see that wordpresslib_xmlrpc works in a similar fashion
    Code:
    post.content = 'This is the body of my new post.'
    How can I post the processed data without it looking funny?

IMN logo majestic logo threadwatch logo seochat tools logo