-
-
Save rioleo/73567597962a59507d0f to your computer and use it in GitHub Desktop.
Arduino to Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (C) 2010 Georg Kaindl | |
// http://gkaindl.com | |
// | |
// This file is part of Arduino EthernetDHCP. | |
// | |
// EthernetDHCP is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU Lesser General Public License as | |
// published by the Free Software Foundation, either version 3 of | |
// the License, or (at your option) any later version. | |
// | |
// EthernetDHCP is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU Lesser General Public License for more details. | |
// | |
// You should have received a copy of the GNU Lesser General Public | |
// License along with EthernetDHCP. If not, see | |
// <http://www.gnu.org/licenses/>. | |
// | |
// Illustrates how to use EthernetDHCP in synchronous (blocking) | |
// mode. | |
#include <SPI.h> | |
#include <SoftwareSerial.h> | |
#define rxPin 2 | |
#define txPin 3 | |
#include <Ethernet.h> | |
#include <EthernetDHCP.h> | |
boolean startRead = false; | |
int stringPos = 0; | |
char inString[32]; | |
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x26, 0xEE }; | |
byte server[] = { 171,64,13,26 }; // www.vg.no | |
SoftwareSerial mySerial(rxPin, txPin); | |
Client client(server, 80); | |
const char* ip_to_str(const uint8_t*); | |
void setup() | |
{ | |
Serial.begin(9600); | |
mySerial.begin(9600); | |
pinMode(rxPin, INPUT); | |
pinMode(txPin, OUTPUT); | |
Serial.println("Attempting to obtain a DHCP lease..."); | |
// Initiate a DHCP session. The argument is the MAC (hardware) address that | |
// you want your Ethernet shield to use. This call will block until a DHCP | |
// lease has been obtained. The request will be periodically resent until | |
// a lease is granted, but if there is no DHCP server on the network or if | |
// the server fails to respond, this call will block forever. | |
// Thus, you can alternatively use polling mode to check whether a DHCP | |
// lease has been obtained, so that you can react if the server does not | |
// respond (see the PollingDHCP example). | |
EthernetDHCP.begin(mac); | |
// Since we're here, it means that we now have a DHCP lease, so we print | |
// out some information. | |
const byte* ipAddr = EthernetDHCP.ipAddress(); | |
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress(); | |
const byte* dnsAddr = EthernetDHCP.dnsIpAddress(); | |
Serial.println("A DHCP lease has been obtained."); | |
Serial.print("My IP address is "); | |
Serial.println(ip_to_str(ipAddr)); | |
Serial.print("Gateway IP address is "); | |
Serial.println(ip_to_str(gatewayAddr)); | |
Serial.print("DNS IP address is "); | |
Serial.println(ip_to_str(dnsAddr)); | |
Serial.println(); | |
Serial.println("connecting..."); | |
delay(1000); | |
if (client.connect()) { | |
Serial.println("connected"); | |
client.println("GET /~rakasaka/cgi-bin/museum.php?q=ardui HTTP/1.0"); | |
client.println(); | |
readPage(); | |
} else { | |
Serial.println("connection failed"); | |
} | |
} | |
void loop() | |
{ | |
// if (client.available()) { | |
// char c = client.read(); | |
// Serial.print(c); | |
// } | |
String ID = ""; | |
for (int i=0;i<=13;i++){ | |
ID += mySerial.read(); | |
} | |
Serial.println(ID); | |
delay(1000); | |
// | |
// if (!client.connected()) { | |
// Serial.println(); | |
// Serial.println("disconnecting."); | |
// client.stop(); | |
// for(;;) | |
// ; | |
// } | |
// You should periodically call this method in your loop(): It will allow | |
// the DHCP library to maintain your DHCP lease, which means that it will | |
// periodically renew the lease and rebind if the lease cannot be renewed. | |
// Thus, unless you call this somewhere in your loop, your DHCP lease might | |
// expire, which you probably do not want | |
EthernetDHCP.maintain(); | |
} | |
// Just a utility function to nicely format an IP address. | |
const char* ip_to_str(const uint8_t* ipAddr) | |
{ | |
static char buf[16]; | |
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); | |
return buf; | |
} | |
String readPage(){ | |
//read the page, and capture & return everything between '<' and '>' | |
stringPos = 0; | |
memset( &inString, 0, 32 ); //clear inString memory | |
while(true){ | |
if (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment