Kod: Zaznacz cały
#include <NewSoftSerial.h>
#define num_to_char(number) ((number) < 10 ? \
('0' + (number)) : \
(('A' - 10) + (number)) )
/* To select the 4 bits we do this */
#define first_four(byte) (0x0F & (byte))
/* Last four: Shift left to get to a number < 16 */
#define last_four(byte) ((0xF0 & (byte)) >> 4)
/* The MSB goes first. */
#define hexdump_a(byte) num_to_char( last_four(byte))
#define hexdump_b(byte) num_to_char(first_four(byte))
/*
*/
NewSoftSerial cell(2,3); //pin 2 is Rx pin 3 is Tx
int incomingByte=0;
int incomingByteSerial=0;
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0;
int timesToSend = 1; // Numbers of SMS to send
int count = 0;
int licznik=0;
char* myStrings[]={"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6"};
void setup(){
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
Serial.begin(9600);
cell.begin(4800);// the GPRS baud rate
Serial.println("Communication is Starting...");
Serial.println("AT");
cell.println("AT");
delay(500);
Serial.println("Wybor formatu PDU: AT+CMGF=0");
cell.println("AT+CMGF=0");
delay(500);
Serial.println("Zapytanie o aktulany format: AT+CMGF=?");
cell.println("AT+CMGF=?");
delay(500);
Serial.print("Otrzymana odpowiedz: ");
incomingByte = cell.read();
Serial.println(incomingByte, DEC);
Serial.println("Wybor pamieci telefonu: AT+CPMS=ME");
cell.println("AT+CPMS=\"ME\"");
delay(1500);
Serial.println("Nadus przycisk");
}
void loop(){
if (cell.available()>0)
{
Serial.println("Jest wiadomosc lub tylko jest obecny telefon");
Serial.print("Otrzymalem: ");
cell.println("AT");
delay(500);
incomingByte = cell.read();
Serial.print("odczyt z po poleceniu AT: ");
Serial.println(incomingByte, DEC);
}
if (Serial.available()>0)
{
Serial.print("Odczyt z serialu: ");
incomingByteSerial= Serial.read();
Serial.println(incomingByteSerial, DEC);
}
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
send_sms(myStrings[licznik]);
licznik++;
delay(1500);
deleteAllMsgs();
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void hexdump_byte(unsigned char byte)
{
cell.print(hexdump_a(byte), BYTE);
Serial.print(hexdump_a(byte), BYTE);
cell.print(hexdump_b(byte), BYTE);
Serial.print(hexdump_b(byte), BYTE);
}
//***** wysyłanie sms'a**********
void send_sms(char *data)
{
size_t data_length, x;
char c, l;
long i;
long n;
data_length = strlen(data);
i = data_length * 7;
/* Round i up to a multiple of 8 */
if (i & 0x07) i = (i & ~0x07) + 0x08;
/* Calculate the number of message octets */
i = i / 8;
Serial.println("Wysylanie sms'a");
cell.println("AT+CMGF=0");
delay(1500);
Serial.println("AT+CMGF=0");
cell.print("AT+CMGS=");
delay(1500);
Serial.print("AT+CMGS=");
cell.println(i + 14);
delay(1500);
Serial.println(i + 14);
cell.print("0011000B918497522815F20000AA"); // E51
Serial.print("0011000B918497522815F20000AA");
hexdump_byte(data_length & 0xFF);
l = 0;
n = 0;
for (x = 0; x < data_length; x++)
{
if (data[x] == '$') data[x] = 0x02;
n |= (data[x] & 0x7F) << l;
l += 7;
if (l >= 8)
{
hexdump_byte(n & 0xFF);
l -= 8;
n >>= 8;
}
}
if (l != 0)
{
hexdump_byte(n & 0xFF);
}
cell.println(0x1A, BYTE);
Serial.println(0x1A, BYTE);
Serial.println("Wyslano");
}
//****** kasowanie sms'a *******
void deleteAllMsgs() {
Serial.println("Kasowanie po wyslaniu: AT+CMGD=1");
cell.println("AT+CMGD=1");
delay(1500);
Serial.println("Skasowano. Czekam na nastepne polecenia.");
}
Kod: Zaznacz cały
Communication is Starting...
AT
Wybor formatu PDU: AT+CMGF=0
Zapytanie o aktulany format: AT+CMGF=?
Otrzymana odpowiedz: 0
Wybor pamieci telefonu: AT+CPMS=ME
Kasowanie pierwszej wiadomosci w pmamieci: AT+CMGD=1
Nadus przycisk
Wysylanie sms'a
AT+CMGF=0
AT+CMGS=28
0011000B918497522815F20000AA1054747A0E4ACF4173BA3CED3E8362
Wyslano
Kasowanie po wyslaniu: AT+CMGD=1
Skasowano. Czekam na nastepne polecenia.
Kod: Zaznacz cały
if (cell.available()>0)
{
Serial.println("Jest wiadomosc lub tylko jest obecny telefon");
Serial.print("Otrzymalem: ");
cell.println("AT");
delay(500);
incomingByte = cell.read();
Serial.print("odczyt z po poleceniu AT: ");
Serial.println(incomingByte, DEC);
}
Z ciekawości podłączyłem ten model bluethoot'em do HyperTerminala i sprawdziłem jakie polecenia działają i co otrzymuję w odpowiedzi. W HT po komendzie AT otrzymuję OK.
Jeżeli w serial monitorze Arduino wpiszę cokolwiek to po zadziałaniu fragmentu kodu:
Kod: Zaznacz cały
if (Serial.available()>0)
{
Serial.print("Odczyt z serialu: ");
incomingByteSerial= Serial.read();
Serial.println(incomingByteSerial, DEC);
}
Powyższy program działa w wersji 0022 po drobnych modyfikacja (biblioteka) działa też w 1.01, niemniej jednak odczyt z telefonu dalej nie działa.
Ma ktoś z Was pomysł jak to ugryźć -odczyt z telefonu?