Remote Control Power Socket with Raspberry Pi and Sim900

Remote Control Power Socket with Raspberry Pi and Sim900

Install wiringPi

git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
cd wiringPi

./build

 

Download SIM900GSM library

build
make
make install

than compile 

gcc -o main.o main.c -lSIM900GSM -lwi

sudo ./main.o

 

Now some code example in "C"

a little part is missing. :)

 

 

#include <stdio.h>
#include <wiringPi.h>
#include "GSM.h"

char phone_num[20];
char sms_text[160], sms_repl_text[160];
char smsPosition;
char smsStatus;
byte callStatus;
int i;
const int ledPin0 = 26;
const int ledPin1 = 19;
const int ledPin2 = 13;
const int ledPin3 = 6;


int choose(const char * data)
{
int value = 0;
if (strstr(data, "reset1"))
value = 1;
if (strstr(data, "reset2"))
value = 2;
if (strstr(data, "reset3"))
value = 3;
if (strstr(data, "reset4"))
value = 4;
if (strstr(data, "resetall"))
value = 5;
if (strstr(data, "resetrasp"))
value = 6;
if (strstr(data, "help"))
value = 7;

return value;
}

void gpiopin(int led)
{
printf("\t\tON\n");
digitalWrite(led, LOW);
delay(3000);
digitalWrite(led, HIGH);
printf("\t\tOFF\n");
}

int main(void)
{
TurnOn(115200);
ResetModule();
wiringPiSetupGpio();

pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin0, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);

WritePhoneNumber(1,"+XXXXXXXXXX");

GetCommLineStatus();

if (CheckRegistration()==REG_NO_RESPONSE)
ResetModule();

while(1)
{
printf("Checking SMS...\n");
smsPosition = IsSMSPresent(SMS_ALL);
if (smsPosition)
{// there is some SMS => read it
smsStatus = GetAuthorizedSMS(smsPosition, phone_num, sms_text, 160, 1, 1);
switch (smsStatus)
{
case GETSMS_NOT_AUTH_SMS:
printf("\tSMS not authorized. Deleting...\n");
printf("\t\tfrom: %s\n", phone_num );
printf("\t\tSMS: %s\n", sms_text);
DeleteSMS(smsPosition);
break;

case GETSMS_AUTH_SMS:
printf("\tSMS authorized.\n");
printf("\t\tfrom: %s\n", phone_num );
for(i = 0; sms_text[i]; i++){
sms_text[i] = tolower(sms_text[i]);
}
const char* test = strip(sms_text);

switch (choose(test))
{
case 1 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
gpiopin(ledPin0);
strcpy(sms_repl_text, "Erfolgreich Durchgefuhrt !");
//SendSMS(phone_num, sms_repl_text);
break;
case 2 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
gpiopin(ledPin1);
strcpy(sms_repl_text, "Erfolgreich Durchgefuhrt !");
//SendSMS(phone_num, sms_repl_text);
break;
case 3 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
gpiopin(ledPin2);
strcpy(sms_repl_text, "Erfolgreich Durchgefuhrt !");
//SendSMS(phone_num, sms_repl_text);
break;
case 4 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
gpiopin(ledPin3);
strcpy(sms_repl_text, "Erfolgreich Durchgefuhrt !");
//SendSMS(phone_num, sms_repl_text);
break;
case 5 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
gpiopin(ledPin0);
gpiopin(ledPin1);
gpiopin(ledPin2);
gpiopin(ledPin3);
strcpy(sms_repl_text, "Erfolgreich Durchgefuhrt !");
//SendSMS(phone_num, sms_repl_text);
break;
case 6 :
printf("\t\tBefehl war : %s\n", sms_text);
DeleteSMS(smsPosition);
strcpy(sms_repl_text, "Raspberry Pi is Rebooting!");
//SendSMS(phone_num, sms_repl_text);
system("sudo reboot");
break;
case 7 :
printf("\t\tCMD: %s\n", sms_text);
DeleteSMS(smsPosition);
strcpy(sms_repl_text, "Befehlsliste :\r reset 1\r reset 2\r reset 3\r reset 4\r reset all\r reset rasp\r");
SendSMS(phone_num, sms_repl_text);
printf("\tDeleting command...\n");
break;

default:
printf("\t\tUnbekannte Befehl: %s\n", sms_text);
DeleteSMS(smsPosition);
strcpy(sms_repl_text, "Unbekannte Befehl\r");
//SendSMS(phone_num, sms_repl_text);
printf("\tDeleting command...\n");

}

break;
}
}
else printf("\tNo SMS.\n");
delay(4000);
}

