120/240 Particle Wifi Switch And Sensor
About the project
After researching the Sonoff WiFi switch, I decided to build my own switch box mountable 120V input IoT switch.
Project info
Difficulty: Difficult
Platforms: Particle
Estimated time: 7 days
License: Creative Commons Attribution-ShareAlike CC BY-SA version 4.0 or later (CC BY-SA 4+)
Items used in this project
Story
**Important Note**
Before starting this project you should be aware that you are hooking 120/240 volts AC to your shield. These voltages could be deadly and or cause a fire if you overload the relay or solder it wrong. You can also be seriously hurt if you don’t use proper work and safety ethic. Do not work on the shield while it is plugged into power. You should only have the power plug plugged in if it is inside a plastic or insulated box and ready to be tested and/or completed. TEST THE SHIELD BEFORE YOU PLUG IT IN using a voltmeter to ensure that 5 volts DC is the output on the VIN pin of the shield. Be very careful not to come into contact with the high voltage side of the shield. As you can see from the included images, I used silicone to isolate the power pins on the underside of the shield. Hot glue would work as well.
*********************************************************************
Particle 120V switch PCB from PCBWay Rear showing status LEDs and sensorsThis is the board soldered up and ready for installation.
Soldered and powered by 120VParticle Code
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
#define LTCADDR1 0x69//Table 1 both LOW (7bit address)Feed
#define LTCADDR2 0x67//Table 1 both LOW (7bit address)Circuit1
#define LTCADDR3 0x6f//Table 1 both LOW (7bit address)Circuit2
#define sda D0
#define scl D1
#define Relay D4
#define COMOk D3
#define CTOn D2
#define MXC6255ADDR 0010101
//================================================================
//Acceleromiter
//================================================================
byte XOUTMSB,XOUTLSB,YOUTMSB,YOUTLSB;
unsigned int SHTH, SHC, ORC;
float XMOT, YMOT, STAT;
//================================================================
//Voltage_Sensor
//================================================================
byte ADCvinMSB, ADCvinLSB, curSenseMSB, curSenseLSB, AinVMSB, AinVLSB;
unsigned int ADCvin, ADCcur, AinV;
float inputVoltage1, current10, current1, current0p1, current0p01;
int set_port = 1;
int incomingByte = 0;
const long interval = 10000;
unsigned long previousMillis = 0;
LEDStatus blinkRed(COMOk, LED_PATTERN_BLINK);
void setup() {
Serial.begin(9600);
waitFor(Serial.isConnected, 30000);
Serial.println("Adafruit_MPL3115A2 test!");
while(! baro.begin()) {
Serial.println("Couldnt find sensor");
delay(1000);
Particle.function("ON", SwitchOn);
pinMode(D4, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D2, OUTPUT);
Serial.println(WiFi.localIP());
blinkRed.setActive(true);
delay(3000);
blinkRed.setActive(false);
}
}
void loop() {
char message[120];
char data1[256];
int sensorValue;
int status;
int8_t result;
char message1[120];
int ADCvoltage;
if (millis() - previousMillis >= (interval )) {
float pascals = baro.getPressure();
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
float altm = baro.getAltitude();
Serial.print(altm); Serial.println(" meters");
float tempC = baro.getTemperature();
Serial.print(tempC); Serial.println("*C");
sprintf(message, "Temp %f",tempC);
Particle.publish("Temperature", message, PRIVATE);
//=======================================================================
Wire.beginTransmission(MXC6255ADDR);//first get Input Voltage - 80V max
Wire.write(0x1E);
Wire.endTransmission(false);
Wire.requestFrom(MXC6255ADDR, 2, true);
delay(1);
XOUTMSB = Wire.read();
XOUTLSB = Wire.read();
SHC = ((unsigned int)(XOUTMSB) << 4) + ((XOUTLSB >> 4) & 0x7F);//formats into 12bit integer
XMOT = SHC;
Wire.beginTransmission(MXC6255ADDR);//get ADC Input 2V max
Wire.write(0x28);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR3, 2, true);
delay(1);
YOUTMSB = Wire.read();
YOUTLSB = Wire.read();
SHTH = ((unsigned int)(YOUTMSB) << 4) + ((YOUTLSB >> 4) & 0x7F);//12 bit format
YMOT = SHTH;
//=======================================================================
Wire.beginTransmission(LTCADDR1);//first get Input Voltage - 80V max
Wire.write(0x1E);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR1, 2, true);
delay(1);
ADCvinMSB = Wire.read();
ADCvinLSB = Wire.read();
ADCvin = ((unsigned int)(ADCvinMSB) << 4) + ((ADCvinLSB >> 4) & 0x0F);//formats into 12bit integer
inputVoltage1 = ADCvin * 0.025; //25mV resolution
Wire.beginTransmission(LTCADDR1);//get ADC Input 2V max
Wire.write(0x28);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR3, 2, true);
delay(1);
AinVMSB = Wire.read();
AinVLSB = Wire.read();
AinV = ((unsigned int)(AinVMSB) << 4) + ((AinVLSB >> 4) & 0x0F);//12 bit format
ADCvoltage = AinV * 0.5E-3; //500uV resolution
Wire.beginTransmission(LTCADDR1);//get sense current
Wire.write(0x14);
Wire.endTransmission(false);
Wire.requestFrom(LTCADDR1, 2, true);
delay(1);
curSenseMSB = Wire.read();
curSenseLSB = Wire.read();
ADCcur = ((unsigned int)(curSenseMSB) << 4) + ((curSenseLSB >> 4) & 0x0F);//12 bit format
current10 = ADCcur * (25E-3) / 10.0; //10mA max, unit is mA
current1 = ADCcur * (25E-3) / 1.0; //100mA max, unit is mA
current0p1 = ADCcur * (25E-3) / 0.1; //1A max, unit is mA
current0p01 = ADCcur * (25E-6) / 0.01;//10A max, unit is A
sprintf(message, "RC %f:RV %f",inputVoltage1,current0p01);
Particle.publish("String", message, PRIVATE);
if(inputVoltage1 < 5 ) {
digitalWrite(D2 ,HIGH);
Particle.publish("CT_Active", "1",PRIVATE);
sprintf(message, "IP %f",WiFi.localIP());
Particle.publish("IP_Address", message, PRIVATE);
sprintf(message, "XMOT %f:YMOT %f",XMOT,YMOT);
Particle.publish("String", message, PRIVATE);
}
//========================================================================
previousMillis = millis();
}
}
int SwitchOn(String command) {
if(command =="1") {
digitalWrite(D4 ,HIGH);
Particle.publish("Switch_On", "1",PRIVATE);
}
else if(command =="2") {
digitalWrite(D4,LOW);
Particle.publish("Switch_Off", "0",PRIVATE);
}
return 1;
}
Node-RED and the App
Leave your feedback...