here is some thing wrong with your code so i am pasting code after modified this and try to understand what i want to tell u
#include <SoftwareSerial.h>
const int PWK= 7; // Analog output pin that the LED is attached to
const int STA= 8;
const int RX=2;
const int TX=3;
int STA_value;
SoftwareSerial mySerial(RX,TX); // RX, TX
void setup() {
// initialize the digital pin as an output.
pinMode(PWK, OUTPUT); // questo pin serve per accendere il dispositivo GSM
pinMode(STA, INPUT);
//pinMode(RX,INPUT); //if u want to use this as serial port then don't declare this I/O
//pinMode(TX,OUTPUT);
gsmOn();
Serial.begin(9600); //Init seriale sw
Serial.println("Arduino serial initialized!");
delay(10);
mySerial.begin(9600); //init seriale hw
Serial.println("Software serial initialized!");
delay(10);
}
// the loop routine runs over and over again forever:
void loop()
{
uint8_t answer=0;
// checks if the module is started
answer = sendCommand("AT", "OK", "OK", 2000);
if (answer == 1)
{
Serial.println("OK");
}
else
Serial.println("Not Responding");
delay(1000);
}
int8_t sendCommand(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( mySerial.available() > 0)
{
mySerial.read(); // Clean the input buffer
}
Serial.println(ATcommand); // Send the AT command to serial port
mySerial.println(ATcommand); // Send the AT command to gsm module
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(mySerial.available() != 0){
response[x] = mySerial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
void gsmOn()
{
// Takes 30 seconds to complete
digitalWrite(PWK, HIGH); // turn the Phone on
delay(2);
}
and if still you have any issue please leave comment