If any of 4 logic inputs activated, trigger an HTTP POST

Hi

I am trying to make a 4 zone alarm with Arduino Ethernet.

I have 4 contact inputs from sensors in the carpet in different rooms. They may only be closed for 1/2 second, maybe 10 seconds. Not likely to be more than one closure per second on any one sensor, but may need to debounce.

If any input closes, I want to make a POST to a php script so I can log which input, at what time, to a SQL database.

I can do the php and SQL ok, but I am struggling to know how to approach the coding on the arduino to trigger the POST.

I see only 2 pins are available for hardware interrupt, but perhaps I don’t need hardware interrupt?

I am completely new to this sort of programming so am a bit lost. I have managed to make HTTP POST for just an internal loop count, but not for 4 logic inputs.

Any guidance would be much appreciated!

Thanks

Since you're struggling, just try to get a minimal version working. No need for interrupts, just poll your inputs. Use digitalRead on each one in turn and if the contact is closed, call the routine that does the post.

Ultimately, you'll want to only send a message when a contact becomes closed, but for the mark I, I'd rather be overwhelmed with reports and have it working.

Many thanks wildbill, will give that a try this evening.

This is what I have so far …

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xC8, 0xDD };

//Change to your server domain
char serverName[] = "www.xxxxxxxxxxx.com";

// change to your server's port
int serverPort = 80;

// change to the page on that server
char pageName[] = "/arduino_post.php";

EthernetClient client;
int totalCount = 0;
// insure params is big enough to hold your variables
char params[32];

// set this to the number of milliseconds delay
// this is 10 seconds
#define delayMillis 5000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;


/// new stuff start

// Do not use pins 10, 11, 12 or 13.
int ledPin = 13; // LED connected to digital pin 13
int inPin1 = 6;   // pushbutton connected to digital pin
int inPin2 = 7;   // pushbutton connected to digital pin
int inPin3 = 8;   // pushbutton connected to digital pin
int inPin4 = 9;   // pushbutton connected to digital pin

int val1 = 0;     // variable to store the read value
int val2 = 0;     // variable to store the read value
int val3 = 0;     // variable to store the read value
int val4 = 0;     // variable to store the read value









void setup() 
{  
  pinMode(inPin1, INPUT);      // sets the digital pin as input
  pinMode(inPin2, INPUT);      // sets the digital pin as input
  pinMode(inPin3, INPUT);      // sets the digital pin as input
  pinMode(inPin4, INPUT);      // sets the digital pin as input
  
  Serial.begin(9600);
  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(Ethernet.localIP());
  delay(2000);
  Serial.println(F("Ready"));
}






void loop()
{
  
  val1 = digitalRead(inPin1);   // read the input pin
  val2 = digitalRead(inPin2);   // read the input pin
  val3 = digitalRead(inPin3);   // read the input pin
  val4 = digitalRead(inPin4);   // read the input pin
  
  
 if(val1==HIGH) 
 {
   totalCount=1;
  // If using a static IP, comment out the next line
  Ethernet.maintain();
  thisMillis = millis();
  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;
    // params must be url encoded.
    sprintf(params,"rx=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) 
    Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    //totalCount++;
   // Serial.println(totalCount,DEC);
  }    
 }
 
 
  if(val2==HIGH) 
 {
   totalCount=2;
  // If using a static IP, comment out the next line
  Ethernet.maintain();
  thisMillis = millis();
  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;
    // params must be url encoded.
    sprintf(params,"rx=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) 
    Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    //totalCount++;
   // Serial.println(totalCount,DEC);
  }    
 }
 
  if(val3==HIGH) 
 {
   totalCount=3;
  // If using a static IP, comment out the next line
  Ethernet.maintain();
  thisMillis = millis();
  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;
    // params must be url encoded.
    sprintf(params,"rx=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) 
    Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    //totalCount++;
   // Serial.println(totalCount,DEC);
  }    
 }


 if(val4==HIGH) 
 {
   totalCount=4;
  // If using a static IP, comment out the next line
  Ethernet.maintain();
  thisMillis = millis();
  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;
    // params must be url encoded.
    sprintf(params,"rx=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) 
    Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    //totalCount++;
   // Serial.println(totalCount,DEC);
  }    
 }

else{}

}






byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
  int inChar;
  char outBuf[64];
 // Serial.print(F("connecting..."));
  if(client.connect(domainBuffer,thisPort) == 1)
  {
  //  Serial.println(F("connected"));

    // send the header
    sprintf(outBuf,"POST %s HTTP/1.1",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",domainBuffer);
    client.println(outBuf);
    client.println(F("Connection: close\r\nContent-Type: application/x-www-form-urlencoded"));
    sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
    client.println(outBuf);

    // send the body (variables)
    client.print(thisData);
  }
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
      connectLoop = 0;
    }

    delay(1);
    connectLoop++;
    if(connectLoop > 10000)
    {
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
  }

  Serial.println();
 // Serial.println(F("disconnecting."));
  client.stop();
  return 1;
}

Break this project into two parts...

Part A: Something that will do the http post when you press a button.

That boils down to...

loop()
{
if (boButtonPressed){SendPost}
}

Now "all" you have to do is write the "SendPost" subroutine and the "boButtonPressed" function.

Begin by making "SendPost" just turn on an LED briefly, and then turn it off again.

Then get boButtonPressed working.

Then go back, make SendPost actually send the post.

Part B: Starts life as an entirely separate Arduino project ("sketch" + minimal hardware)

It boils down to...

loop()
{
if (boFourButtonsPressed){TurnLedOnBrieflyThenOff}
}

boFourButtonsPressed is something very like...

boolean boFourButtonsPressed()
{
if ((boInput0) &&
(boInput1) &&
(boInput2) &&
(boInput3))

{return true;}//no ; here
else
{return false;}

}

... but THAT means you have to write boInput0, boInput1, boInput2, and boInput3... but the approach above makes things happen a lot faster in the long run, and leaves you with Good Code, which is a Good Thing.

Excellent, many thanks tkbyd and wildbill for taking the time to advise me on this - really appreciated, and I can see the process much clearer now. :wink: