├── README.md ├── DHT22Wemos.ino ├── AMS_iAQ_Core_C_MQTT.ino └── HueClone.ino /README.md: -------------------------------------------------------------------------------- 1 | # Arduino 2 | 3 | 4 | Arduino sketckes for ESP8266 with MQTT and Apple HomeKit support! 5 | The PubSubClient library which is necesarry for this project is that one: https://github.com/Imroy/pubsubclient 6 | Note that, it is NOT the same one as in Arduino IDE! 7 | -------------------------------------------------------------------------------- /DHT22Wemos.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DHT.h" 3 | #include 4 | #include 5 | 6 | 7 | const char* WIFI_SSID = "xxxxxxxxx"; // wifi ssid 8 | const char* WIFI_PWD = "xxxxxxxx"; // wifi password 9 | 10 | 11 | IPAddress MQTTserver(192, 168, 1, 92); 12 | WiFiClient wclient; 13 | PubSubClient client(wclient, MQTTserver); 14 | 15 | #define DHTPIN D1 // Wemos D1 Mini's D1 pin ....DHT PIN 2 (data) 16 | #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 17 | 18 | 19 | const int UPDATE_INTERVAL_SECS = 9; // Update every 9 second...modify if you want :-) 20 | 21 | 22 | // Initialize the temperature/ humidity sensor 23 | DHT dht(DHTPIN, DHTTYPE); 24 | 25 | float oldH ; 26 | float oldT ; 27 | 28 | 29 | void setup() { 30 | // Setup console 31 | Serial.begin(115200); 32 | delay(10); 33 | dht.begin(); 34 | WiFi.mode(WIFI_STA); 35 | WiFi.begin(WIFI_SSID, WIFI_PWD); 36 | while (WiFi.status() != WL_CONNECTED) { 37 | delay(500); 38 | Serial.print("."); 39 | } 40 | Serial.println("WiFi connected"); 41 | Serial.println("IP address: "); 42 | Serial.println(WiFi.localIP()); 43 | } 44 | 45 | 46 | void HomeKit(){ 47 | if (WiFi.status() == WL_CONNECTED) { 48 | if (!client.connected()) { 49 | if (client.connect("ESP8266: DHT Sensor")) { 50 | client.publish("HomeKit","Temperature and Humidity Sensor Online!"); 51 | } 52 | } 53 | 54 | if (client.connected()) 55 | 56 | 57 | { 58 | 59 | float h = dht.readHumidity(); 60 | float t = dht.readTemperature(); 61 | 62 | if (isnan(h) || isnan(t) ){ 63 | Serial.println("Failed to read from DHT sensor!"); 64 | return; 65 | } 66 | 67 | 68 | if (t != oldT || h != oldH ) 69 | { 70 | 71 | oldT = t; 72 | oldH = h; 73 | } 74 | 75 | 76 | 77 | Serial.println("publishing " + String(dht.readTemperature()) + "°"); 78 | client.publish("RoomTemperature",String(dht.readTemperature())); // RoomTemperature = temp topic 79 | Serial.println("publishing " + String(dht.readHumidity()) + "%"); 80 | client.publish("RoomHumidity",String(dht.readHumidity())); // RoomHumidity = humidity topic 81 | client.loop(); 82 | } 83 | 84 | } 85 | } 86 | 87 | void loop() { 88 | HomeKit(); 89 | Serial.println("Sending completed"); 90 | delay(5000); // publish delay 91 | } 92 | -------------------------------------------------------------------------------- /AMS_iAQ_Core_C_MQTT.ino: -------------------------------------------------------------------------------- 1 | //AMS iAQ Core C 2 | // http://ams.com/eng/Products/Environmental-Sensors/Air-Quality-Sensors/iAQ-core-C 3 | 4 | #include 5 | #include 6 | #include "brzo_i2c.h" // just google it! 7 | 8 | IPAddress MQTTserver(192, 168, 1, 40); // MQTT server IP address 9 | WiFiClient wclient; 10 | PubSubClient client(wclient, MQTTserver); 11 | 12 | const char* ssid = "ssid"; // Your ssid 13 | const char* pass = "password"; // Your Password 14 | const char* clientName = "ESP8266-AirQualitySensor1"; // must be unique 15 | 16 | uint8_t SCL_PIN = D1; // wemos SCL PIN 17 | uint8_t SDA_PIN = D2; // wemos SDA PIN 18 | uint8_t iaq_adr = 0x5A; 19 | 20 | 21 | uint8_t buffer[10]; 22 | 23 | uint8_t error = 0; 24 | uint16_t co2; 25 | uint16_t tvoc; 26 | 27 | void setup() 28 | { 29 | Serial.begin(115200); 30 | brzo_i2c_setup(SDA_PIN, SCL_PIN, 3000); 31 | WiFi.mode(WIFI_STA); 32 | WiFi.begin(ssid, pass); 33 | while (WiFi.status() != WL_CONNECTED) { 34 | delay(500); 35 | Serial.print("."); 36 | } 37 | Serial.println("WiFi connected"); 38 | Serial.println("IP address: "); 39 | Serial.println(WiFi.localIP()); 40 | } 41 | 42 | 43 | void readAllBytes() { 44 | brzo_i2c_start_transaction(iaq_adr, 100); 45 | brzo_i2c_read(buffer, 9, true); 46 | 47 | if (buffer[2] == 0x10 ){ 48 | Serial.println("Warming up..."); 49 | delay(1000); // ezt én írtam be 50 | } else 51 | { 52 | 53 | co2 = buffer[0] * pow(2, 8) + buffer[1]; 54 | tvoc = buffer[7] * pow(2, 8) + buffer[8]; 55 | 56 | Serial.println("CO2: " + String(co2)); 57 | Serial.println("TVOC: " + String(tvoc)); 58 | } 59 | error = brzo_i2c_end_transaction(); 60 | 61 | if (error == 0) { 62 | //Serial.println("No i2c communication errors"); 63 | } 64 | else { 65 | Serial.print("Brzo error : "); 66 | Serial.println(error); 67 | } 68 | 69 | delay(1000); 70 | } 71 | 72 | 73 | void HomeKit(){ 74 | if (WiFi.status() == WL_CONNECTED) { 75 | if (!client.connected()) { 76 | if (client.connect(clientName)) { 77 | client.publish("outTopic","Air Quality Sensor Online!"); 78 | } 79 | } 80 | 81 | if (client.connected()) 82 | 83 | { 84 | 85 | Serial.println("Publishing CO2: " + String(co2)); 86 | client.publish("airquality1/co2",String(co2)); // publishing to airquality1/co2 topic 87 | Serial.println("Publishing VOC: " + String(tvoc)); 88 | client.publish("airquality1/voc",String(tvoc)); // publishing to airquality1/voc topic 89 | client.loop(); 90 | 91 | } 92 | } 93 | } 94 | 95 | 96 | void loop() 97 | { 98 | readAllBytes(); 99 | HomeKit(); 100 | Serial.println("Sending completed"); 101 | delay(4000); // read the sensor and publish the data every 4 sec 102 | 103 | } 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /HueClone.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define PIN D8 // your ESP8266 GPIO PIN number 9 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800); // number 4 stands for the number of LEDs in your led strip. E.g. if you've a 5 meter, 60LED/M strip, it contains 300 LED 10 | 11 | const char* ssid = "...."; 12 | const char* password = "..."; 13 | const char* host = "hueclone"; // the name of your fixture, and the base channel to listen to 14 | IPAddress MQTTserver(192, 168, 1, 99); //your MQTT servers's IP address (e.g. your Raspberry Pi) 15 | 16 | /* NO NEED TO CHANGE BENEATH THIS LINE */ 17 | int hue = 0; 18 | float brightness = 0.0; 19 | float saturation = 0.0; 20 | 21 | #define BUFFER_SIZE 100 22 | 23 | WiFiClient wclient; 24 | PubSubClient client(wclient, MQTTserver); 25 | 26 | 27 | void callback(const MQTT::Publish& pub) { 28 | 29 | uint16_t i, j; 30 | 31 | currentValues(); 32 | String myMessage = String(pub.payload_string()); 33 | // handle message arrived 34 | Serial.print(pub.topic()); 35 | Serial.print(" => "); 36 | String myTopic = String(pub.topic()); 37 | 38 | 39 | if(myTopic == host) 40 | { 41 | 42 | Serial.println(pub.payload_string()); 43 | 44 | if(pub.payload_string() == "on") 45 | { 46 | 47 | // use this to reset parameters if you want them to always come on bright white. 48 | //hue = 0; 49 | brightness = 1.0; 50 | //saturation = 0.0; 51 | 52 | for(i=0; i