Arduino Sketchs only works in usb

Hi everyone,

Im making a project and I used arduino UNO and SIM808, so a upload that sketch to arduino:

include <SoftwareSerial.h>


#define pinRX 2
#define pinTX 3


SoftwareSerial VTS(pinRX, pinTX);


const int LED_SIM808 = 7;
const int LED_GSM = 8;
const int LED_DATA = 10;
const int LED_GPS = 9;
const int LED_WIFI = 11;
const int LED_B = 6;
const int LED_OK = 5;
const int LED_V = 4;
const int BUTTON_START = 12;

int buttonState = 0;

char resposta[200];

const char gsm_conection[] = "AT+CSTT=\"zap.vivo.com\",\"vivo\",\"vivo\"";
const char host[] = "api.pushingbox.com";
const char DEVID[] = "/pushingbox?devid=xxx";
const char porta[] = "80";


char latitude[15];
char longitude[15];
char altitude[16];
char data[24];
char satelites[3];
char satelites_uso[3];
char velocidade[10];
char curso[15];

void setup() 
{
	
	pinMode(LED_SIM808, OUTPUT);
	pinMode(LED_GSM, OUTPUT);
	pinMode(LED_DATA, OUTPUT);
	pinMode(LED_GPS, OUTPUT);
	pinMode(LED_WIFI, OUTPUT);
	pinMode(LED_B, OUTPUT);
	pinMode(LED_OK, OUTPUT);
	pinMode(LED_V, OUTPUT);
	pinMode(BUTTON_START, INPUT);
	wait_button();
	#ifdef DEBUG_ON
	Serial.begin(9600);
#endif
	808.begin(9600);
	
	start_system();
	
}


void loop() 
{
	delay(2000);

	if (get_http())
	{
		blink_led(LED_OK);
	}
	else
		blink_led(LED_V);
}

void wait_button()
{
	buttonState = digitalRead(BUTTON_START);

	while (buttonState == 0)
	{
		
		digitalWrite(LED_V, HIGH);
		delay(250);
		digitalWrite(LED_V, LOW);
		delay(250);
	//	asm volatile ("  jmp 0");
		buttonState = digitalRead(BUTTON_START);
	}
}

void start_system()
{
	blink_led(LED_SIM808);
	while (!sendATcommand("AT", "OK", 1000))
		blink_led(LED_V);
	digitalWrite(LED_SIM808, HIGH);

	apn_conection();
	while (!data_test());


	
	blink_led(LED_GPS);

	while (!sendATcommand("AT+CGNSPWR=1", "OK", 2000))
		blink_led(LED_V);

	int gps_ok = 0;

	for (int i = 0; i < 150; i++)
	{
		digitalWrite(LED_GPS, HIGH);
		delay(100);
		digitalWrite(LED_GPS, LOW);
		delay(100);
	}

	while(!gps_ok)
	{
		if (sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000))
		{
			blink_led(LED_OK);
			gps_ok = 1;
		}
		else if (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000))
		{
			blink_led(LED_B);
			gps_ok = 1;
		}

		else
			blink_led(LED_V);
	}

	digitalWrite(LED_GPS, HIGH);
	get_http();
}

int apn_conection()
{
	blink_led(LED_GSM);
	if (!sendATcommand("AT+CIFSR", "ERROR", 5000))
	{
		sendATcommand("AT+CIPCLOSE", "ERROR", 2000);
		sendATcommand("AT+CIPSERVER=0", "ERROR", 2000);
		digitalWrite(LED_GSM, HIGH);
		return 1;

	}
	else
	{
		sendATcommand("AT+CIPSHUT", "SHUT OK", 3000);
		if (sendATcommand(gsm_conection, "OK", 5000))
		{
			sendATcommand("AT+CIICR", "OK", 3000);
			if (!sendATcommand("AT+CIFSR", "ERROR", 5000))
			{
				digitalWrite(LED_GSM, HIGH);
				return 1;
			}
			else
			{
				if (data_test())
				{
					digitalWrite(LED_GSM, HIGH);
					return 1;
				}
				else
					return 0;
					
			}

		}
	}
}

