Mbed forum

Get mbed to open a website/url?


(Aleksander Instebø) #1

Hi

I having a little school prosject where i want to put 2 values in a sql database.
I have a PHP file on the server that store mye values/data to the mySQL database
The php file gets my values from the url. (ex. http://server.com/?temp=12&light=1)

This works fine when i put the url in the web browser on any computer or mobile phone
But when i try to do this on Mbed it wont store any values at all.
Its like the mbed dont get to open the url address (?)

I have used thingspeak site earlier and that worked fine, but in this prosject we need to have our own running server.

Could anyone help ?

Mbed code: (something is from earlier tests and could be removed)

////////////////////////////////

#if !FEATURE_LWIP
#error [NOT_SUPPORTED] LWIP not supported for this target
#endif

#include “C12832_lcd.h” // Include for LCD code
#include “mbed.h”
#include “EthernetInterface.h”

Serial pc(USBTX, USBRX);

#define DATA1 11111111
#define DATA2 22222222
#define DATA3 33333333
#define DATA4 44444444
#define DATA5 55555555

C12832_LCD lcd; //Initialize LCD Screen
void send(char *arr, int length);
void data(float value1, float value2, float value3, float value4, float value5);

EthernetInterface eth;
TCPSocket clt_sock;

#define HTTP_STATUS_LINE “HTTP/1.0 200 OK”
#define HTTP_HEADER_FIELDS “Content-Type: text/html; charset=utf-8”
#define HTTP_MESSAGE_BODY “”
“” “\r\n”
" <body style=“display:flex;text-align:center”>" “\r\n”
" <div style=“margin:auto”>" “\r\n”
"

VELKOMMEN

" “\r\n”
"

Hallo du .

" “\r\n”
" " “\r\n”
" " “\r\n”
“”

#define HTTP_RESPONSE HTTP_STATUS_LINE “\r\n”
HTTP_HEADER_FIELDS “\r\n”
“\r\n”
HTTP_MESSAGE_BODY “\r\n”

int main()
{
lcd.printf(“Starting …”);
wait_ms(300);
lcd.cls();
lcd.locate(0,1);

pc.printf("##################################\n");
pc.printf("Waiting for IP address ...\n");


//EthernetInterface eth;
eth.connect();

//const char *ip = eth.get_ip_address();
//pc.printf("Ip address: %s", ip ? ip : "no ip");
pc.printf("Mbed IP address is '%s'\n", eth.get_ip_address());
lcd.cls();
lcd.locate(0,1);
lcd.printf("Mbed IP address is\n '%s'\n", eth.get_ip_address());
pc.printf("##################################\n\n");
 //TCPSocket clt_sock;

TCPServer srv;
SocketAddress clt_addr;

/* Open the server on ethernet stack */
srv.open(&eth);

/* Bind the HTTP port (TCP 80) to the server */
srv.bind(eth.get_ip_address(), 80);

/* Can handle 5 simultaneous connections */
srv.listen(5);

while (true) {
    
    data(DATA1, DATA2, DATA3, DATA4, DATA5);
    pc.printf("DATA SENT !\n\n");
    wait(1000); 
}

}

void send(char *arr, int length) {
clt_sock.open(&eth);

//clt_sock.connect("api.thingspeak.com", 80); //OLD WHEN USING THINGSPEAK

clt_sock.connect("server.com", 80);
clt_sock.send(arr, length);
pc.printf("\nSent: %s\n\n", arr);
clt_sock.close();

}

void data(float value1, float value2, float value3, float value4, float value5) {
char data_array[200];

//sprintf(data_array, "GET https://api.thingspeak.com/update?api_key=XXX&field1=%f&field2=%f&field3=%.2f&field4=%.2f&field5=%.2f\r\n", value1, value2, value3, value4, value5); //OLD WHEN USING THINGSPEAK. WAS WORKING FINE.

sprintf(data_array, "GET http://server.com/iot/?temp=11&lys=3");  //FOR TESTING

send(data_array, 200);   
wait(1000);

}

/////////////////////////

INDEX.PHP ON SERVER (http://server.com/iot/index.php), that gets values from url (?temp=333&light=222)

///////////

<?php mysql_connect("instebo.no", "USERNAME", "PASSWORD") or die(mysql_error()); mysql_query ('SET NAMES `utf8`'); mysql_select_db("IOT_DB") or die(mysql_error()); $temp = ($_GET['temp']); $lys = ($_GET['lighy']); if (isset($_GET['temp']) && (isset($_GET['light'])) && (!empty($_GET['temp'])) && (!empty($_GET['light']))) { $query = "INSERT INTO test (temp, lys) VALUES ($temp, $lys)"; $query = (mysql_query($query) OR die(mysql_error())); $lastID = mysql_insert_id(); echo "TEMP: ".$temp.", LIGHT: ".$lys." er lagret!"; } else { echo "Missing temp og light values."; } echo "<--- GO BACK
"; ?>

////////

THANKS !