Connecting to a web Page

Hi everyone,
I am by no means a programmer – but have fooled around with Arduino so that I can better understand work I have commissioned for microcontroller projects. Recently purchased the Spark and am trying to get up to speed. Please note I have never hooked up a microcontroller to interface with the internet – so forgive any obvious amateurness.

My requirement is basic. I’d like the Spark to call a webpage I have setup that will send an SMS via twilio when the page is opened – I’d like to trigger the call when a digital pin goes HIGH (pin 5). Trawling the net and this forum, the closest I came was some code modified by a user called bstolte shown here - https://community.spark.io/t/how-to-open-a-url-with-spark-core/2800/7 - and corrected by kennethlimcp.

My code is as follows – I simply does not connect. I checked the TCPClient example - but I get nothing at all out of it on a serial monitor, and I can’t use an IP address - I need a written url.

Can anyone point me in the right direction?

My (greatly hacked and borrowed) code is as follows:

#include <string.h>
char server[] = "usblogbook.com";
TCPClient client;
//const int ledPin = 7;
const int butPin = 5;
int buttonState = 0; 

void setup() {

Serial.begin(9600);
delay(1000);
client.connect(server, 80);
pinMode (ledPin, OUTPUT);
pinMode (butPin, INPUT);
delay(1000);
    Serial.println("connecting...");


if (client.connect(server, 80))
    {
        Serial.println("connected");
        client.println();
    }
else
    { 
        Serial.println("connection failed");
        client.stop();
    }
}

