Problem with motion detection (Arduino UNO + CC3000 + Motion sensor)

Hi there. I’m quite new to Arduino, but have been working intensely the last couple of days to get a project up and running.

Though I’m having difficulties getting my script to actually detect for motion. At the moment it only states that motion = LOW, printing out Serial.println(“Motion 0”); forever.
Instead I would obviously like to print the motion whether it’s 0 or 1. I’ve been playing around, but have not been able to find a solution.

Also I would like to only send information to the node.js app, when the state has been changed - can anyone point me to the right direction for doing this?

Thanks a lot!

Here is my piece of code

*  Simple WiFi weather station with Arduino, the DHT11 sensor & the CC3000 chip
*  Part of the code is based on the work done by Adafruit on the CC3000 chip & the DHT11 sensor
*  Writtent by Marco Schwartz for Open Home Automation

// Number of variables & functions

// Include required libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <aREST.h>
#include "utility/debug.h"

// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

// WiFi network (change with your settings !)
#define WLAN_SSID       "XXXX"
#define WLAN_PASS       "XXXX"

// Create CC3000 & DHT instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, 
// Create aREST instance
aREST rest = aREST();

// The port to listen for incoming TCP connections 
#define LISTEN_PORT           80

// Server instance
Adafruit_CC3000_Server restServer(LISTEN_PORT);

int ledPin = 8;   // choose the pin for the LED
int ledPinSecond = 7;  
int inputPin = 2;              // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;

// Variables to be exposed to the API
int temperature;
int humidity;
void setup(void)
  // Start Serial
  Serial.print(F("Launching script\n"));
  pinMode(ledPin, OUTPUT);    // declare LED as output
  pinMode(ledPinSecond, OUTPUT);    
  pinMode(inputPin, INPUT);     // declare sensor as input 
  Serial.print(F("Defining inputs and outputs\n"));
  Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
  // Expose variables to REST API
  Serial.print(F("Exposing REST API variables\n"));
  // Set name
  // Initialise the CC3000 module
  if (!cc3000.begin())
    Serial.print(F("Could not start CC3000, something is wrong with the CC3000 module\n"));
  // Connect to  WiFi network
    char *ssid = WLAN_SSID;             /* Max 32 chars */
    Serial.print(F("Succesfully connected to WiFi "));Serial.println(ssid);
     Serial.print(F("\nRequesting DHCP\n"));
  // Check DHCP
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed to connect to WiFi!"));
  while (!cc3000.checkDHCP())
  Serial.print(F("DHCP acquired\n"));
  // Start server
  Serial.print(F("Starting REST server\n"));

bool displayConnectionDetails(void)
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
    Serial.print(F("Successfully requested DHCP:"));
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    return true;

void loop(){
  // Handle REST calls
   //Detect motion 
    if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED ON
    digitalWrite(ledPinSecond, HIGH);
    Serial.println(F("Motion 1"));
    temperature = 1;
    humidity = 200;

    if (pirState == LOW) {
      // we have just turned on
      Serial.println(F("Motion detected!"));   
      // We only want to print on the output change, not state
      pirState = HIGH;
      } else {
      digitalWrite(ledPin, HIGH); // turn LED OFF
      digitalWrite(ledPinSecond, LOW);
      Serial.println(F("Motion 0"));
      temperature = 99;
      humidity = 123;

      if (pirState == HIGH){
      // we have just turned off
      Serial.println(F("Motion ended!"));
      // We only want to print on the output change, not state
      pirState = LOW;
  Adafruit_CC3000_ClientRef client = restServer.available();

void displayDriverMode(void)
  #ifdef CC3000_TINY_DRIVER
    Serial.println(F("CC3000 is configure in 'Tiny' mode"));
    Serial.print(F("RX Buffer : "));
    Serial.println(F(" bytes"));
    Serial.print(F("TX Buffer : "));
    Serial.println(F(" bytes"));

Hi Thomasoc

You don't appear to digitalRead() the pin connected to the sensor.