ReadAndWrite.ino 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. ArduinoCloudThing ReadAndWrite
  3. Example that explaines the use of read and write properties and how to
  4. manage a property of others devices.
  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 ledPin = 6;
  26. void setup() {
  27. Serial.begin (9600);
  28. // attempt to connect to WiFi network:
  29. Serial.print("Attempting to connect to WPA SSID: ");
  30. Serial.println(ssid);
  31. while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
  32. // unsuccessful, retry in 4 seconds
  33. Serial.print("failed ... ");
  34. delay(4000);
  35. Serial.print("retrying ... ");
  36. }
  37. // setup the "cloudObject"
  38. cloudObject.enableDebug(); // eneble the serial debug output
  39. cloudObject.begin(thingName, userName, thingId, thingPsw, sslClient);
  40. // define the properties
  41. cloudObject.addProperty("bulb", STATUS , RW);
  42. cloudObject.addExternalProperty("lampSwitch", "position", STATUS); // this property is owned by "lampSwitch" object
  43. }
  44. void loop() {
  45. // subscribes to RW properties and look at the connections status
  46. cloudObject.poll();
  47. // read the lamp switch "position", update "bulb" property accordingly
  48. if (cloudObject.readProperty("lampSwitch", "position") == "on") {
  49. cloudObject.writeProperty("bulb", "on");
  50. } else {
  51. cloudObject.writeProperty("bulb", "off");
  52. }
  53. // read the "bulb" property, update the LED accordingly
  54. if (cloudObject.readProperty("bulb") == "on") {
  55. digitalWrite(ledPin, HIGH);
  56. } else {
  57. digitalWrite(ledPin, LOW);
  58. }
  59. delay(1000);
  60. }