コード例 #1
0
void example_main() {
  ipstack::Interface* interface = setupEthernet();
  if (interface==0) {
		// ethernet setup failed
		printf("No ethernet card found!\n");
		while(1);
  }

  //ipstack::api::TCP_Socket<1514,1,128,2> socket; //alloc on stack (variant 2)
  new(&socket) IP::TCP_Socket<1514,1,128,2>; //explicitly call constructor for global object (only for variant 1)
  //socket.setAlarm(TCPAlarmTask0);
  socket.set_sport(80); //server (telnet port)

	while(1){
		printf("Wait for request: Connect with \"http://%s:%u\"\n", "10.0.3.2", socket.get_sport());
		socket.listen();
		
		int i=1;
		char buffer[512];
		char url[100];
	
		memset(buffer, 0, 512);
		i = socket.receive(buffer, 512);
		char* input = buffer;
		
		/// Parse requested url (example: GET /infotext.html HTTP/1.1) ///
		if (memcmp(input, "GET", 3)==0) {
			input += 4;
			unsigned pos = 0;
			for (;pos<99;++pos) {
				char c = input[pos];
				if (c == ' ') break;
				url[pos] = c;
			}
			url[pos] = 0; // last character is 0 to terminate the string
			input += pos + 1; // jump to the character after the whitespace after the url
// 			printf("Content: %s\n", input);
			if (memcmp(input, "HTTP/1", 6)==0) {
				showPage(url, pos);
			} else {
				printf("Request failed: %s\n", url);
				parseError(buffer);
			}
		} else {
			parseError(buffer);
		}

		while(socket.close() == false);
		printf("Terminated\n");
	}
}
コード例 #2
0
void setup() {

	Serial.begin(9600); 
	setupEthernet(); 

	setSyncInterval(NTP_TIME_SYNC_INTERVAL);
	setSyncProvider(getNtpTime);

	if(timeStatus()== timeNotSet)
	{
		Serial.println("Sync failed during start");
	}
	else
	{
		Serial.println("Sync successfully during start");
	}
}
コード例 #3
0
void Alpha::functionTaskTask0() {
    ipstack::Interface* interface = setupEthernet();

    if (interface==0) {
        // ethernet setup failed
        System::haltsystem();
    }

    // You can ping CiAO IP now by using
    // * ping 10.0.3.2 (for icmp ping)
    // * ping6 fe80::6655:44ff:fe33:2211%tap0 (for icmp ping with ipv6 on tap0 device)
    // * echoping -u 10.0.3.2 (for udp ping; use ipv4 addresses, ipv6 is not supported by echoping for udp)
    // * sendip -p ipv6 -p udp -us 5070 -ud 8 -d "Hello" -v fe80::6655:44ff:fe33:2211 (for ipv6 udp ping)
    // * telnet 10.0.3.2 (for testing the tcp reset capability; use ipv4 or ipv6 address)
    // * Send udp to port 88 to get it printed out

    // Setup test ipv6 address: we will ping that address (and test the ndp address resolution)
    ipstack::ipv6addr testipv6;
    if (!parse_ipv6_addr("fe80:0:0:0:a81a:54ff:fef0:3f", testipv6)) {
        System::haltsystem();
    }
    ICMPv6_Socket &icmpv6instance = ICMPv6_Socket::instance();
    icmpv6instance.ipv6.set_dst_addr(testipv6); // src address is determined automatically

    // Setup test ipv4 address: we will ping that address (and test the arp address resolution)
    UInt32 testipv4 = IPv4_Packet::convert_ipv4_addr(10,0,3,1);

    ICMPv4_Socket &icmpv4instance = ICMPv4_Socket::instance();
    icmpv4instance.ipv4.set_dst_addr(testipv4); // src address has been set above

    SendBuffer* sbi;
    const char teststring[] = "HELLO";


    IP::UDP_Socket<1514,1,128,2> socket;
    socket.set_sport(88);
    socket.bind(); // listen

    printf("Send IPv4 ping\n");
    sbi = icmpv4instance.requestSendBuffer(ICMP_Packet::ICMP_HEADER_SIZE+sizeof(teststring));
    if (sbi) {
        sbi->mark("IPv4 ping");
        ICMP_Packet* icmp = (ICMP_Packet*)sbi->getDataPointer();

        icmp->set_type(ICMP_Packet::ICMP_TYPE_ECHO_REQUEST);
        icmp->set_code(ICMP_Packet::ICMP_CODE_ECHO);

        sbi->writtenToDataPointer(ICMP_Packet::ICMP_HEADER_SIZE);
        sbi->write(teststring, sizeof(teststring));

        icmpv4instance.send(sbi);
    }

    printf("Send IPv6 ping\n");
    sbi = icmpv6instance.requestSendBuffer(ICMPv6_Packet::ICMP_HEADER_SIZE+sizeof(teststring)+sizeof(UInt32));
    if (sbi) {
        sbi->mark("IPv6 ping");
        ICMPv6_Packet* icmp = (ICMPv6_Packet*)sbi->getDataPointer();

        icmp->set_type(ICMPv6_Packet::ICMP_TYPE_ECHO_REQUEST);
        icmp->set_code(ICMPv6_Packet::ICMP_CODE_ECHO_REQUEST);

        sbi->writtenToDataPointer(ICMPv6_Packet::ICMP_HEADER_SIZE);
        // write seq and id
        UInt32 idAndSeq = 0xcaffee;
        sbi->write(&idAndSeq, sizeof(idAndSeq));
        // write payload
        sbi->write(teststring, sizeof(teststring));

        icmpv6instance.send(sbi);
    }

    printf("Start udp listen on port %u\n", socket.get_sport());
    while(1) {
        ReceiveBuffer* rbuffer = socket.receiveBlock();

        ReceiveBufferUDPIPv6* ipv6udp = ReceiveBufferUDPIPv6::cast(rbuffer);
        ReceiveBufferUDPIPv4* ipv4udp = ReceiveBufferUDPIPv4::cast(rbuffer);
        if (ipv6udp) {
            printf("Received IPv6/UDP. Bytes: %u\n", rbuffer->getSize());
            char ipstrbuffer[50] = {0};
            ipstack::ipv6_addr_toString(ipv6udp->getRemoteInfo()->ipv6, ipstrbuffer);
            printf("  Remote IPv6-Address: %s, Port: %u\n", ipstrbuffer, ipv4udp->getRemoteInfo()->remoteport);
        } else if (ipv4udp) {
            printf("Received IPv4/UDP. Bytes: %u\n", rbuffer->getSize());
            UInt8 a,b,c,d;
            IPv4_Packet::convert_ipv4_addr(ipv4udp->getRemoteInfo()->ipv4, a, b, c, d);
            printf("  Remote IPv4-Address: %u.%u.%u.%u, Port: %u\n", a,b,c,d, ipv4udp->getRemoteInfo()->remoteport);
        } else {
            printf("error, udp ip version unknown. Bytes: %u\n", rbuffer->getSize());
        }
        ReceiveBuffer::free(rbuffer);
    }
}
コード例 #4
0
void pachube_in_out(){

	if (minute() < last_connect) last_connect = minute();

	if (request_pause){
		if ((minute() - last_connect) > interval){
			ready_to_update = true;
			reading_pachube = false;
			request_pause = false;
			found_status_200 = false;
			found_session_id = false;
			found_CSV = false;

			Serial.print("Ready to connect: ");
			Serial.println(minute());
			Serial.print("interval: ");
			Serial.println(interval);
		}
	}

	if (ready_to_update){
		Serial.println("Connecting...");
		if (localClient.connect(remoteServer, 80) > 0) {

			// here we assign comma-separated values to 'data', which will update Pachube datastreams
			// we use all the analog-in values, but could of course use anything else millis(), digital
			// inputs, etc. . i also like to keep track of successful and failed connection
			// attempts, sometimes useful for determining whether there are major problems.

			sprintf(pachube_data,"%d,%d,%d-%d-%d %d:%d:%d",analogRead(0),analogRead(1), year(),month(),day(),hour(),minute(),second());
			content_length = strlen(pachube_data);

			Serial.print("GET request to retrieve: ");
			Serial.println(pachube_data);

			localClient.print("GET /api/");
			localClient.print(REMOTE_FEED_ID);
			localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
			localClient.print(PACHUBE_API_KEY);
			localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
			localClient.println("\n");

			//Serial.println("finished GET now PUT, to update");

			localClient.print("PUT /api/");
			localClient.print(SHARE_FEED_ID);
			localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
			localClient.print(PACHUBE_API_KEY);

			localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
			localClient.print("\nContent-Type: text/csv\nContent-Length: ");
			localClient.print(content_length);
			localClient.print("\nConnection: close\n\n");
			localClient.print(pachube_data);

			localClient.print("\n");

			ready_to_update = false;
			reading_pachube = true;
			request_pause = false;
			interval = UPDATE_INTERVAL;

			// Serial.print("finished PUT: ");
			// Serial.println(millis());

		} 
		else {
			Serial.print("connection failed!");
			Serial.print(++failures);
			found_status_200 = false;
			found_session_id = false;
			found_CSV = false;
			ready_to_update = false;
			reading_pachube = false;
			request_pause = true;
			last_connect = minute();
			interval = RESET_INTERVAL;
			setupEthernet();
		}
	}

	while (reading_pachube){
		while (localClient.available()) {
			checkForResponse();
		} 

		if (!localClient.connected()) {
			disconnect_pachube();
		}
	} 
}
コード例 #5
0
ファイル: main.cpp プロジェクト: Jbossalini/m2x-mbed
int main()
{
    setbuf(stdout, NULL); // no buffering for this filehandle
    char json[1024];

    int content_length;
    int response_code;
    char buf[1024];

    float amb_temp;

    setupEthernet();

    // set time
    lcd.printf("Reading Time... ");
    ntp.setTime("time-c.nist.gov");
    lcd.printf("Time set");


    while(1) {  // Loop
        char streamName[] = "amb-temp";  // Set value for stream you are using
     
        // GET Stream value
        response_code = getStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("GET Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

/////
        // PUT value to Strem
        amb_temp = tmp.read();

        sprintf(json, "{\"value\":\"%0.2f\"}", amb_temp);

        response_code = putStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("PUT Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);


//////
        // POST value(s) to Stream
        time_t seconds;

        seconds = time(NULL);   // get current time from mbed RTC
        strftime(buf,40, "%Y-%m-%dT%H:%M:%S%z", localtime(&seconds));

        amb_temp = tmp.read();

        sprintf(json, "{ \"values\": [ {\"at\": \"%s\", \"value\":\"%0.2f\"} ] }", buf, amb_temp);

        response_code = postStream(streamName, json);
        content_length = readContentLength(json);

        pc.printf("POST Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // PUT Location to Feed
        sprintf(json, "{ \"name\": \"%s\", \"latitude\": \"%0.8f\", \"longitude\": \"%0.8f\", \"elevation\": \"%0.2f\" }", LOC_NAME, LOC_LAT, LOC_LONG, LOC_ELEV);

        response_code = putLocation(json);
        content_length = readContentLength(json);

        pc.printf("PUT Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // GET Location of Feed
        response_code = getLocation(json);
        content_length = readContentLength(json);

        pc.printf("GET Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

    }

    eth.disconnect();

    while(1) {}
}