WattsThat:
Classic x-y problem
Fixing the root cause is the best solution. Relays that turn on at power up or program loading is a driver circuit issue and/or faulty logic in your sketch. Since you’ve posted neither of these, there is little actual help that can be provided.
How would you fix this in this code:
/*
V0.1 Works fine on Arduino Leonardo
0 - ~1000volt voltmeter for 3.3volt Arduinos
uses the stable internal 1.1volt reference
6k8 resistor from A0 to ground, and 100k resistor from A0 to +batt
100n capacitor from A0 to ground for stable readings
(329000 + 998) / 998 = 330.6593186372745
*/
#include <stdio.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
const char* host = "192.168.10.30";
const char* apikey = "6e8465ac8dc473f4d3555e95e7eb87bf";
ESP8266WiFiMulti WiFiMulti;
#include <MCP3208.h>
#include <SPI.h>
MCP3208 adc(15);
//http://192.168.10.30/emoncms/input/post?json={temp:21,light:300}&apikey=<apikey>
float voltage = 0.000; // calculated voltage
float R1 = 330000;
float R2 = 1000;
float divider = 331; // 299.7012987;
float Vref = 4.155; // Using LM4040 reference voltage
unsigned int total; // can hold max 64 readings
int s0 = 5;
int s1 = 4;
int s2 = 0;
int s3 = 2;
int LED = D0;
void setup() {
Serial.begin(115200);
Serial.println("Voltage monitor");
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delay(10);
Serial.println("Voltage monitor: MCP3208VoltMeter V1.3 using MCP3408 and saving to Emoncms");
Serial.println("Waiting for WiFi... ");
WiFi.mode(WIFI_STA); // We start by connecting to a WiFi network
WiFiMulti.addAP("xxxxxxx", "xxxxxxx");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
adc.begin();
}
void loop() {
// MUX Connection
for (int i = 1; i < 17; i ++) {
//MCP3208 Code
// take a number of analog samples and add them up
for (int x = 0; x < 64; x++) { // multiple analogue readings for averaging
total = total + adc.analogRead(0); // add each value to a total
}
voltage = (total / 64.0) * divider * Vref / 4095 ; // convert readings to volt
// Serial.println(adc.analogRead(0));
Serial.print("Battery ");
Serial.print(i);
Serial.print(": ");
Serial.println(voltage);
Serial.print(readMux(i));
Serial.println("V");
// Serial.println();
total = 0; // reset value
delay(500); // half second between measurements
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
char Volt[15];
dtostrf(voltage, 4, 2, Volt); //float to char 4 numero de caracteres 3 cifras sin espacio
String Voltage = Volt; // char to string
char Number[15];
dtostrf(i, 1, 0, Number); //float to char 4 numero de caracteres 3 cifras sin espacio
String No = Number; // char to string
char url [ 140]; //
sprintf(url, "/emoncms/input/post.json?node=1&json={B%s:%s}&apikey=%s", Number, Volt, apikey) ;
Serial.print("Requesting URL: ");
// Serial.println(url);
// This will send the request to the server
// client.print(String("GET ") + url + " HTTP/1.1\r\n" +
// "Host: " + host + "\r\n" +
// "Connection: close\r\n\r\n");
// delay(500);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.println(line);
digitalWrite(LED, HIGH);
Serial.println("Disabling Relays");
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delay(1000);
digitalWrite(LED, LOW);
}
}
}
int readMux(int channel) {
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4] = {
{0, 0, 0, 0}, //channel 0
{1, 0, 0, 0}, //channel 1
{0, 1, 0, 0}, //channel 2
{1, 1, 0, 0}, //channel 3
{0, 0, 1, 0}, //channel 4
{1, 0, 1, 0}, //channel 5
{0, 1, 1, 0}, //channel 6
{1, 1, 1, 0}, //channel 7
{0, 0, 0, 1}, //channel 8
{1, 0, 0, 1}, //channel 9
{0, 1, 0, 1}, //channel 10
{1, 1, 0, 1}, //channel 11
{0, 0, 1, 1}, //channel 12
{1, 0, 1, 1}, //channel 13
{0, 1, 1, 1}, //channel 14
{1, 1, 1, 1} //channel 15
};
for (int i = 0; i < 4; i ++) {
digitalWrite(controlPin[i], muxChannel[channel][i]);
}
int val = analogRead(voltage);
return val;
}