Advertisement
briprice78

IFTTT_Button

Apr 5th, 2020
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.  * ZOOM RECORDING : https://psu.zoom.us/rec/share/3dFYE7PXtT5JS5X3-ULtcIkYBp6meaa80SRK_PULnRozXKrivk44EH1D91u5EL_C
  4.  * Adapt code so that every time you simulate a button press on Arduino, it reads the values from its onboard accelerometer
  5.  * Send these parameters to an IFTTT maker webhook
  6.  * Event name "accel_readings"
  7.  * Webhook should trigger an email service
  8.  *
  9.  */
  10.  
  11. #include <SPI.h>
  12. #include <WiFiNINA.h>
  13. #include <Arduino_LSM6DS3.h>
  14. #include "arduino_secrets.h"
  15.  
  16. const int BUTTON_PIN = 2;  //  digital pin D2 (near ground pin)
  17.  
  18. char ssid[] = SECRET_SSID; //  your network SSID (name)
  19. char pass[] = SECRET_PSW;//  your network PASSWORD ()
  20. String apiKey = SECRET_APIKEY;
  21.  
  22. int press_counter = 0;
  23. int status = WL_IDLE_STATUS;
  24. char server_name[] = "maker.ifttt.com";  // first 3 parts of URL
  25.  
  26. WiFiClient client;  //  client object
  27.  
  28. void setup() {
  29.   //Initialize serial and wait for port to open:
  30.   Serial.begin(9600);
  31.  
  32.   pinMode(BUTTON_PIN, INPUT_PULLUP);  //  when nothing connected, pulls up to 3.3V - very noisy without it
  33.  
  34.  
  35.   // attempt to connect to Wifi network:
  36.   while (status != WL_CONNECTED) {
  37.     Serial.print("Attempting to connect to SSID: ");
  38.     Serial.println(ssid);
  39.  
  40.     //use the line below if your network is protected by wpa password
  41.     status = WiFi.begin(ssid, pass);
  42.  
  43.     // wait 5 seconds for connection:
  44.     delay(5000);
  45.     Serial.println(status);
  46.   }
  47.   Serial.println("Connected to WiFi!");
  48.  
  49.  
  50.   //  attempt to connect accelerometer
  51.   if (!IMU.begin()) {
  52.       Serial.println("Failed to initialize IMU!");
  53.       while (1);
  54.     }
  55.     else {
  56.       Serial.println("Connected to Accelerometer!");
  57.     }
  58.  
  59. }
  60.  
  61. void loop() {
  62.  
  63.   /*if(digitalRead(BUTTON_PIN)==LOW){  //  checks to see if button pin is low (ground)
  64.     press_counter++;  //  increments
  65.     Serial.println("Button pressed!");
  66.    
  67.     String additional_info = "?value1=";
  68.     additional_info += press_counter;
  69.    
  70.     sendIFTTT("button_pressed", additional_info);
  71.   */
  72.  
  73.     //  declare x, y, z axis variables
  74.     float x, y, z;
  75.  
  76.     if (IMU.accelerationAvailable()) {
  77.       IMU.readAcceleration(x, y, z);  //  read values for float variables
  78.  
  79.       if(digitalRead(BUTTON_PIN)==LOW) {
  80.         delay(1000);  // delay loop by 1 second
  81.         Serial.println("Button pressed!");
  82.        
  83.         //if button simulated send values for x y z
  84.         String x_axis = "?value1=";
  85.         x_axis += x;
  86.         String y_axis = "?value2=";
  87.         y_axis += y;        
  88.         String z_axis = "?value3=";
  89.         z_axis += z;
  90.  
  91.         sendIFTTT("accel_readings", x_axis, y_axis, z_axis);
  92.        
  93.       }
  94.     }
  95. }
  96.  //  String x_axis, String y_axis, String z_axis
  97.  //  x_axis + y_axis + z_axis
  98. void sendIFTTT(String accel_readings, String x_axis, String y_axis, String z_axis) {  //  passing in two parameters
  99.  
  100.   Serial.println("Starting connection to server...");
  101.   // if you get a connection, report back via serial:
  102.   if (client.connect(server_name, 80)) {    //  80 = standard http port
  103.     Serial.println("Connected to server");
  104.    
  105.     // Make a HTTP GET request:
  106.     client.print(String("GET ") + "/trigger/" + accel_readings + "/with/key/" + apiKey + x_axis + y_axis + z_axis);  //  this string much match http string from webhook application Documentation
  107.     client.println(" HTTP/1.1");    //  type of http request
  108.     client.println("Host: maker.ifttt.com");
  109.     client.println("Connection: close");
  110.     client.println(); //It is incredibly important to have the blank println statement after
  111.  
  112.     //close request
  113.     delay(500);
  114.     client.flush();
  115.     client.stop();
  116.   }
  117.  
  118.   //  if unable to connect
  119.   else {
  120.     Serial.println("unable to connect");
  121.   }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement