SIM900 GPRS

#include <BlynkSimpleUserDefined.h>
#include <SoftwareSerial.h>

// SIM900 Shield Serial Pins
SoftwareSerial SIM900(7, 8);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "4fadeMYTOKENee6cc96eded52d";
#define ip_address "AT+CIPSTART=\"TCP\",\"8.8.8.8\",\"8442\""

bool connected = false;

// This function is used by Blynk to receive data
size_t BlynkStreamRead(void* buf, size_t len)
{
  return Serial.readBytes((byte*)buf, len);
}

// This function is used by Blynk to send data
size_t BlynkStreamWrite(const void* buf, size_t len)
{
  return Serial.write((byte*)buf, len);
}

void setup()
{
  // Setup your connection here.
  
  pinMode(9, OUTPUT);
  Serial.begin(19200); // for serial monitor
  Serial.println("Powering on GPRS Shield.");
  SIM900.begin(19200); // Serial for GSM shield
  SIM900poweron();  // turn on shield
  
  sendCommand("AT", "OK", 1000);     //Wake up modem with an AT command
  if(sendCommand("AT", "OK", 1000) == 1){
    Serial.println("SIM900 started");
  }
  
  gprs();
  
  Blynk.begin(auth);

  do {
    connected = Blynk.connect();
  } while (!connected);
}

void loop()
{
  
  Blynk.run(); 
    // Make sure that Blynk.run() is called
  // only when the connection is established
  if (connected) {
    // Okay, handle Blynk protocol
    bool hasIncomingData = (Serial.available() > 0);
    // Tell Blynk if it has incoming data
    // (this allows to skip unneeded BlynkStreamRead calls)
    if (!Blynk.run(hasIncomingData)) {
      // Error happened. No action for serial.
    }
  }
}

void gprs()
{
  //Wake up modem with two AT commands
  sendCommand("AT", "OK", 1000);
  sendCommand("AT", "OK", 2000);
  sendCommand("AT+CIPSHUT", "SHUT OK", 2000);
  // Check if it's currently registered network
  if(sendCommand("AT+CREG?","+CREG: 0,1",2000)){ 
    Serial.println("Connected to the home cell network");
  }
  else{
    Serial.println("Not connected to home cell network");
    delay(10000);
  }
  
  Serial.println("Attaching to Packet Service");
  if(sendCommand("AT+CGATT=1","OK",3000)){ 
    Serial.println("Attached to Packet Service");
  }
  else{
    Serial.println("Unable to attach to Packet Service");
    delay(10000);
 }
Serial.println("Setting APN");
if(sendCommand("AT+CSTT=\"epc.tmobile.com\"","OK",3000)){ 
    Serial.println("APN Set");
  }
  else{
    Serial.println("Unable to set APN");
    delay(10000);
   }
Serial.println("Starting GPRS Connection");
if(sendCommand("AT+CIICR","OK",5000)){ 
    Serial.println("GPRS Connection Established");
    delay(1000);
    sendCommand("AT+CIFSR", "OK", 1000);
  }
  else{
    Serial.println("Unable to start GPRS connection");
    delay(10000);
 }
 Serial.println("Establishing TCP Connection");

 SIM900.println(ip_address);
 delay(1000);
 Serial.println("Connected to server");
  
   
}

void SIM900poweron()
// Software equivalent of pressing the GSM shield "power" button
{
  //Wake up modem with an AT command
  sendCommand("AT", "OK", 1000);
  if(sendCommand("AT", "OK", 2000) == 0){
    digitalWrite(9, HIGH);
    delay(1000);
    digitalWrite(9, LOW);
    delay(7000);
  }

}

int sendCommand(char* ATcommand, char* expected_answer, unsigned int timeout){

    
    int answer=0;
    int responsePos=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Clears array

    delay(100);

    while( SIM900.available() > 0) SIM900.read();    // Clean the input buffer

    SIM900.println(ATcommand);    // Send the AT command 


    responsePos = 0;
    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(SIM900.available() != 0){    
            response[responsePos] = SIM900.read();
            responsePos++;
            // check if the desired answer is in the response of the module
            if (strstr(response, expected_answer) != NULL)    
            {
              answer = 1;
            }
        }
        // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));    
    return answer;
}

  

 

posted @ 2017-05-26 15:08  zhh  阅读(182)  评论(0编辑  收藏  举报