Jump to content
43oh

Unable to send data to webpage


Recommended Posts

Hello friends

I have taken the code from this link: https://hackaday.io/project/5626/logs

Here very nice explanation is given about esp8266 and arduino. I am trying it with ESP8266 and MSP430g2553 using energia.

But i am stuck at ERROR 13

that means all AT commands are working properly but when it try to connect to plot.ly server then at this stage error is coming

 

The code is giving error 13 that means it has not getting response as '~' (as per the code)

So anybody knows what type of error  this is??

please go through below code and reply me.

 

#define ssid       "friends"

#define pass       "walchand"

#define userName   "umeshwe"

#define APIKey     "3yg9lnub2w"

#define fileName   "test"

#define fileopt    "overwrite"

#define nTraces    1

#define maxpoints  "30"

#define world_readable true

#define convertTimestamp true

#define timezone "Indian/Cocos"

 

#include <SoftwareSerial.h>

#include <avr/pgmspace.h>

 

#define prog_char  char PROGMEM

 

char *tokens[nTraces] = {"u6bibkxlmz"};

char stream_site[25] = {0};

 

SoftwareSerial mySerial(5, 6); // RX, TX

 

 

const PROGMEM  char  cmd_0[] = {"AT\r\n"};

const PROGMEM  char  cmd_1[] = {"ATE1\r\n"};

const PROGMEM  char  cmd_2[] = {"AT+CWMODE=1\r\n"};

const PROGMEM  char  cmd_3[] = {"AT+CIPMODE=0\r\n"};

const PROGMEM  char  cmd_4[] = {"AT+RST\r\n"};

const PROGMEM  char  cmd_5[] = {"AT+CIPMUX=1\r\n"};

const PROGMEM  char  cmd_6[] = {"AT+CWJAP=\""};

const PROGMEM  char  cmd_7[] = {"AT+CIPCLOSE=\""};

const PROGMEM  char  cmd_8[] = {"AT+CIPSTART=0,\"TCP\",\""};

const PROGMEM  char  cmd_9[] = {"AT+CIPSEND=0,"};

 

const PROGMEM  char  resp_0[] = {"OK"};

const PROGMEM  char  resp_1[] = {"ready"};

const PROGMEM  char  resp_2[] = {"no change"};

const PROGMEM  char  resp_3[] = {"CONNECT"};

const PROGMEM  char  resp_4[] = {"Unlink"};

const PROGMEM  char  resp_5[] = {"Linked"};

const PROGMEM  char  resp_6[] = {">"};

const PROGMEM  char  resp_7[] = {"~"};

 

const PROGMEM  char  error[] = {"*ERROR "};

const PROGMEM  char  error_0[] = {"0*"};

const PROGMEM  char  error_1[] = {"1*"};

const PROGMEM  char  error_2[] = {"2*"};

const PROGMEM  char  error_3[] = {"3*"};

const PROGMEM  char  error_4[] = {"4*"};

const PROGMEM  char  error_5[] = {"5*"};

const PROGMEM  char  error_6[] = {"6*"};

const PROGMEM  char  error_7[] = {"7*"};

const PROGMEM  char  error_8[] = {"8*"};

const PROGMEM  char  error_9[] = {"9*"};

const PROGMEM  char  error_10[] = {"10*"};

const PROGMEM  char  error_11[] = {"11*"};

const PROGMEM  char  error_12[] = {"12*"};

const PROGMEM  char  error_13[] = {"13*"};

const PROGMEM  char  error_14[] = {"14*"};

const PROGMEM  char  error_15[] = {"15*"};

const PROGMEM  char  error_16[] = {"16*"};

 

const PROGMEM  char  string_0[] = {"Initializing plot with Plot.ly server...\r\n"};

const PROGMEM  char  string_1[] = {"\",\""};

const PROGMEM  char  string_2[] = {"\""};

const PROGMEM  char  string_3[] = {"\",80"};

const PROGMEM  char  string_4[] = {"POST /clientresp HTTP/1.1\r\n"};

const PROGMEM  char  string_5[] = {"Host: plot.ly:80\r\n"};

const PROGMEM  char  string_6[] = {"User-Agent: Arduino/0.6.0\r\n"};

const PROGMEM  char  string_7[] = {"Content-Length: "};

const PROGMEM  char  string_8[] = {"version=2.3&origin=plot&platform=arduino&un="};

