Created
August 11, 2019 17:23
-
-
Save DeanSupertramp/3a29d3e18077d984140fa399cc0364c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFiClient.h> | |
#include <ESP8266WiFi.h> | |
#include <MQTT.h> | |
#include <PubSubClient.h> | |
#include<FastLED.h> | |
#define NUM_LEDS 64 | |
#define DATA_PIN 2 | |
CRGB leds[NUM_LEDS]; | |
int colore[3]; | |
int set = 6; | |
#define SERIAL_SPEED 9600 //Definisco la velocità della seriale | |
#define WIFI_SSID "xxx" //Definisco l'SSID della Wifi | |
#define WIFI_PASSWD "xxx" //Definisco la password della Wifi | |
#define MAX_WIFI_INIT_RETRY 10 | |
#define WIFI_RETRY_DELAY 500 | |
//----MQTT CONFIG ---- | |
#define MQTT_CLIENT_ID "ARDUINO" //Definisco il nome del sub | |
#define MQTT_SERVER "xxx" //Definisco l'indirizzo del broker server MQTT. | |
#define MQTT_BROKER_PORT 1883 //Porta del broker MQTT in ascolto (il valore 1883 è uno standard) | |
#define MQTT_TOPIC "temp" //Definisco il topic, che dovrà essere uguale tra pub e sub per comunicare | |
#define ESP_NAME "ESP-MATRIX" //Definisco il nome del modulino wifi | |
#define ESP_SUB_ROLE //Imposto il ruolo del modulino wifi come sub | |
WiFiClient wifi_client; //Crea un client in grado di connettersi ad un specifico indirizzo Internet IP e porta, come definito nella client.connect (). | |
PubSubClient mqtt_client(wifi_client, MQTT_SERVER, MQTT_BROKER_PORT); | |
bool mqtt_status; //Variabile booleana rappresentativa dello stato della connessione | |
// ---- Inizializzo il Wifi ---- | |
int WiFi_init() | |
{ | |
const char* wifi_ssid = WIFI_SSID; | |
const char* wifi_passwd = WIFI_PASSWD; | |
int retries = 0; //tentativi | |
Serial.println("Connessione all'AP WiFi.........."); | |
WiFi.mode(WIFI_STA); //(dalla libreria esp8266wifi) Definisco il modo tra WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF. | |
WiFi.begin(wifi_ssid, wifi_passwd); //Avvio la connessione Wifi verso l'AP | |
//verificare lo stato della connessione WiFi da WL_CONNECTED | |
while ((WiFi.status() != WL_CONNECTED) && (retries < MAX_WIFI_INIT_RETRY)) { | |
retries++; | |
delay(WIFI_RETRY_DELAY); | |
Serial.println("#"); | |
} | |
Serial.println(String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3])); | |
return WiFi.status(); | |
} | |
//Questa funzione callback viene invocata per ogni messaggio ricevuto | |
void mqtt_callback(const MQTT::Publish& pub) // Lo scopo è quello di informare l'applicazione superiore delle attività che si verificano | |
{ | |
Serial.println("Ricevuto:"); | |
set = pub.payload_string().toInt(); //prende il char sulla posizione che indico | |
} | |
int MQTT_init(boolean topic_subscribe) | |
{ | |
Serial.println("Avvio della comunicazione MQTT........."); | |
mqtt_client.set_callback(mqtt_callback); //callback impostato sui messaggi ricevuti | |
mqtt_client.set_max_retries(255); | |
if (mqtt_client.connect(MQTT::Connect(MQTT_CLIENT_ID).set_keepalive(90))) { //Ci colleghiamo al broker MQTT e aumentiamo il keepalive per una maggiore affidabilità | |
Serial.println("Connessione al broker MQTT: SUCCESS .........."); | |
if (topic_subscribe) { | |
if (mqtt_client.subscribe(MQTT_TOPIC)) { | |
Serial.println("Sottoscrizione al topic MQTT [" + String(MQTT_TOPIC) + "] SUCCESS........."); | |
} else { | |
Serial.println("Impossibile sottoscrivere al topic MQTT [" + String(MQTT_TOPIC) + "] ERROR........."); | |
mqtt_client.disconnect(); | |
return false; | |
} | |
} | |
} else { | |
Serial.println("Connessione al broker MQTT ERROR.........."); | |
} | |
return mqtt_client.connected(); | |
} | |
void setup() { | |
Serial.begin(SERIAL_SPEED); | |
delay(100); | |
Serial.println("Avvio"); | |
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 0; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
Serial.println(); | |
Serial.println("Avvio MQTT...."); | |
while (true) { | |
if (WiFi_init() != WL_CONNECTED) { //Verifico la connessione | |
Serial.println("Connessione WiFi ERROR...."); | |
} else { | |
break; | |
} | |
} | |
Serial.println("Connessione WiFi OK...."); | |
while (true) { | |
#ifdef ESP_SUB_ROLE | |
mqtt_status = MQTT_init(true); //Se l'ESP è definito come Subscriber, si sottoscriverà | |
#endif | |
if (!mqtt_status) { | |
Serial.println("Connessione MQTT ERROR...."); | |
} | |
else { | |
break; | |
} | |
} | |
} | |
void loop() { | |
if (mqtt_status) { | |
#ifdef ESP_SUB_ROLE | |
switch (set) { | |
case 0: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 255; | |
colore[1] = 255; | |
colore[2] = 255; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 1: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 0; | |
colore[2] = 255; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 2: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 150; | |
colore[2] = 255; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 3: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 255; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 4: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 255; | |
colore[1] = 255; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 5: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 255; | |
colore[1] = 0; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
break; | |
case 6: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 0; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
Serial.println("In attesa"); | |
break; | |
default: | |
for (int Led = 0; Led < NUM_LEDS; Led = Led + 1) { | |
colore[0] = 0; | |
colore[1] = 0; | |
colore[2] = 0; | |
leds[Led].setRGB( colore[0], colore[1], colore[2]); | |
FastLED.show(); | |
} | |
Serial.print("Out of case"); | |
break; | |
} | |
mqtt_client.loop(); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment