Jade Perreault
Published

Outdoor Solar, Voltage and Light Sensing

Building a "Solar Power measuring Junction Box" with solar charging a 12 VDC Battery light sensing and Temperature Sensing going to MQTT.

AdvancedWork in progress4 hours2,174
Outdoor Solar, Voltage and Light Sensing

Things used in this project

Hardware components

Photon
Particle Photon
×1
Junction Box
Any Weatherproof Junction Box Will Do
×1
Particle Screw Shield
×1
DrokLM2596 Power converter
×1

Software apps and online services

OpenHab
Mosquitto

Hand tools and fabrication machines

Drill Taps
OpenBuilds Drill Taps

Story

Read more

Custom parts and enclosures

Junction Box with Battery Compartment

I just used an old junction box with a batter compartment ..I also built and installed a 120 volt AC to 12 volt DC power converter and also installed with (Gorilla Two sided tape) a voltage converter to convert 12VDC to 5 VDC ..I also installed DIN Rail for clip in Connectors ..and of course a plexiglass back plane for mounting the Particle and particle screw Shield

Schematics

Basic Wiring

Voltage Input ,DHT22 Input Light Sensor Input

Code

Particle Code

C/C++
This Code is all Accessible on the Particle.io Web IDE
#include "MQTT/MQTT.h"
#include "Adafruit_DHT/Adafruit_DHT.h"

#include "ThingSpeak/ThingSpeak.h"
#include "application.h"

//**MQTT**//

void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192,168,1,117 };
MQTT client(server, 1883, callback);

//**********//

float input_voltage = 0.0;
float temp = 0.0;

#define DHTPIN 5
#define DHTTYPE DHT22
const int ledPin1 = D6 ;
const int ledPin2 = D7 ;
const int ledPin3 = D3 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;
const int analogInPin3 = A2;

//*************//

int sensorValue = 0;
int outputValue = 0;

int sensorValue2 = 0;
int outputValue2 = 0;

int sensorValue3 = 0;
int sensorValue4 = 0;

//int sensorValue5 = 0;
//int sensorValue6 = 0;

//int sensorValue7 = 0;
//int sensorValue8 = 0;

int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;

unsigned long previousMillis = 0;

DHT dht(DHTPIN, DHTTYPE);

/* Thingspeak */

//TCPClient client;
//unsigned long myChannelNumber = 207753;
//const char * myWriteAPIKey = "OMQ9B2I3MPS1H604";

//************//

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
}

//**LCD Setup**//





//****//


void setup() {
    
    Serial.begin(9600);
    
    //**MQTT//

    client.connect("OPENHAB Client");

    if (client.isConnected()) {
        client.publish("/outTopic","hello world");
        client.subscribe("/inTopic");
    }
    
    //**LED's**//
    
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    
    //**LCD**//
    
    
    //**DHT**//
    
    Serial.println("DHTxx test!");
    dht.begin();


}

void loop() {
    
    //***//
    
char message[56];
    
    //***Voltage Sensor***//
 {   
    int analog_value = analogRead(A0);
    input_voltage = (analog_value * 3.3) / 4095;

    Serial.print("V=");
    Serial.print(input_voltage);
    delay(10000);

 //**MQTT SEND VALUES//
 
    sprintf(message, " %d " , input_voltage );
    client.publish("mymosquitto/Particle2/VoltageLVL", message);
    String data = String (10);
    Particle.publish("Voltage Level", message, PUBLIC);
    delay(10000);
    
     if (sensorValue < 12) {
        digitalWrite (ledPin2, HIGH);
        digitalWrite (ledPin1, LOW);
        Particle.publish("Voltage HIGH", PUBLIC);


        delay(10000);
    }
    else if (sensorValue > 10) {
        digitalWrite (ledPin1, HIGH);
        digitalWrite (ledPin2, LOW);
        Particle.publish( "Voltage LOW", PUBLIC);

        delay(10000);
    }
    
 }

    //******************//
    
    if (client.isConnected())
        client.loop();
        
     {   //**MQTT SEND VALUES//
        
    int analog_value = analogRead(A1);
    input_voltage = (analog_value * 3.3) / 4095;

    sensorValue2 = analogRead(analogInPin2);
    outputValue2 = map(sensorValue, 0, 4095, 0, 255);
    Serial.println(sensorValue2);
    sprintf(message, " %d ", sensorValue2);
    client.publish("mymosquitto/Particle2/LightLVL", message);
    Particle.publish("Light Level", message, PUBLIC);
}
    //******************//    
    
     if (sensorValue2 < 1400) {

        Particle.publish( "Light levels low");


        delay(10000);
    }
    
    else if (sensorValue2 > 1400) {

        Particle.publish("Light Levels High");

        delay(10000);
    }
   

    if (client.isConnected())
    Serial.println("connected");
        client.loop();
        
        
        //**DHT**//

	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	
	delay(10000);
	
//**MQTT SEND VALUES//	
	sensorValue3 = (t);
	sprintf(message, " %d ", sensorValue3);
	Particle.publish("OutDoor Temperature", message, PRIVATE);
	client.publish("mymosquitto/Particle2/Temp", message);
	sensorValue4 = (h);
	sprintf(message, " %d ", sensorValue4);
	Particle.publish("OutDoor Humididty" , message ,PRIVATE);
	client.publish("mymosquitto/Particle2/Hum", message);
	
	delay(10000);
	//*****//
	
	Serial.println(Time.timeStr());
	delay(10000);

}

