Hello , So This is a follow up from another thread since it went more into programmin on thingspeak I have created a thread more focused on this ...
My goal is :
I have a proximity sensor HC-SR501 and a nodemcu 8266 , what I want to do is to send data there so that I can recognize when there is activity presence ... so I need at least one variable signalling the activity everytime, this should be ok with the high state I guess... I have a led popping up everytime this happens going low when activity is over, which is controlled by the second variable motionstate.
I wanted to add a third variable for spawning an activity inside another program , I want this asctivity to be launched at the first time there is sensor activity , and I want it to last for about a minute or two after this time the activity in this other program needs to disappear and so needs the arduino get a signal from that program with a write function to set the third variable spawn to 0 ... once this is done if a new activity is detected there is again a spawn set to 1 and so on , but for all the time that I do not receive a signal from the external program the spwan should remain one and not be updated or at least not send me continuously spwans to the external program .
Is this the right way to program it ? Or is better another way ? I am not sure if I am doing this well ...
Here is the preliminary code :
#include "ThingSpeak.h" // Include ThingSpeak Library
#include <ESP8266WiFi.h>
//Replace wifi credentials here
Analytics Cloud platform.
const char* ssid = "*******************";
const char* password = "*******************";
unsigned long myChannelNumber = *******************; //change channel number here
const char *server = "api.thingspeak.com"; //Initializing Server.
const char * apiKey = "*******************"; // Enter your ThingSpeak API Key
int LED = 12 ; // Choose the pin for the LED (Pin D6, GPIO 13)
int sensor = 13; // Choose the input pin for motion sensor (Pin D7, GPIO 14) // Choose the input pin for motion sensor (Pin D6, GPIO 12)
int motionState = 0; // start, assuming no motion detected
int val = 0; // Variable for reading the pin status
int spawn = 0; // Variable to spawn
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
unsigned long resetMillis = 0;
const long interval = 90000; // Time delay interval (90 seconds) to prevent false triggers when setting up device
int status = WL_IDLE_STATUS;
WiFiClient client;
// Program written in setup will run only once for connecting to Wi-Fi network.
void setup() {
WiFi.begin(ssid, password); // Start //WiFi connection
ThingSpeak.begin(client); // Start ThingSpeak connection
Serial.begin(115200); // Serial Baud Rate
delay(500);
pinMode(sensor,INPUT); // declare sensor as input
pinMode (LED,OUTPUT); // Declares LED As output
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
Serial.println("Motion Sensor presence");
ThingSpeak.setField( (unsigned int) 1, (int) val ) ; // store field 1
ThingSpeak.setField( (unsigned int) 2, (int) spawn ) ; // store field 2
ThingSpeak.writeFields(myChannelNumber, apiKey); // write all fields to the channel and reset stored fields
}
void loop (){
//********************* Read motion sensor and upload to ThingSpeak *****************************
currentMillis = millis(); // Set the currentMillis equal to the millis function
val = digitalRead(sensor);
long state = digitalRead (sensor);
if (val == HIGH) {
if ((motionState == 0) && (currentMillis >= interval))
{
digitalWrite(LED, HIGH); // Turn LED ON
Serial.println("Motion detected!"); // Motion Detected
motionState = 1;
ThingSpeak.setField( (unsigned int) 1, (int) val ) ; // store field 1
// ThingSpeak.setField( (unsigned int) 2, (int) motionState ) ; // store field 2
ThingSpeak.writeFields(myChannelNumber, apiKey); // write all fields to the channel and reset stored fields
Serial.println("Data Sent to ThingSpeak!");
delay(500);
}
}
else
{
digitalWrite(LED, LOW); // Turn LED OFF
if (motionState == 1)
{ // Motion stopped
Serial.println("Motion ended!");
motionState = 0;
ThingSpeak.setField( (unsigned int) 1, (int) val ) ; // store field 1
//ThingSpeak.setField( (unsigned int) 2, (int) motionState ) ; // store field 2
ThingSpeak.writeFields(myChannelNumber, apiKey); // write all fields to the channel and reset stored fields
Serial.println("Data Sent to ThingSpeak!");
delay(500);
}
}
if (currentMillis - previousMillis >= 10000) // Update Serial Monitor every ten seconds (10000 milliseonds)
{ // as to how long the program has been running
Serial.print("Program running for ");
Serial.print((currentMillis/1000)/60);
Serial.println(" Minutes");
previousMillis = currentMillis;
yield(); // Yield to prevent the WDT from crashing the NodeMCU
}
if ((val == HIGH) && (spawn==0) && (currentMillis - previousMillis >= interval) )
{
spawn = 1;
ThingSpeak.setField( (unsigned int) 2, (int) spawn ) ; // store field 2
ThingSpeak.writeFields(myChannelNumber, apiKey); // write all fields to the channel and reset stored fields
Serial.println("Data Sent to ThingSpeak!");
delay(interval);
yield();
}
}
