Go Down

Topic: Issue calling PHP using Ethernet Shield (Read 1 time) previous topic - next topic

PaulS

Quote
Because I am using an Ethernet Shield, pins 10-13 cannot be used for standard outputs, so i decided to use pins 8 & 9 as my outputs instead.

You won't be able to use the led on pin 13, either.

Quote
Nothing was changed other than specifying the new startPin and stopPin variables:

#define startPin 8               // The first pin we connect a relay to
#define stopPin 9                // The last pin we connect a relay to

and changing the 'if (pinNum == 8 )' statements.

Why isn't the if statement
Code: [Select]
if (pinNum == startPin)
?

Quote
Unfortunatly the PHP is now not being called and just states 'failed to call PHP' in the serial debug window.

Pin 10 is not now being set as an OUTPUT pin, in setup(). Even if you do not want to deselect the ethernet shield, you must define pin 10 as OUTPUT for the ethernet shield to work.

Quote
Having read the tutorial on arrays, i'm still not fully understanding them, but i guess I need the array to be more flexible to allow for flexibility in the length of the ident string and also for a 1 or 2 digit pin number.  This is what was attempted to be achieved in the initial code variation.

Your failure to connect to the internet has nothing to do with parsing the data that you get from virtual wire.

Tissy

Thanks for your reply and help Paul.

Quote

You won't be able to use the led on pin 13, either.

This has now been moved to a different pin

Quote

Why isn't the if statement
if (pinNum == startPin)

Because ultimately I will have 4 alarm conditions, so for now I thought it easier to define them that way opposed to using variables.  However, I have renamed the 2 if statements using the startPin & stopPin variables for ease whilst testing.

Quote

Pin 10 is not now being set as an OUTPUT pin, in setup(). Even if you do not want to deselect the ethernet shield, you must define pin 10 as OUTPUT for the ethernet shield to work.

I did think this may be an issue, however setting these pins (10-12) to OUTPUT in setup() has made no difference.  In all examples I've seen using the Ethernet shield I have not seen the requirement to set these pins manually.

It seems to ouput the "failed to call PHP" instantly as if its not really even attempting to call it.

Any other suggestions ?

PaulS

Only pin 10 needs to be set as an OUTPUT. The SPI code that the ethernet shield uses to communicate with the Arduino knows how to deal with the other pins. It may be necessary to actually set the pin (10) HIGH, too.

Tissy

This is driving me nuts and seems to defy all logic  :0

The main code below doesn't work and prevents the PHP from being called.

However, if i comment out this section, it works fine ??

I've stripped out as much as i can for testing only leaving a simple loop.

Could something in the RF receiver section be causing problems ?  Seems weird if it is, but commenting it out resolves the issue.

Code: [Select]

  //Set-up the RF receiver
//  vw_setup(1000);                     // Bits per sec
//  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
//  vw_rx_start();                      // Start the receiver


Code: [Select]

static String ident = "server";     // Identification for this device

#include <SPI.h>
#include <Ethernet.h>               // Ethernet Library
#include <DHT.h>                    // DHT Temp & Humidity Library
#include <VirtualWire.h>            // 433Mhz Module Library

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };    // Arduino MAC Address
byte ip[] = { 192,168,1,142 };                          // local Arduino IP
//byte gateway[] = { 192,168,1,1 };                     // IP of Gateway
//byte subnet[] = { 255, 255, 255, 0 };                 // Subnet Address
byte server[] = { 192,168,1,101 };                      // IP of Web Server

Client client(server, 80);

const int ledPin =  6;            // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0;                 // Will store last time LED was updated

// *** Uncomment whatever type of Temp sensor being used ***
#define DHTTYPE DHT11              // DHT 11
#define DHTPIN 5                   // Sets DHT Temp Sensor Pin

// *** Set-up Temperature Sensor ***
DHT dht(DHTPIN, DHTTYPE);

// *** Fan related Variables ***
#define fanPin 7                   // Fan is connected to PIN 5

// *** Temperature Variables ***
#define maxTemp 35                 // Fan will start at this temperature
#define minTemp 32                 // Sets Temp Hysteresis variation (3 degress) 

// *** RF Variables ***
#define rxPin 3                    // Sets 433Mhz Rx Pin

// *** Other Variables ***
#define startPin 8               // The first pin we connect a relay to
#define stopPin 9                // The last pin we connect a relay to

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
 
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);   
   
//    pinMode(11, OUTPUT);
//    digitalWrite(11, HIGH);   
   
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH); 
 
  //Set-up the RF receiver
  vw_setup(1000);                     // Bits per sec
  vw_set_rx_pin(rxPin);               // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver

  dht.begin();  // Begin reading DHT sensor
 
  // Set necessary Pins for OUTPUT and set their initial state
  for (int i = startPin; i <= stopPin; i++)
  {
    Serial.print("Setting pinMode on pin: ");
    Serial.println(i);
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);   
  }
  Serial.println("Ready to receive!");
}

void loop()
{
  alarm_1();
  delay(5000);
  alarm_2();
  delay(5000);
}

void alarm_1(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_1.php");               // location of ProwlPHP script
//     client.println("Host: tisseyre.homeserver.com");           // domain name
//     client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
 
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}

void alarm_2(){
  unsigned long startTime = millis();

  if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /alarm_2.php");               // location of ProwlPHP script
      client.println("Host: tisseyre.homeserver.com");           // domain name
      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.println();
      delay(1000);
  }
  else {
    Serial.println("failed to call PHP");
  }
    delay(1000);
    Serial.print("Response from server: ");
    while ((!client.available()) && ((millis() - startTime ) < 5000));
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
 
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
//      client.println("Connection: close");                       // this line is not related to your problem but I recommend it ;)
      client.stop();
      client.flush();
      Serial.println("Disconnected...");
    }
}

void temperature (){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float humidity = dht.readHumidity();
  float temp = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(temp) || isnan(humidity)) {
    Serial.println("Failed to read from DHT");
  } else {
    if (temp >= maxTemp){
        Serial.print("Temperature: ");
        Serial.print(temp);
        Serial.println(" *C"); 
    fan(true);}
    else if (temp <= minTemp)
    {fan(false);}
  }
  if (temp <= maxTemp && temp >= minTemp){
        Serial.print("Temperature: ");
        Serial.print(temp);
        Serial.println(" *C");}
}
   
void fan(boolean stat){
  digitalWrite(fanPin, stat);
}

PaulS

Code: [Select]
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH); 

I don't think you want to do this.

Go Up