return 0;
}

 

 

Some help here.

 

 

and some Python code 

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import re
import os
import array
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
ser=serial.Serial('/dev/ttyAMA0',115200,timeout=1)

def isnumeric(value):
try:
int(value)
return True
except:
return False

def extract_rufnr(string):
l=len(string)
nr = ''
for i in range(1,l):
if string[i] == '+':
p1 = i+1
for p1 in range(p1,l-p1):
if isnumeric(string[p1]) :
nr = nr + string[p1]
else:
break
if isnumeric(nr):
nr = "+" + nr
else:
nr = ""
return nr

def extract_befehl(string):
l=len(string)
data = ''
for i in range(0,l):
if string[i] == '@':
p2 = i
for p2 in range(p2,l):
data = data + string[p2]
return data

def extract(string):
l=len(string)
nr = ''
p2 = 0
data = ''
out= []
for i in range(1,l):
if string[i] == '+':
p1 = i+1
for p1 in range(p1,l-p1):
if isnumeric(string[p1]) :
nr = nr + string[p1]
else:
break
if string[i] == '@':
p2 = i
for p2 in range(p2,l-1):
data = data + string[p2]
#print data
if isnumeric(nr):
nr = "+" + nr
#out.append(nr)
#out.append(data)
return nr
else:
nr = ""
return data


def stripEscape(string):
delete = ""
i=1
while (i<0x20):
delete += chr(i)
i += 1
t = string.translate(None, delete)
return t

def CheckPowerStatusSoft():
GPIO.setup(17,GPIO.OUT)
if GPIO.input(17):
print "PowerStatus 17 ON"
else:
print "PowerStatus 17 OFF"
return

def PowerOFF_SIM900_Soft():
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,GPIO.LOW)
return

def PowerON_SIM900_Soft():
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,GPIO.HIGH)
return

def Reset_SIM900():
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.HIGH)
time.sleep(10);
GPIO.setup(18,GPIO.OUT)
GPIO.output(18,GPIO.LOW)
return

def PowerOFF_SIM900_Hard():
ser.write('AT\r')
line = ser.readline()
line = ser.readline()
ser.write('AT+CPOWD=0\r')
line = ser.readline()
line = ser.readline()
ser.close()
return

