0

このコードをarduinoに焼き付けました:

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

using namespace std;

#define BUFFSIZE        16
#define PIN0            0
#define PIN1            1
#define PIN2            2
#define PIN3            3
#define PIN4            4
#define PIN5            5
#define PIN6            6
#define PIN7            7
#define PIN8            8
#define PIN9            9
#define PIN10           10
#define PIN11           11
#define PIN12           12
#define PIN13           13

// For mac address please view the Ethernet shield.
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD5};

IPAddress server(192, 168, 0, 61); // IP address of RAAS server

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 62);


// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 8 is selected for RAAS):
EthernetClient client;

String GetNextCommand()
{
    int count,i;
    int temp;
    String command;
        Serial.println("Waiting for next Command.");
    while((count = client.available()) < 1 );
    for(i = 0; i < count ; i++)
        command += (char) client.read();
        command.replace("\r","");
        command.replace("\n","");
        Serial.println(command);
        return command;
}

bool ConnectServer() {
    int resCount,i;
    String response;
    Serial.println("Trying to connect...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 8)) {
        Serial.println("connected");
        // Make a Handshake request:
        client.println("EHLO");
        response = GetNextCommand();
        if (response == "EHLO" ) {
            Serial.println("Server Response OKAY");
            return true;
        }
    } else {
        // kf you didn't get a connection to the server:
        Serial.println("connection failed");
    }
    return false;
}

void InitializeBoard() {
    pinMode(PIN1, OUTPUT);
    pinMode(PIN2, OUTPUT);
    pinMode(PIN3, OUTPUT);
    pinMode(PIN4, OUTPUT);
    pinMode(PIN5, OUTPUT);
    pinMode(PIN6, OUTPUT);
    pinMode(PIN7, OUTPUT);
    pinMode(PIN8, OUTPUT);
    pinMode(PIN9, OUTPUT);
    pinMode(PIN10, OUTPUT);
    pinMode(PIN11, OUTPUT);
    pinMode(PIN12, OUTPUT);
    pinMode(PIN13, OUTPUT);
    digitalWrite(PIN1, LOW);
    digitalWrite(PIN2, LOW);
    digitalWrite(PIN3, LOW);
    digitalWrite(PIN4, LOW);
    digitalWrite(PIN5, LOW);
    digitalWrite(PIN6, LOW);
    digitalWrite(PIN7, LOW);
    digitalWrite(PIN8, LOW);
    digitalWrite(PIN9, LOW);
    digitalWrite(PIN10, LOW);
    digitalWrite(PIN11, LOW);
    digitalWrite(PIN12, LOW);
    digitalWrite(PIN13, LOW);
}

void ParseCommand(String str) {

    int pinNum;
    int pinState = LOW;

    String switchNo,operation;
    int temp;
        temp=0;
    temp = str.indexOf(':',temp);
    switchNo = str.substring(0,temp);
    operation = str.substring(temp+1);//,str.length()-temp-2);
    Serial.println("Port: " + switchNo);
    Serial.println("Operation: " + operation);  
    if(operation == "OFF;")
        pinState = LOW;
    else if(operation == "ON;")
        pinState = HIGH;
    else Serial.println("Invalid Command from server!");

    pinNum = str.toInt();

    Serial.print("Setting ");
    Serial.print( pinNum);
    Serial.print(  " to ");
    Serial.println( pinState);

    switch (pinNum){
        case PIN1:
                digitalWrite(PIN1, pinState);
                break;
        case PIN2:
                digitalWrite(PIN2, pinState);
                break;
        case PIN3:
                digitalWrite(PIN3, pinState);
                break;
        case PIN4:
                digitalWrite(PIN4, pinState);
                break;
        case PIN5:
                digitalWrite(PIN5, pinState);
                break;
        case PIN6:
                digitalWrite(PIN6, pinState);
                break;
        case PIN7:
                digitalWrite(PIN7, pinState);
                break;
        case PIN8:
                digitalWrite(PIN8, pinState);
                break;
        case PIN9:
                digitalWrite(PIN9, pinState);
                break;
        case PIN10:
                digitalWrite(PIN10, pinState);
                break;
        case PIN11:
                digitalWrite(PIN11, pinState);
                break;
        case PIN12:
                digitalWrite(PIN12, pinState);
                break;
        case PIN13:
                digitalWrite(PIN13, pinState);
                break;
        default:
            Serial.println("Invalid Pin Address!");
    }
}

void setup() {

    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        // try to congifure using IP address instead of DHCP:
        Ethernet.begin(mac, ip);
    } else {
      Serial.println("Bound on given Mac");
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);

}

void loop() {
    //Keep trying to connect to the server until the server response okay!
    while (!ConnectServer());

    //While Server is connected, keep listening for incoming commands from server.
    while (client.connected()) {
        String command;
        command = GetNextCommand();
        ParseCommand(command);
    }

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
        Serial.println();
        Serial.println("Server Disconnected.");
        client.stop();
    }
}

ここで、1 ~ 13 のすべてのピンにリレーを接続する必要があります。しかし、ピンは適切に動作していません。イーサネット シールドでコマンド13:ON;または13:OFF;ピンを送信しても、その都度状態が変わりません。しかし、私が送信し03:ON;たり03:OFF;、コマンドの状態を正確に変更したりすると。同様に、一部のピンはピン 3 のように応答し、他のピンは応答しません (pin13 など)。これはコードと関係がありますか?

4

1 に答える 1

0

問題は、イーサネット シールドがピン 13、12、11 を SPI として使用することです。また、10 と 4 で、SD またはイーサネットを選択します。

pinNum = str.toInt();

そして、正しい操作で pinNum をマッチさせるための switch ステートメントがあります。

「ParseCommand()」関数の前に str の値がどのように見えるかを確認したいと思います。「GetNextCommand()」に何が表示されるか教えてください。

于 2013-07-03T18:04:58.383 に答える