Ok. I planned to use this code. Does this include some commands that those attiny85 doesnt support? Will it work in that microcontroller?
#include
SoftwareSerial sim800(8,9);
char phone_number1[]=”+xxxxxx”; // put phone number here, international format, no spaces
char phone_number2[]=”+xxxxxx”;
unsigned long time_pressed4;
unsigned long time_pressed5;
boolean SIMok = false;
int current_status;
void setup() {
delay(5000);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(11, OUTPUT); // green LED
pinMode(12, OUTPUT); // red LED
digitalWrite(11, HIGH); // blink green and red LED
digitalWrite(12, HIGH);
delay(500);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
pinMode(7, OUTPUT); // sim800 RESET
digitalWrite(7, LOW);
Serial.begin(9600);
sim800.begin(9600);
Serial.print(F(“Testing sim800 module.. “));
SIMok = false;
for (int i=0;i<3;i++) {
if (simReady()) {
SIMok = true;
break;
}
}
if (SIMok) simDetails(); // get some details on SIM
else { // if SIM not OK then blink RED forever
for (; {
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(100);
}
}
digitalWrite(11, HIGH); // green LED on for SIM ok
}
void loop() {
current_status = checkStatus();
// Serial.println(current_status); // 48: ready 51: ringing 52: in call
if (current_status == 52) { // IN CALL (should be 4 according to datasheet)
Serial.println("52: in call");
digitalWrite(12, HIGH); // red LED on when in call
delay(500);
if (!digitalRead(4) || !digitalRead(5)) { // if push any button hang up call
digitalWrite(12, LOW); // red LED off after hang up
sim800.println("ATH");
Serial.println(F("Hanging up call"));
simReply();
}
}
else {
if (current_status == 51) { // RINGING (should be 3 according to datasheet)
Serial.println(F("51: incoming call.."));
digitalWrite(12, HIGH); // blink red LED when ringing
delay(500);
digitalWrite(12, LOW);
if (!digitalRead(4)|| !digitalRead(5)) { // if push any button answer call
digitalWrite(12, HIGH); // red LED on when in call
sim800.println("ATA");
Serial.println(F("Answering call"));
simReply();
}
}
else {
digitalWrite(12, LOW);
if (digitalRead(4)) time_pressed4 = millis(); // button not pushed
else if (millis() - time_pressed4 > 500) simCall(phone_number1); // button pushed, call if long push
if (digitalRead(5)) time_pressed5 = millis(); // button not pushed
else if (millis() – time_pressed5 > 500) simCall(phone_number2); // button pushed, call if long push
}
}
}
boolean simReady() { // SIM READY
digitalWrite(7, LOW); // hardware reset after sleep RST
delay(1000);
digitalWrite(7, HIGH);
// time to startup 3 sec
for (int i=0;i<6;i++) {
digitalWrite(11, HIGH); // green LED blink
delay(250);
digitalWrite(11, LOW);
delay(250);
}
sim800.println("AT");
delay(100);
if (sim800.find("OK")) {
Serial.println(F("OK"));
Serial.print(F("Registering on the network.."));
for (int i = 0; i<20; i++) {
// time to register on network 2.5 sec
for (int i=0;i<5;i++) {
digitalWrite(11, HIGH); // green LED blink
delay(250);
digitalWrite(11, LOW);
delay(250);
}
Serial.print(".");
sim800.println("AT+CREG?"); // possible replies: 0,0=not registered 0,1=registered on home network 0,2=not registered but searching 0,3=denied 0,4=unknown 0,5=roaming
delay(1000);
if (sim800.find("0,1")) {
Serial.println(F(" OK"));
return true;
}
}
Serial.println(F("failed to register for 1min, will reset sim800"));
return false;
}
else
{
Serial.println(F("sim800 module not OK, please check"));
return false;
}
}
void simReply() { // SIM REPLY
delay(500);
while (sim800.available()) {
char c = sim800.read();
// if (c != '\n') Serial.write(c); // replace new line with space
// else Serial.print(" ");
delay(5);
}
// Serial.println();
}
int checkStatus() { // CHECK STATUS FOR RINGING or IN CALL
sim800.println("AT+CPAS"); // phone activity status: 0= ready, 2= unknown, 3= ringing, 4= in call
delay(500);
if (sim800.find("+CPAS: ")) { // decode reply
char c = sim800.read();
simReply();
return c;
}
else return -1;
}
void simCall(char number[]) {
digitalWrite(12, HIGH);
Serial.print("Start call.. ");
sim800.println("AT+CREG?"); // possible replies: 0,0=not registered 0,1=registered on home network 0,2=not registered but searching 0,3=denied 0,4=unknown 0,5=roaming
delay(1000);
if (sim800.find("0,1")) {
Serial.println(F("network connection still OK"));
simReply();
}
else {
SIMok = false;
for (int i=0;i<3;i++) {
if (simReady()) {
SIMok = true;
break;
}
}
if (!SIMok) { // SIM not ok, blink RED forever
for (; {
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(100);
}
}
}
Serial.print("Calling number ");
Serial.println(number);
String cmd; // make call command
cmd = "ATD";
cmd += number;
cmd += ";";
sim800.println(cmd);
delay(1000);
simReply();
}
void simDetails() {
Serial.print(F("operator:"));
sim800.println("AT+COPS?"); // OPERATOR
delay(500);
if (sim800.find(":")) { // decode reply
while (sim800.available()) {
char c = sim800.read();
if (c != '\n') Serial.write(c); // replace new line with space
else Serial.print(" ");
delay(5);
}
}
else Serial.print(F("n/a"));
Serial.println();
Serial.print(F("signal strength:"));
sim800.println("AT+CSQ"); // SIGNAL STRENGTH
delay(500);
if (sim800.find(":")) { // decode reply
while (sim800.available()) {
char c = sim800.read();
if (c != '\n') Serial.write(c); // replace new line with space
else Serial.print(" ");
}
}
else Serial.print(F("n/a"));
Serial.println();
Serial.print(F("battery level:"));
sim800.println("AT+CBC"); // BATTERY LEVEL
delay(500);
if (sim800.find(":")) { // decode reply
while (sim800.available()) {
char c = sim800.read();
if (c != '\n') Serial.write(c); // replace new line with space
else Serial.print(" ");
}
}
else Serial.print(F("n/a"));
Serial.println();
sim800.println("AT+CLVL=80"); // set speaker volume 0-100
simReply();
sim800.println("AT+CRSL=80"); // set ringer volume 0-100
simReply();
sim800.println("AT+CMIC=0,12"); // set mic to gain level 0-15
simReply();
// ring tone AT+CALS=5,1 to switch on tone 5, 5,0 to switch off
sim800.println("AT+CALS=19"); // set alert/ring tone
simReply();
Serial.println("Ready");
}