GSM Multifuction Alarm with SIM900

By on June 18, 2013

0909_01

It’s small but packed with features. The GSM alarm we present today, sports a PIR motion sensor, can be battery operated and it’s capable to communicate via GSM. It can transmit alarm conditions and receive commands from remote. It’s also capable to indicate problems such as is insufficient voltage supply or tampering.

It’s not so conventional for antitheft system: to integrate a movement sensor, a PIR motion sensor and a temperature probe in a single appliance: all this accompanied by an SMS sending GSM / GPRS module. This circuit was born as a very versatile, ready to use, built-in alarm system: no installation is required, you can just drop it and it’s ready to work. It’s not by chance that is designed to be battery operated: it also features a battery state control to check the power.

Moreover, it has two digital inputs, one which was is set up to detect tension: with it, you could detect important conditions such as the state of a protection system or even other parameters of various kinds, simply by adding additional sensors.

 

 

The diagram

schematics

All is quite simple, since most of the tasks are managed by a PIC18F46K20 (U1), all is powered between 6 and 32 VDC (even unregulated), thanks to a DC / DC buck converter configuration that is present in the circuit.

The switching regulator is based on the MC34063 (U2), used in the classical configuration of a winding inductance series PWM regulator, whose output voltage depends on the energy stored in L1, the output voltage from the DC / DC is stabilized with reference to the potential demoted to pin CII by the resistive divider R3/R4.

The two components are designed to get 4 V in C8 and C9.

minipir

After power-on-reset, the microcontroller, initializes its I/O settings: RB0 reads the state of the motion sensor, RB1 and RB2 serve to detect the logical condition in inputs IN1 and IN2; RB3 reads the state of the P1 button and RB4 the logical condition of the output of the passive infrared radar PIR. The latter normally has zero on OUT and 5-volt pulses when detects something in its field of action. P1 button serves for the local management of the operation mode.

Note that both IN1 and IN2 are protected by a diode which prevents the application to RB1 and RB2 of voltages exceeding that with which the microcontroller is powered. In the case of  PIC18F46K20, a voltage below 1 V or a short circuit of IN1 or IN2 corresponds to the low logic state, while the opening of said inputs or the application of a DC voltage of 1 to 5 V generates 1.

The AN0 line is an input assigned to the A / D converter contained in the microcontroller and reads the value of the voltage supply through the resistive divider formed by R1 and R2.

The RC0 line is initialized by the PIC as a bidirectional I / O, as it serves to communicate with the temperature sensor, which, in our case is a Dallas DS18B20. This sensor is very precise and it’s capable of measuring temperatures between -5 and 150 ° C with an accuracy of ± 0.5 ° C (in -10 ÷ 85 ° C), making them available over a serial line, in this case going to the RC0 pin of the microcontroller.

Let us now look to the outputs: RD6 and RD7, used to manage the lighting of the LD1 and LD2 LEDs .

Regarding the cellular module, this is an FT900M based on the SIMCOM SIM900. The microcontroller contains a UART that accessible from pins 44 (transmission) and 1 (reception): it’s used to communicate with the GSM module.

The set of I/Os is completed by RC5 and RD0: the first one controls the switching of the GSM (via a transistor arranged in the base) while the second line provides to phone reset when necessary; in any case after the initialization after resetting circuit.

topsilk

BOM

[code]

R1: 10 kohm (0805)
R2: 1 kohm (0805)
R3: 2,2 kohm (0805)
R4: 1 kohm (0805)
R5: 0,1 ohm 1W (1206)
R6: 330 ohm (0805)
R7: 330 ohm (0805)
R8: 4,7 kohm (0805)

C1: 100 nF (0805)
C2: 470 µF 6,3 VL (X)
C3: 470 µF 6,3 VL (X)
C4: 100 nF (0805)
C5: 22 µF 25 VL (D)
C6: 100 nF  (0805)
C7: 100 nF (0805)
C8: 470 µF 6,3 VL (D)
C9: 100 nF (0805)
C10: 100 nF (0805)
C11: 100 pF (0805)
C12: 22 µF 25 VL (D)

L1: 22 µH

U1: PIC18F46K20-I/PT
U2: MC34063AD

LD1: LED red 5 mm
LD2: LED green 5 mm

P1: Microswitch

GSM: Mini breakout  SIM900 (FT900M)

D1: GF1M
D2: GF1M
D3: MBRA140TRPBF

MOV: Motion sensor
PIR: PIR sensor
TEMP: DS18B20+

[/code]

How it works

The system is designed to be configured via SMS and, when alarms are triggered, it sends one ore more SMS to the numbers stored in a secial list, to warn one or more persons.

Our system fires an alarm on the verification of one or more of the following conditions:

– the IN1 input changes state, moves at high or low state,

– the motion sensor detects a vibration

– the PIR sensor detects presence,

– the temperature goes out of a preset range

– the supply voltage drops below a certain value

0909_02

When one of these events occurs, the circuit sends a text message to te contact list.

You can also set a custom message for each event.

RB2 is used to activate and deactivate the alarm: leaving it open or putting a high logic level, after an interval can be set between 0 and 59 seconds with the SMS command TATT, the circuit will be activated.

After activation of IN1, the green LED LD2 will flash until activation.

You can also ask the device to send a text message when activated.

You can define a sensors inhibition time thanks to the SMS command INI:xx, ( xx is the time in minutes). Inhibition time is to avoid multiple alarms for the same event. caused by something that produces more pulses (such as an unstable contact in IN2, an oscillating temperature around the threshold value etc..).

One particular feature of the circuit is the Sleep mode, useful to prolong the operating time when the circuit is battery powered: sending the SLEEP:xxx command the system goes to a sleep mode (low power) for xxx seconds. During this mode, the operation is minimal, ie, the circuit neither detects any alarm, nor it sends or reads i ncoming SMS. Sleep mode suspends every xxx seconds, when the circuit queries the phone to verify the arrival of messages and move on the execution of commands.

 

Sleep mode enables automatically only if the circuit is turned off, or if the IN1 is at a low logic level.

If the circuit is on sleep, but you need to read data you can awaken it momentarily in two ways: locally, by pressing the P1 button, or remotely, making a phone call to it. Upon awakening, we can send an SMS command to read the data we want, the circuit will respond and then will automatically go back to sleep.

 

Configuration

0909_03

All circuit settings can be made ​​by SMS: note that the system will only accept SMS from phone numbers stored in the approved numbers list, a maximum of 8. This list is the same to which the system will send the SMS to. Alternatively you must send commands accompanied by a password, which by default and following a reset (imposed by SMS).

The command to store a new number is the following (a SMS containing the text): NUMxnnnnnnnnnnnpwd, where x is the position (1 to 8) where to save the number amd pwd is the current password (required if the SMS command originates from a phone is not in the list and needed when you store the first number)

The pwd is due in any case if the position is already taken by another number. Each number can be up to 19 characters and must contain the national prefix.

You can also query the device to find all the numbers stored: just issue the command SMS NUM?;pwd.

Our device implements also an Easy Setup functionality: after switching on, keeps waiting for a phone call and then saves the caller’s phone number and enters the normal operation.
Once you stored the numbers in the list, you must define which ones to send SMS alarms. The command is SMSxxxxxxxx:ON or SMSxxxxxxxx:OFF, where xxxxxxxx is the position in the list where the numbers are stored.
For example, SMS1346: ON sets that the numbers in positions 1, 3, 4 and 6 will receive the SMS. OFF will than deactivate the corresponding SMS sending.

At the first start and after each reset, the tracker sends SMS to all numbers stored.

Certain commands can be executed only when accompanied by a password: the default password (reset after every hard reset of the system) is 12345, and you can replace it by issuing the command PWDnewpwd, pwd, where newpwd is the password to be stored and pwd is the current one.

A reset, which restores the default settings and clears phone numbers list is done thanks to the RESpwd command, where pwd is the current password.

You can send text messages containing multiple comma separated commands, and with a special command, you can disable answers to some of the commands by inputting the command RISP.

 

Let’s disuss of how to manage the alarm functions. Let’s start with the temperature. By sending the TEMPcommand you get the current temperature, by ALLMAX:YXX and ALLMIN:YXX (where y is the sign and xx corresponds to the Celsius degrees) you can set maximum and minimum temperatures (before issuing alarms).

Temperature settings can be reset by sending the command TRES. The ALL?   command instead queries the system, which responds with the currently

set temperatures. Temperature alarms are activated by the SAT:1 command: to deactivate you can issue the SAT:0 command. The setting will be stored until reset.

 

Let’s look at the commands of the motion alarm: SAM:1 enables sensor, and SAM:0 disables it, settings are saved until hard reset.

The PIR sensor, is activated and deactivated via SAP:1, SAP:0 commands. The settings are kept until reset as well.

 

Command SAI:1 and  SAI: 0 activate and deactivate control on the IN2 input line. The commands to define what the system should consider an alarm are the following: LIV:A  makes the input sensitive to the logic high, LIV: B activates it with zero and LIV:V activates the alarm when there’s a change in the logic level. The LIV? command asks the system to send a text message containing the current setting for the IN2 alarm input.

Command: SAV:1 gives alarm if the battery voltage drops below 6 volts; with SAV: 0 we can disable this function.

To get an SMS when circuit is activated (meaning IN has been activated) you must send the command AVV:1; you can disable this feature with AVV:0.

CODE

'******************************************************************************
'* MF909
'* Sistema allarme gsm dotato di sensore di temperatura, movimento,PIR
'* Il sistema è basato su un PIC18F46K20
'* by Boris Landoni
'******************************************************************************



'CONNECT

'HTTP/1.1 200 OK

'Content-Type: application/binary

'Content-Length: 25

'Date: Mon, 25 Oct 2010 13:15:43 GMT

'Expires: Mon, 25 Oct 2010 13:15:43 GMT

'Cache-Control: private, max-age=0

'X-Content-Type-Options: nosniff

'X-Frame-Options: SAMEORIGIN

'X-XSS-Protection: 1; mode=block

'Server: GSE

'Connection: close







'CONNECT

'HTTP/1.1 200 OK

'Content-Type: text/plain; charset=UTF-8

'Vary: Accept-Language

'Date: Mon, 25 Oct 2010 13:16:12 GMT

'Server: mafe

'Cache-Control: private, x-gzip-ok=""

'X-XSS-Protection: 1; mode=block

'Connection: close



'200,8,"Via Adige, 21, 21013 Gallarate VA, Italia"

'CLOSED



'+CMGL: 1,"REC UNREAD","+","","10/10/25,15:15:19+08"

'My Wind. Ti ho cercato alle 15:15 del 25/10.
'                                            Info wind.it







'include "18F6722LANDO.BAS"

DEFINE OSC 16


'@		DEVICE BOD_OFF		'disabilita il reset quando cala la tensione	
'RCON.6=1
'@		DEVICE HS_OSC

'Configurazione usart 9600
define HSER_RCSTA 90h   

define HSER_TXSTA 24h

define HSER_BAUD 115200

define HSER_CLROERR 1


DEFINE ADC_BITS 10       ' Set number of bits in result
DEFINE ADC_CLOCK 3     ' Set clock source 
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds 



'DEFINE DEBUG_REG PORTB
'DEFINE DEBUG_BIT 4
'DEFINE DEBUG_BAUD 115200
'DEFINE DEBUG_MODE 0




'DEFINE ADC_BITS 8       ' Set number of bits in result
'DEFINE ADC_CLOCK 3     ' Set clock source 
'DEFINE ADC_SAMPLEUS 5000 ' Set sampling time in microseconds 

SYMBOL	POWERGSM =PORTC.5		'Alimentazione cellulare ON/OFF
SYMBOL  RESET    =PORTD.0       'RESET GSM
SYMBOL  LD1      =PORTD.7           ' LED1 ROSSO
SYMBOL  LD2      =PORTD.6           ' LED3 VERDE
SYMBOL  PULS     =PORTB.3
SYMBOL  RING     =PORTD.3           ' RING GSM
SYMBOL  INPIR    =PORTB.4           ' PIR
SYMBOL  MOV      =PORTB.0           ' MOVIMENTO
SYMBOL  SONDA    =PORTC.0           ' DS18B20
SYMBOL  ATTIVA   =PORTB.1           ' INGRESSO 2 DI ATTIVAZIONE SISTEMA DI ALLARME  APERTO ATTIVATO / CHIUSO DISATTIVATO
SYMBOL  IN1      =PORTB.2           ' IN1
SYMBOL	STATO    =PORTC.4		    'STATO



SYMBOL  TXPC     =PORTB.7



OUTPUT POWERGSM
OUTPUT LD1
OUTPUT LD2
OUTPUT RESET

INPUT PULS

TMP         VAR BYTE
TMP1        VAR word
TMP2        VAR BYTE
TMP3        VAR BYTE
TMPL        VAR LONG
TMPL1       VAR LONG
TMPW		VAR WORD
TMPW1   	VAR WORD
TIMESLEEP   VAR BYTE 
LEGGINUM	VAR WORD
LEGGINUM2  	VAR WORD
ADDRL2      VAR LONG
ORA	        VAR BYTE[25]
BUFF        VAR BYTE[160]
PWD 	    VAR BYTE[6]
NUMERO      VAR BYTE[25]
MITTENTE    VAR BYTE[25]
MITTENTEECHO  VAR BYTE[25]
POSIZIONE   VAR BYTE
LUNGNUM	    VAR BYTE
LUNGMESS    VAR BYTE
LUNGMITT    var byte
LUNGMITTECHO var byte
TROVATO     VAR BIT
PWDOK       VAR BIT
DATO        VAR BYTE
FRASE       VAR BYTE
ERR1        VAR BYTE
ERR2        VAR BYTE
'INDEX      VAR BYTE
TENTATIVI   VAR BYTE
RISPOSTA    VAR BYTE
CERCAMITTENTE   VAR BYTE
MULTIMESS   VAR BYTE
POSMULTIMESS    var byte
POSPUNTI    VAR BYTE
PULSANTE    VAR BYTE
NONREGISTRATO VAR BYTE
MINUTIGSM       VAR BYTE
SECONDIGSM      VAR BYTE
ORAGSM          VAR BYTE
GSMSECTOT       VAR LONG
GSMSEC          VAR WORD
GSMMIN          VAR WORD
GSMMINOLD       VAR WORD
GSMSECOLD       VAR WORD
VARSEC          VAR BYTE
VARMIN          VAR BYTE
ATTESAAZZERA    VAR BYTE
NUMERISMS       VAR BYTE
TEMPO           VAR BYTE
PCLATH_TEMP     var byte
STATUS_TEMP     var byte
W_TEMP          var byte  
POSTROVATO      VAR BYTE
SQUILLO         VAR BIT
MCCS            VAR BYTE[5]
MNCS            VAR BYTE[5]
MCC             VAR LONG
MNC             VAR LONG
LAC             VAR LONG
CID             VAR LONG
CONV            VAR BYTE
TEMP            VAR WORD
TEMPW           VAR WORD
SONDAOK         VAR BIT
ATTIVATO        VAR BIT
TIMEINIBIZIONE1 VAR WORD
TIMEINIBIZIONEP VAR WORD
TIMEINIBIZIONEM VAR WORD
TIMEINIBIZIONET VAR WORD
TIMEINIBIZIONEV VAR WORD
POSALL          VAR BYTE  
ALLARME         VAR BYTE
STATOIN1        VAR BYTE
'MODULOON        VAR BIT
'TEMPOSPEGNIMENTO    VAR BYTE    
VARIAZIONEIN    VAR BIT
VARIAZIONEPIR   VAR BIT
VARIAZIONEMOV   VAR BIT
BATT            VAR WORD
BATTR            VAR WORD

VER CON 1       'MODIFICARE LA VERSIONE SE SI CAMBIA 

'ADDBASE	CON	40000
'ADDFIN	CON 60000

ADCON0=%00000000
ADCON1=%00000000
ADCON2=%10000000
ANSEL=1
ANSELH=0
'INTCON=0
'INTCON2.7=0	'Abilita resistenze di pull-up
'INTCON3=0
INTCON      = %11011000     'Abilita interrupt on GPIO change
INTCON2     = %00000010	    'Abilita resistenze di pull-up E INTERRUPT SU RB
INTCON3     = 0   
IOCB        = %00010000     ' INTERRUPT SU RB4



