Guest User

Untitled

a guest
Aug 20th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. extern "C" {
  4. #include "user_interface.h"
  5. }
  6. #include <ESP8266WiFi.h>
  7. #include <PubSubClient.h>
  8. #include <Adafruit_NeoPixel.h>
  9. #ifdef __AVR__
  10. #include <avr/power.h>
  11. #endif
  12. #define PIN 14
  13.  
  14. Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
  15.  
  16. //////// Network stuff ///////////////////////////////////////
  17.  
  18. const char* ssid = "my_ssid";
  19. const char* password = "my_wifi_password";
  20. const char* mqtt_server = "my_mqtt_server_address";
  21.  
  22. WiFiClient espClient;
  23. PubSubClient client(espClient);
  24. long lastMsg = 0;
  25. char msg[50];
  26. int value = 0;
  27. int r;
  28. int g;
  29. int b;
  30.  
  31.  
  32. /////////// Wifi Setup section //////////////////////////////////////
  33.  
  34. void setup_wifi() {
  35.  
  36. delay(10);
  37. // We start by connecting to a WiFi network
  38. Serial.println();
  39. Serial.print("Connecting to ");
  40. Serial.println(ssid);
  41. WiFi.begin(ssid, password);
  42. while (WiFi.status() != WL_CONNECTED) {
  43. delay(500);
  44. Serial.print(".");
  45. }
  46. Serial.println("");
  47. Serial.println("WiFi connected");
  48. Serial.println("IP address: ");
  49. Serial.println(WiFi.localIP());
  50. }
  51.  
  52. ////////////// Color Wipe /////////
  53.  
  54. void colorWipe(uint32_t c, uint8_t wait) {
  55. for(uint16_t i=0; i<strip.numPixels(); i++) {
  56. strip.setPixelColor(i, c);
  57. strip.show();
  58. delay(wait);
  59. }
  60. }
  61.  
  62. //////////////////// Callback ////////////////////////////
  63.  
  64. void callback(char* topic, byte* payload, unsigned int length) {
  65.  
  66. String receivedtopic = topic;
  67. String receivedpayload ;
  68.  
  69. for (int i = 0; i < length; i++) {
  70. receivedpayload += (char)payload[i];
  71. }
  72.  
  73. Serial.print(receivedtopic);
  74. Serial.print(" => ");
  75. Serial.println(receivedpayload);
  76. int myvalue;
  77.  
  78. /* THIS LINE TOOK AGES TO SORT (BELOW)
  79. I HAS TO CHANGE THE STRING (receivedpayload) into an int)
  80. */
  81.  
  82. myvalue = (receivedpayload.toInt());
  83. Serial.print(myvalue);
  84. if(myvalue < 10)
  85. {
  86. r = 25;
  87. g = 0;
  88. b = 0;
  89. }
  90. else if(myvalue < 25)
  91. {
  92. b = map(myvalue, 10, 25, 0, 25);
  93. r = map(myvalue, 10, 25, 25,25);
  94. g = map(myvalue, 10, 25, 0, 25);
  95. }
  96. else if(myvalue > 25)
  97. {
  98. g = 25;
  99. r = 25;
  100. b = 25;
  101. }
  102. colorWipe(strip.Color(r, g, b), 10);
  103. }
  104.  
  105. //////////////////// Setup /////////////////////////
  106.  
  107. void setup() {
  108. Serial.begin(115200);
  109. setup_wifi();
  110. client.setServer(mqtt_server, 1883);
  111. client.setCallback(callback);
  112. strip.begin();
  113. strip.show(); // Initialize all pixels to 'off'
  114. }
  115.  
  116. ////////////////////// Reconnect /////////////////////////////
  117.  
  118. void reconnect() {
  119. // Loop until we're reconnected
  120. while (!client.connected()) {
  121. Serial.print("Attempting MQTT connection...");
  122. // Attempt to connect
  123. if (client.connect("ESP12_DOWNLOAD", "mqtt_username", "mqtt_password")) {
  124. Serial.println("connected");
  125. client.subscribe("internet/download");
  126. // client.subscribe("temp1");
  127. } else {
  128. Serial.print("failed, rc=");
  129. Serial.print(client.state());
  130. Serial.println(" try again in 5 seconds");
  131. // Wait 5 seconds before retrying
  132. delay(5000);
  133. }
  134. }
  135. }
  136.  
  137. /////////////////////// Void loop ///////////////////////////
  138.  
  139. void loop() {
  140.  
  141. if (!client.connected()) {
  142. reconnect();
  143. }
  144. client.loop();
  145. /*
  146. long now = millis();
  147. if (now - lastMsg > 20000) {
  148. lastMsg = now;
  149. ++value;
  150. snprintf (msg, 75, "hello world #%ld", value);
  151. Serial.print("Publish message: ");
  152. Serial.println(msg);
  153. client.publish("outTopic", msg);
  154. }
  155. */
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment