Jade Perreault
Published © CC BY-SA

120/240 Particle WiFi Switch and Sensor

After researching the Sonoff WiFi switch, I decided to build my own switch box mountable 120V input IoT switch.

AdvancedWork in progress5 hours946
120/240 Particle WiFi Switch and Sensor

Things used in this project

Story

Read more

Schematics

Particle Gerber FIle

Fritzing File

Code

Particle Code

C/C++
// 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;
}

Credits

Jade Perreault

Jade Perreault

32 projects • 59 followers
27 Years as a Journeyman Electrician , 4 Years as a PCB design engineer for Penteon Corporation Out of New York ..

Comments