CCP2CON=0
RCON.7=0
OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
WPUB.4=0





ON INTERRUPT GOTO PULSANTI

'parametri ok
MEMORIA1:


EEPROM 370,[1]    'FORMATO MESS SMS
'EEPROM 371,[1]    'FORMATO MESS EMAIL
'EEPROM 372,[1]    'FORMATO MESS URL


EEPROM 700,[0]   ' GESTISCO L'INGRESSO 1 COME ALLARME
EEPROM 701,[0]   ' GESTISCO IL SENSORE PIR
EEPROM 702,[0]   ' GESTISCO IL MOVIMENTO
EEPROM 703,[1]   ' GESTISCO IL SENSORE TEMPERATURA
EEPROM 704,[1]   ' GESTISCO L'ALLARME DELLA BATTERIA

EEPROM 705,[100] ' TENSIONE MINIMA DI ALLARME 10.0V

EEPROM 710,["S"]      ' INVIO I MESSAGGI a tutti gli utenti abilitati

EEPROM 711,[30]   ' TEMPO DI ATTIVAZIONE ALLARME
EEPROM 712,[30]   ' TEMPO STAND BY


EEPROM 768,[60]      'SLEEP PER 60 SECCNDI.. OGNI 60 SEOCNDI VIENE LETTO SE C'è UN SMS..


'DA 730 A 750 SALVO L'IMEI
'DA 751 A 767 SALVO IL NOME
EEPROM 751,["F","T","9","0","9"]
'DA 770 A 899 SALVO LE AREE GEOFENCE  
'GEO:1/I-O/NOMEAREA/LAT1/LON1/LAT2/LON2  
'LATITUDINE LONGITUDINE IN/OUT  NOME12CAR     'PER OGNI AREA CI SONO 43 CARATTERI
'LL LLLL    lll llll      I      NOME        '770÷801 area 1 '801÷832 area2 ' 832÷863 area3 


'EEPROM 900,[0]      'DA 900 A 1000 SALVO L'ULTIMO PUNTO VALIDO
EEPROM 1000,[0]         'ECHO DISABILITATO
EEPROM 1001,["1","2","3","4","5"]    'PASSWORD DI DEFAULT
EEPROM 1006,[0]                      'SE 0 PRIMA ACCENSIONE

EEPROM 1007,[10]                      'TEMPERATURA MINIMA DI ALLARME
EEPROM 1008,[25]                      'TEMPERATURA MASSIMA DI ALLARME

EEPROM 1009,[0]                      'abilita il mess all'avvio

EEPROM 1010,[127]                      'TEMPERATURA MINIMA RAGGIUNTA
EEPROM 1011,[255]                      'TEMPERATURA MASSIMA RAGGIUNTA

EEPROM 1012,[0]                      'SE DURANTE IL TEMPO DI INIBIZIONE L'INGRESSO 1 TORNA NELLO STATO STABILE, NON AZZERARE IL TEMPO
EEPROM 1014,[1]                      'TEMPO OSSERVAZIONE INGRESSO 1 (00÷59 SEC)

EEPROM 1016,[5]                      'TEMPO INIBIZIONE INGRESSO 
EEPROM 1017,[5]                      'TEMPO INIBIZIONE VOLTAGE 
EEPROM 1018,[0]                      'LIVELLO DI ALLARME PER L'INGRESSO 1 (PRESENZA TENSIONE)


EEPROM 1021,[255]                    'SMS ABILITATO SU TUTTI I NUMERI
'EEPROM 1022,[255]                    'EMAIL ABILITATA SU TUTTE LE EMAIL
EEPROM 1023,[0]                      'QUI SALVO I NUMERI CHE HANNO CHIESTO LE COORDINATE CON SQUILLO

'ADDBASE	CON	60160   'memorizzo gli 8 indirizzi email  ATTENZIONE: SCRIVE NELLA PARTE ALTA $10000 + 60160
'ADDFIN	CON 61000


'parametri di prova
'EEPROM 201,[0]    'MEMORIZZO NELLA LOCAZIONE 201 SE è LA PRIMA ACCENSIONE, 0=PRIMA
'EEPROM 702,[3]    'SE 1 I DATI DELLE COORDINARE VERRANNO INVIATI IN GRADI DECIMALI PER TUTTOCITTA' ALTRIMENTI IN GRADI MINUTI SECONDI PER FUGAWI
'EEPROM 703,[1]    'INVIO IN POLLING ATTIVATO
'EEPROM 704,[0]    'ORE TEMPO POLLING
'EEPROM 705,[1]   'MINUTI TEMPO POLLING
'EEPROM 206,[1]    'GPS NORMALMENTE acceso
'EEPROM 700,[1]    'NORMALMENTE INVIO email

'EEPROM 210,["1","2","3","4","5"]  'PASSWORD DI DEFAULT

LOW LD1
LOW LD2
FOR TMP=0 TO 3    
    TOGGLE LD1
    TOGGLE LD2
    PAUSE 100
NEXT TMP


STATOIN1=IN1

'TMP=0
'WHILE TMP<200
'    IF P1=0 THEN
'        HIGH LD1
'        TMP=0
'    ELSE
'        LOW LD1
'    ENDIF
'    IF P2=0 THEN
'        HIGH LD2
'        TMP=0
'    ELSE
'        LOW LD2
'    ENDIF
'    IF P3=0 THEN
'        HIGH LD3
'        TMP=0
'    ELSE
'        LOW LD3
'    ENDIF
'    TMP=TMP+1
'    PAUSE 10
'WEND


'gosub GSM_OFF

'LOOP:

'    GOSUB LEGGIGPS
'    PAUSE 2000


'GOTO LOOP

'MEMORIA DA 100 A 200 MEMORIZZO IL NUMERO DELL'EMAIL E L'EMAIL SEPARATE DALLA VIRGOLA E TERMINANI COL ;


''PROCEDURA DI ACCENSIONE:
''ALTO RESET
''ALTO POWER
''BASSO RESET
'pause 100

'SEROUT P2,2,["FT596 Versione Pic18F2620 V4",10,13]

 CLEAR
 
INTCON3.0=0 
INTCON3.1=0 
INTCON.1=0 
INTCON.0=0 
        
        
        