int data_test()
{

	blink_led(LED_DATA);
	808.print("AT+CIPSTART=\"TCP\",\"");
	808.print(host);
	808.print("\",\"");
	808.print(porta);
	808.print("\"");
	if (sendATcommand("", "OK", 10000))
	{
		blink_led(LED_OK);
	}
	else
	{
		blink_led(LED_V);
		return 0;
	}

	delay(5000);

	if (sendATcommand("AT+CIPSEND", ">", 2000))
	{
		blink_led(LED_OK);
	}
	else
	{
		char end_c[2];
		end_c[0] = 0x1a;
		end_c[1] = '\0';
		808.print(end_c);
		808.print("\n\n");
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		blink_led(LED_V);
		return 0;
	}

	
	808.print("GET ");
	808.print(DEVID);
	808.print(" HTTP/1.0\nHost: ");
	808.print(host);
	808.print("\nUser - Agent: Arduino");

	if (sendATcommand("", "SEND OK", 5000))
	{
		digitalWrite(LED_DATA, HIGH);
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 1;
	}
	else
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 0;
	}
	
}
int8_t get_GPS() {

	int8_t answer;
	char * auxChar;
	// request Basic string
	sendATcommand("AT+CGNSINF", "O", 2000);

	auxChar = strstr(resposta, "+CGNSINF:");
	if (auxChar != NULL)
	{

		// Limpando as variaveis
		memset(longitude, '\0', 15);
		memset(latitude, '\0', 15);
		memset(altitude, '\0', 16);
		memset(data, '\0', 24);
		memset(satelites, '\0', 3);
		memset(satelites_uso, '\0', 3);
		memset(velocidade, '\0', 10);
		memset(curso, '\0', 15);

		strcpy(resposta, auxChar);


		strtok(resposta, ",");
		strtok(NULL, ",");
		strcpy(data, strtok(NULL, ",")); // Gets date
		strcpy(latitude, strtok(NULL, ",")); // Gets latitude
		strcpy(longitude, strtok(NULL, ",")); // Gets longitude
		strcpy(altitude, strtok(NULL, ",")); // Gets altitude    
		strcpy(velocidade, strtok(NULL, ",")); // Gets speed over ground. Unit is knots. 
		strcpy(curso, strtok(NULL, ",")); // Gets course
		strtok(NULL, ",");
		strtok(NULL, ",");
		strtok(NULL, ",");
		strtok(NULL, ",");
		strcpy(satelites, strtok(NULL, ",")); // Gets satellites 
		strcpy(satelites_uso, strtok(NULL, ",")); // Gets satellites                                        

		answer = 1;
	}
	else
		answer = 0;

	return answer;
}

int get_http()
{

	808.print("AT+CIPSTART=\"TCP\",\"");
	808.print(host);
	808.print("\",\"");
	808.print(porta);
	808.print("\"");
	if (!sendATcommand("", "OK", 10000))
		return 0;

	delay(3000);

	if (!get_GPS())
		return 0;

	if (!sendATcommand("AT+CIPSEND", ">", 2000))
		return 0;
	
	808.print("GET /pushingbox?devid=XXXC&lat=");
	808.print(latitude);
	808.print("&long=");
	808.print(longitude);
	808.print("&alt=");
	808.print(altitude);
	808.print("&curso=");
	808.print(curso);
	808.print("&sat=");
	808.print(satelites);
	808.print("&satuso=");
	808.print(satelites_uso);
	808.print("&vel=");
	808.print(velocidade);
	808.print(" HTTP/1.0\nHost: ");
	808.print(host);
	808.print("\nUser - Agent: Arduino");
	if (!sendATcommand("", "SEND OK", 5000))
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 0;
	}
	else
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 1;
	}

}
void blink_led(int led)
{
	for (int i = 0; i < 3; i++)
	{
		digitalWrite(led, HIGH);
		delay(125);
		digitalWrite(led, LOW);
		delay(125);
	}
}

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {

	uint8_t x = 0, answer = 0;
	unsigned long previous;
	char readVar[200];
	char * auxChar;

	char end_c[2];
	end_c[0] = 0x1a;
	end_c[1] = '\0';


	memset(resposta, '\0', 200);    // Initialize the string
	memset(readVar, '\0', 200);    // Initialize the string

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

	808.write(ATcommand);    // Send the AT command 
	808.write("\r\n\r\n");    // Send enter
	808.print(end_c);

	
	#ifdef DEBUG_ON
	Serial.println(ATcommand);
	#endif
	x = 0;
	previous = millis();

	// this loop waits for the answer
	do {
		if (808.available() != 0) {
			readVar[x] = 808.read();
			x++;
			// check if the desired answer is in the response of the module
			auxChar = strstr(readVar, expected_answer1);
			if (auxChar != NULL)
			{
				if (strstr(readVar, "+CGNSINF:") == NULL)
					strcpy(resposta, auxChar);
				else
					strcpy(resposta, readVar);

				answer = 1;
			}
		}
		// Waits for the asnwer with time out
	} while ((answer == 0) && ((millis() - previous) < timeout));

	if (auxChar == NULL)
	{
#ifdef DEBUG_ON
		Serial.println(readVar);
#endif
		
	}

	
	return answer;
}