def GPIO_ERST():
GPIO.setup(26,GPIO.OUT)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(6,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.output(19,GPIO.LOW)
GPIO.output(13,GPIO.LOW)
GPIO.output(6,GPIO.LOW)
time.sleep(4);
GPIO.output(26,GPIO.HIGH)
GPIO.output(19,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)
GPIO.output(6,GPIO.HIGH)
return

def Gpio_PIN(nr):
if nr == 1 :
GPIO.output(26,GPIO.LOW)
time.sleep(4);
GPIO.output(26,GPIO.HIGH)

if nr == 2 :
GPIO.output(19,GPIO.LOW)
time.sleep(4);
GPIO.output(19,GPIO.HIGH)

if nr == 3 :
GPIO.output(13,GPIO.LOW)
time.sleep(4);
GPIO.output(13,GPIO.HIGH)

if nr == 4 :
GPIO.output(6,GPIO.LOW)
time.sleep(4);
GPIO.output(6,GPIO.HIGH)

if nr == 5 :
GPIO.output(26,GPIO.LOW)
GPIO.output(19,GPIO.LOW)
GPIO.output(13,GPIO.LOW)
GPIO.output(6,GPIO.LOW)
time.sleep(4);
GPIO.output(26,GPIO.HIGH)
GPIO.output(19,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)
GPIO.output(6,GPIO.HIGH)
return

def DeleteSMS():
ser.write('AT\r')
line = ser.readline()
line = ser.readline()
print "Delete ALL SMS.."
os.system("sudo gammu --config .gammurc deleteallsms 1");
line = ser.readline()
os.system("sudo gammu --config .gammurc deleteallsms 2");
line = ser.readline()
return

def SendSMS(nr,text):
ser.write('AT\r')
line = ser.readline()
ser.write('AT+CMGS=\"'+nr+'\"\r')
time.sleep(3);
line = ser.readline()
ser.write(text);
ser.write(chr(26));
line = ser.readline()
print line
print "Sending SMS..."
time.sleep(5);
DeleteSMS()
return


def ReadSMS():
arr = []
i=0
n=1
print "Read SMS...."
ser.write('AT\r')
line = ser.readline()
ser.write('AT+CMGF=1\r')
line = ser.readline()
line = ser.readline()
ser.write('AT+CSCS="8859-1"\r')
line = ser.readline()
line = ser.readline()
line = ser.readline()
line = ser.readline()
ser.write('AT+CMGL="ALL"\r')
while (n != 0):
line = ser.readline()
if stripEscape(line) != '':
arr.append(line)
for i in range(0,len(arr)):
if stripEscape(arr[i]).lower() == 'ok':
n = 0
return arr


def Main():
ser.close()
ser.open()
cmd = ''
rufnr = ''
print "Main ...."
while True:
cmd = ''
rufnr = ''
arr2= ReadSMS()
for i in range(0,len(arr2)):
if (extract_rufnr(arr2[i]) != ''):
rufnr=stripEscape(extract_rufnr(arr2[i]))

if (extract_befehl(arr2[i]) != ''):
cmd=stripEscape(extract_befehl(arr2[i]))

MSG = 'Erfolgreich Durchgefuhrt !'
print "COMMAND IST ... " + cmd
print "RUF NR IST ... " + rufnr

if cmd.lower() == '@reset 1' :
print "Befehl war RESET 1"
Gpio_PIN(1)
SendSMS(rufnr,MSG)
rufnr=''

if cmd.lower() == '@reset 2' :
print "Befehl war RESET 2"
Gpio_PIN(2)
SendSMS(rufnr,MSG)
rufnr=''

if cmd.lower() == '@reset 3' :
print "Befehl war RESET 3"
Gpio_PIN(3)
SendSMS(rufnr,MSG)
rufnr=''

if cmd.lower() == '@reset 4' :
print "Befehl war RESET 4"
Gpio_PIN(4)
SendSMS(rufnr,MSG)
rufnr=''

if cmd.lower() == '@reset all' :
print "Befehl war RESET ALL"
Gpio_PIN(5)
SendSMS(rufnr,MSG)
rufnr=''

if cmd.lower() == '@help' :
print "Befehl war HELP"
HCMD='Befehlsliste :\r @reset 1\r @reset 2\r @reset 3\r @reset 4\r @resetl all\r @reset rasp\r @reset sim\r'
SendSMS(rufnr,HCMD)
rufnr=''

if cmd.lower() == '@reset rasp' :
print "Befehl war Reset Raspberry"
HCMD='Raspberry Rebooting!'
SendSMS(rufnr,HCMD)
rufnr=''
os.system("sudo reboot");

if cmd.lower() == '@reset sim' :
print "Befehl war Reset SIM900 Module"
HCMD='Back ONLINE!'
Reset_SIM900()
time.sleep(5)
SendSMS(rufnr,HCMD)
rufnr=''

if rufnr != '':
if (cmd.lower() != '@reset sim' or
cmd.lower() != '@reset rasp' or
cmd.lower() != '@reset 1' or
cmd.lower() != '@reset 2' or
cmd.lower() != '@reset 3' or
cmd.lower() != '@reset 4' or
cmd.lower() != '@reset all' or
cmd.lower() != '@help') :
HCMD='Unbekannte Befehl\r'
print HCMD
SendSMS(rufnr,HCMD)
rufnr=''

time.sleep(5)
#return

print "Starting Sim Module..."
PowerON_SIM900_Soft()
time.sleep(10)
Reset_SIM900()
time.sleep(5)

print "Initialisierung GPIO STATE..."
GPIO_ERST()
time.sleep(5)
print "Start PROGRAMM..."
Main()