'DEBUG  "SYS UP DATI 1.0",13,10
'LOW RTS
HIGH POWERGSM       'SUL PIN HO UN LIVELLO LOGICO BASSO
HIGH RESET          'LO TENGO RESETTATO SUL PIN HO UN LIVELLO LOGICO BASSO
START:
    SEROUT2 TXPC,6,["-SYS UP V. ",#ver,13,10 ] 

'READ 703,TMP    
'IF TMP=1 THEN   ' SE C'è IL POLLING VADO A CARICARE IL TEMPO
'    'SEROUT2 TXPC,6,["-POLLING CONTINUO ATTIVATO",13,10 ]
'    POLLTIMEC=0
'    READ 704,POLLTIMECh         'RILEGGO IL TEMPO IMPOSTATO
'    READ 705,POLLTIMECl
                
'    'POLLTIMEC=POLLTIMEC+GSMSECTOT 
'    POLLTIMEC=((ORAIMP*60)*60)+(MINUTIIMP*60)
'ENDIF

'READ 712,TMP    
'IF TMP=1 THEN   ' SE C'è IL POLLING VADO A CARICARE IL TEMPO
'    'SEROUT2 TXPC,6,["-POLLING CONTINUO ATTIVATO",13,10 ]
'    READ 713,ORAIMP         'RILEGGO IL TEMPO IMPOSTATO
'    READ 714,MINUTIIMP
'    POLLTIMEM=((ORAIMP*60)*60)+(MINUTIIMP*60)
'ENDIF

FOR ADDRL2=200 TO 300
    WRITE ADDRL2,0        'SEGNO CHE HO INVIATO TUTTI I MESS
NEXT ADDRL2

'High LD1
'For TMP=0 TO 5
'	Toggle LD1
'	Pause 500
'Next TMP
'HIGH LD1
'PAUSE 1000




GOSUB ON_OFF

'FOR TMP=0 TO 10
'    TOGGLE LD1
'    PAUSE 500
'NEXT TMP
'LOW LD1

'FOR TMP=0 TO 10
'    TOGGLE LD2
'    PAUSE 500
'NEXT TMP
'LOW LD2



''PAUSE 2000 


GOSUB SETUP




READ 1006,TMP
IF TMP=0 THEN

    'SEROUT2 P2,6,[ "PRIMA ACCENSIONE",13,10 ]
    FOR TMP=0 TO 160
        WRITE TMP,255
    NEXT TMP    
    
    FOR tmp = 0 TO 24 ' Count from 0 to 5

      LOOKUP tmp,["ALLARME!! INGRESSO 1 ALTO"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=24
    POSIZIONE=1
    GOSUB MEMOMESS
    
    FOR tmp = 0 TO 25 ' Count from 0 to 5

    LOOKUP tmp,["ALLARME!! INGRESSO 1 BASSO"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=25
    POSIZIONE=2
    GOSUB MEMOMESS
    
    FOR tmp = 0 TO 36 ' Count from 0 to 5

      LOOKUP tmp,["ALLARME!! SUPERATA SOGLIA TEMPERATURA"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=36
    POSIZIONE=3
    GOSUB MEMOMESS
    
    FOR tmp = 0 TO 36 ' Count from 0 to 5

    LOOKUP tmp,["ALLARME!! SUPERATA SOGLIA TEMPERATURA"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=36
    POSIZIONE=4
    GOSUB MEMOMESS


    FOR tmp = 0 TO 28 ' Count from 0 to 5

    LOOKUP tmp,["ALLARME!! RILEVATA INTRUSIONE"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=28
    POSIZIONE=5
    GOSUB MEMOMESS
    
    
    FOR tmp = 0 TO 27 ' Count from 0 to 5

    LOOKUP tmp,["ALLARME!! RILEVATO MOVIMENTO"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=27
    POSIZIONE=6
    GOSUB MEMOMESS    
    
    FOR tmp = 0 TO 13 ' Count from 0 to 5

    LOOKUP tmp,["SYSTEM STARTUP"],TMP1 ' Get character number B0 from string to variable B1
      buff[tmp]=tmp1
     
      'SEROUT 0,N2400,[B1] ' Send character in B1 to Pin0 serially
    NEXT tmp ' Do next character
    lungnum=13
    POSIZIONE=7
    GOSUB MEMOMESS
    
    write 1006,1
ENDIF
   
    
For POSIZIONE=1 TO 7
    gosub LEGGIMESS
NEXT POSIZIONE   
    




gosub leggisms




MEMOMASTER:
    READ 0,TMP
    IF TMP=255 THEN   'SE NON è PRESENTE IL MASTER VADO CICLICAMENTE IN CHIAMA PER UN TOTALE DI 3 MINUTI E FACCIO LAMPEGGIARE IL LED ROSSO

        
       SEROUT2 TXPC,6,["ASPETTO MASTER",13,10] 
        FOR TMPW=0 TO 720         '60 * 3 SECONDI = 180 SECONDI = 3MINUTI
                IF RING=0 THEN
                    SEROUT2 TXPC,6,[ "CHIAMATA",13,10 ]
                    FOR TMP=0 TO 10 
                        PAUSE 100
                        TOGGLE LD1
                    NEXT TMP
                    GOSUB CHIAMATA
                    POSIZIONE=1
                    FOR TMP=0 TO LUNGMITT
                        numero[tmp]=MITTENTE[TMP]
                    NEXT TMP
                    LUNGNUM=LUNGMITT                  
                    GOSUB MEMONUMERO    
                    FOR TMP=0 TO 20
                        TOGGLE LD1
                        PAUSE 100
                    NEXT TMP
                    'CLEAR
    '                GOSUB DORMI
                    low ld1
                    
                    GOTO MAIN1
                ENDIF
                PAUSE 250            
                HIGH LD1           'DURANTE L'ATTESA DELLA CHIAMATA IL LD1 LAMPEGGIA OGNI 3 SECONDI            
        NEXT TMPW
    ENDIF
    low LD1 


MAIN1:
CLEAR

GOSUB STARTTIME 'COSì VARSECTOT=0
'GOSUB PIR
'GOSUB TEMPERATURA      'COSì VEDO SE C'è IL SENSORE
'GOSUB VERIFICAINGRESSI
    SONDAOK=1 'DICO CHE LA SONDA C'è
    TMP=0
    OWOUT sonda, 1, [$CC, $44]
    OWIN sonda, 4, [CONV]
    TMP=TMP+1
    PAUSE 3
    IF TMP=250 THEN
        SEROUT2 txpc,6,[ "SONDA NON PRESENTE",13,10]
        SONDAOK=0      ' LA SONDA NON C'è NON VENIRE A LEGGERLA PIù...
    ENDIF


READ 1009,TMP
IF TMP=1 THEN
    RISPOSTA=1
    POSIZIONE=1
    gosub legginumeri
    FOR TMP3=0 TO LUNGNUM
        MITTENTE[TMP3]=NUMERO[TMP3]
    NEXT TMP3
    LUNGMITT=LUNGNUM
    IF LUNGMITT<>0 AND LUNGMITT<>255 THEN
        frase=19
        GOSUB INVIOSMS                   
    ENDIF 
ENDIF 


MAIN:





'    HSEROUT ["AT+CGATT?",13]  
'    HSERIN 2000,EXITIMPOSTAGPRS,[wait ("+CGATT: "),TMP]
'    PAUSE 500
'    IF TMP="1" THEN
'        HSEROUT ["AT+CGATT=0",13]    
'    ENDIF
'    PAUSE 500
    
    'sleep 10


'        IF LOWPOWER=0 THEN
'            INTCON3.5=0

    'SEROUT2 TXPC,6,["ON OFF ",#MODULOON,13,10] 
    'IF MODULOON=1 THEN   'SE IL GMS è ACCESO
            
            HSEROUT ["AT+CSQ",13] 
            HSERIN 2000,CHIAMANO,[wait ("+CSQ: "),STR BUFF\5\13]
            PAUSE 100
            HSEROUT ["AT+CGREG?",13]  '+CREG: 2,1,271C,998D
            PAUSE 100
            
                HSEROUT ["AT+COPS?",13]                 
                '+COPS: 0,2,"22210"
                FOR TMP=0 TO 5
                    MNCS[TMP]=0
                NEXT TMP
                HSERIN 2000,CHIAMANO,[WAIT ("+COPS: "),TMP,TMP,TMP,TMP,TMP,MCCS[0],MCCS[1],MCCS[2],STR MNCS\5\34]
                PAUSE 50     
                MCC=0
                FOR TMP=0 TO 2                                                                          
                    MCC=(MCC*10)+(MCCS[TMP]-48)
                NEXT TMP
                
                MNC=0
                FOR TMP=0 TO 6
                    IF MNCS[TMP]<>0 THEN                                                                            
                        MNC=(MNC*10)+(MNCS[TMP]-48)
                    ELSE
                        TMP=7
                    ENDIF                    
                NEXT TMP
                
                HSEROUT ["AT+CREG?",13]                
                HSERIN 2000,CHIAMANO,[WAIT ("+CREG: "),TMP1,TMP,TMP2,TMP,HEX4 LAC,TMP,HEX4 CID]
                PAUSE 100
                '+CREG: 2,1,271C,998D
                IF TMP1<>"2" OR (TMP2<>"1" AND TMP2<>"5") THEN  '5 SE è IN ROAMING   'attenzione al 2= richiedo anche le celle
        '            HSEROUT ["AT+CREG=1",13]
        '            PAUSE 1000
                    SEROUT2 TXPC,6,["NO ",#NONREGISTRATO,32,STR BUFF\5,13,10]
                    NONREGISTRATO=NONREGISTRATO+1
                    IF NONREGISTRATO=15 THEN
                        NONREGISTRATO=0
                        GOTO START
                    ENDIF                              
                    'HIGH LD2
                    'PAUSE 50
                    'LOW LD2
                else
                    NONREGISTRATO=0 
                    SEROUT2 TXPC,6,["REG ",STR BUFF\5,13,10]
                    SEROUT2 TXPC,6,["MCC ",DEC MCC,13,10]       'NUMERO
                    SEROUT2 TXPC,6,["MNC ",DEC MNC,13,10]       'NUMERO
                    SEROUT2 TXPC,6,["LAC ",DEC LAC,13,10]       'NUMERO
                    SEROUT2 TXPC,6,["CID ",DEC CID,13,10]       'NUMERO

                    
                ENDIF

        
        
                
        GOSUB LEGGISMS
    
    


'        SELECT CASE PULSANTE
'            CASE 10 'PRESSIONE BREVE DI P1
'                SEROUT2 TXPC,6,["INVIO SOS",13,10] 
    
                
'                NOTIFICASQUILLO=1        'verificare
'                MOTIVOCOORD=5
'                'RITENTA=1       'IMPOSTANDO RITENTA=1 INVIA LE COORDINATE FINO A QUANDO NON SAREANNO DISPONIBILI
'                PULSANTE=0
'                'gosub inviocoordinate
'                'gosub REVERSEGEO
'            CASE 11 'PRESSIONE LUNGA DI P1
'                'SEROUT2 TXPC,6,["BHO",13,10]     
'                PULSANTE=0 
    
'        END SELECT



        IF RING=0 THEN
            SQUILLO=1
            GOTO CHIAMANO
        ENDIF 




'        IF RING=0 THEN
'            SQUILLO=1
'            GOTO CHIAMANO
'        ENDIF    
    

    
CHIAMANO:                          
        IF SQUILLO=1 or ring=0 THEN           
    '        SEROUT2 TXPC,6,[ "CHIAMATA",13,10 ]
    '        GOSUB SVEGLIA
            GOSUB CHIAMATA
            SQUILLO=0
        ENDIF
    'ELSE
        'IF PULS=0 and MODULOON=0 THEN  
'        IF PULS=0 THEN  
'            GOSUB RIAVVIA
'        ENDIF
    
    'ENDIF   'IF SE GSM è ACCESO
    
    
'        READ 1020,TMP
'        IF TMP=1 THEN
'            MODULOON=1    
'        ELSE    'SE DEVO SPEGNERE IL MODULO
'            IF MODULOON=1 AND TEMPOSPEGNIMENTO=0 THEN  'SE IL MODULO è ACCESO
'                READ 712,TEMPOSPEGNIMENTO
'                SEROUT2 TXPC,6,[ "TEMPOSPEGNIMENTO ",#TEMPOSPEGNIMENTO,13,10 ] 
'            ENDIF
'        ENDIF
    
    GOSUB INGRESSOATTIVAZIONE
    IF ATTIVATO=1 THEN
        INTCON      = %11011000     'Abilita interrupt on GPIO change
        INTCON2     = %00000010	    'Abilita resistenze di pull-up E INTERRUPT SU RB 
        READ 700,TMP
        IF TMP=1 THEN           
            GOSUB VERIFICAINGRESSI  
        endif
        
        READ 701,TMP
        IF TMP=1 THEN
            GOSUB PIR 
        endif
        
        READ 702,TMP
        IF TMP=1 THEN
             GOSUB SENSOREMOV             
        ENDIF   
        
        READ 704,TMP
        IF TMP=1 THEN
             GOSUB VERIFICABATT
        ENDIF
        
        'SEROUT2 TXPC,6,[ "SONDAOK ",#SONDAOK,13,10]
        READ 703,TMP
        IF TMP=1 THEN
            IF SONDAOK=1 THEN    GOSUB TEMPERATURA
            'POSALL=5    POS 3 E 4
        endif
        HIGH LD2
    ELSE
        INTCON      = 0     'Disabilita interrupt 
        INTCON2     = 0     'Disabilita interrupt
        LOW LD2
        VARIAZIONEIN=0
        VARIAZIONEMOV=0
        VARIAZIONEPIR=0
        
    ENDIF
    
    'IF TIMEINIBIZIONE1>0 OR TIMEINIBIZIONET>0 OR TIMEINIBIZIONEP>0 OR TIMEINIBIZIONEM>0 OR TEMPOSPEGNIMENTO>0 THEN
    IF TIMEINIBIZIONE1>0 OR TIMEINIBIZIONET>0 OR TIMEINIBIZIONEP>0 OR TIMEINIBIZIONEM>0 OR TIMEINIBIZIONEv>0  THEN
        GOSUB time
    ENDIF   
    
    
    READ 768,TMP
    FOR TIMESLEEP=0 TO (tmp/2)   'diviso due perchè in realtà lo sleep dura circa 2 sec
        'SEROUT2 TXPC,6,[ "TIMESLEEP ",#TIMESLEEP," TMP",#TMP,13,10]        
        sleep 1
        'PAUSE 100
        IF PULS=0 OR ATTIVA=1 THEN
            TIMESLEEP=900    'COSì ESCO
        ENDIF
        'IF RING=0 AND MODULOON=1 THEN
        IF RING=0 THEN
            SEROUT2 TXPC,6,[ "CHIAMATA",13,10]
            HSEROUT ["AT",13]
            pause 500
            GOSUB CHIAMATA
            TMPW=900    'COSì VADO A VEDERE SE ERA UN SMS
        ENDIF  
    NEXT TIMESLEEP
    


    
    
GOTO MAIN



RIAVVIA:
        SEROUT2 TXPC,6,[ "ACCENDO GSM",13,10 ] 
        GOSUB ON_OFF
        GOSUB SETUP   
RETURN


CHIAMATA:
'    INTCON3.5=0
'    CHIAMA=0
    SEROUT2 TXPC,6,[ "CHIAMATA",13,10]
    HIGH LD1
            HSERIN 10000,EXITCHIAMATA,[WAIT ("+CLIP: ",34),STR MITTENTE\20]   'SE DOPO 10 SECONDI NON ARRIVA LA CHIAMATA REINVIO I SETTAGGI    - IN TMP METTO IL +39
        '***** FINE PROVA
        
        HIGH LD1
        HIGH LD2
        FOR TMP=0 TO 20
                TMP1=MITTENTE[TMP]
                IF TMP1=34 THEN
                    LUNGMITT=TMP-1
                    'SEROUT2 TXPC,6,[ "LUNGMITT1: ",#LUNGMITT,13,10]
                    TMP=35
                ENDIF
        NEXT TMP
            
        'MITTENTE[0]="+"            
        SEROUT2 TXPC,6,[ "CHIAM DA: "]
        
        FOR TMP=0 TO LUNGMITT
            SEROUT2 TXPC,6,[ MITTENTE[TMP]]
        NEXT TMP
        SEROUT2 TXPC,6,[ 13,10]
        SEROUT2 TXPC,6,[ "LUNGMITT ",#LUNGMITT,13,10 ]
        
        
        hserout["ATH",13] 
        pause 1000
        FOR TMP=0 TO 10
            TOGGLE LD1
            PAUSE 200
        NEXT TMP
        LOW LD1
        
    SEROUT2 TXPC,6,[ "-CHIAMATA DA: "]
    FOR TMP=0 TO LUNGMITT
        SEROUT2 TXPC,6,[ MITTENTE [TMP]]
    NEXT TMP
    SEROUT2 TXPC,6,[ 13,10]
'    HSEROUT ["AT&V",13]
'     PAUSE 1000
    CERCAMITTENTE=1
    GOSUB CERCANUMERO       'VERIFICO SE è PRESENTE
    'GOSUB GESTCHIAMATA
    IF TROVATO=1 THEN

    ENDIF

                          
EXITCHIAMATA:

    HSEROUT ["AT",13] 'CONTROLLO CHE NON DIA L'ERRORE 
    HSERIN 3000,RIAVVIA,[WAIT ("OK"),tMP]  

RETURN






LEGGISMS:
'INTCON3.5=0

 
        
        FOR TMP=0 TO 160
            BUFF[TMP]=13
        NEXT TMP      
        

        
'        SEROUT2 TXPC,6,[ "SMS",13,10 ] 

            RISPOSTA=1

            HSEROUT ["AT+CMGL=",34,"ALL",34,13]
            
            HSERIN 1000,EXITSMS,[SKIP 10,WAIT (34,",",34),STR MITTENTE\25\44]  
            HSERIN 1000,EXITSMS,[SKIP 1,WAIT (34,",",34),STR ora\25\34]          
            HSERIN 1000,LEGGISMS,[WAIT (10),STR BUFF\160\10]                     
                        
            
            
                 
            LOW LD1
            
            FOR TMP=0 TO 10
                TOGGLE LD1
                PAUSE 200
            NEXT TMP
            LOW LD1
            
            SEROUT2 TXPC,6,["AT+CCLK=",34,STR ORA,34,13,10]      'ORA AZZERATA               
            HSEROUT ["AT+CCLK=",34,STR ORA,34,13]      'ORA AZZERATA 
            
            
            FOR TMP=0 TO 25
                TMP1=MITTENTE[TMP]
                IF TMP1=34 THEN
                    LUNGMITT=TMP
                    'SEROUT2 TXPC,6,[ "LUNGMITT1: ",#LUNGMITT,13,10 ]
                    TMP=35
                ENDIF
            NEXT TMP
 
            LUNGMITT=LUNGMITT-1      
            
            '****PROVA PER SPAGNA
'            FOR TMP=0 TO LUNGMITT
'                MITTENTE[TMP]=MITTENTE[TMP+3]
'            NEXT TMP
            
'            LUNGMITT=LUNGMITT-3 
            '**** FINE PROVA PER SPAGNA
            
            'SEROUT2 TXPC,6,[ "LUNGMITT: ",#LUNGMITT,13,10 ]
            
'            IF LOWPOWER=1 THEN
'                GOSUB SVEGLIA
'            ENDIF 
        
        
            SEROUT2 TXPC,6,[ "-MITTENTE: " ]
            FOR TMP=0 TO LUNGMITT
                SEROUT2 TXPC,6,[ MITTENTE[TMP] ]
            NEXT TMP
            SEROUT2 TXPC,6,[ 13,10  ]
            
         
         
                    
            FOR TMP=0 TO 160
                'TMP1=BUFF[TMP]
                IF BUFF[TMP]=13 THEN
                    LUNGMESS=TMP
                    TMP=170
                ENDIF
            NEXT TMP
            'SEROUT2 TXPC,6,[ "LUNGMESS : ",#LUNGMESS,13,10 ]
            'SEROUT2 TXPC,6,[ "BUFF[0]: ",BUFF[0],13,10 ]
            SEROUT2 TXPC,6,[ "-MESSAGGIO: "  ]
            FOR TMP=0 TO LUNGMESS
                SEROUT2 TXPC,6,[ BUFF[TMP] ]
            NEXT TMP
            SEROUT2 TXPC,6,[ 13,10  ]
            FOR TMP1=0 TO 4    
                PWD[TMP1]=0     'AZZERO LA PASSWORD
            NEXT TMP1
            
            FOR TMP=0 TO LUNGMESS          'METTO IN PWD[X] LA PASSWORD
                IF BUFF[TMP]=";" THEN
                    'SEROUT2 TXPC,6,[ "PUNTO E VIRGOLA",13,10 ]
                    TMP=TMP+1
                    FOR TMP1=0 TO 4    
                        PWD[TMP1]=BUFF[TMP+TMP1]
                    NEXT TMP1
                    TMP=200
                ENDIF
            NEXT TMP
            SEROUT2 TXPC,6,[ "-PWD: "  ]
            FOR TMP=0 TO 4
                SEROUT2 TXPC,6,[ PWD[TMP]]
            NEXT TMP
            SEROUT2 TXPC,6,[ 13,10  ]
            
            'GOSUB VERIFICASMS
             '   SEROUT2 TXPC,6,[ "VERIFICASMS",13,10 ]
    

                
            
CANCELLASMS:            
            'PAUSE 3000
            


'            SEROUT2 txpc,6,[ "-CANC TUTTO",13,10 ]           
            HSEROUT ["AT+CMGD=0,4",13]   'CANCELLO TUTTI I MESSAGGI
            HSERIN 10000,EXITCANCELLASMS,[wait ("OK")]
            PAUSE 1000  

EXITCANCELLASMS:


            CERCAMITTENTE=1
            GOSUB CERCANUMERO
'            IF TROVATO=0 THEN
'                SEROUT2 TXPC,6,[ "-NO NUMERO",13,10 ]
'            ENDIF
            GOSUB VERIFICAPWD
            IF TROVATO=1 OR PWDOK=1 THEN    'SE IL NUMERO è IN MEMORIA O LA PASSWORD è OK VADO AVANTI O IL FILTRO SU SMS è DISABILITATPO
                'SEROUT2 TXPC,6,[ "TROVATO=1 OR PWDOK=1",13,10 ]
                MULTIMESS=1
                WHILE MULTIMESS=1
                '    SEROUT2 TXPC,6,[ "-MULTI",13,10]
                    IF LUNGMITT>1 THEN
                        GOSUB ANALIZZASMS
                        'pause 3000
                       ' RETURN
                    ELSE
                        MULTIMESS=0
                    ENDIF
                WEND
            ELSE        'SE LA PWD è KO E IL NUMERO NON è IN MEMO CONTROLLO SE HO ABILITATO L'ECHO
                READ 1000,TMP
                IF TMP<>0 THEN
                    GOSUB INVIOECHO
                ENDIF
            ENDIF


EXITSMS:
            'PAUSE 200
'            SEROUT2 TXPC,6,[ "EXIT SMS",13,10     ]       
            'HSEROUT ["AT+CMGL=",34,"ALL",34,13] 'CONTROLLO CHE NON DIA L'ERRORE SULLA LETTURA DEL MESSAGGIO
            'HSERIN 3000,RIAVVIASMS,[WAIT ("OK"),TMP]  
            PAUSE 200
        
        'HSEROUT ["AT+CCLK=",34,"01/01/01,",DEC2 ORAGSM,58,DEC2 MINUTIGSM,58,DEC2 SECONDIGSM,"+00",34,13]    'RIPRISTINO L'ORA PER EVITARE PROBLEMI DI TIMING
'INTCON3.5=1
RETURN

'RIAVVIASMS:

'    GOSUB ON_OFF
''    low  LED
'    GOSUB SETUP
'    PAUSE 5000

'RETURN

ANALIZZASMS:

    SEROUT2 TXPC,6,["-ANALIZZA SMS ",BUFF[0],13,10]
    
    IF BUFF[0]<>"?" THEN    'SE C'è IL ? VUOL DIRE CHE è UN MESS TIPO TR102 E ALLORA NON PUò ESSERE UN MULTIMESS
        FOR TMP=0 TO LUNGMESS
            IF BUFF[TMP]="," THEN
                MULTIMESS=1
                POSMULTIMESS=TMP
                TMP=200
                SEROUT2 TXPC,6,["MULTI",13,10 ]
                'pause 1000
            ELSE
                MULTIMESS=0
                'SEROUT2 TXPC,6,["NON MULTIMESSAGGIO",13,10 ]
            ENDIF
        NEXT TMP
    ELSE
        MULTIMESS=0
    ENDIF
    

    FRASE=0
    SELECT CASE BUFF[0]
               
            
        CASE "A","a"     
        
            SELECT CASE BUFF[1]
            CASE "V","v"
                IF BUFF[3]<>"?" THEN            
                    SEROUT2 TXPC,6,[ "MESS INIZIALE",13,10 ]
                    TMP=BUFF[3]-48
                    WRITE 1009,TMP                    
                ENDIF
                FRASE=9
                
            CASE "L","l"    
                FRASE=14
                SELECT CASE BUFF[3]
                CASE "M","m"  'allmin  allmax
                    TEMP=0
                        IF BUFF[7]="-" THEN
                            TEMP.7=1                    
                        ENDIF 
                        TEMP=TEMP+((BUFF[8]-48)*10)+(BUFF[9]-48)
                        SEROUT2 TXPC,84,[ "TEMPERATURA DI ALLARME ",#TEMP,13,10] 
                        SELECT CASE BUFF[5]
                            CASE "N","n"  'min  
                                WRITE 1007,TEMP                              
                            CASE "X","x"  'max
                                WRITE 1008,TEMP                         
                        END SELECT
                CASE "V","v"   ' ALLVOLT:xxx
                        BATT=((BUFF[8]-48)*100)+((BUFF[9]-48)*10)+(BUFF[10]-48)
                        SEROUT2 TXPC,84,[ "TENSIONE DI ALLARME ",#BATT,13,10] 
                        WRITE 705,BATT           

                END SELECT
            END SELECT
            
        CASE "E","e"    'ECHO:0 ECHO DISABILITATO    ECHO:2 ECHO ABILITATO VERSO IL NUMERO 2    ECHO? CHIEDE COME è MESSO...
                IF BUFF[4]<>"?" THEN            
                    SEROUT2 TXPC,6,[ "ECHO",13,10 ]
                    TMP=BUFF[5]-48
                    WRITE 1000,TMP                    
                ENDIF
                FRASE=24    


            
        
        CASE "I","i"           'INI:xx
            IF BUFF[3]<>"?" THEN            
                TMP=(BUFF[4]-48)*10+(BUFF[5]-48)
                IF TMP>=0 AND TMP<=59 THEN
                    SEROUT2 TXPC,6,[ "TEMPO INIB ",#TMP,13,10 ]
                            WRITE 1016,TMP
                            TIMEINIBIZIONE1=TMP 
                ELSE
                    SEROUT2 TXPC,6,[ "TEMPO NON VALIDO ",#TMP,13,10 ]
                ENDIF
                
            ENDIF
            FRASE=4
            
            
        CASE "L","l"    'LIV:A
            IF BUFF[3]<>"?" THEN            
                
                    SELECT CASE BUFF[4]
                        CASE "A","a"
                            WRITE 1018,0                
                        CASE "B","b"
                            WRITE 1018,1
                        CASE "V","v"
                            WRITE 1018,2                         
                    END SELECT
                
            ENDIF
            FRASE=4
                
         CASE "N","n"    
            SELECT CASE BUFF[1]
            CASE "U","u"
                SEROUT2 TXPC,6,[  "-NUM",13,10 ]
                POSIZIONE=BUFF[3]-48
                'NUMx+393355760937;12345 PER LA SPAGNA MEMORIZZO ANCHE IL, +34
                IF BUFF[4]="+" THEN    'SE IL MESSAGGIO  è NUMx+39...
                    TMP1=((POSIZIONE-1)*19)
                    read tmp1,tmp
                    IF TMP=255 OR PWDOK=1 THEN  ' SE IN QUELLA POSIZIONE NON C'è NESSUN NUMERO O LA PASSOWRD è CORRETTA ALLORA MEMROIZZO IL MESS
                        SEROUT2 TXPC,6,["-CANC POS ",#POSIZIONE,13,10 ]
                        FOR TMP=0 TO 18
                            TMP1=((POSIZIONE-1)*19+TMP)
                            WRITE TMP1,255
                        NEXT TMP
                        FOR TMP=4 TO LUNGMESS
                            'SEROUT2 TXPC,6,[  "TMP ",#TMP,13,10 ]
                            IF BUFF[TMP]=","  OR BUFF[TMP]=";" OR TMP=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
                                LUNGNUM=TMP-1-4
                                'SEROUT2 TXPC,6,["-LUNGNUM ",#lungnum,13,10 ]
                                GOSUB MEMONUMERO
                                FRASE=2                        
                                TMP=200 'SERVE PER USCIR DAL CICLO DI FOR
                            ELSE
                                NUMERO[TMP-4]=BUFF[TMP]
                                SEROUT2 TXPC,6,[ NUMERO[TMP-4] ]
                            ENDIF
                        NEXT TMP
                    ELSE
                        FRASE=1
                    ENDIF
                
                ELSE
                        FRASE=2
                    if BUFF[3]<>"?" then                        'SE IL MESSAGGIO  è NUMx                    
                        SEROUT2 TXPC,6,["-CANC POS ",#POSIZIONE,13,10 ]
                        FOR TMP=0 TO 18
                            TMP1=((POSIZIONE-1)*19+TMP)
                            WRITE TMP1,255
                        NEXT TMP
                    endif                   
                    
                ENDIF
            CASE "A","a"    'NAME
                SEROUT2 TXPC,6,["NOME ",13,10 ]
                    IF BUFF[4]=":" THEN
                        FOR TMP=0 TO 30
                            tmp2=tmp+5
                            IF BUFF[TMP2]=","  OR BUFF[TMP2]=";" OR TMP2=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
                                WRITE 751+TMP,"#"
                                tmp=31
                            ELSE                 
                                WRITE 751+TMP,BUFF[TMP2]
                                SEROUT2 TXPC,6,[BUFF[TMP2]]
                            ENDIF
                        NEXT TMP
                    ENDIF
                    FRASE = 23
            END SELECT
                                              
        CASE "P","p"
            SELECT CASE BUFF[1]    

                
            CASE "W","w"
                SEROUT2 TXPC,6,["-PWD"]
                IF PWDOK=1 THEN
                    'SEROUT2 TXPC,6,[ "-CAMBIO PWD",13,10]
                    FOR TMP=0 TO 4
                        WRITE (1001+TMP),BUFF[3+TMP]
'                        SEROUT2 TXPC,6,[  BUFF[3+TMP]]
                    NEXT TMP
'                        SEROUT2 TXPC,6,[ 13,10  ]
                    FRASE=3
                ELSE
                    FRASE=1
                ENDIF
            END SELECT
            
        CASE "R","r"    'RES
            SELECT CASE BUFF[1]
            CASE "E","e"       'VERIFICARE

                    if PWDOK=1 then
                        SEROUT2 TXPC,6,[ "-RESET",13,10 ] 
                        WRITE 1001,"1"
                        WRITE 1002,"2"
                        WRITE 1003,"3"
                        WRITE 1004,"4"
                        WRITE 1005,"5"
                        WRITE 1006,0                      
                        WRITE 1007,1                      
                        WRITE 1021,255                      
                        WRITE 1023,0                     
                        WRITE 700,0  
                        WRITE 709,"S"
                        WRITE 710,"S"
                        WRITE 703,0
                        WRITE 704,14
                        WRITE 705,100
                        WRITE 712,30
                        frase=20
                    ELSE
                        FRASE=1 
                    endif 

            CASE "I","i"    'rips
'                    if BUFF[2]="P" OR BUFF[2]="p" THEN
'                        IF BUFF[3]<>"?" THEN            
'                            SEROUT2 TXPC,6,[ "-RIPRISTINO",13,10 ]
'                            TMP=BUFF[3]-48
'                            WRITE 1009,TMP
                            
'                        ENDIF
                                      
'                    ELSE
                        SEROUT2 TXPC,6,[ "-NO RISP",13,10 ]
                        RISPOSTA=0
'                    ENDIF
            END SELECT
            
        CASE "O","o"        'OSS:XX
                
                    IF BUFF[3]<>"?" THEN            
                        TMP=(BUFF[4]-48)*10+(BUFF[5]-48)
                        IF TMP>=0 AND TMP<=59 THEN
                            SEROUT2 TXPC,84,[ "TEMPO OSS ",#TMP,13,10 ]
                            WRITE 1014,TMP
                        ENDIF
                    ENDIF
                    FRASE=4
            
                    
            
                
        CASE "S","s"    'SMS                  'SPIR  SMOV STEM SING
            'IF BUFF[1]="M" or BUFF[1]="m" THEN     'SMs
                select case buff[2]
                    case "S","s"
                        FRASE=2
                        SEROUT2 TXPC,6,[ "-SMS",13,10]
                            FOR TMP=0 TO LUNGMESS
                                IF BUFF[TMP]=":" THEN
                                    
                                    POSPUNTI=TMP
                                    TMP=200
                                    SEROUT2 TXPC,6,[ ": ",#POSPUNTI,13,10 ]
                                    pause 1000
                                ELSE
                                    FRASE=0
                                    'SEROUT2 TXPC,6,[  "NON MULTIMESSAGGIO",13,10]
                                ENDIF
                            NEXT TMP
                            
                            READ 1021,TMP3
                            GOSUB CALCOLANUM
                            WRITE 1021,TMP3
                            FRASE=2
                            
                    

                    case "E","e"    'SLeEP:xxx
                        FRASE=8 
                        TMP=(BUFF[6]-48)*100+(BUFF[7]-48)*10+(BUFF[8]-48)  
                        write 768,tmp  
                    
                    
                    CASE "I","i"    '    SAi
                    
                        TMP=BUFF[4]-48
                        WRITE 700,TMP
                        FRASE=6
                        TIMEINIBIZIONE1=0
                    CASE "P","p"    '   SAp:0
                        TMP=BUFF[4]-48
                        WRITE 701,TMP
                        FRASE=6
                        VARIAZIONEPIR=0
                        TIMEINIBIZIONEP=0
                    CASE "M","m"    '    SAm
                        TMP=BUFF[4]-48
                        WRITE 702,TMP                    
                        FRASE=6
                        VARIAZIONEMOV=0
                        TIMEINIBIZIONEM=0
                    CASE "B","b"    '    SAb
                        TMP=BUFF[4]-48
                        WRITE 704,TMP                    
                        FRASE=6
                        TIMEINIBIZIONEV=0
                    CASE "T","t"    '     SAt
                        TMP=BUFF[4]-48
                        WRITE 703,TMP   
                        FRASE=6
                        TIMEINIBIZIONET=0
                    CASE "L","l"    'SAl   PER RICHIEDERE COME SONO MESSI I SENSORI
                        FRASE=6
                                
                END SELECT
                
                
        CASE "T","t"
            SELECT CASE BUFF[1]
            
                CASE "A","a"   'TATT
                    TMP=(BUFF[5]-48)*10+(BUFF[6]-48)  
                    write 711,tmp
                    FRASE=7
'                CASE "D","d"   'TDEA      
'                    TMP=(BUFF[5]-48)*10+(BUFF[6]-48)  
'                    write 712,tmp  
'                    FRASE=7

                CASE "E","e"               'TEMPERATURA
                    FRASE=11
                CASE "R","r"               'TRES   RESET MIN E MAX                                                             
                    WRITE 1010,127                      'TEMPERATURA MINIMA RAGGIUNTA
                    WRITE 1011,255                      'TEMPERATURA MASSIMA RAGGIUNTA
                    FRASE=12
                    
'                CASE "L","l"        'TLO
'                    POSIZIONE=1
'                      FOR TMP=4 TO LUNGMESS
'                            'SEROUT2 P2,84,[  "TMP ",#TMP,13,10 ]
'                            IF BUFF[TMP]=";" OR TMP=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
'                                LUNGNUM=TMP-4
'                                SEROUT2 TXPC,84,[  "LUNGNUM ",#lungnum,13,10 ]
'                                'POSIZIONE=1
'                                FOR TMP=LUNGNUM TO 250
'                                    BUFF[TMP]=255
'                                NEXT TMP
'                                GOSUB MEMOMESS
'                                FRASE=15                       
'                                TMP=200 'SERVE PER USCIR DAL CICLO DI FOR
'                            ELSE
'                                BUFF[TMP-4]=BUFF[TMP]
'                                SEROUT2 TXPC,84,[ BUFF[TMP-4] ]
'                            ENDIF
'                        NEXT TMP            
            
'                CASE "H","h"        'THI
'                    POSIZIONE=2
'                        FOR TMP=4 TO LUNGMESS
'                            'SEROUT2 P2,84,[  "TMP ",#TMP,13,10 ]
'                            IF BUFF[TMP]=";" OR TMP=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
'                                LUNGNUM=TMP-4
'                                SEROUT2 TXPC,84,[  "LUNGNUM ",#lungnum,13,10 ]
'                                'POSIZIONE=1
'                                FOR TMP=LUNGNUM TO 250
'                                    BUFF[TMP]=255
'                                NEXT TMP
'                                GOSUB MEMOMESS
'                                FRASE=15                       
'                                TMP=200 'SERVE PER USCIR DAL CICLO DI FOR
'                            ELSE
'                                BUFF[TMP-4]=BUFF[TMP]
'                                SEROUT2 TXPC,84,[ BUFF[TMP-4] ]
'                            ENDIF
'                        NEXT TMP
                        
                CASE "I","i"        'TIN     TIZ
                    SELECT CASE BUFF[2]
                        CASE "N","n"
                            POSIZIONE=0
                                SELECT CASE BUFF[3]
                                    CASE "1"    'TINI1H  TINI1L
                                        IF BUFF[4]="H" OR BUFF[4]="h" THEN
                                            POSIZIONE=1
                                        ELSE
                                            POSIZIONE=2
                                        ENDIF
                                    CASE "P","p"  'TINPIR
                                        POSIZIONE=5
                                    CASE "M","m"  'TINMOV
                                        POSIZIONE=6                            
                                    CASE "T","t"  'TINTEh
                                        IF BUFF[5]="H" OR BUFF[5]="h" THEN
                                            POSIZIONE=3
                                        ELSE
                                            POSIZIONE=4
                                        ENDIF                                
                                    
                                END SELECT
                                      IF POSIZIONE<>0 THEN
                                          FOR TMP=7 TO LUNGMESS
                                                'SEROUT2 TXPC,6,[  "TMP ",#TMP,10,13 ]
                                                IF BUFF[TMP]=";" OR TMP=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
                                                    LUNGNUM=TMP-6
                                                    SEROUT2 TXPC,6,[  "LUNGNUM ",#lungnum,13,10 ]
                                                    'POSIZIONE=1
                                                    FOR TMP=LUNGNUM TO 250
                                                        BUFF[TMP]=255
                                                    NEXT TMP
                                                    GOSUB MEMOMESS
                                                    FRASE=15                       
                                                    TMP=200 'SERVE PER USCIR DAL CICLO DI FOR
                                                ELSE
                                                    BUFF[TMP-6]=BUFF[TMP]
                                                    SEROUT2 TXPC,6,[ BUFF[TMP-6] ]
                                                ENDIF
                                            NEXT TMP
                                      ENDIF
                                
                                    FRASE=15 
                        CASE "Z","z"  'TIZ:0

                            IF BUFF[3]<>"?" THEN  
                                    TMP=BUFF[4]-48
                                    WRITE 1012,TMP
                            ENDIF
                            FRASE=4  
                    end select 
            
                                


                
                CASE "S","s"        'TSU
                              FOR TMP=4 TO LUNGMESS
                                    'SEROUT2 P2,84,[  "TMP ",#TMP,13,10 ]
                                    IF BUFF[TMP]=";" OR TMP=LUNGMESS THEN   'SE TROVO IL ; O FINISCO IL MESS VUOL DIRE CHE IL MESS è FINITO E SLAVO IL NUMERO 
                                        LUNGNUM=TMP-4
                                        SEROUT2 TXPC,84,[  "LUNGNUM ",#lungnum,13,10 ]
                                        POSIZIONE=7
                                        FOR TMP=LUNGNUM TO 250
                                            BUFF[TMP]=255
                                            NEXT TMP
                                        GOSUB MEMOMESS
                                        FRASE=15                        
                                        TMP=200 'SERVE PER USCIR DAL CICLO DI FOR
                                    ELSE
                                        BUFF[TMP-4]=BUFF[TMP]
                                        SEROUT2 TXPC,84,[ BUFF[TMP-4] ]
                                    ENDIF
                                NEXT TMP 
                         FRASE=15               
                
                 END SELECT
                 
                 
                
                        
        CASE "V","v"    
        
            SELECT CASE BUFF[1]
            
                CASE "E","e"   'VER?
                    IF BUFF[3]="?" THEN     
                        FRASE=29
                    ENDIF
                    
                CASE "O","o"   'VOLT?
                    GOSUB TENS
                        FRASE=21
            END SELECT
                    
                
    END SELECT
    
'    IF POS=0 THEN
'        GOSUB INVIOSMS
'        POS=0
'    ENDIF    
    
    
            IF RISPOSTA=1 AND FRASE<>0 THEN      'SE RICHIEDO LA RISPOSTA INVIO SMS SE LA FRASE=0 NON INVIO
                WRITE 200+FRASE,1       ' SCRIVO IN MEMORIA CHE DEVO RISPONDERE        
'                SEROUT2 TXPC,6,[ "SALVO IN MEMORIA L'INVIO DELLA FRASE ",#FRASE,13,10 ]
                IF MULTIMESS=0 THEN 'SE HO FINITO I MULTIMESSAGGI VADO A RISPONDERE
                    GOSUB PREPARAINVIOSMS
                ENDIF
            ENDIF
            
            IF MULTIMESS=1 THEN
                FOR TMP=0 TO LUNGMESS
                    BUFF[TMP]=BUFF[TMP+POSMULTIMESS+1]
                NEXT TMP
                SEROUT2 TXPC,6,[ "-MESSAGGIO: " ]
                FOR TMP=0 TO LUNGMESS
                    SEROUT2 TXPC,6,[ BUFF[TMP] ]
                NEXT TMP
                SEROUT2 TXPC,6,[ 13,10   ]
            ENDIF
            
RETURN


'RILEGGIIMP:
                   
'    POLLTIMEC=0
'    READ 704,POLLTIMECh         'RILEGGO IL TEMPO IMPOSTATO
'    READ 705,POLLTIMECl                
'    POLLTIMEC=POLLTIMEC+GSMSECTOT 
'    SEROUT2 TXPC,6,[ "POLLTIMEC  ",#POLLTIMEC,13,10]
'    IF POLLTIMEC>=86399 THEN 'SE SFORO LE 23:59:59 ALLORA RIAZZERO
'        POLLTIMEC=POLLTIMEC-86399
'        OVERDAYC=1
'    ENDIF
    


    
    
'RETURN
PREPARAINVIOSMS:

    'SEROUT2 TXPC,6,[ "PREPARAINVIOSMS",13,10]
    FOR ADDRL2=200 TO 300     'VERIFICO DALLA LOCAZIONE 200 ALLA 400 QUALI MESSA DEVO INVIARE
        READ ADDRL2,TMP
            IF TMP=1 THEN       'HO TROVATO IL MESS
                FRASE=ADDRL2-200
    '            SEROUT2 TXPC,6,[ "TROVATO NELLA LOCAZIONE ",#ADDRL2," LA FRASE ",#FRASE,13,10]                
                IF FRASE=99 THEN
                    'GOSUB INVIOCOORDINATE
                ELSE
                    GOSUB INVIOSMS
                ENDIF
            ENDIF
    NEXT ADDRL2
    
    'SEROUT2 TXPC,6,[ "CANCELLO LA MEMORIA",13,10 ]
    FOR ADDRL2=200 TO 300
        WRITE ADDRL2,0        'SEGNO CHE HO INVIATO TUTTI I MESS
    NEXT ADDRL2

RETURN

VERIFICAPWD:
SEROUT2 TXPC,6,[ "VERIFICAPWD",13,10]
    PWDOK=0
    ERR1=0

   	For TMP=0 TO 4
		READ (1001+TMP),DATO
		SEROUT2 TXPC,6,[ "PWD ",DATO, " PWDR ",PWD[TMP],13,10]
		IF PWD[TMP]<>DATO THEN
			ERR1=1
		EndIF
		
	Next TMP
        
	IF ERR1=0  Then
        PWDOK=1 
    ENDIF  
RETURN                    
    


CALCOLANUM:

        'SEROUT2 TXPC,6,[ "-NUMERI LETTI ",BIN TMP3,13,10 ]
        'SEROUT2 TXPC,6,[ "-BUFF3 ",BUFF[3],13,10]
        FOR TMP=3 TO POSPUNTI     
            IF BUFF[TMP]>48 AND BUFF[TMP]<57 THEN
                'PORTO AD UNO IL BIT PERTANTO è COME SE FOSSE ON
                    TMP1=BUFF[TMP]-48-1       'TOLGO 48 PER TRASFORMARLO IN UN NUMERO E TOLGO 1 PER PERCHè CONTO DA 0
                    'SEROUT2 TXPC,6,[ "-TMP1 ",#TMP1,13,10]
                    TMPW=%00000001
                    FOR TMP2=1 TO TMP1
                        TMPW=TMPW<<1
                    NEXT TMP2
                    'SEROUT2 TXPC,6,[ "-TMPW ",BIN TMPW,13,10 ]
                    TMP3 = TMP3 | TMPW        ' Set bit TMPW of TMP       'PORTO AD 1 IL BIT          
                    'SEROUT2 TXPC,6,[ "-NUMERI ON ",BIN TMP3,13,10 ]
                    'SEROUT2 TXPC,6,[ "-ON OFF ",BUFF[POSPUNTI+2],13,10 ]
                IF BUFF[POSPUNTI+2]="F" OR BUFF[POSPUNTI+2]="f" THEN    'VERIFICO SE è OFF
                'SE OFF RIPORTO A ZERO IL BIT
                    TMP3 = TMP3 ^ TMPW                      
                    'SEROUT2 TXPC,6,[ "-NUMERI OFF ",BIN TMP3,13,10 ]
                ENDIF
            ENDIF
        NEXT TMP

RETURN

LEGGINUMERI:
        SEROUT2 TXPC,6,["LEGGI NUMERI ",13,10]  

            FOR TMPL=0 TO 19
                NUMERO[TMPL]=0            
            NEXT TMPL
            'SEROUT2 TXPC,6,[ "1 NUMERO -$GPRMC",STR D\75,13,10 ]
            FOR TMPL=0 TO 19
                TMPL1=((POSIZIONE-1)*20+TMPL)
                READ TMPL1,DATO
                IF DATO<>255 THEN
                    NUMERO[TMPL]=DATO
                ELSE
                    LUNGNUM=TMPL-1
                    'SEROUT2 TXPC,6,[ "LUNGNUM ",#LUNGNUM,13,10]
                    RETURN
                    TMPL=25
                ENDIF    
            NEXT TMPL
RETURN

INVIOECHO:
    SEROUT2 TXPC,84, ["Echo on number: "]
    FOR TMP3=0 TO LUNGMITT                  'METTO IN MITTENTEECHO CHI HA INVIATO L'SMS
        MITTENTEECHO[TMP3]=MITTENTE[TMP3]
    NEXT TMP3
    LUNGMITTECHO=LUNGMITT
    
    read 1000,POSIZIONE
    gosub LEGGINUMERI
    SEROUT2 TXPC,84, ["Pos",#POSIZIONE," ",STR NUMERO\LUNGNUM+1,13]
    FOR TMP3=0 TO LUNGNUM
        MITTENTE[TMP3]=NUMERO[TMP3]
    NEXT TMP3
    LUNGMITT=LUNGNUM
    IF LUNGMITT<>0 AND LUNGMITT<>255 THEN 
        FRASE=25                           
        GOSUB INVIOSMS                   
    ENDIF
    
RETURN
INVIOSMSALLARME:


'            HIGH LED
            read 1021,NUMERISMS
            SEROUT2 TXPC,84,[ "TMP ",BIN NUMERISMS,13,10]
            TMPW=%00000001     
            for tmp1=0 to 7
                tmp2 = NUMERISMS & tmpw ' Isolate bit TMP1 of TMP
                SEROUT2 TXPC,84,[ "NUMERI ISOLATO ",BIN TMP2,13,10 ]
                IF TMP2<>0 THEN
                    POSIZIONE=TMP1+1
                    gosub legginumeri
                    FOR TMP3=0 TO LUNGNUM
                        MITTENTE[TMP3]=NUMERO[TMP3]
                    NEXT TMP3
                    LUNGMITT=LUNGNUM
                    IF LUNGMITT<>0 AND LUNGMITT<>255 THEN                            
                        GOSUB INVIOSMS                   
                    ENDIF
                ENDIF 
                TMPW=TMPW<<1
            NEXT TMP1
            
             

RETURN


INVIOSMS:

'     SEROUT2 TXPC,6,[ "INVIOSMS INTCON  ",bin8 INTCON,13,10 ]
'     SEROUT2 TXPC,6,[ "INVIOSMS INTCON2 ",bin8 INTCON2,13,10 ]
'     SEROUT2 TXPC,6,[ "INVIOSMS INTCON3 ",bin8 INTCON3,13,10,13,10 ]    

            'HSEROUT ["AT+CNMI=3,0,0,0",13]
    
    TENTATIVI=0
    
REINVIOSMS:
'    SEROUT2 TXPC,6,[ "INVIO FRASE ",#FRASE,13,10 ]

        HIGH LD1
        
    hserout ["ATE0",13]
    SEROUT2 TXPC,6,[ "ECHO OFF",13,10 ] 
    PAUSE 2000

'     SEROUT2 TXPC,6,[ "REINVIOSMS INTCON  ",bin8 INTCON,13,10 ]
'     SEROUT2 TXPC,6,[ "REINVIOSMS INTCON2 ",bin8 INTCON2,13,10 ]
     SEROUT2 TXPC,6,[ "INVIO SMS A ",STR MITTENTE\(LUNGMITT+1),13,10 ]

    hserout ["AT+CMGS=",34,STR MITTENTE\(LUNGMITT+1),34,13]
    
    HSERIN 2000,EXITINVIOSMS,[wait (">")]
    PAUSE 200

CAMBIOFRASE:
    SELECT CASE FRASE
        CASE 0
            hserout ["Wrong SMS"]        
        CASE 1
            hserout ["Insert PWD"]
        CASE 2
            IF PWDOK=1 THEN                
                TMPW=%00000001
                FOR POSIZIONE=1 TO 8
                    gosub LEGGINUMERI
                    READ 1021,TMP
                    tmp2 = tmp & tmpw ' Isolate bit TMP1 of TMP
                    'SEROUT2 TXPC,6,[ "-NUMERI ISOLATO ",BIN TMP2,13,10]
                    IF TMP2<>0 THEN                            
                        tmp3="S"
                    else
                        tmp3=32
                    ENDIF 
                    
                    

                    TMPW=TMPW<<1
                    
                    
                    hserout ["Pos",#POSIZIONE," ",TMP3," ",STR NUMERO\LUNGNUM+1,13]
                NEXT POSIZIONE           
            ELSE
                FRASE=1
                GOTO CAMBIOFRASE
            ENDIF
            
        CASE 3
            hserout ["PWD modified"]
        CASE 4
            hserout ["IN 1:",13,"Allarm "]
            read 1018,tmp
            SELECT CASE TMP
                CASE 0
                    hserout ["on voltage presence",13]
                CASE 1
                    hserout ["on voltage absence",13]
                CASE 2
                    hserout ["on variation",13]
            end select
            read 1016,tmp
                hserout ["T ini ",#tmp," min",13]
            READ 1012,TMP
            IF TMP=0 THEN
                hserout ["Reset T ini OFF",13]                
            ELSE
                hserout ["Reset T ini ON",13]                
            ENDIF
            read 1014,tmp
                hserout ["T oss ",#tmp," sec",13]
            
            
                
        CASE 5
            POSIZIONE=POSALL
            SEROUT2 TXPC,6,[ "POSIZIONE ",#POSIZIONE,13,10 ]
            GOSUB LEGGIMESS
            
            
        CASE 6
            READ 700,TMP
            hserout ["Input "]
            if tmp=0 then
                hserout ["off ",13]
            else
                hserout ["on ",13]
            endif    
            
            READ 701,TMP
            hserout ["PIR "]
            if tmp=0 then
                hserout ["off ",13]
            else
                hserout ["on ",13]
            endif 
            
            READ 702,TMP
            hserout ["Move "]
            if tmp=0 then
                hserout ["off ",13]
            else
                hserout ["on ",13]
            endif 
            
            READ 703,TMP
            hserout ["Temperature "]
            if tmp=0 then
                hserout ["off ",13]
            else
                hserout ["on ",13]
            endif 
        
            READ 704,TMP
            hserout ["Voltage "]
            if tmp=0 then
                hserout ["off ",13]
            else
                hserout ["on ",13]
            endif 

            
        CASE 7
            read 711,tmp
            hserout ["Activation time ",#tmp," sec",13]                    
            'read 712,tmp
            'hserout ["Disactivation time ",#tmp," sec",13]        
            
        CASE 8
            READ 768,TMP       
            hserout ["Sleep time: ",#tmp," sec"]            
            
                   
        CASE 9
            READ 1009,TMP

            if tmp=0 then
                hserout ["Startup message disabled"]
            else
                hserout ["Startup message enabled"]
            endif   
                
            
        CASE 10 
            
        CASE 11
            gosub temperatura
            hserout ["Temperature "]
            IF TEMP.7=0 THEN
                hserout [ "+"]
            ELSE
                hserout [ "-"]
            ENDIF
            hserout [#TmpW1,",",DEC2 TMPw," C",13]
            
            
            hserout [ "MIN Temperature "]
            READ 1010,TMP
            IF TMP.7=0 THEN
                hserout [ "+"]
            ELSE
                hserout [ "-"]
            ENDIF        
            TMP.7=0 
            hserout [#TMP,13 ]

            hserout [ "MAX Temperature "]
            READ 1011,TMP
            IF TMP.7=0 THEN
                hserout [ "+"]
            ELSE
                hserout [ "-"]
            ENDIF   
            TMP.7=0
            hserout [#TMP,13 ]
    
        case 12
            'gosub temperatura
            
            hserout ["LOW VOLTAGE",13]
            hserout ["Power voltage: ",#BATT," mV"]

            
        case 13
            'gosub temperatura
            
            hserout ["Temperature stored, deleted",13]
            

        CASE 14            
            hserout ["Temperature alarm ",13]
            hserout [ "MIN "]
            READ 1007,TMP
            IF TMP.7=0 THEN
                hserout [ "+"]
            ELSE
                hserout [ "-"]
            ENDIF        
            TMP.7=0 
            hserout [#TMP,13 ]

            hserout [ "MAX "]
            READ 1008,TMP
            IF TMP.7=0 THEN
                hserout [ "+"]
            ELSE
                hserout [ "-"]
            ENDIF   
            TMP.7=0
            hserout [#TMP,13 ]
            
            hserout ["Voltage alarm ",13]
            READ 705,TMP
            hserout [#(tmp/10)]
            hserout ["."]
            hserout [#tmp-((tmp/10)*10)]
            hserout ["mV"]
            
                                
        CASE 15
            hserout ["Message stored",13]        
        
        CASE 16
            hserout ["GPRS parameters:",13,"APN: "]
            FOR TMP=0 TO 30
                READ 500+TMP,TMP1
                IF TMP1<>"#" AND TMP1<>255 THEN
                    HSEROUT [TMP1]
                ELSE
                    TMP=31
                ENDIF
            NEXT TMP
            IF PWDOK=1 THEN
                hserout [13,"Username: "]
                FOR TMP=0 TO 30
                    READ 530+TMP,TMP1
                    IF TMP1<>"#" AND TMP1<>255 THEN
                        HSEROUT [TMP1]
                    ELSE
                        TMP=31
                    ENDIF
                NEXT TMP
                hserout [13,"Password: "]
                FOR TMP=0 TO 30
                    READ 560+TMP,TMP1
                    IF TMP1<>"#" AND TMP1<>255 THEN
                        HSEROUT [TMP1]
                    ELSE
                        TMP=31
                    ENDIF
                NEXT TMP
            else
                hserout [13,"To view GPRS parameters, enter the password"]
            endif
            
        case 19
            posizione=7 'indica la posizione da cui leggere il messaggio
            GOSUB LEGGIMESS   
        
        CASE 20
            hserout ["Reset"]
        CASE 21
            hserout ["Power voltage: ",#BATT," mV"]

        CASE 22
            hserout ["GSM IMEI: "]
            FOR TMP=0 TO 20
                READ 730+TMP,TMP1
                IF TMP1<>"#" AND TMP1<>255 THEN
                    HSEROUT [TMP1]
                ELSE
                    TMP=31
                ENDIF
            NEXT TMP
        CASE 23
            hserout ["Name: "]
            FOR TMP=0 TO 20
                READ 751+TMP,TMP1
                IF TMP1<>"#" AND TMP1<>255 THEN
                    HSEROUT [TMP1]
                ELSE
                    TMP=31
                ENDIF
            NEXT TMP
            
            
            
            
            
        CASE 24          
            IF PWDOK=1 THEN                
                hserout ["Echo on number: "]
                read 1000,POSIZIONE
                gosub LEGGINUMERI
                hserout ["Pos",#POSIZIONE," ",STR NUMERO\LUNGNUM+1,13]
            ELSE
                FRASE=1
                GOTO CAMBIOFRASE
            ENDIF
            
       CASE 25
            hserout ["Echo",13,"Sender:",13]
            FOR TMP3=0 TO LUNGMITTECHO
                HSEROUT [MITTENTEECHO[TMP3]]
            NEXT TMP3
            
            hserout ["Text:",13]
            FOR TMP=0 TO LUNGMESS
                HSEROUT [ BUFF[TMP] ]
            NEXT TMP
            
       CASE 28
            hserout ["ATTENTION!!! SOS MESSAGE!!!"]
            
       CASE 29
            hserout ["Ver Firmware ",#VER]        
            
           ' next POSIZIONEGEO
        CASE 30             
            IF BUFF[3]<>0 THEN    'CONTROLLO ANCHE CHE BUFF NON SIA VUOTO            
                SEROUT2 TXPC,6,["Probable position: ",STR BUFF\100]
                HSEROUT ["Probable position: ",STR BUFF\100]
            ENDIF    
    END SELECT 
    
        hserout [26]
    
'    TENTATIVI=TENTATIVI+1    
'    IF TENTATIVI=3 THEN
'        PAUSE 3000
'        GOTO EXITINVIOSMS
'    ENDIF
     
        
    hserin 10000,EXITINVIOSMS,[WAIT("O"),TMP]
    
    'hserin 10000,REINVIOSMS,[WAIT("OK")]
    
    SEROUT2 TXPC,6,["-SMS INVIATO",13,10 ]
    TENTATIVI=5

    PAUSE 1000
   
    
EXITINVIOSMS: 
TENTATIVI=TENTATIVI+1
IF TENTATIVI<3 THEN REINVIOSMS
TENTATIVI=0
    hserout ["ATE1",13]
    SEROUT2 TXPC,6,[ "ABILITO L'ECHO",13,10 ] 
    LOW LD1

'SEROUT2 TXPC,6,[ "1 EXITINVIOSMS -$GPRMC",STR DAT\75,13,10 ]
    
RETURN


SCELTAFORMATO:        'S SMS E Email U URL  3 SMS+Email  4 SMS+URL  5 Email+URL  9 SMS+Email+URL
    SELECT CASE TMP             
        CASE "S","s"    'SMS
            hserout ["SMS"]    
        CASE "E","e"    'EMAIL
            hserout ["EMAIL"]              
        CASE "3"    'SMS & EMAIL
            hserout ["SMS & EMAIL"] 
        CASE "M","m"    'rispondi al mittente
            hserout ["CALLER"]             
        CASE ELSE
            hserout ["Not defined"]                                           
    END SELECT   
RETURN

MEMONUMERO:

    SEROUT2 TXPC,6,[ "-MEM POS ",#POSIZIONE,13,10]
    FOR TMP=0 TO LUNGNUM
        TMP1=((POSIZIONE-1)*20+TMP)
        WRITE TMP1,NUMERO[TMP]                             
    NEXT TMP
    
    
'    SEROUT2 TXPC,6,[ "-NUMERO SALVATO "]
'    FOR TMP=0 TO 20
'        TMP1=((POSIZIONE-1)*20+TMP)
'        READ TMP1,TMPW
'        SEROUT2 TXPC,6,[TMPW]
'    NEXT TMP
'    SEROUT2 TXPC,6,[13,10]
        
RETURN

CERCANUMERO:

    IF CERCAMITTENTE=1 THEN
        FOR TMP=0 TO LUNGMITT
            NUMERO[TMP]=MITTENTE[TMP]
        NEXT TMP
        LUNGNUM=LUNGMITT
        CERCAMITTENTE=0
    ENDIF

    SEROUT2 TXPC,6,["-Cerca NUMERO "]
    FOR TMP=0 TO (LUNGNUM+1)
        SEROUT2 TXPC,6,[numero[tmp]]
    NEXT TMP
    SEROUT2 TXPC,6,[13,10]

TROVATO=0
POSTROVATO=99
For TMP1=0 TO 100
	
	Read TMP1,DATO
	'DEBUG"DATO ",DATO," POS ",#TMP1,13,10
   	IF DATO=numero[0] Then
   	    POSTROVATO=TMP1/20
        For TMP2=0 TO LUNGNUM
            TMPW=TMP2+TMP1
            Read TMPW,DATO
			'DEBUG "DATO LETTO ",DATO," NUMERO ",NUMERO[TMP2],13,10    
            IF DATO<>numero[TMP2] Then
                POSTROVATO=99
			    GoTo SALTA
            EndIF    
        Next TMP2          	
            SEROUT2 TXPC,6,["TROVATO IN ",#POSTROVATO,13,10]
    		TROVATO=1
        Return
    EndIF  

SALTA:
Next TMP1


RETURN

LEGGIMASTER:
    SEROUT2 TXPC,6,["-LEGGO MASTER",13,10]

    FOR TMP=0 TO 20 
        READ TMP,DATO
        IF DATO<>255 THEN
            MITTENTE[TMP]=DATO
        ELSE
            LUNGMITT=TMP-1
            TMP=21
        ENDIF
    NEXT TMP
    
    SEROUT2 TXPC,6,[ "-MASTER: "]
    FOR TMP=0 TO LUNGMITT
        SEROUT2 TXPC,6,[ MITTENTE [TMP]]
    NEXT TMP
    SEROUT2 TXPC,6,[ 13,10]
        

RETURN

'LEGGIEMAIL:
'   ' SEROUT P2,2,["LEGGO MAIL",13,10]
'    FOR TMP=100 TO 200 
'        READ TMP,DATO
'        IF DATO<>"," THEN
'            MITTENTE[TMP-100]=DATO
'        ELSE
'            LUNGMITT=TMP-101
'            TMP=201
'        ENDIF
'    NEXT TMP
        

'RETURN




STARTTIME:  'SERVE PER RESETTARE LE VARIABILE DEL TIME

    GOSUB TIME
    PAUSE 1000
    GOSUB TIME
    'gosub RILEGGIIMP
RETURN

TIME:


    HSEROUT ["AT+CCLK?",13]      'ORA +CCLK: "01/01/01,00:08:34+00"
    HSERIN 2000,EXITTIME,[WAIT (": "),SKIP 7,WAIT (","),DEC2 ORAGSM,TMP,DEC2 MINUTIGSM,TMP,DEC2 SECONDIGSM]
    HSERIN 2000,EXITTIME,[WAIT ("OK")]

    GSMSECTOT=((ORAGSM*60)*60)+(MINUTIGSM*60)+SECONDIGSM
    SEROUT2 txpc,6,[ "GSMSECTOT=",#GSMSECTOT,13,10]   
    

    
    GSMSEC=(MINUTIGSM*60)+SECONDIGSM
    GSMMIN=(ORAGSM*60)+MINUTIGSM
    
    VARSEC=0
    VARMIN=0    
    
    IF GSMSEC>GSMSECOLD THEN
        VARSEC=GSMSEC-GSMSECOLD
    ENDIF
    IF GSMMIN>GSMMINOLD THEN
        VARMIN=GSMMIN-GSMMINOLD
    ENDIF
    
    GSMSECOLD=GSMSEC 
    GSMMINOLD=GSMMIN


    'IF TIMEINIBIZIONE1>0 OR TIMEINIBIZIONET>0 OR TIMEINIBIZIONEP>0 OR TIMEINIBIZIONEM>0 or TEMPOSPEGNIMENTO>0 THEN
    IF TIMEINIBIZIONE1>0 OR TIMEINIBIZIONET>0 OR TIMEINIBIZIONEP>0 OR TIMEINIBIZIONEM>0 OR TIMEINIBIZIONEv>0 THEN
        GOSUB GESTINIBIZIONE
    ENDIF   
    
    
     
EXITTIME:
RETURN

PIR:
    IF VARIAZIONEPIR=1 THEN
        VARIAZIONEPIR=0
        IF TIMEINIBIZIONEP=0 THEN        
            SEROUT2 txpc,6,[ "PIR",13,10] 
'            if MODULOON=0 THEN
'                SEROUT2 TXPC,6,[ "ACCENDO GSM",13,10 ] 
''                GOSUB ON_OFF
''                GOSUB SETUP 
'            ENDIF    
            PAUSE 500   
            POSALL=5     
            GOSUB STARTTIME
            READ 1016,TIMEINIBIZIONEP               
            SEROUT2 TXPC,6,[ "TIMEINIBIZIONEP  ",#TIMEINIBIZIONEP,13,10]            
            frase=5
            GOSUB INVIOSMSALLARME
            LOW LD1  
        ENDIF               
    ENDIF 
RETURN


SENSOREMOV:
    IF VARIAZIONEMOV=1 THEN
        VARIAZIONEMOV=0
        IF TIMEINIBIZIONEM=0 THEN
            SEROUT2 TXPC,6,[ "MOVIMENTO",13,10 ]    
'            if MODULOON=0 THEN
'                SEROUT2 TXPC,6,[ "ACCENDO GSM",13,10 ] 
''                GOSUB ON_OFF
''                GOSUB SETUP 
'            ENDIF
            PAUSE 500           
            POSALL=6     
            GOSUB STARTTIME
            READ 1016,TIMEINIBIZIONEM             
            SEROUT2 TXPC,6,[ "TIMEINIBIZIONEM  ",#TIMEINIBIZIONEM,13,10]            
            frase=5
            GOSUB INVIOSMSALLARME
            LOW LD1     
        ENDIF            
    ENDIF 
RETURN


INGRESSOATTIVAZIONE:
     IF ATTIVA=0 THEN 
        IF ATTIVATO=1 THEN   'SE PREMO L'INGRESSO E è ATTIVATO L'ALLARME
            ATTIVATO=0
'            READ 711,TEMPO
'            FOR TMP=0 TO TEMPO
'                IF ATTIVA=1 THEN
'                    SEROUT2 txpc,6,[ "RESET FOR",13,10]      
'                    ATTIVATO=1
'                    TMP=TEMPO
'                ENDIF        
'                TOGGLE LD2
'                PAUSE 500   
'                SEROUT2 txpc,6,[ "TEMPO PER DISATTIVAZIONE ",#TMP," TO TEMPO ",#TEMPO,13,10]                
'            NEXT TMP
            for tmp=0 to 5
                TOGGLE LD2
                PAUSE 200
            NEXT TMP
            LOW LD2
            SEROUT2 txpc,6,[ "ALLARME DISATTIVATO",13,10]      
            PAUSE 500
        ENDIF
    ELSE
        IF ATTIVATO=0 THEN   'SE APRO L'INGRESSO E L'ALLARME è DISATTIVATO 
            ATTIVATO=1
            READ 711,TEMPO
            FOR TMP=0 TO TEMPO
                IF ATTIVA=0 THEN 
                    SEROUT2 txpc,6,[ "RESET FOR",13,10]         
                    ATTIVATO=0
                    TMP=TEMPO
                ENDIF        
                TOGGLE LD2 
                PAUSE 500    
                SEROUT2 txpc,6,[ "TEMPO PER ATTIVAZIONE ",#TMP," TO TEMPO ",#TEMPO,13,10]                               
            NEXT TMP
            IF ATTIVATO=1 THEN
                SEROUT2 txpc,6,[ "ALLARME ATTIVATO",13,10]      
            ELSE
                SEROUT2 txpc,6,[ "ALLARME DISATTIVATO",13,10]      
            ENDIF
            PAUSE 500
        ENDIF           
    ENDIF 
    

RETURN


VERIFICABATT:

    GOSUB TENS
    READ 705,TMP 
    BATTR=BATT/100   
    
    IF BATTR<TMP and TIMEINIBIZIONEv=0THEN
        GOSUB STARTTIME
        READ 1016,TIMEINIBIZIONEV                
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONEV  ",#TIMEINIBIZIONEV,13,10]  
        frase=12
        GOSUB INVIOSMSallarme
    ENDIF
    
          


RETURN

VERIFICAINGRESSI:
     'ROUTINE PER LA VERIFICA DEGLI INGRESSI, L'INGRESSO DU Eè SOLO PER L'ATTIVAZIONE DEL SISTEMA

    
    

        
    
    
    ALLARME=0

    IF VARIAZIONEIN=1 THEN
        VARIAZIONEIN=0
        SEROUT2 TXPC,6,["VARIAZIONE IN1  ",#IN1,13,10]
        READ 1018,TMP    
        IF IN1=TMP OR (TMP=2 AND IN1<>STATOIN1 ) THEN
            IF TIMEINIBIZIONE1=0 THEN    
                    HIGH LD1 
                    SEROUT2 TXPC,6,["IN1 ",#IN1," STATOIN1 ",#STATOIN1,13,10]      
                    STATOIN1=IN1  
                    READ 1014,TMP1      'LEGGO TEMPO OSSERVAZIONE
                    WHILE TMP1>0 AND IN1=STATOIN1
                        PAUSE 1000 
                        TMP1=TMP1-1
                        SEROUT2 TXPC,6,["TEMPO DI OSSERVAZIONE1 ",#TMP1,13,10]
                    WEND
                    IF TMP1=0 AND IN1=STATOIN1 THEN
                        
                        IF IN1=0 THEN   'MEMORIZZO LA CONDIZIONE CHE HA DATO L'ALLARME
                            POSALL=1
                        ELSE
                            POSALL=2
                        ENDIF
                        ALLARME=1   'MEMORIZZO CHE A DARE L'ALLARME è STATO L'INGRESSO 1
                        SEROUT2 TXPC,6,["ALLARME1",13,10]
                    ELSE
                        LOW LD1
                    ENDIF
                    STATOIN1=IN1
            ENDIF
        ELSE
        
            READ 1012,TMP
            IF TMP=1 THEN
                TIMEINIBIZIONE1=0
            ENDIF
                    
        ENDIF
        
        
        IF ALLARME<>0 THEN
'            if MODULOON=0 THEN
'                SEROUT2 TXPC,6,[ "ACCENDO GSM",13,10 ] 
'                'GOSUB ON_OFF
'                'GOSUB SETUP 
'            ENDIF            
            GOSUB STARTTIME
            READ 1016,TIMEINIBIZIONE1                
            SEROUT2 TXPC,6,[ "TIMEINIBIZIONE1  ",#TIMEINIBIZIONE1,13,10]            
            frase=5
            GOSUB INVIOSMSALLARME
            LOW LD1                 
        ENDIF
    ENDIF
    
    
        
    
    

RETURN


TENS:

    adcin 0,TMPW
    SEROUT2 TXPC,6,["TMPW ",#TMPW,13,10 ]
    BATT=tmpw*41
    SEROUT2 TXPC,6,["TENSIONE ",#BATT,13,10 ]
     
    'PAUSE 1000
    
return


TEMPERATURA:

    SONDAOK=1 'DICO CHE LA SONDA C'è


            TMP=0
            OWOUT sonda, 1, [$CC, $44]
CONVER0:    OWIN sonda, 4, [CONV]
            TMP=TMP+1
            PAUSE 3
            IF TMP=250 THEN
                SEROUT2 txpc,6,[ "SONDA NON PRESENTE",13,10]
                SONDAOK=0      ' LA SONDA NON C'è NON VENIRE A LEGGERLA PIù...
                
            ENDIF
            IF CONV = 0 THEN 
                GOTO CONVER0  'Se la conversione non e' finita
            ENDIF
            OWOUT sonda, 1, [$CC, $BE] 
            'OWIN sonda, 0, [TMP2, TMP1,skip 7]
            OWIN sonda, 0, [Temp.LOWBYTE,Temp.HIGHBYTE,skip 7]
            'SEROUT2 TXPC,6,[ "TEMPERATURA BIN ",BIN16 TEMP,13,10 ]   '***togliere
                
                
            IF Temp.bit15 = 0 THEN ' Check for temp below 0°C
                'Sign = "+"
                TMPW1 = 0
                TmpW1 = (Temp >> 4) 
                tmpw=1000/(160/(TEMP & %0000000000001111))
                IF (TEMP & %0000000000001111)=%1111 THEN 
                    TMPW=0
                    TmpW1=TmpW1+1
                ENDIF                     
                'SEROUT2 TXPC,6,["1Temp = +", DEC TmpW1,".", DEC2 TmpW, "C ",13,10]
                TEMP=0
                TEMP=TMPW1
            ELSE
                
                tmpw=1000/(160/(TEMP & %0000000000001111))
                IF (TEMP & %0000000000001111)=%1111 THEN 
                    TMPW=0
                    TMP3=TMP3+1
                ENDIF
                TMPW1 = 0 
                TMPW1 = 65535 - TEMP    'OTTENGO IL VALORE COME SE FOSSE POSITIVO
                'TMPW1 = 0
                TmpW1 = (TMPW1 >> 4) 
                tmpw=1000/(160/(TMPW1 & %0000000000001111))
                IF (TMPW1 & %0000000000001111)=%1111 THEN 
                    TMPW=0
                    TmpW1=TmpW1-1
                ENDIF   
'                TmpW1 = (TEMP & %0000011111111111)
'                TMPW1 = TMPW1 ^ %0000011111111111
'                TMPW1 = TmpW1 >> 4
                'SEROUT2 TXPC,6,["1Temp = -", DEC TmpW1,".", DEC2 TmpW, "C ",13,10]
                TEMP=0
                TEMP=TMPW1
                TEMP.7=1
            ENDIF
                TEMPW=(TMPW1*10)+(TMPW/10)
               ' SEROUT2 TXPC,6,["TempW =", #TEMPW,"C ",13,10]
                

            SEROUT2 txpc,6,[ "TEMPERATURA DEC "]
'            TEMP=0        
            IF TEMP.7=0 THEN
                SEROUT2 txpc,6,[ "+"]
            ELSE
                SEROUT2 txpc,6,[ "-"]
            ENDIF
            SEROUT2 txpc,6,[#TmpW1,",",DEC2 TMPw," GRADI",13,10 ]
'            TMP3  =0
'            TMP3.6=TMP1.2
'            TMP3.5=TMP1.1
'            TMP3.4=TMP1.0
'            TMP3.3=TMP2.7
'            TMP3.2=TMP2.6
'            TMP3.1=TMP2.5
'            TMP3.0=TMP2.4
'            TMP4  =0
'            TMP4.0=TMP2.0
'            TMP4.1=TMP2.1
'            TMP4.2=TMP2.2
'            TMP4.3=TMP2.3
'            'SEROUT2 TXPC,6,[ "TMP4 ",#TMP4,13,10 ]
            
'            tmpw=1000/(160/tmp4)
'            IF TMP4=15 THEN
'                TMPW=0
'                TMP3=TMP3+1
'            ENDIF 
'            SEROUT2 TXPC,6,[#TMP3,",",DEC2 TMPw," GRADI",13,10 ]
'            TEMP=TEMP+TMP3 
                  
            PAUSE 1000
            
            SEROUT2 txpc,6,[ "TEMPERATURA MIN "]
            READ 1010,TMP
            IF TEMP.7=0 THEN    'TEMPERATURE LETTE          POSITIVE
                IF TMP.7=0 THEN 'TEMPERATURE MEMORIZZATE    POSITIVE
                    IF TEMP<TMP THEN    'SALVO LA MINIMA
                        WRITE 1010,TEMP
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    
                    'WRITE 1012,TEMP
                ENDIF
            ELSE                'TEMPERATURE LETTE          NEGATIVE
                IF TMP.7=1 THEN 'TEMPERATURE MEMORIZZATE    NEGATIVE
                    IF TEMP>TMP THEN    'SALVO LA MINIMA
                        WRITE 1010,TEMP
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE POSITIVE (LA RILEVATA è NEGATIVA)
                    WRITE 1010,TEMP
                ENDIF
            ENDIF
            READ 1010,TMP
            IF TMP.7=0 THEN
                SEROUT2 txpc,6,[ "+"]
            ELSE
                SEROUT2 txpc,6,[ "-"]
            ENDIF        
            TMP.7=0 
            SEROUT2 txpc,6,[#TMP,13,10 ]
            
            
            SEROUT2 txpc,6,[ "TEMPERATURA MAX "]
            READ 1011,TMP
            IF TEMP.7=0 THEN    'TEMPERATURE LETTE          POSITIVE
                IF TMP.7=0 THEN 'TEMPERATURE MEMORIZZATE    POSITIVE
                    IF TEMP>TMP THEN    'SALVO LA MASSIMA
                        WRITE 1011,TEMP
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    NEGATIVE 
                    WRITE 1011,TEMP
                ENDIF
            ELSE                'TEMPERATURE LETTE          NEGATIVE
                IF TMP.7=1 THEN 'TEMPERATURE MEMORIZZATE    NEGATIVE
                    IF TEMP<TMP THEN    'SALVO LA MINIMA
                        WRITE 1011,TEMP
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    POSITIVE (NON MEMORIZZO NIENTE)
                ENDIF
            ENDIF            
            READ 1011,TMP
            IF TMP.7=0 THEN
                SEROUT2 txpc,6,[ "+"]
            ELSE
                SEROUT2 txpc,6,[ "-"]
            ENDIF   
            TMP.7=0
            SEROUT2 txpc,6,[#TMP,13,10 ]
            
            
            
            'GEST ALL TEMP
            
            ALLARME=0
            
            READ 703,TMP   'SEGGO SE C'è L'ALLARME SU TEMPERATURA O NO
            IF TIMEINIBIZIONET=0 AND TMP=1 THEN 
                
                READ 1007,TMP   'LEGGO LA TEMPERATURA MIN DI ALLARME
                'SEROUT2 TXPC,6,[ "TEMP ATTUALE ",#TEMP,13,10 ]
                IF TMP.7=0 THEN
                    SEROUT2 TXPC,6,[ "TEMP ALL MIN +",#TMP,13,10 ]
                else
                    TMP1=TMP
                    TMP1.7=0
                    SEROUT2 TXPC,6,[ "TEMP ALL MIN -",#TMP1,13,10 ]
                ENDIF
            

       
        'SEROUT2 TXPC,6,[ "TEMPERATURA MIN "]
            IF TEMP.7=0 THEN    'TEMPERATURE LETTE          POSITIVE
                IF TMP.7=0 THEN 'TEMPERATURE MEMORIZZATE    POSITIVE
                    IF TEMP<TMP THEN    'SALVO LA MINIMA
                        SEROUT2 TXPC,6,[ "ALLARME MIN 1 ",13,10 ]
                        ALLARME=4
                    ENDIF
                'ELSE            'TEMPERATURE MEMORIZZATE    
                    'SEROUT2 TXPC,6,[ "ALLARME MIN 2 ",13,10 ]
                    'ALLARME=1
                ENDIF
            ELSE                'TEMPERATURE LETTE          NEGATIVE
                IF TMP.7=1 THEN 'TEMPERATURE MEMORIZZATE    NEGATIVE
                    IF TEMP>=TMP THEN    'SALVO LA MINIMA
                        SEROUT2 TXPC,6,[ "ALLARME MIN 3 ",13,10 ]
                        ALLARME=4
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    POSITIVE (NON MEMORIZZO NIENTE)
                    SEROUT2 TXPC,6,[ "ALLARME MIN 2 ",13,10 ]
                    ALLARME=4
                ENDIF
            ENDIF
            
        'GOSUB TEMPERATURA
        READ 1008,TMP   'LEGGO LA TEMPERATURA MAX DI ALLARME
        'SEROUT2 TXPC,6,[ "TEMP ATTUALE ",#TEMP,13,10 ]
        IF TMP.7=0 THEN
            SEROUT2 TXPC,6,[ "TEMP ALL MAX +",#TMP,13,10 ]
        else
            TMP1=TMP
            TMP1.7=0
            SEROUT2 TXPC,6,[ "TEMP ALL MAX -",#TMP1,13,10 ]
        ENDIF           
        'SEROUT2 TXPC,6,[ "TEMPERATURA MAX "]
            IF TEMP.7=0 THEN    'TEMPERATURE LETTE          POSITIVE
                IF TMP.7=0 THEN 'TEMPERATURE MEMORIZZATE    POSITIVE
                    IF TEMP=>TMP THEN    'SALVO LA MASSIMA
                        SEROUT2 TXPC,6,[ "ALLARME MAX 1 ",13,10 ]
                        ALLARME=3
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    NEGATIVE 
                    SEROUT2 TXPC,6,[ "ALLARME MAX 2 ",13,10 ]
                    ALLARME=3
                ENDIF
            ELSE                'TEMPERATURE LETTE          NEGATIVE
                IF TMP.7=1 THEN 'TEMPERATURE MEMORIZZATE    NEGATIVE
                    IF TEMP<TMP THEN    'SALVO LA MINIMA
                        SEROUT2 TXPC,6,[ "ALLARME MAX 3 ",13,10 ]
                        ALLARME=3
                    ENDIF
                ELSE            'TEMPERATURE MEMORIZZATE    POSITIVE (NON MEMORIZZO NIENTE)
                ENDIF
            ENDIF 

    ENDIF
    
    
    
        
        
        
    
    
    
        IF ALLARME<>0 THEN
            POSALL=ALLARME
            GOSUB STARTTIME
            READ 1016,TIMEINIBIZIONET                
            SEROUT2 TXPC,6,[ "TIMEINIBIZIONET  ",#TIMEINIBIZIONET,13,10]            
            frase=5
            GOSUB INVIOSMSALLARME
            LOW LD1                 
        ENDIF
    
    
            
            
           
            
RETURN



 
GESTINIBIZIONE:
    IF TIMEINIBIZIONE1>0 THEN
        'GOSUB TIME
        if TIMEINIBIZIONE1<= VARMIN then
            TIMEINIBIZIONE1=0
        else
            TIMEINIBIZIONE1=TIMEINIBIZIONE1-VARMIN       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
        ENDIF
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONE1= ",#TIMEINIBIZIONE1,13,10 ]        
    ENDIF
    
    IF TIMEINIBIZIONET>0 THEN
        'GOSUB TIME
        if TIMEINIBIZIONET<= VARMIN then
            TIMEINIBIZIONET=0
        else
            TIMEINIBIZIONET=TIMEINIBIZIONET-VARMIN       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
        ENDIF
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONET= ",#TIMEINIBIZIONET,13,10 ]        
    ENDIF
    
    IF TIMEINIBIZIONEM>0 THEN
        'GOSUB TIME
        if TIMEINIBIZIONEM<= VARMIN then
            TIMEINIBIZIONEM=0
        else
            TIMEINIBIZIONEM=TIMEINIBIZIONEM-VARMIN       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
        ENDIF
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONEM= ",#TIMEINIBIZIONEM,13,10 ]        
    ENDIF
    
    IF TIMEINIBIZIONEP>0 THEN
        'GOSUB TIME
        if TIMEINIBIZIONEP<= VARMIN then
            TIMEINIBIZIONEP=0
        else
            TIMEINIBIZIONEP=TIMEINIBIZIONEP-VARMIN       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
        ENDIF
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONEP= ",#TIMEINIBIZIONEP,13,10 ]        
    ENDIF
    
    
    IF TIMEINIBIZIONEV>0 THEN
        'GOSUB TIME
        if TIMEINIBIZIONEV<= VARMIN then
            TIMEINIBIZIONEV=0
        else
            TIMEINIBIZIONEV=TIMEINIBIZIONEV-VARMIN       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
        ENDIF
        SEROUT2 TXPC,6,[ "TIMEINIBIZIONEV= ",#TIMEINIBIZIONEV,13,10 ]        
    ENDIF
    
   ' IF TEMPOSPEGNIMENTO>0 and TIMEINIBIZIONE1=0 and TIMEINIBIZIONET=0 and TIMEINIBIZIONEP=0 and TIMEINIBIZIONEM=0 THEN
'    IF TIMEINIBIZIONE1=0 and TIMEINIBIZIONET=0 and TIMEINIBIZIONEP=0 and TIMEINIBIZIONEM=0 THEN
'        'GOSUB TIME
'        if TEMPOSPEGNIMENTO<= VARSEC then
'            TEMPOSPEGNIMENTO=0
'            GOSUB SPEGNIMODULO
'        else
'            TEMPOSPEGNIMENTO=TEMPOSPEGNIMENTO-VARSEC       'PER ENTRARE IN QUESTA ROUTINE PASSANO CIRCA 2 SECONDI
'        ENDIF
'        SEROUT2 TXPC,6,[ "TEMPOSPEGNIMENTO= ",#TEMPOSPEGNIMENTO,13,10 ]        
'    ENDIF
    
RETURN
   
    
    


'EXITSVEGLIA:

'        GOSUB ON_OFF
'        GOSUB SETUP

''    LOWPOWER=0
''INTCON3.5=1
'RETURN

'SPEGNIMODULO:
'            'HSEROUT ["AT+CPOWD=0",13]  'spengo il modulo
'            'HSEROUT ["AT+CFUN=4",13]  'spengo il modulo
'            pause 5000  
'            MODULOON=0
'            TEMPOSPEGNIMENTO=0
'RETURN



SETUP:

SEROUT2 TXPC,6,[ "SETUP",13,10 ]

HSEROUT ["AT&F",13]
HSERIN 5000,ERRORE,[WAIT ("OK")]
PAUSE 1000

HSEROUT ["AT+IPR=115200",13]
HSERIN 5000,ERRORE,[WAIT ("OK")]
PAUSE 200
HSEROUT ["AT+CMEE=2",13]                'AT+CMEE=0     Mobile Equipment Error Code
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200

HSEROUT ["AT+CGSN",13]      'richiedi imei
HSERIN 2000,SETUP,[SKIP 5,WAIT (10),STR BUFF\20\13]
SEROUT2 TXPC,6,[ "IMEI ",STR BUFF,13,10 ]
FOR TMP=0 TO 20
    if BUFF[tmp]=0 then
        WRITE 730+TMP,"#"   'FINE STRINGA
        tmp=21
    else
        WRITE 730+TMP,BUFF[TMP]
    ENDIF
NEXT TMP
PAUSE 200


'HSEROUT ["AT+CMGF?",13]                'Set message format  Text mode
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT+QSIMDET?",13]                'Set message format  Text mode
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT+QSIMDET=0",13]                'Set message format  Text mode
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 20000
HSEROUT ["AT+CMGF=1",13]                'Set message format  Text mode
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200

HSEROUT ["AT+CLIP=1",13]                'Request calling line identification
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200
HSEROUT ["AT+CFUN?",13]                'Request calling line identification
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200
HSEROUT ["AT+CFUN=?",13]  
pause 200
'HSEROUT ["AT+CFUN=1",13]  
'pause 2000

HSEROUT ["AT+CSMINS=1",13]  
pause 200


    HSEROUT ["AT+CRC=1",13] 
'    HSERIN 2000,SETUP,[wait ("O")]
    PAUSE 500
    
    'HSEROUT ["AT+COPS?",13]
    'PAUSE 1000
    
    HSEROUT ["AT+COPS=0,2",13]
    PAUSE 500
    
    HSEROUT ["AT+CREG=2",13]    
    PAUSE 500
    
'HSEROUT ["AT#CAP=0",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT#CAP?",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT+CLVL?",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000

HSEROUT ["AT+CSCLK=2",13]                'Request calling line identification
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200


HSEROUT ["AT&V",13]                'Request calling line identification
HSERIN 2000,ERRORE,[WAIT ("OK")]
PAUSE 200

'HSEROUT ["AT&D",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT&K",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000
'HSEROUT ["AT&W",13]                'Request calling line identification
'HSERIN 2000,ERRORE,[WAIT ("OK")]
'PAUSE 1000

'MODULOON=1 'IL MODULO è ACCESO



RETURN


ON_OFF: 
SEROUT2 TXPC,84,[ "ON/OFF",13,10 ]
    SEROUT2 TXPC,6,[ "H POWERGSM",13,10 ]
    HIGH POWERGSM      'accendi il modulo
    SEROUT2 TXPC,6,[ "H RESET",13,10 ]
    HIGH RESET  'RESETTA IL MODULO
    PAUSE 200
    SEROUT2 TXPC,6,[ "L RESET",13,10 ]
    LOW RESET


    PAUSE 2100
    SEROUT2 TXPC,6,[ "L POWERGSM",13,10 ]
    LOW POWERGSM
    PAUSE 2000
    
    for TMP=0 TO 30
        PAUSE 100    
        SEROUT2 TXPC,6,[ "STATO ",#STATO,13,10 ]
        IF STATO=0 THEN
            TMP=31
        ENDIF
    NEXT TMP
    
    IF STATO=1 THEN 
        SEROUT2 TXPC,6,[ "RIAVVIO",13,10 ]
'        LOW LED
        PAUSE 2000
        GOTO ON_OFF
    ENDIF
    
'    INPUT VEXTGSM    
'    SEROUT2 TXPC,84,[ "VEXTGSM ",#VEXTGSM,13,10 ]  
'    IF VEXTGSM=1 THEN             
        FOR TMP=0 TO 10
            PAUSE 500
            TOGGLE LD1
        NEXT TMP
        
'        HIGH DTRGSM         'FORZA L'AUTOBAUD
'        PAUSE 2200
'        LOW  DTRGSM
'        PAUSE 1000
        
        
        LOW LD1
        FOR TMP=0 TO 2
            HSEROUT ["A"]
            PAUSE 100
            HSEROUT ["T"]
            PAUSE 100
            HSEROUT [13]
            PAUSE 100
        NEXT TMP
        'HSERIN 2000,ERRORE,[WAIT ("OK")]
        PAUSE 3000
'    ELSE
'        SEROUT2 TXPC,84,[ "SCARICO I CONDENSATORI ",13,10 ]
'        LOW VEXTGSM    'SCARICO I CONDENSATORI
'        PAUSE 2000
'    ENDIF
RETURN 



'ON_OFF: 
'SEROUT2 TXPC,6,[ "ON/OFF",13,10 ]
'    SEROUT2 TXPC,6,[ "H POWERGSM",13,10 ]
'    HIGH POWERGSM      'accendi il modulo
'    SEROUT2 TXPC,6,[ "H RESET",13,10 ]
'    HIGH RESET  'RESETTA IL MODULO
'    PAUSE 200
'    SEROUT2 TXPC,6,[ "L RESET",13,10 ]
'    LOW RESET


'    PAUSE 2100
'    SEROUT2 TXPC,6,[ "L POWERGSM",13,10 ]
'    LOW POWERGSM
'    PAUSE 2000
    
''    for TMP=0 TO 30
''        PAUSE 100    
''        SEROUT2 TXPC,6,[ "STATO ",#STATO,13,10 ]
''        IF STATO=0 THEN
''            TMP=31
''        ENDIF
''    NEXT TMP
    
''    IF STATO=1 THEN 
''        SEROUT2 TXPC,6,[ "RIAVVIO",13,10 ]
'''        LOW LED
''        PAUSE 2000
''        GOTO ON_OFF
''    ENDIF
    
''    INPUT VEXTGSM    
''    SEROUT2 TXPC,6,[ "VEXTGSM ",#VEXTGSM,13,10 ]  
''    IF VEXTGSM=1 THEN             
'        FOR TMP=0 TO 10
'            PAUSE 500
'            TOGGLE LD1
'        NEXT TMP
        
''        HIGH DTRGSM         'FORZA L'AUTOBAUD
''        PAUSE 2200
''        LOW  DTRGSM
''        PAUSE 1000
        
        
'        LOW LD1
'        FOR TMP=0 TO 2
'            HSEROUT ["A"]
'            PAUSE 100
'            HSEROUT ["T"]
'            PAUSE 100
'            HSEROUT [13]
'            PAUSE 100
'        NEXT TMP
'        'HSERIN 2000,ERRORE,[WAIT ("OK")]
'        PAUSE 1000
        
        
        
        
''    ELSE
''        SEROUT2 TXPC,6,[ "SCARICO I CONDENSATORI ",13,10 ]
''        LOW VEXTGSM    'SCARICO I CONDENSATORI
''        PAUSE 2000
''    ENDIF
'RETURN 



ERRORE:

    SEROUT2 TXPC,6,[ "ERRORE",13,10]
    HSEROUT ["AT+CPOWD=0",13]  'spengo il modulo
    pause 5000  
    
    GOTO START
END



MEMOMESS:

    SEROUT2 TXPC,6,["MEMOMESS",13,10]

    FOR TMP=0 TO 192    'leggo 160 CELLE DALLA MEMORIA PROGRAMMA
        TMPW1=60160+((POSIZIONE-1)*192+TMP)
        READCODE TMPW1,TMP2
        'SEROUT2 P2,6,[ TMP2]
        IF TMP2<>255 THEN
            SEROUT2 TXPC,6,[ #TMP2,32 ]        
        ELSE
            tmp=200
        ENDIF
    NEXT TMP
    SEROUT2 TXPC,6,[10,13 ]
    
    
    FOR TMP=0 TO 190    'leggo 160 CELLE DALLA MEMORIA PROGRAMMA
        TMPW1=60160+((POSIZIONE-1)*192+TMP)
        ERASECODE TMPW1
        'SEROUT2 P2,6,[ "CANCELLO LA POS ",#TMPW1,10,13 ]
        'SEROUT2 P2,6,[ TMP2]
        'SEROUT2 P2,6,[ #TMP2,32 ]        
    NEXT TMP
    'SEROUT2 P2,6,[10,13 ]
    
    SEROUT2 TXPC,6,[ "POI MEM POS ",#POSIZIONE,10,13 ]
    SEROUT2 TXPC,6,[ "MEMORIZZO LA FRASE ",#TMP3]
    
    FOR TMP=0 TO LUNGNUM
        SEROUT2 TXPC,6,[BUFF[TMP]]                     
    NEXT TMP            
    SEROUT2 TXPC,6,[10,13]
        
    
    'SEROUT2 P2,6,[ "PRIMA CANCELLO LA POS: ",#POSIZIONE,10,13 ]
   
    'LUNGNUM=LUNGNUM+1
    'SEROUT2 P2,6,[ "POI MEM POS ",#POSIZIONE,10,13 ]
    WHILE ((lungnum+1)//8)<>0       'se non scrivo almeno 8 caratteri ricalcola la lunghezza perchè ciò avvenga
        LUNGNUM=LUNGNUM+1
        BUFF[LUNGNUM]=255
        SEROUT2 TXPC,6,["LUNGNUM     : ",#LUNGNUM,10,13]
    WEND
    
    TMPW1=60160+((POSIZIONE-1)*192)
    SEROUT2 TXPC,6,[ "TMPW1 ",#TMPW1,10,13 ]
    WHILE ((TMPW1)//8)<>0
        TMPW1=TMPW1+1
        SEROUT2 TXPC,6,["inizio     : ",#TMPW1,10,13]
    WEND 
    
    FOR TMP=0 TO LUNGNUM        
        SEROUT2 TXPC,6,[ "POSIZIONE ",#TMPW1," DATO ",BUFF[TMP],10,13]
        WRITECODE TMPW1,BUFF[TMP]
        TMPW1=tmpw1+1                             
    NEXT TMP
    
    
        WRITECODE TMPW1,255 'AGGIUNTO 
    
    
    SEROUT2 TXPC,6,[ "MESS SALVATO "  ]
    FOR TMP=0 TO LUNGNUM
        TMPW1=60160+((POSIZIONE-1)*192+TMP)
        READCODE TMPW1,TMP2
        SEROUT2 TXPC,6,[ "POSIZIONE ",#TMPW1," DATO ",TMP2,10,13]
        IF TMP2<>255 THEN
            SEROUT2 TXPC,6,[ TMP2]
        ENDIF
    NEXT TMP
    SEROUT2 TXPC,6,[ 10,13  ]
        
RETURN

LEGGIMESS:
    SEROUT2 TXPC,6,["LEGGIMESS POS ",#POSIZIONE,13,10]
'    FOR TMP=0 TO 192    'leggo 160 CELLE DALLA MEMORIA PROGRAMMA
'        TMPW1=60160+((POSIZIONE-1)*192+TMP)
'        READCODE TMPW1,TMP2
'        'SEROUT2 P2,6,[ TMP2]
'        SEROUT2 P2,6,[ TMP2,32 ]        
'    NEXT TMP
'    SEROUT2 P2,6,[10,13 ]
    
    FOR TMP=0 TO 192
        TMPW1=60160+((POSIZIONE-1)*192+TMP)
        READCODE TMPW1,TMP2
        IF TMP2<>255 THEN
            SEROUT2 TXPC,6,[ TMP2]
            HSEROUT [TMP2]
        ELSE
            GOTO EXITLEGGIMESS
        ENDIF
    NEXT TMP
EXITLEGGIMESS:
    SEROUT2 TXPC,6,[ 10,13  ]
        
RETURN




'BASE64CONV:

''INTCON3.5=0    
'    'SEROUT2 TXPC,6,["LUNGNUM PRIMA ",#LUNGNUM,13,10]
'        WHILE (LUNGNUM+1)//3<>0 
'                LUNGNUM=LUNGNUM+1
'                BUFF[LUNGNUM]=0
'        WEND
    
'    'SEROUT2 TXPC,6,["LUNGNUM DOPO  ",#LUNGNUM,13,10]
    
'    FOR TMP=0 TO LUNGNUM-TMP4
        
'        TMP1=BUFF[TMP]
'        TMP=TMP+1
'        TMP2=BUFF[TMP]
'        TMP=TMP+1
'        TMP3=BUFF[TMP]
    
        
'        SEROUT2 TXPC,6,[TMP1,TMP2,TMP3,32]
'        'SEROUT2 TXPC,6,[BIN8 TMP1,32,BIN8 TMP2,32,BIN8 TMP3,13,10] 
'        TMPL=TMP1>>2
'        'SEROUT2 TXPC,6,[BIN6 tmpl,32]
'        GOSUB BASE64ABB
'        SEROUT2 TXPC,6,[tmpl1]
'        HSEROUT [tmpl1]
'        TMPL=TMP2>>4
'        tmpl.4=tmp1.0
'        tmpl.5=tmp1.1
'        'SEROUT2 TXPC,6,[BIN6 tmpl,32]
'        GOSUB BASE64ABB
'        SEROUT2 TXPC,6,[tmpl1]
'        HSEROUT [tmpl1]
'        IF TMP2<>0 THEN
'            tmpl=tmp2<<4
'            tmpl.3=tmp3.7
'            tmpl.2=tmp3.6
'            tmpl=tmpl>>2
'            'SEROUT2 TXPC,6,[BIN6 tmpl,32]
'            GOSUB BASE64ABB
'            SEROUT2 TXPC,6,[tmpl1]
'            HSEROUT [tmpl1]
'        ELSE
'            SEROUT2 TXPC,6,["="]
'            HSEROUT ["="]
'        ENDIF
'        IF TMP3<>0 THEN
'            tmpl=tmp3 & %00111111
'            'SEROUT2 TXPC,6,[BIN6 tmpl,32]
'            GOSUB BASE64ABB
'            SEROUT2 TXPC,6,[tmpl1]
'            HSEROUT [tmpl1]
'        ELSE
'            SEROUT2 TXPC,6,["="]
'            HSEROUT ["="]
'        ENDIF
'        SEROUT2 TXPC,6,[13,10]
'    NEXT TMP
'    SEROUT2 TXPC,6,[13,10] 
'    HSEROUT [13,10] 
    
    
'RETURN


'BASE64ABB:
'    LOOKUP tmpl,["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"],tmpl1
''INTCON3.5=1
'RETURN












' Rilevato pulsante premuto
Disable                     
'PULSANTI:



''    IF LOWPOWER=1 THEN
''        GOSUB SVEGLIA
''    ENDIF
    
''asm
'' movwf _W_TEMP   ; Salvo W in W_TEMP
'' swapf STATUS,W  ; Metto STATUS in W
'' clrf STATUS   ; Seleziono banco 0, indipendentemente dal banco corrente. 
''          ; Clears IRP, RP1, RP0
'' movwf _STATUS_TEMP  ; metto STATUS (contenuto in W) in STATUS_TEMP
'' movf PCLATH,W  ; Salvo PCLATH
'' movwf _PCLATH_TEMP  ; in PCLATH_TEMP
'' clrf PCLATH   ; seleziono la pagina 0
''endasm
''STATUS_TEMP= STATUS
''PCLATH_TEMP= PCLATH
'    'SEROUT2 TXPC,6,[ "INTCON  ",bin8 INTCON,13,10 ]
'    'SEROUT2 TXPC,6,[ "INTCON2 ",bin8 INTCON2,13,10 ]
'    'SEROUT2 TXPC,6,[ "INTCON3 ",bin8 INTCON3,13,10 ]
'    'pause 5000
'   IF INTCON3.0=1 THEN              'IL PULSANTE 1 INVIA LE MAIL CON LE COORDINATE
''    INTCON  01010001
''    INTCON2 00000010    
''    INTCON3 11111001
'        SEROUT2 TXPC,6,[ "-P1 PREMUTO",13,10 ] 
''        SEROUT2 TXPC,6,[ "ABILITO INTERRUPT SU RB",13,10 ] 
''        INTCON      = %10011000
'        RITENTA=1       'IMPOSTANDO RITENTA=1 INVIA LE COORDINATE FINO A QUANDO NON SAREANNO DISPONIBILI
'        PULSOUT LD1,10000
'        INTCON3.0=0
'   ENDIF
'   IF INTCON.1=1 THEN
''    INTCON  01010011    
''    INTCON2 00000010    
''    INTCON3 11111000
        
'        SEROUT2 TXPC,6,[ "-P2 PREMUTO",13,10 ]         
'        PULSOUT LD1,10000
''        HSEROUT ["AT+CCLK=",34,"01/01/01,00:00:00+00",34,13]      'ORA
''        PAUSE 500
''        READ 703,TMP
''        IF TMP=0 THEN
''            SEROUT2 TXPC,6,[ "ATTIVO IL POLLING",13,10 ]
''            WRITE 703,1     'ATTIVO IL POLLING             
''        ELSE
''            SEROUT2 TXPC,6,[ "DISATTIVO IL POLLING",13,10 ]
''            WRITE 703,0     'DISATTIVO IL POLLING
''        ENDIF
'         INTCON.1=0
'   ENDIF
'   IF INTCON3.1=1 THEN
''    INTCON  01010001
''    INTCON2 00000010    
''    INTCON3 11111010
'        SEROUT2 TXPC,6,[ "-P3 PREMUTO",13,10 ]         
'        PULSOUT LD1,10000
''        READ 705,TMP
''        IF TMP=2 THEN
''            WRITE 705,30
''            FOR TMP=1 TO 10
''                TOGGLE LD1
''                PAUSE 200
''            NEXT TMP
''        ELSE
''            WRITE 705,2
''            FOR TMP=1 TO 3
''                TOGGLE LD1
''                PAUSE 200
''            NEXT TMP
''        ENDIF
        
'        INTCON3.1=0
'   ENDIF
'   IF INTCON3.2=1 THEN
'        SEROUT2 TXPC,6,[ "-SENSORE",13,10 ]         
'        PULSOUT LD1,10000        
'        SENSORE=1
''        SEROUT2 TXPC,6,[ "DISABILITO INTERRUPT SU RB",13,10 ] 
''        INTCON      = %10010000
'         INTCON3.2=0
'         INTCON3.5=0    'disabilito il sensore
         
'   ENDIF
   
   
'   'INTCON      = %10010000             ' Tacita interrupt flag
'   'INTCON3     = %11111000
   
   
''ASM
''movf _PCLATH_TEMP,W  ; Recupero PCLATH
''movwf PCLATH   ;
''swapf _STATUS_TEMP,W  ; Recupero STATUS (cioè riseleziono i banchi come erano prima)
''movwf STATUS   ;
''swapf _W_TEMP,F  ; Recupero W
''swapf _W_TEMP,W  ;
''ENDASM

''STATUS= STATUS_TEMP 
''PCLATH= PCLATH_TEMP

'Resume                      
disable 
PULSANTI:


PULSANTE=0

'   IF INTCON3.0=1 THEN              'INT1
'        SEROUT2 TXPC,6,[ "INGRESSO 2",13,10 ]
'        INTCON3.0=0 
'   ENDIF
   
   IF INTCON3.1=1 THEN              'INT2
        SEROUT2 TXPC,6,[ "INT INGRESSO 1",13,10 ] 
        VARIAZIONEIN=1
        INTCON3.1=0 
        TIMESLEEP=900    'COSì ESCO
   ENDIF
   
   
   IF INTCON.1=1 THEN              'INT1
        SEROUT2 TXPC,6,[ "INT MOVIMENTO",13,10 ] 
        VARIAZIONEMOV=1
        INTCON.1=0 
        TIMESLEEP=900    'COSì ESCO
   ENDIF
   
   IF INTCON.0=1 THEN              'INT1
        SEROUT2 TXPC,6,[ "INT PORTB (PIR)",13,10 ] 
        VARIAZIONEPIR=1
        INTCON.0=0 
        TIMESLEEP=900    'COSì ESCO
   ENDIF


Resume                      
Enable 


Where to buy

The GSM alarm is available in the store.

About Boris Landoni

Boris Landoni is the technical manager of Open-Electronics.org. Skilled in the GSM field, embraces the Open Source philosophy and its projects are available to the community.