Until here thats OK on USB works perfectly but when i remove de usb and conect a 12DC 2A, for supply the 808 and arduino the code doesnt works... For works i have to turn on the system and push the reset button from arduino... I have no ideia what can be... Why i have to push reset one time to work?

#endif

Where is the #if statement that goes with this?

sketch_aug28a.ino:56:2: error: invalid suffix "begin" on floating constant
sketch_aug28a.ino:181:2: error: invalid suffix "print" on floating constant
sketch_aug28a.ino:182:2: error: invalid suffix "print" on floating constant
sketch_aug28a.ino:183:2: error: invalid suffix "print" on floating constant
sketch_aug28a.ino:184:2: error: invalid suffix "print" on floating constant

You can NOT have an instance of anything named 808. Variables can NOT start with a number.

PaulS:

#endif

Where is the #if statement that goes with this?

You can NOT have an instance of anything named 808. Variables can NOT start with a number.

Hi, about a variable named 808 i changed every one... About the #if statement, i declared now:

#define debug
#ifdef debug
	Serial.begin(9600);
#endif

And is just works in usb or when i turn on without usb and reset the arduino...

j_serep:
i dont know very well how to use, can you help me?

The program you posted in your Original Post is wrong and it cannot work. For some reason there was a glitch when you tried to copy your program. I am assuming you do have a working program.

Post the correct version of your working program in your next Reply.

...R

Robin2:
The program you posted in your Original Post is wrong and it cannot work. For some reason there was a glitch when you tried to copy your program. I am assuming you do have a working program.

Post the correct version of your working program in your next Reply.

...R

OK i fixed:

#include <SoftwareSerial.h>


#define pinRX 2
#define pinTX 3

#define debug

SoftwareSerial VTS(pinRX, pinTX);


const int LED_SIM808 = 7;
const int LED_GSM = 8;
const int LED_DATA = 10;
const int LED_GPS = 9;
const int LED_WIFI = 11;
const int LED_B = 6;
const int LED_OK = 5;
const int LED_V = 4;
const int BUTTON_START = 12;

int buttonState = 0;

char resposta[200];

const char gsm_conection[] = "AT+CSTT=\"zap.vivo.com\",\"vivo\",\"vivo\"";
const char host[] = "api.pushingbox.com";
const char DEVID[] = "/pushingbox?devid=mydevid";
const char porta[] = "80";


char latitude[15];
char longitude[15];
char altitude[16];
char data[24];
char satelites[3];
char satelites_uso[3];
char velocidade[10];
char curso[15];

void setup() 
{
	
	pinMode(LED_SIM808, OUTPUT);
	pinMode(LED_GSM, OUTPUT);
	pinMode(LED_DATA, OUTPUT);
	pinMode(LED_GPS, OUTPUT);
	pinMode(LED_WIFI, OUTPUT);
	pinMode(LED_B, OUTPUT);
	pinMode(LED_OK, OUTPUT);
	pinMode(LED_V, OUTPUT);
	pinMode(BUTTON_START, INPUT);
	wait_button();
	#ifdef debug
	Serial.begin(9600);
#endif
	VTS.begin(9600);
	
	start_system();
	
}


