0% found this document useful (0 votes)
53 views3 pages

P2 Pir

This Arduino code connects an ESP8266 WiFi module to a Carriots IoT platform. It monitors a PIR motion sensor and sends data to Carriots when motion is detected, including turning on an LED. The code connects to WiFi, defines the Carriots API key and device ID, and sends JSON data to Carriots using HTTP when the PIR sensor reads HIGH, indicating motion.

Uploaded by

Hasslay Pakistan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

P2 Pir

This Arduino code connects an ESP8266 WiFi module to a Carriots IoT platform. It monitors a PIR motion sensor and sends data to Carriots when motion is detected, including turning on an LED. The code connects to WiFi, defines the Carriots API key and device ID, and sends JSON data to Carriots using HTTP when the PIR sensor reads HIGH, indicating motion.

Uploaded by

Hasslay Pakistan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

// written by Junaid Ahmed

// This is a free software protected by the creative commons license


// This is for as is use and there may be bugs and other issues.

#include <SPI.h>
#include "ESP8266WiFi.h"

const char* ssid = "NETGEAR45";


const char* password = "curlybird210";

const char* server = "api.carriots.com";


const String APIKEY =
"1e2f6920a92e3ca079bf60329d7871d69b3881df88df9b2c44e6d71f17610d8f"; // Replace with
your Carriots apikey
const String DEVICE = "light@junaid.junaid"; // Replace with the id_developer of
your device

int ledpin = 4;
int pirpin = 12;
int pirstate = LOW;
int val = 0;

void setup(){
Serial.begin(115200);
delay(10);
pinMode(ledpin,OUTPUT);
pinMode(pirpin,INPUT);
Serial.println("calibrating");
for(int i = 0; i < 20; i++){
Serial.print(".");
delay(1000);
}

// start wifi

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

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

// function to talk to the Carriot Platform

void sendStream()
{

String txt = ""; // Text to send


if ( pirstate == HIGH ) { // Alarm OFF
txt = "Motion Detected";
} else { // Alarm ON
txt = "Somethig wrong";
}
Serial.println(txt);
//Serial.println(val);// For debugging purpose only
WiFiClient client;
//const int httpPort = 80;
if (client.connect(server, 80)) { // If there's a successful connection
Serial.println(F("connected"));
// Build the data field
String json =
"{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":
{\"light\":\""+txt+"\"}}";

// Make a HTTP request


client.println("POST /streams HTTP/1.1");
client.println("Host: api.carriots.com");
client.println("Accept: application/json");
client.println("User-Agent: Arduino-Carriots");
client.println("Content-Type: application/json");
client.print("carriots.apikey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = json.length();
client.println(thisLength);
client.println("Connection: close");
client.println();

client.println(json);
}
else {
// If you didn't get a connection to the server:
Serial.println(F("connection failed"));
}

void loop() {
val = digitalRead(pirpin);
Serial.println(val);
if(val == HIGH){

digitalWrite(ledpin,HIGH);
if (pirstate == LOW){
Serial.println("Motion Detected");
pirstate =HIGH;
Serial.println(F("Send Stream"));
sendStream();
delay(30000);
}
/* while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
delay(30000);
} */
}
else{
digitalWrite(ledpin,LOW);
if(pirstate == HIGH){
Serial.println("motion Detected email sent");
pirstate = LOW;
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy