Advertisement
cvalente

Ground - ping on change

Aug 21st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. /*******************************************************************************
  2.  *  GROUND code
  3.  *  looks for substancial change from sensors
  4.  *
  5.  *  Publishes: ping
  6.  *  Subscribes: NA
  7.  *
  8.  *  Setters:
  9.  *  - setBrightness(String val)
  10.  *  - setSpeed(String val)
  11.  *  Convention for setters, return 1 if successful, -1 otherwise
  12.  *
  13.  *  Published Variables:
  14.  *  - int lastPing // i think its better deleting? check possible functionality
  15.  *
  16.  *  Extension:
  17.  *  - wifi status
  18.  *  - runtime and battery status
  19.  *  Built for Photon, no dependecies
  20.  *
  21.  *******************************************************************************/
  22.  
  23. /*  
  24.  *  Every refresh the arduino looks for substancial
  25.  *  change in any pin since last
  26.  *  Should investigate sleep function to save photon battery
  27.  *  https://github.com/spark/firmware/issues/655
  28.  *
  29.  *  You get also a battery status report and last time since ping
  30.  *  http://community.particle.io/t/name-resolving-of-tcpclient-when-connecting-to-other-photon-running-mdns/24843/2
  31.  *  http://community.particle.io/t/sample-code-for-connecting-2-photons-wifi-direct/21386/7
  32.  *  https://community.particle.io/t/real-time-communication-between-two-photons-on-internet/15053/5
  33.  */
  34.  
  35.  #define LED_PIN D7 // onboard LED pin
  36.  #define WAIT 1000
  37.  #define WAIT2 500
  38.  
  39.  
  40. int pins[]   = {A0, A1, A2, A3};
  41. int numInput = sizeof(pins) - 1;//  sizeof(myPins) / sizeof(myPins[0]); // check this
  42. int lastSum  = -1;
  43.  
  44. int thresh    = 1000; // amount of difference to tolerate;
  45. bool ignore   = false; // ignore one cycle
  46.  
  47. SYSTEM_MODE(AUTOMATIC);
  48. STARTUP(WiFi.selectAntenna(ANT_AUTO)); // continually switches at high speed between antennas
  49.  
  50. void setup() {
  51.  
  52.  
  53.   /* Initialize analog ins */
  54.  for (int i = 0; i < numInput; i++) {
  55.     pinMode(pins[i], INPUT);
  56.  }
  57.  
  58.  /* Initialize led pin */
  59.  pinMode(LED_PIN, OUTPUT);
  60.  pinResetFast(LED_PIN);
  61.  
  62.  /* Turn off on board LED */
  63.  RGB.control(true);
  64.  RGB.color(0, 0, 0);
  65.    
  66.  Particle.variable("LDRsum", &lastSum, INT);
  67.  // https://api.particle.io/v1/devices/1b0022001947353236343033/LDRsum?access_token=e9ff4e5ec3b85e66f7e50e1c0e2fa606d0722c02
  68.  
  69.  Particle.function("setThresh", newThresh); // sets a new threshold
  70.  
  71. }
  72.  
  73. void loop() {
  74.    
  75.     int sum = 0;
  76.     if (!ignore) {
  77.         for (int i = 0; i < 6; i++){
  78.             int myVal = analogRead(pins[i]);
  79.             sum += myVal;
  80.         }
  81.    
  82.         int diff  = abs(lastSum - sum);
  83.         if (diff > thresh) {
  84.             char publishString[4];
  85.             sprintf(publishString,"%u", diff);
  86.             // how do i make it private
  87.             Particle.publish( "ping", publishString, 60, PRIVATE );  // event name, data, TTL, status
  88.             ignore = true;
  89.         }
  90.         lastSum = sum;
  91.         delay(WAIT2);
  92.     }
  93.     ignore = false;
  94.     delay(WAIT);
  95.    
  96. }
  97.  
  98. int newThresh(String command) {
  99.     /* Particle.functions always take a string as an argument and return an integer. */
  100.     // sets new threshold value
  101.     thresh = command.toInt();
  102.     return 1;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement