Thursday, April 23, 2015

Code for Gas booking python

Create a file called config.txt  and add following lines

phonenum: 9666261963
min_weight: 3
gas_agency: 9985203976


smartgasbooking.py

import serial
from random import randint
from time import sleep
from threading import Thread
import threading

resume=threading.Event()
 

def read_configfile():
    fin = open("config.txt","r")
    lines= tuple(fin)
    config = {}
    for line in lines:
       config[line.split(":")[0]]=line.split(":")[1]  
       fin.close()
    return config

config_arr=read_configfile()
   
def weight_read():
    global config_arr
    global resume
    while True :
     try:
        ser=serial.Serial('/dev/ttyAMA0',9600,timeout=1)  
        ser.flush()
        num=ser.inWaiting()
        if(num != 0):
            s=ser.read(num)
            print "weight",s
            weight= s.split(" Kg.")[0]
   #print weight
            if(float(weight) < 4):
                resume.clear()
                #call_number(config_arr["phonenum"])
                call_number("9666261963")                            
                print "calling"          
        ser.close()
     except:
        print "...."

def call_number(phnum):
   try:
    global resume
    ser = serial.Serial('/dev/ttyUSB0',9600,timeout=1)
    ser.flush()
    print "Calling ...",phnum
    cmd="ATD"+str(phnum)+";\r"
    ser.write(cmd)
    sleep(15)
    ser.write('ATH\r')
    ser.close()
   except:
    print "call_error"  
   resume.set()
 




try:
    resume.set()  
    wt_thread=Thread(target = weight_read);
    wt_thread.start()
    wt_thread.join()  
except:
    print " thread Error"
   

   

Interfacing Load cell with Raspberry Pi


Connectivity of  Load cell  to Load cell Amplifier for weighing scale

Input voltage - Excitation
Red wire  --------- Excitation input voltage (+)
Black wire -------- Excitation input Ground(-)

Output voltage - Signal
Green wire ------- Signal output (+)
White  wire ------ Signal output (-)
 
Load cell documentation :                   http://www.sunrom.com/p/load-cell-40-kg
Load cell  Amplifier documentation  : http://www.sunrom.com/p/load-cell-amplifier-for-weighing-scale-serial-output

Connectivity of Load cell Amplifier for weighing scale  to Raspberry Pi
 5V    ---------- pin 4 (5v)
GND  --------- pin 6 (GND)
 RXD --------- pin 8 (TXD)
 TXD --------- pin 10 (RXD)


Python code for weight reading

import serial
from random import randint
from time import sleep
from threading import Thread

def weight_read():
    global config_arr
    while True :
     try:
        ser=serial.Serial('/dev/ttyAMA0',9600,timeout=1)   
        ser.flush()
        num=ser.inWaiting()
        if(num != 0):
            s=ser.read(num)
            print s
            weight= s.split(" Kg.")[0]
        #print weight
            if(float(weight) < 4):
                #call_number(config_arr["phonenum"])
                print "calling"           
        ser.close()
     except:
        print "Weight read error"

try:
    wt_thread=Thread(target = weight_read);
    wt_thread.start()
    wt_thread.join()  
except:
    print " thread creation Error"






     


Friday, April 10, 2015

Interfacing SIM900A GSM Modem with Raspberry Pi

Interfacing SIM900A GSM Modem with Raspberry Pi
gsm_phone.py

import serial
import time
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=1)
# 9600 is the default Baudrate for SIM900A modem
ser.flush()
ser.write('ATD9020XXXXXX;\r')
# AT command to call a number using GSM Modem
#ser.read(2)
# read 2 bytes of data from the serial port
time.sleep(10)
# wait for 10 seconds
ser.write('ATH\r')
# Hold the call
ser.close()
# close the serial port
connect RXD of USB2Serial to TXD of GSM Modem and TXD of Serial to RXD of GSM Modem. Also check if you can make a call to the modem and see whether a ring sound, so that network connection can be ensured.

You may use AT+CMGS=”phonenumber” for the sms