Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- extern "C" {
- #include "user_interface.h"
- }
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define PIN 14
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, PIN, NEO_GRB + NEO_KHZ800);
- //////// Network stuff ///////////////////////////////////////
- const char* ssid = "my_ssid";
- const char* password = "my_wifi_password";
- const char* mqtt_server = "my_mqtt_server_address";
- WiFiClient espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- int value = 0;
- int r;
- int g;
- int b;
- /////////// Wifi Setup section //////////////////////////////////////
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- ////////////// Color Wipe /////////
- void colorWipe(uint32_t c, uint8_t wait) {
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- delay(wait);
- }
- }
- //////////////////// Callback ////////////////////////////
- void callback(char* topic, byte* payload, unsigned int length) {
- String receivedtopic = topic;
- String receivedpayload ;
- for (int i = 0; i < length; i++) {
- receivedpayload += (char)payload[i];
- }
- Serial.print(receivedtopic);
- Serial.print(" => ");
- Serial.println(receivedpayload);
- int myvalue;
- /* THIS LINE TOOK AGES TO SORT (BELOW)
- I HAS TO CHANGE THE STRING (receivedpayload) into an int)
- */
- myvalue = (receivedpayload.toInt());
- Serial.print(myvalue);
- if(myvalue < 10)
- {
- r = 25;
- g = 0;
- b = 0;
- }
- else if(myvalue < 25)
- {
- b = map(myvalue, 10, 25, 0, 25);
- r = map(myvalue, 10, 25, 25,25);
- g = map(myvalue, 10, 25, 0, 25);
- }
- else if(myvalue > 25)
- {
- g = 25;
- r = 25;
- b = 25;
- }
- colorWipe(strip.Color(r, g, b), 10);
- }
- //////////////////// Setup /////////////////////////
- void setup() {
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- }
- ////////////////////// Reconnect /////////////////////////////
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP12_DOWNLOAD", "mqtt_username", "mqtt_password")) {
- Serial.println("connected");
- client.subscribe("internet/download");
- // client.subscribe("temp1");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- /////////////////////// Void loop ///////////////////////////
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- /*
- long now = millis();
- if (now - lastMsg > 20000) {
- lastMsg = now;
- ++value;
- snprintf (msg, 75, "hello world #%ld", value);
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("outTopic", msg);
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment