Arduino Ethernet

今天用Arduino写了一个可以访问网页的小程序。

现在你可以用计算机访问: http://twd2.me/arduino/index.php

这里是半残代码:

#include <ST7920.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(74,117,63,100);
EthernetClient client;
ST7920 lcd;

void lcdPrint(String s)
{
  String s1,s2,s3,s4;
  if(s.length()<=16)
  {
    s1=s;
    s2="";
    s3="";
    s4="";
  }
  if(s.length()>16 && s.length()<=32)
  {
    s1=s.substring(0,16);
    s2=s.substring(16);
    s3="";
    s4="";
  }
  if(s.length()>32 && s.length()<=48)
  {
    s1=s.substring(0,16);
    s2=s.substring(16,32);
    s3=s.substring(32);
    s4="";
  }
  if(s.length()>48 && s.length()<=64)
  {
    s1=s.substring(0,16);
    s2=s.substring(16,32);
    s3=s.substring(32,48);
    s4=s.substring(48);
  }
  
  /*s1="ABCDEFGHIJKLMNOPQ";
  s2="ABCDEFGHIJKLMNOPQ";
  s3="ABCDEFGHIJKLMNOPQ";
  s4="ABCDEFGHIJKLMNOPQ";*/
  s1+="                ";
  s2+="                ";
  s3+="                ";
  s4+="                ";
  char c1[17],c2[17],c3[17],c4[17];
  s1.toCharArray(c1,17);
  s2.toCharArray(c2,17);
  s3.toCharArray(c3,17);
  s4.toCharArray(c4,17);
  
  lcd.LCD_DisplayStrings_WithAddress(0x80,c1);
  lcd.LCD_DisplayStrings_WithAddress(0x90,c2);
  lcd.LCD_DisplayStrings_WithAddress(0x88,c3);
  lcd.LCD_DisplayStrings_WithAddress(0x98,c4);
}
String StringIP(int IP){
  int a,b,c,d;
  a=IP & 0xFF;
  IP=IP>>8;
  b=IP & 0xFF;
  IP=IP>>8;
  c=IP & 0xFF;
  IP=IP>>8;
  d=IP & 0xFF;
  IP=IP>>8;
  return String(a)+'.'+String(b)+'.'+String(c)+'.'+String(d);
}
void setup() {
  lcd.LCD_Init();
  lcd.LCD_ClearRam();
  lcdPrint("Loading...");
  if (!Ethernet.begin(mac)) {
    lcdPrint("Failed to configure Ethernet using DHCP.");
    // no point in carrying on, so do nothing forevermore:
    while(1);
  }
  lcdPrint(StringIP(String(Ethernet.localIP()).toInt()));
  // give the Ethernet shield a second to initialize:
  for(int i=5;i>=1;i--)
  {
    //lcdPrint("Loading..."+String(i));
    delay(1000);
  }
  
  lcdPrint("Connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    lcdPrint("Connected");
    lcdPrint("Requesting...");
    // Make a HTTP request:
    client.println("GET /arduino/index.php HTTP/1.1");
    client.println("Host: twd2.me");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: Close");
    client.println();
    lcdPrint("Downloading...");
  } 
  else {
    // kf you didn't get a connection to the server:
    lcdPrint("connection failed");
  }
}
String clientRead(int num){
  String s="";
  while(s.length()<num){
    if (client.available()) {
      char c = client.read();
      s+=c;
    }
  }
  return s;
}
String clientReadline(){
  String s="";
  while(1){
    if (client.available()) {
      char c = client.read();
      s+=c;
    }
    if(s.charAt(s.length()-2)=='\r' && s.charAt(s.length()-1)=='\n') break;
  }
  return s.substring(0,s.length()-2);
}

void loop(){
  int clength=0;
  while(1){
    String s=clientReadline();
    if(s=="") break;
    if(s.startsWith("Content-Length: ")){
      clength=s.substring(s.indexOf(':')+1).toInt();
    }
  }
  lcdPrint(String(clength));
  lcdPrint(clientRead(clength));
  //lcdPrint(clientReadline());
  // if there are incoming bytes available 
  // from the server, read them and print them:
  //delay(1000);

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    //lcdPrint(ss);
    //lcdPrint("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

 
不提供任何说明XD

发表评论?

5 条评论。

  1. 努力学习,才干报国! :cool:

  2. 牛人啊!博主好像花很多时间编程啊。你还有时间在家做功课吗?

  3. 难道Arduino还有网卡模块?

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/2017QrCode