Advertisement
microrobotics

SIM808

Apr 14th, 2023
1,641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The SIM808 is a GSM/GPRS/GPS module that can be used for cellular communication and GPS tracking. In this example, I'll provide you with an Arduino code to test the all-round functionality of the SIM808 module by sending various AT commands and receiving responses. The code will cover basic operations such as checking the module status, registering to the network, getting network information, sending an SMS, and getting GPS data. Note that this code assumes you are using an Arduino Uno or a similar board.
  3.  
  4. First, connect the SIM808 module to the Arduino as follows:
  5.  
  6. Vcc to 5V
  7. GND to GND
  8. RX (SIM808) to TX (Arduino)
  9. TX (SIM808) to RX (Arduino)
  10.  
  11. Upload this code to your Arduino and open the Serial Monitor. The Arduino will send various AT commands to the SIM808 module and display the received responses in the Serial Monitor. This will help you test the all-round functionality of the SIM808 module.
  12.  
  13. Make sure you have a proper power supply for the SIM808 module, as it may draw a high current during operation. Also, ensure you have a GSM SIM card inserted into the module and that it is registered with the network.
  14.  
  15. When running the test, keep in mind that the GPS functionality may not work indoors or in an area with poor signal reception. Make sure the SIM808 has a clear view of the sky to receive GPS signals. Additionally, it may take a few minutes for the SIM808 module to get a GPS fix after it has been powered on.
  16.  
  17. This example code should help you test the basic functionalities of the SIM808 module, including GSM communication and GPS tracking. You can modify the code or add more AT commands to test other functionalities provided by the SIM808 module. Please refer to the SIM808 AT Command Set documentation for more information on the available AT commands and their usage.
  18. */
  19.  
  20. // SIM808 GSM/GPRS/GPS Module
  21. // Example code for Arduino
  22.  
  23. #include <SoftwareSerial.h>
  24.  
  25. const int simRxPin = 10; // Connect the RX pin of the SIM808 to Digital Pin 10
  26. const int simTxPin = 11; // Connect the TX pin of the SIM808 to Digital Pin 11
  27.  
  28. SoftwareSerial simSerial(simRxPin, simTxPin); // Create a SoftwareSerial object for communication with the SIM808
  29.  
  30. void setup() {
  31.   Serial.begin(9600); // Initialize the serial communication for debugging
  32.   simSerial.begin(9600); // Initialize the software serial communication with the SIM808
  33.  
  34.   delay(5000); // Wait for the SIM808 to power on and initialize
  35.   Serial.println("SIM808 Test");
  36. }
  37.  
  38. void loop() {
  39.   // Test AT command
  40.   sendATCommand("AT");
  41.  
  42.   // Check SIM card status
  43.   sendATCommand("AT+CPIN?");
  44.  
  45.   // Query network registration status
  46.   sendATCommand("AT+CREG?");
  47.  
  48.   // Get network information
  49.   sendATCommand("AT+COPS?");
  50.  
  51.   // Send SMS
  52.   sendATCommand("AT+CMGF=1"); // Set SMS mode to text
  53.   sendATCommand("AT+CMGS=\"+1234567890\""); // Replace +1234567890 with the phone number to send the SMS to
  54.   simSerial.print("Hello, World!"); // The SMS text
  55.   simSerial.write(0x1A); // Send Ctrl+Z (0x1A) to indicate the end of the SMS text
  56.  
  57.   // Enable GPS
  58.   sendATCommand("AT+CGPSPWR=1"); // Power on the GPS
  59.   sendATCommand("AT+CGPSRST=0"); // Set GPS to standalone mode
  60.   sendATCommand("AT+CGPSINF=0"); // Get GPS data
  61.  
  62.   delay(30000); // Wait for 30 seconds before running the test again
  63. }
  64.  
  65. void sendATCommand(const char *command) {
  66.   // Send an AT command to the SIM808
  67.   simSerial.println(command);
  68.  
  69.   // Wait for a response from the SIM808
  70.   while (!simSerial.available()) {
  71.     delay(100);
  72.   }
  73.  
  74.   // Read the response from the SIM808
  75.   String response = simSerial.readStringUntil('\n');
  76.  
  77.   // Print the command and response to the Serial Monitor
  78.   Serial.print("Command: ");
  79.   Serial.println(command);
  80.   Serial.print("Response: ");
  81.   Serial.println(response);
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement