Advertisement
signorettae

hassio

Jun 10th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include<Hassio.h>
  2. #include <Fishino.h>
  3. #include <SPI.h>
  4. #include <PubSubClient.h>
  5. #include <FishinoDallasTemperature.h>
  6. #include <OneWire.h>
  7. #define ONE_WIRE_BUS 9 //DS18B20 pin
  8. double temperatura;
  9. String switch1;
  10. String strTopic;
  11. String strPayload;
  12.  
  13.  
  14.  
  15. char* deviceId  = "Fishino";
  16. #define mqtt_user "fishino"
  17. #define mqtt_password "fishino"// Name of the sensor
  18. char* stateTopic = "sensor/temperature";
  19. char* bin = "luce";// MQTT topic where values are published
  20. int sensorPin = A0; // Pin to which the sensor is connected to
  21. char buf[4]; // Buffer to store the sensor value
  22. int updateInterval = 1000; // Interval in milliseconds
  23.  
  24. // MQTT server settings
  25. IPAddress mqttServer(192, 168, 1, 90);
  26. int mqttPort = 1883;
  27. #define mqtt_user "fishino"
  28. #define mqtt_password "fishino"
  29.  
  30. OneWire oneWire(ONE_WIRE_BUS);//Create the oneWire object. Creo l'oggetto oneWire
  31. DallasTemperature sensors(&oneWire);//Create the sensors object. Creo l'oggetto sensors
  32.  
  33. //////////////////////////////////////////////////////////////////////////////////////////
  34. //////////////////////////////////////////////////////////////////////////////////////////
  35. // CONFIGURATION DATA       -- ADAPT TO YOUR NETWORK !!!
  36. // DATI DI CONFIGURAZIONE   -- ADATTARE ALLA PROPRIA RETE WiFi !!!
  37. #ifndef __MY_NETWORK_H
  38.  
  39. // here pur SSID of your network
  40. // inserire qui lo SSID della rete WiFi
  41. #define MY_SSID "TIM-Signoretta"
  42.  
  43. // here put PASSWORD of your network. Use "" if none
  44. // inserire qui la PASSWORD della rete WiFi -- Usare "" se la rete non │ protetta
  45. #define MY_PASS "Signoretta63649700"
  46.  
  47. // here put required IP address (and maybe gateway and netmask!) of your Fishino
  48. // comment out this lines if you want AUTO IP (dhcp)
  49. // NOTE : if you use auto IP you must find it somehow !
  50. // inserire qui l'IP desiderato ed eventualmente gateway e netmask per il fishino
  51. // commentare le linee sotto se si vuole l'IP automatico
  52. // nota : se si utilizza l'IP automatico, occorre un metodo per trovarlo !
  53. #define IPADDR  192, 168,   1, 101
  54. #define GATEWAY 192, 168,   1, 1
  55. #define NETMASK 255, 255, 255, 0
  56.  
  57. #endif
  58. //                    END OF CONFIGURATION DATA                      //
  59. //                       FINE CONFIGURAZIONE                         //
  60. ///////////////////////////////////////////////////////////////////////
  61.  
  62. // define ip address if required
  63. // NOTE : if your network is not of type 255.255.255.0 or your gateway is not xx.xx.xx.1
  64. // you should set also both netmask and gateway
  65. #ifdef IPADDR
  66.     IPAddress ip(IPADDR);
  67.     #ifdef GATEWAY
  68.         IPAddress gw(GATEWAY);
  69.     #else
  70.         IPAddress gw(ip[0], ip[1], ip[2], 1);
  71.     #endif
  72.     #ifdef NETMASK
  73.         IPAddress nm(NETMASK);
  74.     #else
  75.         IPAddress nm(255, 255, 255, 0);
  76.     #endif
  77. #endif
  78.  
  79. // Initialize the Fishino client library
  80. FishinoClient Fclient;
  81. PubSubClient client(Fclient);
  82.  
  83. void reconnect() {
  84.   while (!client.connected()) {
  85.  
  86.     Serial.print("Attempting MQTT connection...");
  87.  
  88.    if (client.connect(deviceId,mqtt_user,mqtt_password)) {
  89.    // if (client.connect(deviceId)) {  
  90.     Serial.println("connected");
  91.  client.subscribe("switch1/set");
  92.     } else {
  93.  
  94.       Serial.print("failed, rc=");
  95.       Serial.print(client.state());
  96.       Serial.println(" try again in 5 seconds");
  97.  
  98.       delay(5000);
  99.     }
  100.   }
  101. }
  102. void callback(char* topic, byte* payload, unsigned int length) {
  103.   payload[length] = '\0';
  104.   strTopic = String((char*)topic);
  105.   if(strTopic == "switch1/set")
  106.     {
  107.     switch1 = String((char*)payload);
  108.     if(switch1 == "ON")
  109.       {
  110.         Serial.println("ON");
  111.         digitalWrite(6, HIGH);
  112.         delay(50);
  113.            digitalWrite(6, LOW);
  114.        
  115.  
  116.        
  117.        
  118.       }
  119.     else
  120.       {
  121.         Serial.println("OFF");
  122.                 digitalWrite(6, HIGH);
  123.         delay(50);
  124.            digitalWrite(6, LOW);
  125.       }
  126.     }
  127. }
  128.  
  129.  
  130. void setup()
  131. {
  132.     // Initialize serial and wait for port to open
  133.     // Inizializza la porta seriale e ne attende l'apertura
  134.     Serial.begin(115200);
  135.    
  136.     // only for Leonardo needed
  137.     // necessario solo per la Leonardo
  138. /*  while (!Serial)
  139.         ;
  140. */
  141.  
  142.  sensors.begin();
  143.  
  144.   sensors.setWaitForConversion(false);
  145.  
  146.   sensors.setResolution(12);
  147.  
  148.   sensors.requestTemperatures();
  149. pinMode(6, OUTPUT);
  150. pinMode(5, INPUT);
  151.  
  152.     // initialize SPI
  153.     // inizializza il modulo SPI
  154. //  SPI.begin();
  155. //  SPI.setClockDivider(SPI_CLOCK_DIV2);
  156.    
  157.     // reset and test WiFi module
  158.     // resetta e testa il modulo WiFi
  159.     while(!Fishino.reset())
  160.         Serial << F("Fishino RESET FAILED, RETRYING...\n");
  161.     Serial << F("Fishino WiFi RESET OK\n");
  162.    
  163.     Fishino.setPhyMode(PHY_MODE_11N);
  164.  
  165.     // go into station mode
  166.     // imposta la modalità stazione
  167.     Fishino.setMode(STATION_MODE);
  168.  
  169.     // try forever to connect to AP
  170.     // tenta la connessione finchè non riesce
  171.     Serial << F("Connecting to AP...");
  172.     while(!Fishino.begin(MY_SSID, MY_PASS))
  173.     {
  174.         Serial << ".";
  175.         delay(2000);
  176.     }
  177.     Serial << "OK\n";
  178.    
  179.    
  180.     // setup IP or start DHCP client
  181.     // imposta l'IP statico oppure avvia il client DHCP
  182. #ifdef IPADDR
  183.     Fishino.config(ip, gw, nm);
  184. #else
  185.     Fishino.staStartDHCP();
  186. #endif
  187.  
  188.     // wait till connection is established
  189.     Serial << F("Waiting for IP...");
  190.     while(Fishino.status() != STATION_GOT_IP)
  191.     {
  192.         Serial << ".";
  193.         delay(500);
  194.     }
  195.     Serial << "OK\n";
  196.  digitalWrite(6,LOW);
  197.  
  198.  
  199. client.setServer(mqttServer, mqttPort);
  200.   client.setCallback(callback);
  201. }
  202.  
  203.  
  204.  
  205. void loop() {
  206.    sensors.requestTemperatures();
  207.   temperatura = sensors.getTempCByIndex(0);
  208.   if (!client.connected()) {
  209.     reconnect();
  210.   }
  211.   client.loop();
  212.  
  213.  int sensorValue = analogRead(sensorPin);
  214.  
  215.  
  216.   Serial.print("Sensor value: ");
  217.   Serial.println(temperatura);
  218.  
  219.   client.publish(stateTopic, itoa(temperatura, buf, 10));
  220.   if (digitalRead(5) == HIGH){
  221.    Serial.println("Stato ON");
  222.   client.publish(bin, "ON", true);}
  223.  else  if (digitalRead(5) == LOW){
  224.      Serial.println("Stato OFF");
  225.   client.publish(bin, "OFF", true);
  226.  }
  227. delay(2000);
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement