SimpleCloudButton.ino 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. ArduinoCloudThing Cloud Button
  3. Simple cloud button example that use WiFi101 client and display on the
  4. arduino cloud dashboard the value of button.
  5. Arduino Cloud -> https://cloud.arduino.cc/cloud
  6. IMPORTANT: in order to establish the tls connection is necessary to add
  7. the arduino.cc SSL cerificate to your board, look at
  8. https://github.com/arduino-libraries/WiFi101-FirmwareUpdater#to-update-ssl-certificates
  9. created May 2016
  10. by Gilberto Conti and Sandeep Mistry
  11. */
  12. #include <WiFi101.h>
  13. #include <ArduinoCloud.h>
  14. /////// Wifi Settings ///////
  15. char ssid[] = "";
  16. char pass[] = "";
  17. // Arduino Cloud settings and credentials
  18. const char userName[] = "";
  19. const char thingName[] = "";
  20. const char thingId[] = "";
  21. const char thingPsw[] = "";
  22. WiFiSSLClient sslClient;
  23. // build a new object "cloudObject"
  24. ArduinoCloudThing cloudObject;
  25. const int buttonPin = 6;
  26. void setup() {
  27. // configure the button pin as input
  28. pinMode(buttonPin, INPUT);
  29. Serial.begin (9600);
  30. delay(1000);
  31. // attempt to connect to WiFi network:
  32. Serial.print("Attempting to connect to WPA SSID: ");
  33. Serial.println(ssid);
  34. while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
  35. // unsuccessful, retry in 4 seconds
  36. Serial.print("failed ... ");
  37. delay(4000);
  38. Serial.print("retrying ... ");
  39. }
  40. // setup the "cloudObject"
  41. cloudObject.enableDebug(); // eneble the serial debug output
  42. cloudObject.begin(thingName, userName, thingId, thingPsw, sslClient);
  43. // define the properties
  44. cloudObject.addProperty("position", STATUS , R);
  45. }
  46. void loop() {
  47. // subscribes to RW properties and look at the connections status
  48. cloudObject.poll();
  49. // read the button
  50. if (digitalRead(buttonPin) == HIGH) {
  51. // button is pressed, write position as "on"
  52. cloudObject.writeProperty("position", "on");
  53. } else {
  54. // button is released, write position as "off"
  55. cloudObject.writeProperty("position", "off");
  56. }
  57. delay(1000);
  58. }