Loading...
Pages: [1]   Go Down
Author Topic: Connecting Arduino to a server using Ethernet Shield  (Read 143 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As a project for an electronics class, we have tried to connect an RGB LED to an Arduino, and using an Ethernet Shield, connect to a server that a we created, which calculates whether a tweet from twitter is positive, neutral, or negative, and we hope to make the LED turn a different color depending on what the server tells it.  Actually getting the Arduino to connect to the server is confusing me though, and the more tutorials I look at the more confused I get because they seem to all have different goals.  Could someone please point me in the right direction for how to connect the Arduino to a server and get it to read the data using the Ethernet Shield?
Logged

0
Offline Offline
God Member
*****
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

File -> Examples -> Ethernet -> WebClient is the place to start.  Searching the forum for "web client" will get you an earful, too.  And no doubt Zoomkat will be along in a moment to share some code.

Good luck with your project.

-br

Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5171
CMiYC
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When you say "connect to a server" do you mean a server you are running, or twitter?
Logged

www.cmiyc.com - A guide to being an Enginerd

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

a server we are running.  Our server is already reading and interpreting the data from twitter, the arduino is just going to be taking the data and changing the colors of an LED based off of the data. 
Logged

0
Online Online
Tesla Member
***
Karma: 50
Posts: 6546
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basic client code that connects to a server and retrieves data.

Code:
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

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

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600);
  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  } 
}

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Logged

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Pages: [1]   Go Up
Print
 
Jump to: