Skip to content

Instantly share code, notes, and snippets.

@smetronic
Created January 4, 2018 11:40
Show Gist options
  • Save smetronic/3eead360bd41e85cb43c9b86baa9d30e to your computer and use it in GitHub Desktop.
Save smetronic/3eead360bd41e85cb43c9b86baa9d30e to your computer and use it in GitHub Desktop.
ATMega 328p: Sending TCP packet using SIM808, SIM900
#include <SoftwareSerial.h>
SoftwareSerial SIM(8, 9);
int8_t answer;
int onModulePin = 2;
char aux_str[50];
char ip_data[40] = "Test string from GPRS shieldrn";
void setup() {
pinMode(onModulePin, OUTPUT);
SIM.begin(9600);
Serial.begin(9600);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sendATcommand2("AT+CPIN=****", "OK", "ERROR", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000) == 0);
}
void loop() {
// Selects Single-connection mode
if (sendATcommand2("AT+CIPMUX=0", "OK", "ERROR", 1000) == 1)
{
// Waits for status IP INITIAL
while (sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0);
delay(5000);
// Sets the APN, user name and password
if (sendATcommand2("AT+CSTT=\"www\",\"\",\"\"", "OK", "ERROR", 30000) == 1)
{
// Waits for status IP START
while (sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0);
delay(5000);
// Brings Up Wireless Connection
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
{
// Waits for status IP GPRSACT
while (sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0);
delay(5000);
// Gets Local IP Address
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
{
// Waits for status IP STATUS
while (sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0);
delay(5000);
Serial.println("Openning TCP");
// Opens a TCP socket
if (sendATcommand2("AT+CIPSTART=\"TCP\",\"104.130.162.53\",\"1997\"", "CONNECT OK", "CONNECT FAIL", 30000) == 1)
{
Serial.println("Connected");
// Sends some data to the TCP socket
sprintf(aux_str, "AT+CIPSEND=%d", strlen(ip_data));
if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
{
sendATcommand2(ip_data, "SEND OK", "ERROR", 10000);
}
// Closes the socket
sendATcommand2("AT+CIPCLOSE", "CLOSE OK", "ERROR", 10000);
}
else
{
Serial.println("Error openning the connection");
}
}
else
{
Serial.println("Error getting the IP address");
}
}
else
{
Serial.println("Error bring up wireless connection");
}
}
else
{
Serial.println("Error setting the APN");
}
}
else
{
Serial.println("Error setting the single connection");
}
sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
delay(10000);
}
void power_on() {
uint8_t answer = 0;
// checks if the module is started
answer = sendATcommand2("AT", "OK", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin, HIGH);
delay(3000);
digitalWrite(onModulePin, LOW);
// waits for an answer from the module
while (answer == 0) { // Send AT every two seconds and wait for the answer
answer = sendATcommand2("AT", "OK", "OK", 2000);
}
}
}
int8_t sendATcommand2(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, ' ', 100); // Initialize the string
delay(100);
while (SIM.available() > 0) SIM.read(); // Clean the input buffer
SIM.println(ATcommand); // Send the AT command
x = 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 (SIM.available() != 0) {
response[x] = SIM.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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment