// This is the final MQTT version of the 433Mhz remote control sketch. // 433 Mhz code /* Example for different sending methods https://github.com/sui77/rc-switch/ */ #include RCSwitch mySwitch = RCSwitch(); //end 433 Mhz code // Variables used for the remote switches. int state; int switchstatus; // DHT sensor libraries #include "DHT.h" // including the library of DHT11 temperature and humidity sensor #define DHTTYPE DHT11 // DHT 11 #define VIRTUAL_CHANNEL 1 #define ACTUATOR_PIN 15 #define dht_dpin 0 DHT dht(dht_dpin, DHTTYPE); //int state; //#define CAYENNE_DEBUG #define CAYENNE_PRINT Serial #include unsigned long lastMillis = 0; // WiFi network info. char ssid[] = "myssid"; char wifiPassword[] = "wifipassword "; // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. char username[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; char password[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"; char clientID[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"; void setup(void) { // 433 Mc Transmitter is connected to Wemos D1 Port 2 (which is D9 on theboard) for nodemcu is port D5 on the board an 14 for enabletransmit mySwitch.setProtocol(5); mySwitch.enableTransmit(14); switchstatus = 0; pinMode(ACTUATOR_PIN, OUTPUT); dht.begin(); Serial.begin(9600); Cayenne.begin(username, password, clientID, ssid, wifiPassword); } void loop() { Cayenne.loop(); if (millis() - lastMillis > 1000) { lastMillis = millis(); // Added a dht 11 sensor to read temperature and humidity float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.print("Current humidity = "); Serial.print(h); Serial.print("% "); Serial.print("temperature = "); Serial.print(t); Serial.println("C "); // Logging for remote switch debugging purposes Serial.print("switchstatus "); Serial.println(switchstatus); Serial.print("state "); Serial.println(state); Cayenne.celsiusWrite(0, t); Cayenne.celsiusWrite(8, h); } } CAYENNE_CONNECTED() { CAYENNE_LOG("CAYENNE_CONNECTED"); } CAYENNE_DISCONNECTED() { CAYENNE_LOG("CAYENNE_DISCONNECTED"); } // This function will be called every time our Dashboard actuator widget writes a value to Virtual Pin 1. // Alternatively, we could have put this code into a CAYENNE_IN_DEFAULT() function and used logic to detect // whether the command was on virtual channel 1. CAYENNE_IN(1) { state = getValue.asInt(); //CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value); digitalWrite(ACTUATOR_PIN, state); Serial.print("State: "); Serial.println(state); //Check switch status if it is 1 and then activate the remote switch //Only one activation event will do, the switchstatus variable will take care of that. if( state == 1 and switchstatus == 0){ mySwitch.send(329041, 24); } // Below it sets the switchstatus to 1 the in the first cylce after the button was pressed. (will be reversed if the button is set to off) if( state == 1 and switchstatus == 0) switchstatus =1; delay (200); //Check switch status if it is 0 and de-activate the remote switch //Only one de-activation event will do, the switchstatus variable will take care of that. if( state == 0 and switchstatus == 1){ mySwitch.send(329044, 24); } if( state == 0 and switchstatus == 1) switchstatus =0; delay (200); }