const PROGMEM  char  string_9[] = {"&key="};

const PROGMEM  char  string_10[] = {"&args=["};

const PROGMEM  char  string_11[] = {"{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \""};

const PROGMEM  char  string_12[] = {"\", \"maxpoints\": "};

const PROGMEM  char  string_13[] = {"}}"};

const PROGMEM  char  string_14[] = {", "};

const PROGMEM  char  string_15[] = {"]&kwargs={\"fileopt\": \""};

const PROGMEM  char  string_16[] = {"\", \"filename\": \""};

const PROGMEM  char  string_17[] = {"\", \"world_readable\": "};

const PROGMEM  char  string_18[] = {"true"};

const PROGMEM  char  string_19[] = {"false"};

const PROGMEM  char  string_20[] = {"}"};

const PROGMEM  char  string_21[] = {"Successfully Initialized.\r\n"};

const PROGMEM  char  string_22[] = {"Please visit: \"http://plot.ly/~"};

const PROGMEM  char  string_23[] = {"\"."};

const PROGMEM  char  string_24[] = {"POST / HTTP/1.1\r\n"};

const PROGMEM  char  string_25[] = {"Host: arduino.plot.ly\r\n"};

const PROGMEM  char  string_26[] = {"User-Agent: Arduino\r\n"};

const PROGMEM  char  string_27[] = {"Transfer-Encoding: chunked\r\n"};

const PROGMEM  char  string_28[] = {"Connection: close\r\n"};

const PROGMEM  char  string_29[] = {"plotly-convertTimestamp: \"Australia/Melbourne\"\r\n\r\n"};

const PROGMEM  char  string_30[] = {"\r\n{\"x\": "};

const PROGMEM  char  string_31[] = {", \"y\": "};

const PROGMEM  char  string_32[] = {", \"streamtoken\": \""};

const PROGMEM  char  string_33[] = {"\"}\n\r\n0\r\n\r\n"};

 

const char* const resp_table[] PROGMEM = {resp_0, resp_1, resp_2, resp_3, resp_4, resp_5, resp_6, resp_7};

 

char buffer[20]={0};

 

/* -------------- Setup ------------------ */

void setup() {

  // Setup Serial

  mySerial.begin(9600);

  Serial.begin(115200);

  Serial.setTimeout(8000);

  

  delay(1000);

  ESP8266_Init();

  plotly_init();

  ESP8266_Disconnect(0);   // Making sure disconnected

  ESP8266_Connect("arduino.plot.ly");

}

 

/* ------------- Loop -------------------- */

void loop() {

  mySerial.println("Reading ADC...");

  int val = analogRead(0);

  mySerial.println("Sending Jason...");

  plotly_plot(millis(),val,tokens[0]);

}

 

/* --------------- Functions -------------------- */

 

void mySerial_PrintConstString(const prog_char str[]) {

  char c;

  if(!str) return;

  while((c = pgm_read_byte(str++)))

    mySerial.write©;

}

 

void Serial_PrintConstString(const prog_char str[]) {

  char c;

  if(!str) return;

  while((c = pgm_read_byte(str++)))

    Serial.write©;

}

 

boolean find_resp(unsigned char a, const prog_char error_str[]){

  strcpy_P(buffer, (char*)pgm_read_word(&(resp_table[a])));

  if(!Serial.find(buffer)){

    mySerial_PrintConstString(error);

    mySerial_PrintConstString(error_str);

    mySerial.println();

    return false;

  }

  else{

    return true;

  }

}

 

void ESP8266_Init(){

  mySerial_PrintConstString(string_0);                   // send debug "Initializing plot with Plot.ly server...\r\n"

  // Startup test

  Serial_PrintConstString(cmd_0);                        // send "AT\r\n"

  if(!find_resp(0,error_0)){while(1){}}                  // find "OK"

  

  // Turn ECHO off

 // Serial_PrintConstString(cmd_1);                        // send "ATE1\r\n"

  //if(!find_resp(0,error_1)){while(1){}}                  // find "OK"

  

  // Put WiFi into Station mode

  Serial_PrintConstString(cmd_2);                        // send "AT+CWMODE=1\r\n"

  if(!find_resp(0,error_2)){                             // find "no change"

    Serial_PrintConstString(cmd_2);                      // send "AT+CWMODE=1\r\n"

    if(!find_resp(0,error_3)){while(1){}}                // find "OK"

  }

  

  // Set data transmission mode to no data (0)

 // Serial_PrintConstString(cmd_3);                        // send "AT+CIPMODE=0\r\n"

 // if(!find_resp(0,error_4)){while(1){}}                  // find "OK"

  

  // Restart ESP8266

  Serial_PrintConstString(cmd_4);                        // send "AT+RST\r\n"

  if(!find_resp(1,error_5)){while(1){}}                  // find "IP"

  

  // Enable multiple connections (MUX)

  Serial_PrintConstString(cmd_5);                        // send "AT+CIPMUX=1\r\n"

  if(!find_resp(0,error_6)){while(1){}}                  // find "OK"

  

  Serial_PrintConstString(cmd_6);                        // send "AT+CWJAP=\"

  Serial.print(ssid);                                    // send ssid

  Serial_PrintConstString(string_1);                     // send "\",\""

  Serial.print(pass);                                    // send password

  Serial_PrintConstString(string_2);                     // send "\""

  Serial.println();

  if(!find_resp(0,error_7)){while(1){}}                  // find "OK"

}

 

 

void ESP8266_Disconnect(unsigned char id){

  Serial_PrintConstString(cmd_7);                        // send "AT+CIPCLOSE=\""

  Serial.print(id);                                      // send id

  Serial_PrintConstString(string_2);                     // send "\""

  Serial.println();

  if(!find_resp(4,error_8)){                             // find "Unlink"

    Serial_PrintConstString(cmd_7);                      // send "AT+CIPCLOSE=\""

    Serial.print(id);                                    // send id

    Serial_PrintConstString(string_2);                   // send "\""

    Serial.println();

    while(1){}

  }

}

 

 

void ESP8266_Connect(char *url){

  int p = (int)url;

  Serial_PrintConstString(cmd_8);                // send "AT+CIPSTART=0,\"TCP\",\""

  url = (char *)p;

  while(*url){                                   // send URL

    Serial.print(*url);

    url++;

  }

  Serial_PrintConstString(string_3);                       // send "\",80"

  Serial.println();

  if(!find_resp(0,error_9)){                               // find "Linked"

    Serial_PrintConstString(cmd_8);                        // send "AT+CIPSTART=0,\"TCP\",\""

    url = (char *)p;

    while(*url){                                           // send URL

      Serial.print(*url);

      url++;

    }

    Serial_PrintConstString(string_3);                      // send "\",80"

    Serial.println();

    if(find_resp(3,error_10)){                              // find "CONNECT"

      ESP8266_Disconnect(0);

      Serial_PrintConstString(cmd_8);                       // send "AT+CIPSTART=0,\"TCP\",\""

      url = (char *)p;

      while(*url){                                          // send URL

        Serial.print(*url);

        url++;

      }

      Serial_PrintConstString(string_3);                    // send "\",80"

      Serial.println();

      if(!find_resp(5,error_11)){while(1){}}                // find "Linked"

    }

  }

}

 

 

 

