Trying to open a url to post parameters through asp.net application to sql

In short I’m trying to open a url with the spark core. I had this working the other night. I have an Arduino with this same code working as we speak. With the spark core, I want to open this url:
192.168.1.9:80/energy/LogArduinoData.aspx?Location=999&Voltage=23&Ampere=70
I’m posting code running on the Arduino first because it works and it might be easier to see why the Spark code doesn’t. Anything pop out at anyone why the spark code isn’t opening the url? Thanks!

Here’s the Arduino code that currently works:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0E, 0x03, 0xE2};
IPAddress ip(192,168,1,251);
byte server[] = {
  192, 168,   1,  9}; 
// char server[] = "localhost";
IPAddress myDns(8,8,8,8);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
EthernetClient client;

unsigned long lastUpdate = millis();

void setup(){
  Serial.begin(9600); 
  Ethernet.begin(mac, ip, dns, gateway, subnet); 
  delay(2000);
}

void loop(){ 

  double temp0 = analogRead(A0);
  double temp1 = analogRead(A1);
  double arduino;

  if (millis() - lastUpdate > 5000){ //reset after 60 seconds
    lastUpdate = millis();
    Serial.print("OK");

    UploadData(arduino, analogRead(A1), temp0);
  }
}

void UploadData(double location, int voltage, int ampere){
  client.connect(server, 80);

  if (client.connected()) {
    Serial.println("Client Connected to server -- writing"); 
    client.print("GET /energy/LogArduinoData.aspx?Location=");
    Serial.print("GET /energy/LogArduinoData.aspx?Location=");

    client.print(location);
    Serial.print(location);

    client.print("&Voltage=");
    client.print(voltage);
    Serial.print("&voltage");

    client.print("&Ampere=");
    client.print(ampere);
    Serial.print("&ampere");

    client.print(" HTTP/1.1\n"); 
    Serial.print(" HTTP/1.1\n"); 

    client.print("Host:192.168.1.5 \n"); 
    Serial.print("Host:192.168.1.5 \n"); 

    client.print("Connection: close\n");  
    Serial.print("Connection: close\n");     

    client.print("Content-Type: application/x-www-form-urlencoded\n");
    Serial.print("Content-Type: application/x-www-form-urlencoded\n");

    client.print("Content-Length: 0 \n\n" );
    Serial.print("Content-Length: 0 \n\n" );

    Serial.println("Wrote everything"); 

    delay(1000);

    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.stop();
  client.flush();
}

The spark code I currently have that doesn’t seem to work is:

TCPClient client;

char server[] = {192, 168,   1,  9}; 

unsigned long lastUpdate = millis();

void setup(){
  Serial.begin(9600); 
  delay(2000);
}

void loop(){ 

  double temp0 = analogRead(A0);
  double temp1 = analogRead(A1);
  double arduino;
  
  if (millis() - lastUpdate > 5000){ //reset after 60 seconds
    lastUpdate = millis();
    
    Serial.println("OK");
    UploadData(999, analogRead(A1), temp0);
    Serial.print("Trying to upload");
  }
}

void UploadData(double location, int voltage, int ampere){
  client.connect(server, 80);

  if (client.connected()) {
   Serial.println("Client Connected to server -- writing"); 
    client.print("GET /energy/LogArduinoData.aspx?Location=");
        Serial.print("GET /energy/LogArduinoData.aspx?Location=");

    client.print(location);
        Serial.print(location);

    client.print("&Voltage=");
    client.print(voltage);
        Serial.print("&voltage");

    client.print("&Ampere=");
    client.print(ampere);
        Serial.print("&ampere");

    client.print(" HTTP/1.1\n"); 
        Serial.print(" HTTP/1.1\n"); 

    client.print("Host:192.168.1.9 \n"); 
        Serial.print("Host:192.168.1.9 \n"); 

    client.print("Connection: close\n");  
     Serial.print("Connection: close\n");     
   
    client.print("Content-Type: application/x-www-form-urlencoded\n");
        Serial.print("Content-Type: application/x-www-form-urlencoded\n");

    client.print("Content-Length: 0 \n\n" );
        Serial.print("Content-Length: 0 \n\n" );

    Serial.println("Wrote everything"); 

    delay(1000);

    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.stop();
  client.flush();
}

Use this to format:

Code here

Thank you kennethlimcp.

Just glancing at the code, I don’t think I see anything wrong with it.

How far does your code get before you don’t see the intended results? Do you see all of the Serial.print() stuff printed out? Do you see any logs on your web server for the request?

Maybe you can combine the entire GET line into a single String or char array variable and post it all at once with client.println(). Instead of using client.print(" ... \n"), can you try it with client.println(" ... ")?

Serial port just outputs:
OK
Trying to upload

That’s it.

I tried your suggestion, still nothing.

int lastUpdate;
//byte server[] = { 192, 168, 1, 9 };
IPAddress server(192, 168, 1, 9);
void setup()
{
  Serial.begin(9600);
}

void loop()
{
if(millis() - lastUpdate > 5000){
    lastUpdate = millis();

 Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /energy/logArduinoData.aspx?Location=123&Voltage=24&Ampere=25 HTTP/1.1 Host:192.168.1.9");
    client.println("Host: 192.168.1.9");
    client.println("Content-Length: 0");
    client.println();
    
//client.print("GET /energy/LogArduinoData.aspx?Location=");
//client.print(location);
//client.print("&Voltage=");
//client.print(voltage);
//client.print("&Ampere=");
//client.print(ampere);
//client.print(" HTTP/1.1\n"); 
//client.print("Host:192.168.1.9 \n"); 
//client.print("Connection: close\n");  
//client.print("Content-Type: application/x-www-form-urlencoded\n");
//client.print("Content-Length: 0 \n\n" );

  }
  else
  {
    Serial.println("connection failed");
  }
  
  
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
  client.print("Connection: close\n");  
}
}

I can see it on my home router as a client with 192.168.1.109 ip. I’d like to force a dns server. I’ve done some research but still haven’t found anything. I’ve tried this tool by mtnscott and I can see dns changing a couple times but I don’t know how to force a dns.

Thanks.

Hi @jaysettle

The @mtnscott DNS tool is for cores that cannot get DNS from their router via the internal to the TI CC3000 DNS process. I don’t think this tool will help you. Does your local server at 192.168.1.9 have a DNS name you could use?

I know you are trying to simplify your code for testing but the println for GET looks funny. After the “HTTP/1.1” should come a new line, so that the Host: line is separated.

Do the logs on the server tell you anything connected?

I don’t know if it’s required, but I usually put the “Host:” line before everything else.

1 Like

The @mtnscott DNS tool is for cores that cannot get DNS from their router via the internal to the TI CC3000 DNS process. I don’t think this tool will help you. Does your local server at 192.168.1.9 have a DNS name you could use?

Right I wasn’t sure how to Serial print my DNS because I’ve been having minor home network issues lately and sometimes forcing something exposes symptoms for issues.

How can I allow the spark core a DNS name to use?

I know you are trying to simplify your code for testing but the println for GET looks funny. After the “HTTP/1.1” should come a new line, so that the Host: line is separated.

I tried the following Spark TCPClient example from documentation and it connects:

// SYNTAX
TCPClient client;
// EXAMPLE USAGE

TCPClient client;
byte server[] = { 74, 125, 224, 72 }; // Google
void setup()
{
  // Make sure your Serial Terminal app is closed before powering your Core
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) SPARK_WLAN_Loop();

  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /search?q=unicorn HTTP/1.0");
    client.println("Host: www.google.com");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

But as soon as I stub in my get statement(below), I never get a “Connected” status from serial.

byte server[] = { 192, 168, 1, 9 }; // Google
void setup()
{
  // Make sure your Serial Terminal app is closed before powering your Core
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) SPARK_WLAN_Loop();

  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /energy/LogArduinoData.aspx?Location=999.00&voltage120&ampere=13&A=176.00 HTTP/1.1");
    client.println("Host: 192.168.1.9");
    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

This could be server connection related but again the Arduino is still posting every 5 seconds. I don’t have much experience with monitoring IIS or Active Server Page stuff so I’m researching this now. I don’t know where SQL is reporting failed connection attempts either, so I don’t have a good feedback system for me to debug with. Any direction there would be appreciated but that might not be in Spark community’s scope here.

I noticed that I had my server logged into my network over wifi through a public profile. I have changed that connection to private.

After uploading this code, it worked but…

TCPClient client;

byte server[] = {
  192, 168,   1,  3}; 

unsigned long lastUpdate = millis();

void setup(){
  Serial.begin(9600); 


  delay(2000);
}

void loop(){ 
  i++;
  if(i < 2){
    WiFi.setCredentials("ddjag", "Arvin311");
    Serial.println("Wifi Credentials ran");
  }
  
  if (millis() - lastUpdate > 5000){ //reset after 60 seconds
    lastUpdate = millis();

    Serial.println("OK");
    UploadData(999, 888, 777);
    Serial.print("Trying to upload");
  }
}


void UploadData(double location, int voltage, int ampere){
  client.connect(server, 80);
  Serial.print("Attempting GET function");

  if (client.connected()) {
    Serial.println("Client Connected to server -- writing"); 
    client.print("GET /energy/LogArduinoData.aspx?Location=");
    Serial.print("GET /energy/LogArduinoData.aspx?Location=");

    client.print(location);
    Serial.print(location);

    client.print("&Voltage=");
    client.print(voltage);
    Serial.print("&voltage");

    client.print("&Ampere=");
    client.print(ampere);
    Serial.print("&ampere");

    client.print(" HTTP/1.1\n"); 
    Serial.print(" HTTP/1.1\n"); 

    client.print("Host:192.168.1.3 \n"); 
    Serial.print("Host:192.168.1.3 \n"); 

    client.print("Connection: close\n");  
    Serial.print("Connection: close\n");     

    client.print("Content-Type: application/x-www-form-urlencoded\n");
    Serial.print("Content-Type: application/x-www-form-urlencoded\n");

    client.print("Content-Length: 0 \n\n" );
    Serial.print("Content-Length: 0 \n\n" );

    Serial.println("Wrote everything"); 

    delay(1000);

    if (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  }
  client.stop();
  client.flush();
}

According to the database timestamp based on data entry, it only communicated from 2014-11-23 10:42:33.540 to 2014-11-23 11:03:08.230 which was roughly 21 minutes and now is blinking blue which means it’s lost wifi credentials. Why has this happened. Is there another way to post with the spark core that I can try?

Thanks!

Well this is weird, I had forgotten that I put this in the code above,

  i++;
  if(i < 2){
    WiFi.setCredentials("ddjag", "Arvin311");
    Serial.println("Wifi Credentials ran");
  }

This is interesting because I have since changed my network SSID to “jag” as you can see in the circled picture above. So my mistake on leaving the wrong credentials in code but how the heck after all the trouble I’ve had, did it work for 20 minutes with the wrong wifi credentials in the first place?

Also like to point out that some chips on the back of my spark core are reading temperatures of 161F.

I’ve noticed that when the spark does finally transmit my “get” statement, it only last for a short period of time. To try to trend the spark’s patterns I created this excel sheet - the green highlights are the sparks first transmits, and the red highlights are when it stopped transmitting.

Each time the sparks stops, I have to factory default the device and reflash my code.