I have problems getting a code working on the DUE, that works on the UNO with no problems. The reason that I what to shift from UNO to DUE is to get more SRAM for a bitmap to be requested through a HTTP-request (through a 3G shield from Cooking-Hacks) and printed out of on a Thermal Printer (not part of the attached code).
However, when I run this code (build on a tutorial provided by Cooking-Hacks for the UNO), it does't return any values to data[]. I read that the DUE sometimes has problems with serial.available/serial.read and needs a firmware update. Is this also the case with the 3rd revision board I just bought?
Here is my code for reference:
//
#include <string.h>
#define MAX_STRING_LEN 100
int8_t answer;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
char *p, *i;
char *text;
char *filename;
char *date;
char *time2;
char aux_str[50];
char data[400];
int http_x;
int http_status; // 0: end, 1 waiting data with header, 2 waiting data without header, 3 timeout waiting data, 4 unknow response
int x = 0;
long previous;
//Write here you SIM data
const char pin_number[] = "1001";
const char apn[] = "internet";
const char user_name[] = "";
const char password[] = "";
//
char url[ ]="www.infraordinary.dk";
int port= 80;
char request[ ]="GET /display_vars.php?test HTTP/1.1\r\nHost: www.infraordinary.dk\r\nContent-Length: 0\r\n\r\n";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
//sets the PIN code
sprintf(aux_str, "AT+CPIN=%s", pin_number);
sendATcommand(aux_str, "OK", 2000);
delay(3000);
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
// sets APN, user name and password
sprintf(aux_str, "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
sendATcommand(aux_str, "OK", 2000);
sprintf(aux_str, "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
sendATcommand(aux_str, "OK", 2000);
}
void loop(){
// request the url
sprintf(aux_str, "AT+CHTTPACT=\"%s\",%d", url, port);
answer = sendATcommand(aux_str, "+CHTTPACT: REQUEST", 60000);
// Sends the request
Serial.println(request);
// Sends <Ctrl+Z>
Serial.write(0x1A);
http_status = 1;
while ((http_status == 1) || (http_status == 2))
{
answer = sendATcommand("", "+CHTTPACT: ", 60000);
if (answer == 0)
{
http_status = 3;
}
else
{
// answer == 1
while(Serial.available()==0);
aux_str[0] = Serial.read();
if ((aux_str[0] == 'D') && (http_status == 1))
{
// Data packet with header
while(Serial.available()<4);
Serial.read(); // A
Serial.read(); // T
Serial.read(); // A
Serial.read(); // ,
// Reads the packet size
x=0;
do{
while(Serial.available()==0);
aux_str[x] = Serial.read();
x++;
}
while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
aux_str[x-1] = '\0';
data_size = atoi(aux_str);
// Now, search the end of the HTTP header (\r\n\r\n)
do{
while (Serial.available() < 3);
data_size--;
if (Serial.read() == '\r')
{
data_size--;
if (Serial.read() == '\n')
{
data_size--;
if (Serial.read() == '\r')
{
data_size--;
if (Serial.read() == '\n')
{
// End of the header found
http_status = 2;
}
}
}
}
}
while ((http_status == 1) && (data_size > 0));
if (http_status == 2)
{
// Reads the data
http_x = 0;
do{
if(Serial.available() != 0){
data[http_x] = Serial.read();
http_x++;
data_size--;
}
else
{
delay(1);
}
}
while(data_size > 0);
data[http_x] = '\0';
}
}
else if ((aux_str[0] == 'D') && (http_status == 2))
{
// Data packet with header
while(Serial.available()<4);
Serial.read(); // A
Serial.read(); // T
Serial.read(); // A
Serial.read(); // ,
// Reads the packet size
x=0;
do{
while(Serial.available()==0);
aux_str[x] = Serial.read();
x++;
}
while((aux_str[x-1] != '\r') && (aux_str[x-1] != '\n'));
aux_str[x-1] = '\0';
data_size = atoi(aux_str);
do{
if(Serial.available() != 0){
data[http_x] = Serial.read();
http_x++;
}
else
{
delay(1);
}
}
while(data_size > 0);
data[http_x] = '\0';
}
else if (aux_str[0] == '0')
{
// end of the AT command
http_status = 0;
}
else
{
// unknow response
http_status = 4;
Serial.print(char(aux_str[0]));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
Serial.print(char(Serial.read()));
}
}
}
if (http_status == 0)
{
// Makes variables out of data[]:
Serial.print(F("HTTP data: "));
Serial.println(data);
Serial.println("Split record1: ");
filename = subStr(data, "=", 2);
date = subStr(data, "=", 3);
time2 = subStr(data, "=", 4);
text = subStr(data, "=", 5);
Serial.println(filename);
Serial.println(date);
Serial.println(time2);
Serial.println(text);
}
else
{
Serial.print(F("http_status: "));
Serial.println(http_status, DEC);
}
delay(1000);
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "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 = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand( char* ATcommand,
char* expected_answer1,
unsigned int timeout)
{
return sendATcommand2( ATcommand,
expected_answer1,
expected_answer1,
timeout);
}
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, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
// Function to return a substring defined by a delimiter at an index
char* subStr (char* str, char *delim, int index) {
char *act, *sub, *ptr;
static char copy[MAX_STRING_LEN];
int i;
// Since strtok consumes the first arg, make a copy
strcpy(copy, str);
for (i = 1, act = copy; i <= index; i++, act = NULL) {
//Serial.print(".");
sub = strtok_r(act, delim, &ptr);
if (sub == "=") break;
}
return sub;
}
Thanks a lot in advance!
HTTP_Terminal_get_var_DUE.ino (7.3 KB)