/*
Arduino UART Demo Sketch. This Sketch is made for an Genuino 101 IoT board with a Grove
UART WiFi module based on the popular ESP8266 IoT SoC to communicate to the AllThingsTalk
IoT developer cloud.
The Grove UART WiFi module has firmware installed which includes the ATT_IOT library. It
communicates through Serial1 of the Genuino 101 board.
Version 1.0 dd 26/12/2015
This sketch is an example sketch to deploy the Grove Button (101020003) to the
AllThingsTalk IoT developer cloud.
### Instructions
1. Setup the Arduino hardware
- Use an Arduino Genuino 101 IoT board
- Connect the Arduino Grove shield
- Connect USB cable to your computer
- Connect a Grove Button to pin D2 of the Arduino shield
- Grove UART wifi to pin UART (D0,D1)
2. Add 'ATT_IOT_UART' library to your Arduino Environment
More info can be found at http://arduino.cc/en/Guide/Libraries
3. Fill in the missing strings (deviceId, clientId and clientKey) in the keys.h file
4. Optionally, change sensor names, labels as appropiate
5. Upload the sketch
Note: for use of extra actuators, extend the callback function at the end of the sketch
*/
#include "ATT_IOT_UART.h" // AllThingsTalk Arduino UART IoT library
#include <SPI.h> // Required to have support for signed/unsigned long type
#include "keys.h" // Keep all your personal account information in a seperate file
ATTDevice Device(&Serial1);
char httpServer[] = "api.smartliving.io"; // HTTP API Server host
char mqttServer[] = "broker.smartliving.io"; // MQTT Server Address
// Define PIN numbers for assets
// For digital and analog sensors, we recommend to use the physical pin id as the asset id
// For other sensors (I2C and UART), you can select any unique number as the asset id
// Digital sensor is connected to pin D2 on grove shield
// Required for the device
#include <ATT_IOT_UART.h>
#include <SerialCommands.h>
#define PIR_MOTION_SENSOR 2//Use pin 2 to receive the signal from the module
#define LED 4//the Grove - LED is connected to D4 of Arduino
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin2 = 4; // the number of the LED pin
const int switchPin = 3;
const int ledPin4 = 5;
// variables will change:
int buttonState = 0;
bool b = 0;
#include <math.h> // lightsensor in A0
const int ledPin=6; //Connect the LED Grove module to Pin8, Digital 8
const int thresholdvalue=100; //The threshold for which the LED should turn on.
float Rsensor; //Resistance of sensor in K
int pushButton = 7;
void pinsInit()
{
pinMode(PIR_MOTION_SENSOR, INPUT);
pinMode(LED,OUTPUT);
}
void turnOnLED()
{
digitalWrite(LED,HIGH);
}
void turnOffLED()
{
digitalWrite(LED,LOW);
}
boolean isPeopleDetected()
{
int sensorValue = digitalRead(PIR_MOTION_SENSOR);
if(sensorValue == LOW)//if the sensor value is HIGH?
{
return true;//yes,return true
}
else
{
return false;//no,return false
}
}
void callback(int pin, String& value)
{
Serial.print("Incoming data for: "); // Display the value that arrived from the AllThingsTalk IOT developer cloud.
Serial.print(pin);
Serial.print(", value: ");
Serial.println(value);
Device.Send(value, pin); // Send the value back for confirmation
}
void callback(int pin, String& value); //this is for smartliving. we can see how bad the frsh air is.
#define gasId 0
bool sensorVal = false;
void SendValue()
{
if(sensorVal){
Serial.println("Motion detected"); // The void for or gas sensor. It says when the sensor is false or true
Device.Send("true", motionId);
}
else{
Serial.println("Motion stopped");
Device.Send("false", motionId);
}
}
void setup()
{
pinMode(switchPin, INPUT);
pinMode(ledPin4, OUTPUT);
digitalWrite(switchPin, HIGH);
Serial.begin(57600); // Init serial link for debugging
while(!Serial && millis() < 1000); // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc
Serial.println("Starting sketch");
Serial1.begin(115200); // Init serial link for WiFi module
while(!Serial1);
while(!Device.StartWifi())
Serial.println("Retrying...");
while(!Device.Init(DEVICEID, CLIENTID, CLIENTKEY)) // If we can't succeed to initialize and set the device credentials, there is no point to continue
Serial.println("Retrying...");
while(!Device.Connect(httpServer)) // Connect the device with the AllThingsTalk IOT developer cloud. No point to continue if we can't succeed at this
Serial.println("Retrying");
Device.AddAsset(gasId, "Gas sensor (MQ2)", "gas", false, "number"); // Create the sensor asset for your device
delay(1000); // Give the WiFi some time to finish everything
while(!Device.Subscribe(mqttServer, callback)) // Make sure that we can receive message from the AllThingsTalk IOT developer cloud (MQTT). This stops the http connection
Serial.println("Retrying");
pinMode(gasId, INPUT); // Initialize the digital pin as an input.
Serial.println("Gas sensor is ready for use!");
}
void loop() {
if(digitalRead(switchPin) == HIGH){ //This is for the door. when 2 magnets doesn't toush each other then is the door closed.
digitalWrite(ledPin4, LOW);
}
else{
digitalWrite(ledPin4, HIGH);
}
bool sensorVal = true;
float voltage = 5.0;
pinsInit();
int sensorValue = analogRead(0); //if the sensor says:'its day', a led wil turn on for 2 seconds. we don't have a motor at the moment so we use a led for exampel.
Rsensor=(float)(1023-sensorValue)*10/sensorValue;
if((b == 0) && (Rsensor>thresholdvalue))
{
b = 1;
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
if((b == 1) && (Rsensor<=thresholdvalue))
{
b = 0;
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
}
Serial.println("the analog read data is ");
Serial.println(sensorValue);
Serial.println("the sensor resistance is ");
Serial.println(Rsensor,DEC);//show the light intensity on the serial monitor;
/////////////////////////////////////////////////////////////////////////////////////////////
delay(1000);
buttonState = digitalRead(buttonPin);
if(isPeopleDetected() && buttonState == HIGH )//if it detects the moving people and the button is pushed in.
turnOnLED();
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
{
float sensor_volt;
float RS_air; // Get the value of RS via in a clear air
float R0; // Get the value of R0 via in H2
float sensorValue = 0.0;
for(int x = 0 ; x < 100 ; x++) //this is the gas sensor system. its just measurement and place its on smartliving.
{
sensorValue = sensorValue + analogRead(A1);
}
sensorValue = sensorValue/100.0;
sensor_volt = sensorValue/1024*voltage;
RS_air = (voltage-sensor_volt)/sensor_volt; // omit *RL
R0 = RS_air/6.5; // The ratio of RS/R0 is 6.5 in a clear air from Graph (Found using WebPlotDigitizer)
Serial.print("sensor_volt = ");
Serial.print(sensor_volt);
Serial.println("V");
Serial.print("R0 = ");
Serial.println(R0);
delay(1000);
Device.Send(String(R0), gasId);
Device.Process();
}
}