void loop() 
{
	delay(2000);

	if (get_http())
	{
		blink_led(LED_OK);
	}
	else
		blink_led(LED_V);
}

void wait_button()
{

	while (buttonState == 0)
	{
		
		digitalWrite(LED_V, HIGH);
		delay(250);
		digitalWrite(LED_V, LOW);
		delay(250);

		buttonState = digitalRead(BUTTON_START);
	}
}

void start_system()
{
	blink_led(LED_SIM808);
	while (!sendATcommand("AT", "OK", 1000))
		blink_led(LED_V);
	digitalWrite(LED_SIM808, HIGH);

	apn_conection();


	
	blink_led(LED_GPS);

	while (!sendATcommand("AT+CGNSPWR=1", "OK", 2000))
		blink_led(LED_V);

	int gps_ok = 0;

	for (int i = 0; i < 150; i++)
	{
		digitalWrite(LED_GPS, HIGH);
		delay(100);
		digitalWrite(LED_GPS, LOW);
		delay(100);
	}

	while(!gps_ok)
	{
		if (sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000))
		{
			blink_led(LED_OK);
			gps_ok = 1;
		}
		else if (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000))
		{
			blink_led(LED_B);
			gps_ok = 1;
		}

		else
			blink_led(LED_V);
	}

	digitalWrite(LED_GPS, HIGH);
	get_http();
}

int apn_conection()
{
	blink_led(LED_GSM);
	if (!sendATcommand("AT+CIFSR", "ERROR", 5000))
	{
		sendATcommand("AT+CIPCLOSE", "ERROR", 2000);
		sendATcommand("AT+CIPSERVER=0", "ERROR", 2000);
		digitalWrite(LED_GSM, HIGH);
		return 1;

	}
	else
	{
		sendATcommand("AT+CIPSHUT", "SHUT OK", 3000);
		if (sendATcommand(gsm_conection, "OK", 5000))
		{
			sendATcommand("AT+CIICR", "OK", 3000);
			if (!sendATcommand("AT+CIFSR", "ERROR", 5000))
			{
				digitalWrite(LED_GSM, HIGH);
				return 1;
			}
			else
			{
				if (data_test())
				{
					digitalWrite(LED_GSM, HIGH);
					return 1;
				}
				else
					return 0;
					
			}

		}
	}
}

int data_test()
{

	blink_led(LED_DATA);
	VTS.print("AT+CIPSTART=\"TCP\",\"");
	VTS.print(host);
	VTS.print("\",\"");
	VTS.print(porta);
	VTS.print("\"");
	if (sendATcommand("", "OK", 10000))
	{
		blink_led(LED_OK);
	}
	else
	{
		blink_led(LED_V);
		return 0;
	}

	delay(5000);

	if (sendATcommand("AT+CIPSEND", ">", 2000))
	{
		blink_led(LED_OK);
	}
	else
	{
		char end_c[2];
		end_c[0] = 0x1a;
		end_c[1] = '\0';
		VTS.print(end_c);
		VTS.print("\n\n");
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		blink_led(LED_V);
		return 0;
	}

	
	VTS.print("GET ");
	VTS.print(DEVID);
	VTS.print(" HTTP/1.0\nHost: ");
	VTS.print(host);
	VTS.print("\nUser - Agent: Arduino");

	if (sendATcommand("", "SEND OK", 5000))
	{
		digitalWrite(LED_DATA, HIGH);
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 1;
	}
	else
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 0;
	}
	
}
int8_t get_GPS() {

	int8_t answer;
	char * auxChar;
	// request Basic string
	sendATcommand("AT+CGNSINF", "O", 2000);

	auxChar = strstr(resposta, "+CGNSINF:");
	if (auxChar != NULL)
	{

		// Limpando as variaveis
		memset(longitude, '\0', 15);
		memset(latitude, '\0', 15);
		memset(altitude, '\0', 16);
		memset(data, '\0', 24);
		memset(satelites, '\0', 3);
		memset(satelites_uso, '\0', 3);
		memset(velocidade, '\0', 10);
		memset(curso, '\0', 15);

		strcpy(resposta, auxChar);


		strtok(resposta, ",");
		strtok(NULL, ",");
		strcpy(data, strtok(NULL, ",")); // Gets date
		strcpy(latitude, strtok(NULL, ",")); // Gets latitude
		strcpy(longitude, strtok(NULL, ",")); // Gets longitude
		strcpy(altitude, strtok(NULL, ",")); // Gets altitude    
		strcpy(velocidade, strtok(NULL, ",")); // Gets speed over ground. Unit is knots. 
		strcpy(curso, strtok(NULL, ",")); // Gets course
		strtok(NULL, ",");
		strtok(NULL, ",");
		strtok(NULL, ",");
		strtok(NULL, ",");
		strcpy(satelites, strtok(NULL, ",")); // Gets satellites 
		strcpy(satelites_uso, strtok(NULL, ",")); // Gets satellites                                        

		answer = 1;
	}
	else
		answer = 0;

	return answer;
}