Openhab Code(Linux)

Liquid
Various code snippets from Openhab.org if you copy and past it should work for you ..If not I can walk you through it ..You can also check out http://www.homeautomationforgeeks.com
Linux Programming From LX Terminal

Sitemap File

Text label="Outdoor Junction Box" icon="shield-1" {
Text item=OutdoorTemp
Text item=OutdoorHum
Text item=Light Period=W

Items File

//Groups
MyHome (All)

//MQTT//

//My House
Number OutdoorTemp "Outdoor Temperature[%.2f C]" <temperature> (MyHouse)  {mqtt="<[mymosquitto:mymosquitto/Particle2/Temp:state:default]"}

Number OutdoorHum "Outdoor Humidity[%.2f %%]" <water> (MyHouse) {mqtt="<[mymosquitto:mymosquitto/Particle2/Hum:state:default]"}
Number Battery "Battery Level[%.2f VDC]" <solarplant> (MyHouse) {mqtt="<[mymosquitto:mymosquitto/Particle2/VoltageLVL:state:default]"}
Number Light "Sun Light[%.2f %%]" <sun_clouds> (MyHouse) {mqtt="<[mymosquitto:mymosquitto/Particle2/LightLVL:state:default]"}


OpenHAB.cfg file
#r connections here for use in the MQTT Binding or MQTTPersistence bundles. Replace <broker> with a id you choose.e MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883

mqtt:mymosquitto.url=tcp://localhost:1883

Particle Code (Thingsspeak)

C/C++
Particle.io IDE Code
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"
#include "ThingSpeak/ThingSpeak.h"
#include "Adafruit_DHT/Adafruit_DHT.h"
TCPClient client;
//**********//

float input_voltage = 0.0;
float temp = 0.0;

#define DHTPIN 0
#define DHTTYPE DHT22
const int ledPin1 = D6 ;
const int ledPin2 = D7 ;
const int ledPin3 = D3 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;
const int analogInPin3 = A2;

//*************//

int sensorValue = 0;
int outputValue = 0;

int sensorValue1 = 0;
int outputValue1 = 0;

int sensorValue3 = 0;
int sensorValue4 = 0;

//int sensorValue5 = 0;
//int sensorValue6 = 0;

//int sensorValue7 = 0;
//int sensorValue8 = 0;

int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;

unsigned long previousMillis = 0;

DHT dht(DHTPIN, DHTTYPE);

unsigned long myChannelNumber = YOUR CHANNEL NUMBER HERE;
const char * myWriteAPIKey = "YOUR APIKEY HERE";

void setup() {
    
        //**LED's**//
    
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    
    //**LCD**//
    analogRead(A0);
    
    //**DHT**//
    ThingSpeak.begin(client);
    dht.begin();
    


    // Give time for the message to reach ThingSpeak
    delay(5000);

}

void loop() {
    
    char message[56];
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
}

// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();
  Serial.println();
	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% ");
	Serial.println();
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");

	
sensorValue3 = (t);
sprintf(message, " %d ", sensorValue3);
Particle.publish("Temperature", message, PRIVATE);
sensorValue4 = (h);
sprintf(message, " %d ", sensorValue4);
Particle.publish("Humidity", message, PRIVATE);

    
   // Read Sensor
    double temperature = dht.getTempCelcius();
    double humidity = dht.getHumidity();

    // Update the 2 ThingSpeak fields with the new data
    
    ThingSpeak.setField(1, (float)temperature);
    ThingSpeak.setField(2, (float)humidity);  
    

  //**************//
  float pinVoltage = analogRead(A0) * (3.3 / 4095.0);
  ThingSpeak.setField(3,pinVoltage*100);
  pinVoltage = analogRead(A1) * (3.3 / 4095.0);
  ThingSpeak.setField(4,pinVoltage*11);
  pinVoltage = analogRead(A2) * (3.3 / 4095.0);
//  ThingSpeak.setField(3,pinVoltage);
//  pinVoltage = digitalRead(D0) * (3.3 / 4095.0);
//  ThingSpeak.setField(4,pinVoltage);
//  pinVoltage = digitalRead(D0) * (3.3 / 4095.0);
//  ThingSpeak.setField(5,pinVoltage);
//  pinVoltage = analogRead(A5) * (3.3 / 4095.0);
//  ThingSpeak.setField(6,pinVoltage);
//  pinVoltage = analogRead(A6) * (3.3 / 4095.0);
//  ThingSpeak.setField(7,pinVoltage);
//  pinVoltage = analogRead(A7) * (3.3 / 4095.0);
//  ThingSpeak.setField(8,pinVoltage);
  //*************//
  
   delay(20000);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  
  
}

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