void loop() {
    buttonState = digitalRead(butPin);
 if (buttonState == HIGH){//digital pin 5 high
client.println("GET/GetTwilio/index.php");
client.print("Host: ");
client.println(server);
client.println("Accept: text/html, text/plain");
client.println();
client.flush();
//delay(3000);
}

Hi @DRCO

There are a couple of HTTP libraries in the library portion of the webIDE that will make you job a lot easier if you are just looking to get the job done.

A few things jumped out at me from your code:

  • If you are on Windows, you should look up the trick here in the forum for starting the serial USB port. Generally you have to wait in setup for either a few seconds or loop waiting for a keypress from the host. Its a Windows thing.
  • You should probably use the defined pin variables like D0 and D7 since the values are not always the same as on Arduino.
  • You need a space after GET and before the path of the URL
  • You do work a bit harder to flush the return data from the webserver–otherwise it will build up and crash your program
  • You would probably do better to open the connection in loop() when the button is pressed and not just in setup(). If the server closes the connection for any reason, your code can’t recover.
  • You are not debouncing the switch input. Switches have mechanican bounce causes the contacts to make and break contact rapidly for a period of time (usually less than 5ms). This will cause your code to send multiple SMS messages for each button press.

Hope this helps!

1 Like

Thanks BKO, will try your suggestions soonest - really appreciate the prompt response.

1 Like

Hi BKO,
OK - have now tried pretty much all the options and examples I could find - and never get a connection. In the last attempt I used your published code shown here https://community.spark.io/t/how-to-open-a-url-with-spark-core/2800/41 - without modification and just got continuous “Not Connected” “Return 0” messages.
Just thought I’d let you know - and that I appreciate your help. As I can’t seem to get any connection, it must be some sort of server issue, I guess.

Maybe you should try to do a GET request from Google for a search–that is kind of the standard easy test.

You can have a look at the code for my smart doorbell, https://community.spark.io/t/smart-doorbell-sends-email/4665
it has a de-bounce on the doorbell button, and connects to pushingbox to send an email, it also sends a GET request script to my XBMC server to bring up a video on the TV. the code for each tcp client is slightly different as it took a few attempts to get things working reliably.

Hi Hootie - and BKO,
Thanks again for your suggestions. In my initial code, I had wired up pin 5 to 3.3 v - effectively setting it high. So I don’t think there’s a problem with transitioning a switch. Regarding the GET command, I really don’t think I’m getting that far in the program - but as per BKO’s suggestion I tried using Google “www.google.com” and “store?hl=en” (the Google PLAY Store) in his code (and mine) as a really common url. Same result - no connection.

I will study Hootie81s code more closely, and play around with it but I think there is a fundamental problem - something I’m just not doing right. To be clear on the setup, I’m using a windows PC and chrome browser to access the Spark Build IDE - the PC is wired directly to the internet router - the Spark Core is on my WiFi (same local network). The Spark seems to have no problem loading code - I’ve tried a few “blinky” and simple program uploads - no problem. Could it be something in my router setup?

Does the tinker app work? can you control the LED with the app?

also in tinker do you see the pin go high when you connect the 3.3v to D5

Give me an hour or so and ill put together a test app that will test a few different things at once

Try this, i cant actually get it to work with my slow connection at the moment but that’s nothing unusual. @bko could you give it a quick try too, I’ve been trying it for 3 hrs now and still nothing will connect for me… its a couple of your test/example codes combined :slight_smile:

char hostname[] = "www.google.com"; 
char url[] = "search?q=unicorn";

TCPClient client;

void setup() {
    Serial.begin(9600);
    while (!Serial.available()) SPARK_WLAN_Loop();
    
    IPAddress dnshost(ip_config.aucDNSServer[3], ip_config.aucDNSServer[2], 
                  ip_config.aucDNSServer[1], ip_config.aucDNSServer[0]);


    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    Serial.print("Core IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway: ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("Mask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("DNS host: ");
    Serial.println(dnshost);        // DNS host
    Serial.print("WiFi RSSI: ");
    Serial.println(WiFi.RSSI());
    
}

void loop() {
    
    Serial.println("Starting request");
    uint32_t ip_addr = 0;
    unsigned long tic = millis();
    Serial.print("Looking up IP for: ");
    Serial.println(hostname);
    int16_t retval = gethostbyname((char*)hostname, strlen(hostname), &ip_addr);
    unsigned long toc = millis();
    IPAddress resolvedIP(BYTE_N(ip_addr, 3), BYTE_N(ip_addr, 2), BYTE_N(ip_addr, 1), BYTE_N(ip_addr, 0));
    Serial.print("The IP is: ");
    Serial.print(resolvedIP);
    Serial.print(" Return Code: ");
    Serial.println(retval);
    Serial.print("Time taken: ");
    Serial.println(toc-tic);
    Serial.print("Connecting to server.");
    tic = millis();
    
    if (client.connect(hostname, 80)) {
        Serial.print(" Connected OK");
        client.print("GET ");
        client.print(url);
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(hostname);
        client.println("Connection: close");
        client.println();
        // client.println(); //sometimes another print line is required on some servers.
        Serial.println(" Sent GET request, Awaiting resopnse");

        unsigned int count = 0;
        unsigned long lastTime = millis();
        while( client.available()==0 && millis()-lastTime<10000) { //ten second timeout
          }  //do nothing
        lastTime = millis();
        while( client.available() && millis()-lastTime<10000 ) {  //ten seconds
          Serial.print(client.read());  //flush data
          count++;
        }
        client.flush();  //for safety

        //client.flush();
        delay(400);
        client.stop();
        Serial.println();
        Serial.print("Done, Total bytes: ");
        Serial.println(count);
    
     }
    else {
        client.flush();
        client.stop();
        Serial.println("Could not connect");
    }

    delay(10000);
}
1 Like

Hi Hootie 81,
I had to reconnect Tinker (I used it once when I first got the Spark Core) - but it works fine. I hooked up a LED to D0 and could control it.

Hi,
Really want to thank you for putting in the time. I ran your code and got the following output - again… no connection.
SSID: BronteHome
Core IP: 192.168.2.112
Gateway: 192.168.2.1
Mask: 255.255.255.0
DNS host: 76.83.0.0
WiFi RSSI: -44
Starting request
Looking up IP for: www.google.com
The IP is: 0.0.0.0 Return Code: -85
Time taken: 904
Connecting to server.Could not connect
Starting request
Looking up IP for: www.google.com
The IP is: 0.0.0.0 Return Code: -85
Time taken: 904
Connecting to server.Could not connect
Starting request
Looking up IP for: www.google.com
The IP is: 0.0.0.0 Return Code: -85
Time taken: 905
Connecting to server.Could not connect
Starting request
Looking up IP for: www.google.com
The IP is: 0.0.0.0 Return Code: -85
Time taken: 905
Connecting to server.Could not connect
Starting request
Looking up IP for: www.google.com
The IP is: 0.0.0.0 Return Code: -85
Time taken: 905
Connecting to server.Could not connectc

You have DNS host problem with your TI CC3000. Is your internet connection "slow"? Some folks have had better luck using a separate DNS library. Search the forum for details.

1 Like

My internet is pretty good for Australia - I’m getting about 14MB Down - about 500Mb Up on the LAN.I am using a service to access US websites(UnblockUS http://www.unblock-us.com/) that is a sort of VPN, which re-routes the DNS, but I turned that off and defaulted to an ISP (automatic) DNS allocation Same result - no connection. I’ll check the forum as suggested but getting a little out of my depth here. I have 4G phone service here and can allocate a WIFI Hotspot. Is that worth a go?
Thanks again…
Chris

Just an update - I tried using my mobile phone hotspot and got the same result, with the same DNS host: 76.83.0.0.

Another user has reported doing the cc3000 patch twice fixed the issue for him. another tried the same think and it went back to the 76.83.0.0 shortly after. but may be worth trying as it only takes a few minutes with the cli.

Otherwise as bko said i ported a DNS library a while back as a workaround to the issue.

1 Like

Thanks Hootie81and BKO, you guys have been patient and helpful but it’ll take a while for me to get my head around all this. I can’t in good conscience ask you to do any more - you’ve been great.
Thanks

1 Like

Hi Hootie81 and BKO - SUCCESS! (at least with Hootie’s code). I installed the CLI, completed the “spark flash --usb deep_update_2014_06” update and everything worked as advertised. Can’t thank you enough.
Best wishes,
Chris

2 Likes