int get_http()
{

	VTS.print("AT+CIPSTART=\"TCP\",\"");
	VTS.print(host);
	VTS.print("\",\"");
	VTS.print(porta);
	VTS.print("\"");
	if (!sendATcommand("", "OK", 10000))
		return 0;

	delay(3000);

	if (!get_GPS())
		return 0;

	if (!sendATcommand("AT+CIPSEND", ">", 2000))
		return 0;
	
	VTS.print("GET /pushingbox?devid=mydevid&lat=");
	VTS.print(latitude);
	VTS.print("&long=");
	VTS.print(longitude);
	VTS.print("&alt=");
	VTS.print(altitude);
	VTS.print("&curso=");
	VTS.print(curso);
	VTS.print("&sat=");
	VTS.print(satelites);
	VTS.print("&satuso=");
	VTS.print(satelites_uso);
	VTS.print("&vel=");
	VTS.print(velocidade);
	VTS.print(" HTTP/1.0\nHost: ");
	VTS.print(host);
	VTS.print("\nUser - Agent: Arduino");
	if (!sendATcommand("", "SEND OK", 5000))
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 0;
	}
	else
	{
		sendATcommand("AT+CIPCLOSE", "CLOSED", 2000);
		return 1;
	}

}
void blink_led(int led)
{
	for (int i = 0; i < 3; i++)
	{
		digitalWrite(led, HIGH);
		delay(125);
		digitalWrite(led, LOW);
		delay(125);
	}
}

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {

	uint8_t x = 0, answer = 0;
	unsigned long previous;
	char readVar[200];
	char * auxChar;

	char end_c[2];
	end_c[0] = 0x1a;
	end_c[1] = '\0';


	memset(resposta, '\0', 200);    // Initialize the string
	memset(readVar, '\0', 200);    // Initialize the string

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

	VTS.write(ATcommand);    // Send the AT command 
	VTS.write("\r\n\r\n");    // Send enter
	VTS.print(end_c);

	
	#ifdef debug
	Serial.println(ATcommand);
	#endif
	x = 0;
	previous = millis();

	// this loop waits for the answer
	do {
		if (VTS.available() != 0) {
			readVar[x] = VTS.read();
			x++;
			// check if the desired answer is in the response of the module
			auxChar = strstr(readVar, expected_answer1);
			if (auxChar != NULL)
			{
				if (strstr(readVar, "+CGNSINF:") == NULL)
					strcpy(resposta, auxChar);
				else
					strcpy(resposta, readVar);

				answer = 1;
			}
		}
		// Waits for the asnwer with time out
	} while ((answer == 0) && ((millis() - previous) < timeout));

	if (auxChar == NULL)
	{
#ifdef debug
		Serial.println(readVar);
#endif
		
	}

	
	return answer;
}

For works i have to turn on the system and push the reset button from arduino... I have no ideia what can be... Why i have to push reset one time to work?

What do you mean by "turn on the system"?

...R

Robin2:
What do you mean by "turn on the system"?

...R

power on

It's a long time since 25 Sept and I have forgotten all about this.

Maybe some of your peripherals need time to start up. Try putting delay(5000); as the first thing in setup()

If that solves the problem then you can experiment with shorter delay()s

...R