Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Hassio.h>
- #include <Fishino.h>
- #include <SPI.h>
- #include <PubSubClient.h>
- #include <FishinoDallasTemperature.h>
- #include <OneWire.h>
- #define ONE_WIRE_BUS 9 //DS18B20 pin
- double temperatura;
- String switch1;
- String strTopic;
- String strPayload;
- char* deviceId = "Fishino";
- #define mqtt_user "fishino"
- #define mqtt_password "fishino"// Name of the sensor
- char* stateTopic = "sensor/temperature";
- char* bin = "luce";// MQTT topic where values are published
- int sensorPin = A0; // Pin to which the sensor is connected to
- char buf[4]; // Buffer to store the sensor value
- int updateInterval = 1000; // Interval in milliseconds
- // MQTT server settings
- IPAddress mqttServer(192, 168, 1, 90);
- int mqttPort = 1883;
- #define mqtt_user "fishino"
- #define mqtt_password "fishino"
- OneWire oneWire(ONE_WIRE_BUS);//Create the oneWire object. Creo l'oggetto oneWire
- DallasTemperature sensors(&oneWire);//Create the sensors object. Creo l'oggetto sensors
- //////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////
- // CONFIGURATION DATA -- ADAPT TO YOUR NETWORK !!!
- // DATI DI CONFIGURAZIONE -- ADATTARE ALLA PROPRIA RETE WiFi !!!
- #ifndef __MY_NETWORK_H
- // here pur SSID of your network
- // inserire qui lo SSID della rete WiFi
- #define MY_SSID "TIM-Signoretta"
- // here put PASSWORD of your network. Use "" if none
- // inserire qui la PASSWORD della rete WiFi -- Usare "" se la rete non │ protetta
- #define MY_PASS "Signoretta63649700"
- // here put required IP address (and maybe gateway and netmask!) of your Fishino
- // comment out this lines if you want AUTO IP (dhcp)
- // NOTE : if you use auto IP you must find it somehow !
- // inserire qui l'IP desiderato ed eventualmente gateway e netmask per il fishino
- // commentare le linee sotto se si vuole l'IP automatico
- // nota : se si utilizza l'IP automatico, occorre un metodo per trovarlo !
- #define IPADDR 192, 168, 1, 101
- #define GATEWAY 192, 168, 1, 1
- #define NETMASK 255, 255, 255, 0
- #endif
- // END OF CONFIGURATION DATA //
- // FINE CONFIGURAZIONE //
- ///////////////////////////////////////////////////////////////////////
- // define ip address if required
- // NOTE : if your network is not of type 255.255.255.0 or your gateway is not xx.xx.xx.1
- // you should set also both netmask and gateway
- #ifdef IPADDR
- IPAddress ip(IPADDR);
- #ifdef GATEWAY
- IPAddress gw(GATEWAY);
- #else
- IPAddress gw(ip[0], ip[1], ip[2], 1);
- #endif
- #ifdef NETMASK
- IPAddress nm(NETMASK);
- #else
- IPAddress nm(255, 255, 255, 0);
- #endif
- #endif
- // Initialize the Fishino client library
- FishinoClient Fclient;
- PubSubClient client(Fclient);
- void reconnect() {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect(deviceId,mqtt_user,mqtt_password)) {
- // if (client.connect(deviceId)) {
- Serial.println("connected");
- client.subscribe("switch1/set");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000);
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- payload[length] = '\0';
- strTopic = String((char*)topic);
- if(strTopic == "switch1/set")
- {
- switch1 = String((char*)payload);
- if(switch1 == "ON")
- {
- Serial.println("ON");
- digitalWrite(6, HIGH);
- delay(50);
- digitalWrite(6, LOW);
- }
- else
- {
- Serial.println("OFF");
- digitalWrite(6, HIGH);
- delay(50);
- digitalWrite(6, LOW);
- }
- }
- }
- void setup()
- {
- // Initialize serial and wait for port to open
- // Inizializza la porta seriale e ne attende l'apertura
- Serial.begin(115200);
- // only for Leonardo needed
- // necessario solo per la Leonardo
- /* while (!Serial)
- ;
- */
- sensors.begin();
- sensors.setWaitForConversion(false);
- sensors.setResolution(12);
- sensors.requestTemperatures();
- pinMode(6, OUTPUT);
- pinMode(5, INPUT);
- // initialize SPI
- // inizializza il modulo SPI
- // SPI.begin();
- // SPI.setClockDivider(SPI_CLOCK_DIV2);
- // reset and test WiFi module
- // resetta e testa il modulo WiFi
- while(!Fishino.reset())
- Serial << F("Fishino RESET FAILED, RETRYING...\n");
- Serial << F("Fishino WiFi RESET OK\n");
- Fishino.setPhyMode(PHY_MODE_11N);
- // go into station mode
- // imposta la modalità stazione
- Fishino.setMode(STATION_MODE);
- // try forever to connect to AP
- // tenta la connessione finchè non riesce
- Serial << F("Connecting to AP...");
- while(!Fishino.begin(MY_SSID, MY_PASS))
- {
- Serial << ".";
- delay(2000);
- }
- Serial << "OK\n";
- // setup IP or start DHCP client
- // imposta l'IP statico oppure avvia il client DHCP
- #ifdef IPADDR
- Fishino.config(ip, gw, nm);
- #else
- Fishino.staStartDHCP();
- #endif
- // wait till connection is established
- Serial << F("Waiting for IP...");
- while(Fishino.status() != STATION_GOT_IP)
- {
- Serial << ".";
- delay(500);
- }
- Serial << "OK\n";
- digitalWrite(6,LOW);
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- }
- void loop() {
- sensors.requestTemperatures();
- temperatura = sensors.getTempCByIndex(0);
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- int sensorValue = analogRead(sensorPin);
- Serial.print("Sensor value: ");
- Serial.println(temperatura);
- client.publish(stateTopic, itoa(temperatura, buf, 10));
- if (digitalRead(5) == HIGH){
- Serial.println("Stato ON");
- client.publish(bin, "ON", true);}
- else if (digitalRead(5) == LOW){
- Serial.println("Stato OFF");
- client.publish(bin, "OFF", true);
- }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement