Post topics, source code that relate to the Arduino Platform

User avatar
By rarbolay
#19230 Actually I ended up re-writing those functions, the web server did not like how I initially wrote them. A new odd problem has popped up though. when I first apply power to the ESP, a bunch of garbage streams on the serial and the blue light stays on and it does nothing else (like it cant find the bootloader). If I shut it off and turn it right back on it works fine.
Anyway, here is the updated code:
Code: Select all// A PIR sensor that sends status to web server. Compiled on Arduino IDE 1.6.4 and written for ESP8266 (ESP-01)
// based upon:
// PIR sensor tester by Limor Fried of Adafruit and the published work of Andy Reischle (AReResearch)



 
#include "ESP8266WiFi.h"

const char* ssid     = "MommaBug";
const char* password = "u*****************7";
const char* SensorID = "2";

int inputPin = 2;               // GPIO2 is pin 2 on Arduino, pin 4 on NodeMCU
int pirState = 0;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup() {

  pinMode(inputPin, INPUT);     // declare sensor as input
  Serial.begin(9600);
 
 //Connect to AP
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid); 
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(15000);
    Serial.print(".");
}

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == 1) {            // check if the input is 1
     
    delay(150);   
    if (pirState == 0) {
      // we have just turned on
      Serial.println("Motion detected!");
      SendAlarm();
      // We only want to print on the output change, not state
      pirState = 1;
    }
  } else {
     
      delay(300);   
      if (pirState == 1){
      // we have just turned off
      Serial.println("Motion ended!");
      SendClear();
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

// Send alarm to Fred
void SendAlarm()

{
 WiFiClient client;
 String url = ("http://172.16.0.1/alarm.php?SensorID=2&status=ALARM"); //This one is hardcoded to print URL, you can change it to anything really, it wont affect the actual operation 
  Serial.print("Sending web message to: ");
  Serial.println(url);

  // This will send the request to the server

if

  (client.connect("172.16.0.1", 80));
 
  {
  client.println("GET /alarm.php?SensorID=2&status=ALARM" "HTTP/1.1\r\n");//This one is hardcoded to send an ALARM status
  client.println("Host: MOMMABUG.HOME\r/n");
  client.println("Accept: */*\r\n");
  client.print("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n");//I kept the LUA reference on the header, might not make any difference....
  client.println("\r\n");
}
 
if (client.connected()) {
   client.stop();
}
}

// Send clear to Fred


void SendClear()
{
  String clr = ("http://172.16.0.1/alarm.php?SensorID=2&status=CLEAR");
  WiFiClient client;
 
  Serial.print("Sending web message to: ");
  Serial.println(clr);

  // This will send the request to the server

if

 (client.connect("172.16.0.1", 80));
 
 {
  client.println("GET /alarm.php?SensorID=2&status=CLEAR" "HTTP/1.1\r\n");
  client.println("Host: MOMMABUG.HOME\r/n");
  client.println("Accept: */*\r\n");
  client.print("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n");
  client.println("\r\n");
 
}
 
 if (client.connected()) {
   client.stop();
}
}