Post topics, source code that relate to the Arduino Platform

User avatar
By torntrousers
#25222 Thats terrific Barnabybear. Can't wait to give it a go. And it could be really handy for other things too, eg just a simple momentary on switch that you press and the ESP powers up and does some stuff and then shuts itself down but you only have to press the switch briefly not hold it down till the ESP work is done.
User avatar
By Barnabybear
#25233 Hi torntrousers (or anyone who can help), I've bastardised this code together (mostly from places I cant remember now, and never intended to publish, so sorry if I havent given credit, if its yours let me know & I'll credit). Hopefully still just on topic, if not please move.
It is a PIR that reports to thingSpeak when the detector is activated and again when it resets, with a 20 second delay so as not to breach the thingSpeak "can only be updated every 15 seconds rule".
Have you any thoughts as to how I would incorparate our current findings into this code?
I only expect 8 activations a day, 'enter' activate-> high (send), check 20 sec later or untill -> low (send), sleep for 30 to 60 mins, 'leave' activate -> high (send), check 20 sec later or untill-> low (send), sleep for about 4 hours, repeat. This code currently works.
Lots of notes, mostly by me, just me working out what all the bits do.
Code: Select all 
#include <ESP8266WiFi.h>
 
// replace with your channel's thingspeak API key,
// thingSpeak can be found at https://thingspeak.com/
String apiKey = "<api_key>";
// replace with your router's SSID,
const char* ssid = "<SSID>";
// replace with you router's password,
const char* password = "<password>";
 
const char* server = "api.thingspeak.com"; // dont know what this does?
int pirPin = 2; // the pin you are detecting on
int pir = 0;  // set the value of pir to 0

WiFiClient client;
   
 void setup() { 
  pinMode(pirPin, INPUT); // sets pirPin to input         
  Serial.begin(115200);  // starts serial monitor
  delay(10);
 
  WiFi.begin(ssid, password); // starts WiFi
 
  Serial.println(); // prints to serial monitor a blank line
  Serial.print("Connecting to "); //prints to serial monitor "Connecting to"
  Serial.println(ssid); // prints to serial monitor the station name connecting to
   
  WiFi.begin(ssid, password); // just noticed this is in twice, works with it in, havent got time to look at it now
   
  while (WiFi.status() != WL_CONNECTED) { // checks WiFi is connected but only partly understand
    delay(500);
    Serial.print("."); // prints to serial monitor dots whilst waiting for connect
  }
  Serial.println(""); // prints to serial monitor a blank line
  Serial.println("WiFi connected"); // prints to serial monitor "WiFi connected"
  Serial.println(WiFi.localIP()); // prints to serial monitor ip address of ESP8266
 
}
 
 void loop() {{
  if(digitalRead(pirPin) == (pir)) // read pirPin and compare to value of pir (the last state sent to thingSpeak)
  //if the same just carry out the 2 lines below and return to read pirPin again
  //if not miss 2 lines below and continue at 3rd line & send new value to thingSpeak
    {Serial.print("x"); // print to serial monitor "x" just to show its working
    delay(500);} // wait 500ms then check pirPin again
  else // what to do if the state has changed from the last send to thingSpeak
   { pir = digitalRead(pirPin); // update the value of pir
    Serial.println(pir); // print the value of pir to serial monitor
    Serial.println("Waiting to send."); //print to serial monitor "Waiting to send"
    delay(20000); // wait 20 sec (as thingSpeak can only be updated every 15 max)
   
       //stuff that I dont totaly understand but it sends the value of pir to thinkSpeak & works
      if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
        String postStr = apiKey;  // api key for update is added to the message sent to thingSpeak
               postStr +="&field1="; //  which field to update is added to the message sent to thingSpeak
               postStr += String(pir); // where pir is added to the message sent to thingSpeak
               postStr += "\r\n\r\n"; // endy stuff is added to the message sent to thingSpeak
     
         client.print("POST /update HTTP/1.1\n");
         client.print("Host: api.thingspeak.com\n");
         client.print("Connection: close\n");
         client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
         client.print("Content-Type: application/x-www-form-urlencoded\n");
         client.print("Content-Length: ");
         client.print(postStr.length());
         client.print("\n\n");
         client.print(postStr);
               
     
         Serial.print("Sensor Status: "); // prints to serial monitor "Sensor Status: "
         Serial.print(pir); // prints to serial monitor the value of pir
         Serial.println(" sent to thingSpeak"); // prints to serial monitor "sent to thingSpeak"
      }
      client.stop();
 
   }}}

Any help appreciated, I'm slowly getting this coding thing (or not), at 50 any help is appreciated (anyone remember putting graphite marks on cards and sending them off to you nearest computer & getting the printout back the following week?).
User avatar
By tytower
#25257 Yes .I used to code bank statements with a 3 digit code for type of expense /asset/income /whatever in the late 60's and send them away to be input to a computer for a printout a few days later . Cost a lot back then !

Just tied up on code elsewhere atm but have a look at the wiki (scroll up) in grey bar above at the bottom of the first page is a sketch on Thingspeak and Sparkfun with a deepSleep() operation to close it down . You might try incorporating it and seeing if what has been suggested can be done .

I'll come back in a few days after I get my stuff done and help if still needed but it looks like you have it going the right way.