void plotly_init(){

  unsigned int i = 0;

  char charbuffer;

  

  unsigned int contentLength = 126 + strlen(userName) + strlen(fileopt) + nTraces*(87+strlen(maxpoints)) + (nTraces - 1)*2 + strlen(fileName);

  if(world_readable){

    contentLength += 4;

  }

  else{

    contentLength += 5;

  }

  

  String contentLengthString = String(contentLength);

  const char* contentLengthConstString = contentLengthString.c_str();

  

  unsigned int postLength = contentLength + 94 + strlen(contentLengthConstString);

  

  ESP8266_Connect("plot.ly");

  

  Serial_PrintConstString(cmd_9);                  // send "AT+CIPSEND=0,"

  Serial.println(postLength);

  if(find_resp(6,error_12)){                       // find ">"

    Serial_PrintConstString(string_4);             // send "POST /clientresp HTTP/1.1\r\n"

    Serial_PrintConstString(string_5);             // send "Host: plot.ly:80\r\n"

    Serial_PrintConstString(string_6);             // send "User-Agent: Arduino/0.6.0\r\n"

    Serial_PrintConstString(string_7);             // send "Content-Length: "

    Serial.print(contentLength);

    Serial.println();

    Serial.println();      

    Serial_PrintConstString(string_8);             // send "version=2.3&origin=plot&platform=arduino&un="

    Serial.print(userName);                        // send userName

    Serial_PrintConstString(string_9);             // send "&key="

    Serial.print(APIKey);                          // send APIKey

    Serial_PrintConstString(string_10);            // send "&args=["

    for(int i=0; i<nTraces; i++){

        Serial_PrintConstString(string_11);        // send "{\"y\": [], \"x\": [], \"type\": \"scatter\", \"stream\": {\"token\": \""

        Serial.print(tokens);                   // send token

        Serial_PrintConstString(string_12);        // send "\", \"maxpoints\": "

        Serial.print(maxpoints);                   // send maxpoints

        Serial_PrintConstString(string_13);        // send "}}"

        if(nTraces > 1 && i != nTraces-1){

           Serial_PrintConstString(string_14);     // send ", "

        }

    }

    Serial_PrintConstString(string_15);            // send "]&kwargs={\"fileopt\": \""

    Serial.print(fileopt);                         // send fileopt

    Serial_PrintConstString(string_16);            // send "\", \"filename\": \""

    Serial.print(fileName);                        // send fileName

    Serial_PrintConstString(string_17);            // send "\", \"world_readable\": "

    if(world_readable){

      Serial_PrintConstString(string_18);          // send "true"

    }

    else{

      Serial_PrintConstString(string_19);          // send "false"

    }

    Serial_PrintConstString(string_20);            // send "}"

    Serial.println();

    if(find_resp(7,error_13)){                     // find "~"

      i=0;

      while(1){

        while(!Serial.available()){}

        charbuffer = Serial.read();

        if(charbuffer == 34){break;}

        stream_site = charbuffer;

        i++;

      }

      if(!find_resp(0,error_14)){             // find "OK"

        while(1){}

      }

      mySerial_PrintConstString(string_21);   // send debug "Successfully Initialized.\r\n"

      mySerial_PrintConstString(string_22);   // send debug "Please visit: \"http://plot.ly/~"

      i=0;

      while(stream_site){

        mySerial.print(stream_site);

        i++;

      }

      mySerial_PrintConstString(string_23);    // send debug "\"."

      mySerial.println();

    }

    else{

      while(1){}

    }

  }

  else{

    while(1){}

  }

}

 

void plotly_plot(unsigned long x, int y, char *token){

  String xString = String(x);

  String yString = String(y);

  

  const char* xConstString = xString.c_str();

  const char* yConstString = yString.c_str();

  

  unsigned int jasonLength = 44 + strlen(xConstString) +  strlen(yConstString);

  String jasonLengthString = String(jasonLength, HEX);

  

  const char* ConstJasonLengthString = jasonLengthString.c_str();

  unsigned int postLength = 167 + strlen(ConstJasonLengthString) + jasonLength;

  

  Serial_PrintConstString(cmd_9);           // send "AT+CIPSEND=0,"

  Serial.println(postLength);

  if(find_resp(6,error_15)){                // find ">"

    Serial_PrintConstString(string_24);     // send "POST / HTTP/1.1\r\n"

    Serial_PrintConstString(string_25);     // send "Host: arduino.plot.ly\r\n"

    Serial_PrintConstString(string_26);     // send "User-Agent: Arduino\r\n"

    Serial_PrintConstString(string_27);     // send "Transfer-Encoding: chunked\r\n"

    Serial_PrintConstString(string_28);     // send "Connection: close\r\n"

    Serial_PrintConstString(string_29);     // send "plotly-convertTimestamp: \"Australia/Melbourne\"\r\n\r\n"

  }

  else{

    while(1){}

  }

  Serial.print(jasonLengthString);

  Serial_PrintConstString(string_30);       // send "\r\n{\"x\": "

  Serial.print(x);                          // send x

  Serial_PrintConstString(string_31);       // send ", \"y\": "

  Serial.print(y);                          // send y

  Serial_PrintConstString(string_32);       // send ", \"streamtoken\": \""

  while(*token){                            // send token

    Serial.print(*token);

    token++;

  }

  Serial_PrintConstString(string_33);       // send "\"}\n\r\n"

  if(find_resp(0,error_16)){                // find "OK"

    mySerial.println("Sent data!");

  }

  else{

    mySerial.println("*Couldn't send data!!*");

  }  

}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...