jueves, 5 de diciembre de 2019

SIM800L and Raspberry Pi 3 B Controlled 3 LED -- Tutorial

SIM800L and Raspberry Pi 3 B Controlled 3 LED -- Tutorial




SIM800L and Raspberry Pi 3 B Controlled 3 LED -- Tutorial




Introduction

Raspberry Pi is a series of credit card-sized single-board computer while SIM800L is available for GPRS and SMS message data remote transmission. Combining both of them we would be able to control different devices via GPRS data communication or Text messaging. 
In this tutorial we will be using SIM800L SMS message data remote transmission mode to control three LED. We will be covering also how to setup serial communication for raspberry pi.

Hardware Required:

Configuring the GPIO Serial Port On

The first thing you’ll need to do is to ensure all software components are up-to-date.
Update the repository and upgrade the installed packages:
pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get upgrade -y
Update the firmware:
pi@raspberrypi:~ $ sudo rpi-update
Reboot to apply changes:
pi@raspberrypi:~ $ sudo reboot

Enable UART

Raspberry Pi 3 compared to previous Raspberry pi behaves differently with regards to the UART interface on the GPIO header, that's why we need to enable it first.
Enabling UART with minimum core frequency:
pi@raspberrypi:~ $ sudo nano /boot/config.txt
Add this line to the bottom
# Enable UART
enable_uart=1

Disable Console

In case you would like to connect a HAT using the UART interface, it’s recommended to disable the console.
To disable the console, edit the following file as follows:
pi@raspberrypi:~ $ sudo nano /boot/cmdline.txt 
you will see something like:
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
remove the line: console=serial0,115200 and save.
 Reboot to apply changes:
pi@raspberrypi:~ $ sudo reboot

Circuit Connection:

  • SIM800L ------ Rasp GPIO
  • Vcc ------- 5V
  • Gnd ------ Gnd
  • Rx ------- Tx
  • Tx ------- Rx
  • LED1 --- GPIO 23
  • LED2 --- GPIO 24
  • LED3 ---- GPIO 25

Code

The code focuses more on receiving messages, but it can be modify depending on the usage.

#This is an open-source code
#Created by Noel S. Orbong Jr. 
#16 january 2017

import RPi.GPIO as GPIO
import serial
import time, sys
import datetime

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(23, GPIO.OUT)
    GPIO.setup(24, GPIO.OUT)
    GPIO.setup(25, GPIO.OUT)

SERIAL_PORT = "/dev/ttyS0"    # Rasp 3 UART Port

ser = serial.Serial(SERIAL_PORT, baudrate = 115200, timeout = 5)
setup()
ser.write("AT+CMGF=1\r") # set to text mode
time.sleep(1)
ser.write('AT+CMGDA="DEL ALL"\r') # delete all SMS
time.sleep(1)
reply = ser.read(ser.inWaiting()) # Clean buf
print "Listening for incomming SMS..."
while True:
    reply = ser.read(ser.inWaiting())
    if reply != "":
        ser.write("AT+CMGR=1\r") 
        time.sleep(1)
        reply = ser.read(ser.inWaiting())
        print "SMS received. Content:"
        print reply
  if "ON" in reply.upper():
    if "LED1" in reply.upper():
                print "LED 1 ON"
                GPIO.output(23,GPIO.HIGH)
    if "LED2" in reply.upper():
                print "LED 2 ON"
                GPIO.output(24,GPIO.HIGH)
            if "LED3" in reply.upper():
                print "LED 3 ON"
                GPIO.output(25,GPIO.HIGH)
            if "ALL" in reply.upper():
print ("ALL LED ON")
                GPIO.output(23,GPIO.HIGH)
                GPIO.output(24,GPIO.HIGH)
                GPIO.output(25,GPIO.HIGH)
if "OFF" in reply.upper():
    if "LED1" in reply.upper():
                print "LED 1 OFF"
                GPIO.output(23,GPIO.LOW)
    if "LED2" in reply.upper():
                print "LED 2 OFF"
                GPIO.output(24,GPIO.LOW)
            if "LED3" in reply.upper():
                print "LED 3 OFF"
                GPIO.output(25,GPIO.LOW)
            if "ALL" in reply.upper():
                print "ALL LED OFF"
                GPIO.output(23,GPIO.LOW)
                GPIO.output(24,GPIO.LOW)
                GPIO.output(25,GPIO.LOW)
        time.sleep(.500)
        ser.write('AT+CMGDA="DEL ALL"\r') # delete all
        time.sleep(.500)
        ser.read(ser.inWaiting()) # Clear buffer
        time.sleep(.500)    


No hay comentarios:

Publicar un comentario