Advertisement
Guest User

nodemcu

a guest
Jul 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1.  
  2. /************************ Adafruit IO Configuration *******************************/
  3.  
  4. // visit io.adafruit.com if you need to create an account,
  5. // or if you need your Adafruit IO key.
  6. #define IO_USERNAME "yayanoktaviani"
  7. #define IO_KEY "8f00030d8dd84d70928134dfd2009113"
  8.  
  9. /******************************* WIFI Configuration **************************************/
  10.  
  11. #define WIFI_SSID "smarthome"
  12. #define WIFI_PASS "smarthomeproject"
  13.  
  14. #include "AdafruitIO_WiFi.h"
  15. AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
  16.  
  17. /************************ Main Program Starts Here *******************************/
  18. #include <ESP8266WiFi.h>
  19. #include <AdafruitIO.h>
  20. #include <Adafruit_MQTT.h>
  21. #include <ArduinoHttpClient.h>
  22.  
  23. #define RELAY1 0
  24.  
  25. // button state
  26. int current = 0;
  27. int last = 0;
  28.  
  29. // set up the 'command' feed
  30. AdafruitIO_Feed *command = io.feed("lights");
  31.  
  32. void setup() {
  33.  
  34. pinMode(RELAY1, OUTPUT);
  35.  
  36. // start the serial connection
  37. Serial.begin(115200);
  38.  
  39. // connect to io.adafruit.com
  40. Serial.print("Connecting to Adafruit IO");
  41. io.connect();
  42.  
  43. command->onMessage(handleMessage);
  44.  
  45. // wait for a connection
  46. while(io.status() < AIO_CONNECTED) {
  47. Serial.print(".");
  48. delay(500);
  49. }
  50.  
  51. // we are connected
  52. Serial.println();
  53. Serial.println(io.statusText());
  54. digitalWrite(RELAY1, LOW);
  55. }
  56.  
  57. void loop() {
  58. io.run();
  59.  
  60.  
  61. }
  62.  
  63. void handleMessage(AdafruitIO_Data *data) {
  64.  
  65. int command = data->toInt();
  66.  
  67. if (command == 1){ //light up the LED
  68. Serial.print("received <- ");
  69. Serial.println(command);
  70. digitalWrite(RELAY1, HIGH);
  71. } else {
  72. Serial.print("received <- ");
  73. Serial.println(command);
  74. digitalWrite(RELAY1, LOW);
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement