tobeedelafuente

uv-index-notifier.ino

May 25th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <RestClient.h>
  2.  
  3. #define INTERVAL 5000
  4. #define SENSOR A0
  5.  
  6. RestClient client = RestClient("maker.ifttt.com");
  7. String response;
  8. int uvIndex = -1;
  9.  
  10. unsigned long previous = 0;
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14.   pinMode(SENSOR, INPUT);
  15.  
  16.   client.begin("MyWiFi", "qwerty123");
  17.   client.setContentType("application/json");
  18. }
  19.  
  20. void loop() {
  21.   if (millis() - previous >= INTERVAL) {
  22.     previous = millis();
  23.  
  24.     String message;
  25.     int sensorValue;
  26.     int analogValue = analogRead(SENSOR);
  27.    
  28.     if (analogValue < 20) {
  29.       sensorValue = 0;
  30.     } else {
  31.       sensorValue = (0.05 * analogValue) - 1;
  32.     }
  33.  
  34.     if (uvIndex != sensorValue) {
  35.       uvIndex = sensorValue;
  36.  
  37.       if (uvIndex < 3) {
  38.         message = "It's the best time to go out on the beach! If you burn easily, put some sunscreen!";
  39.       } else if (uvIndex > 2 && uvIndex < 6) {
  40.         message = "It's okay to go out on the beach but be sure to put some sunscreen!";
  41.       } else if (uvIndex > 5 && uvIndex < 8) {
  42.         message = "Don't risk it! Go out if it's 4PM onwards!";
  43.       } else if (uvIndex > 7) {
  44.         message = "You don't want to go to the beach right now. Just don't";
  45.       }
  46.  
  47.       postDataToIfttt(message);
  48.     }
  49.   }
  50. }
  51.  
  52. void postDataToIfttt(String data) {
  53.   response = "";
  54.   String postData = "{ \"value1\" : \"" + data + "\"," + " \"value2\" : \"\", \"value3\" : \"\" }";
  55.   Serial.println(postData);
  56.   int statusCode = client.post("/trigger/event/with/key/your_maker_key", postData.c_str(), &response);
  57.   Serial.print("Status code from server: ");
  58.   Serial.println(statusCode);
  59.   Serial.print("Response body from server: ");
  60.   Serial.println(response);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment