人体感应模块控制LCD1602背景灯是否开启

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

#define ResetPIN 7



// 初始化引脚
LiquidCrystal lcd(9, 8, 5, 4, 3, 6);

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAA, 0xCE, 0xEA, 0xFF, 0xE1 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "192.168.9.6";    // name address for Google (using DNS)
String clientPwd="-1";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(172,16,0,222);
IPAddress gateway(172,16,0,1);
IPAddress dns_server(60,191,134,196);
IPAddress subnet(255,255,0,0);
// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;


int errCount=0;

void setup() {
  
  
  //LCD
  // 设置行列值 
  lcd.begin(16, 2);


  
   pinMode(ResetPIN,OUTPUT);
   digitalWrite(ResetPIN,LOW);   
   
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection:
  // give the Ethernet shield a second to initialize:
   if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip,dns_server,gateway,subnet);
  }
  

  delay(1000);
  Serial.println("Ready");
}


void loop()
{

 Serial.println(clientPwd); 
 // 打印字符串
 lcd.setCursor(0,0);
 lcd.print(clientPwd);
 
 
 httpRequest();
 delay(1000);
  

}


//================Http Request Code========================
void httpRequest(){
    // if you get a connection, report back via serial:
    
  if(!client.connected()){
     Serial.println("disconnecting.");
     client.stop();
     if (client.connect(server, 9988)) {
        Serial.println("connected");
     }else{
     // kf you didn't get a connection to the server:
        Serial.println("connection failed"); 
        errCount++;
        if(errCount>=3){
          digitalWrite(ResetPIN,HIGH);
          delay(1000);
        } 
    }
  } else{
       
    // Make a HTTP request:
    client.println("GET /main.ashx?m=c&mil=" + String(millis()) + "&cpwd="+clientPwd+"&rf=0" +" HTTP/1.1");
    client.println("Host:" +String(server));
    //client.println("Connection: close");
    
    client.println();
    delay(10);
    String reply="";
    // if there are incoming bytes available 
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      reply+=c;
      
    }
    //Serial.print(reply);
    int index=reply.indexOf("\"Model\":\"8888");
    if(index>=0){
      index=index+9;
      clientPwd= reply.substring(index,index+8);
      Serial.println("Changed:" + clientPwd);

   
    }

    errCount=0;
    //wdt_reset();
  }

  

}
//================End Http Request Code===============
View Code

 arduino网卡扩展板,会使用 2,10,11,12,13 i/o口,所以要调整lcd使用的接口

人体感应器信号输出连S9013三极管(来控制lcd背景灯的亮与灭(使用K编号的脚)

 

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen
 
 */
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <avr/wdt.h>  

#define ResetPIN 7
#define HasManPIN 8
#define RebootPIN 6



// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAA, 0xCE, 0x3A, 0x6F, 0xE1 };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "192.168.9.6";    // name address for Google (using DNS)
String clientPwd="-1";
int hasMan=0;
LiquidCrystal_I2C lcd(0x27,16,2); 

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(172,16,0,172);
IPAddress gateway(172,16,0,1);
IPAddress dns_server(60,191,134,196);
IPAddress subnet(255,255,0,0);
// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;


int errCount=0;
unsigned long lastRequest=0;

void setup() {
  
   pinMode(HasManPIN,INPUT);
   pinMode(ResetPIN,OUTPUT);
   pinMode(RebootPIN,OUTPUT);
   
   digitalWrite(ResetPIN,LOW);   
   digitalWrite(RebootPIN,LOW);
   
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection:
  // give the Ethernet shield a second to initialize:
   if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip,dns_server,gateway,subnet);
  }
  

  lcd.init();
  lcd.backlight();  
  lcd.print("-1");
  delay(1000);
  Serial.println("Ready");
  lastRequest=millis();
  
  wdt_enable(WDTO_8S); 
}


void loop()
{
  //digitalWrite(RebootPIN,LOW);
  
  if((millis()-lastRequest)>=1200){
       Serial.println("reboot");
       digitalWrite(ResetPIN,HIGH);
       delay(1000);
  }
 Serial.println("lcd...");
 Serial.println(clientPwd); 
 // 打印字符串
 lcd.setCursor(0,0);
 lcd.print(clientPwd);

 
  //digitalWrite(RebootPIN,HIGH);
 
 hasMan=digitalRead(HasManPIN);
 Serial.println(hasMan);
 if(hasMan==1){
  lcd.backlight();
  }else{
   lcd.noBacklight(); 
 }
 
 if( (millis() - lastRequest) >=1000)
 {
    httpRequest();
    lastRequest=millis();
 }
 
 delay(25);
  Serial.println("lcd over!");
 //
  wdt_reset();

}


//================Http Request Code========================
void httpRequest(){
    // if you get a connection, report back via serial:
    
  if(!client.connected()){
     Serial.println("disconnecting.");
     client.stop();
     if (client.connect(server, 9988)) {
        Serial.println("connected");
     }else{
     // kf you didn't get a connection to the server:
        Serial.println("connection failed"); 
        errCount++;
        if(errCount>=3){
          digitalWrite(ResetPIN,HIGH);
          delay(1000);
        } 
    }
  } else{
       
    // Make a HTTP request:
    client.println("GET /main.ashx?m=c&mil=" + String(millis()) + "&cpwd="+clientPwd+"&rf="+String(hasMan) +" HTTP/1.1");
    client.println("Host:" +String(server));
    //client.println("Connection: close");
    
    client.println();
    delay(10);
    String reply="";
    // if there are incoming bytes available 
    // from the server, read them and print them:
    while (client.available()) {
      char c = client.read();
      reply+=c;
      
    }
    //Serial.print(reply);
    int index=reply.indexOf("\"Model\":\"8888");
    if(index>=0){
      index=index+9;
      clientPwd= reply.substring(index,index+8);
      Serial.println("Changed:" + clientPwd);

   
    }

    errCount=0;
    //wdt_reset();
  }

  

}
//================End Http Request Code===============
View Code

 

你可能感兴趣的:(人体感应模块控制LCD1602背景灯是否开启)