rare output bij 6 relay schakel sketch

Projecten die niet passen in bovenstaande onderwerpen
Berichten: 1
Geregistreerd: 01 Sep 2016, 19:09

rare output bij 6 relay schakel sketch

Berichtdoor ctg » 01 Sep 2016, 19:38

Hallo allemaal

Voor een projectje zijn wij bezig om 6 relays aan en uit te schakelen door een waardestring van 6 cijfers van 1 0f 0 (bijvoorbeeld 110100) van een serverpagina uit te lezen.
maar zodra er een netwerk disconnect of packet error plaats vind, word de waarde van de string overschreven met 111111 terwijl er dan geen commando of functie wordt gebruikt. ?
we snappen er helemaal niets meer van ? wie kan ons helpen dit probleem te vinden ??

groeten daniel

board: arduino uno
ethcard wiz 5100

relay1 = digital pin 2
relay2 = digital pin 3
relay3 = digital pin 5
relay4 = digital pin 6
relay5 = digital pin 7
relay6 = digital pin 8

resetrelay = digital pin 9


ENGLISH ( When the network connection fails (either cable disconnect or packet-error) the outputs are overwritten.
While there is no command or function for changing the output high or low.

In the LOOP, 'read_current' and 'read_last' was created to filter network failures.


the code :



// !!!!???? comments on Function init_pins > RESET [Switch]
//
//-----------------------------------------------------------------------//
Power switch (RELAY_6)
//-----------------------------------------------------------------------//
#include <SPI.h>
#include <Ethernet.h>
// ResponseTimeout (Ethernet.h/Dhcp.h) (beginWithDHCP 60000 >changed> 8000)
// http://forum.arduino.cc/index.php?topic=105397.0

#include <EEPROM.h>
#include <TrueRandom.h>
#include <SoftwareSerial.h>

//-RESET--------------------//
int resetfailcounter = 0; // Network reset counter
const int resetfailcount = 3; // (resetpin) # fail counter

//-DELAYS-------------------//
const int delay_startup = 1000;
const int delay_loop = 1000;
const int delay_connection = 10;

//-----------------------------------------------------------------------//
//-DEFINITIONS-----------------------------------------------------------//
//-----------------------------------------------------------------------//
// !! Undefinable pins 4(XCK), 10(SS), 0(RS232), 1(RS232)
// POST / GET DATA FORMAT - // http://blog.protoneer.co.nz/arduino-http-post-requests/
//-PINS---------------------//
const int rsx_alarm_pin = 14;
//
const int rsx_pin[] = {-1, 2, 3, 5, 6, 7, 8}; // output pin definitions [1..6] (0 is not defined thus -1)
const int rsx_pins = 6;
const int resetpin = 9; // reset pin connection
//
const int rsx_pins_port = rsx_pins + 1;
boolean rsx_port[rsx_pins_port];

//-SERIAL-------------------//
const int serialbaudrate = 9600;

//-NETWORK------------------//
// EEPROM MAP
//--------------------------//
// 01 mac-address is configured? (0x23 == #) is configured.
// 02 mac+0 Byte // 03 mac+1 Byte // 04 mac+2 Byte // 05 mac+3 Byte // 06 mac+4 Byte // 07 mac+5 Byte
//--------------------------//
byte mac[] = { 0x02, 0x00, 0x17, 0x01, 0xE0, 0x01 }; // mac-addr, required
int eeprom_mac[] = {2, 3, 4, 5, 6, 7}; // mac-addres eeprom
byte ip[] = { 10, 4, 20, 16}; // switch6
byte server[] = { 10, 4, 20, 1};
int server_port = 80;

// Ethernet, declare to client.
EthernetClient client;

// -HTTP-GET---------------//
//
String client_httpserver_string = "10.4.20.1";
const int client_httpserver_port = 80;
// postbox_type (helloduino.php)
String client_httplocation = "/dev/tx/transmit.php";
String client_httppostbox = "/dev/rx/helloduino.php";
String client_urlpath = "/dev/tx/";
String posturlfile = "transmit.php";

const char client_httpserver[] = "10.4.20.1";

// Client HTTP
// GET
String geturlfile = "0";
String geturlextention = ".php";
// POST DATA
String client_postpath ="/dev/rx/";
// String posturlextention = ".php";
// POST BOX
String posturlboxfile = "helloswitch.php";
//String posturlboxextention = ".php";
String box_type = "switch6";
//String posturlbox_type = "bs=switch6" ;
//String posturlbox_type = "bs=" + box_type;
String switchstatus[6];
String transmitdata="101101";

//-Variables-------------------------------------------------------------//
//char ipstring[16];
//String txData = ".txt";
// boolean lastread[rsx_pins_port] = {}; // output pin definitions [1..6] (0 is not defined thus -1)
String read_last = "";
String read_current = "";
int read_ok = 0;
//-----------------------------------------------------------------------//
//--VOID SETUP-----------------------------------------------------------//
//-----------------------------------------------------------------------//
void setup() {
init_serial();
DEBUG_start();
// DEBUG_eeprom_mac_reset();
// DEBUG_eeprom_mac_read();
init_pins();
init_rsx_pins();
rsx_output_low();

delay(delay_startup);
// Randomize mac if not set.
if (EEPROM.read(1) != 0x23) eeprom_mac_randomize();
eeprom_mac_read();
ethernet_start();
delay(delay_startup);

// DEBUG_print_mac();
DEBUG_print_ip();
DEBUG_print_boxid();

// HTTP Post box
String http_postdata = "bid=" + box_mac() + "&" + "bs=" + box_type + "&" + "ip=" + ethernet_ip();
http_post("/dev/tx/", "helloswitch.php", http_postdata);
client_disconnect();
delay(delay_startup);

// Error fixed, Startup all output is HIGH on startup.
//
client_data_connect();
client_data_read();
rsx_output_low();
// DEBUG_printoutput();
// client_disconnect();

}
//-Setup-END-------------------------------------------------------------//
//-----------------------------------------------------------------------//

//-----------------------------------------------------------------------//
//--MAIN-----------------------------------------------------------------//
//-----------------------------------------------------------------------//
void loop() {
// hardware reset when failcounter > failcount
resetonfail();
Serial.print("{");
// Serial.print(box_mac_int());

delay(delay_loop);

if (resetfailcounter == 0) {
client_data_connect();
client_data_read();
client_disconnect();
}

read_current="";
for (int x = 1; x <= rsx_pins; x++) {
read_current += rsx_port[x];
}

if (read_last == read_current) {
read_ok++;

};

if (resetfailcounter == 0 && read_ok >= 3) {
Serial.print("[SETOUTPUT]");
for (int i = 1; i <= rsx_pins; i++) {
rsx_output_set(i); // rsx_pin[], rsx_port[]
}
read_ok=0;
}

// DEBUG_printoutput_pins();
DEBUG_printoutput();


read_last="";
for (int x = 1; x <= rsx_pins; x++) {
read_last += rsx_port[x];
};

Serial.println("}");
}
//--[END]-MAIN-----------------------------------------------------------//
//-----------------------------------------------------------------------//


//-----------------------------------------------------------------------//
//--Sketch-Specific-functions--------------------------------------------//
//-----------------------------------------------------------------------//
//-INIT-RSX-PINS-[Switch]---//
// Set as output
void init_rsx_pins() {
for (int i = 1; i <= rsx_pins; i++) {
pinMode(rsx_pin[i], OUTPUT);
rsx_port[i] = 0;
}
}
//-RSX-output-functions-----//
void rsx_output_low() {
for (int i = 1; i < rsx_pins; i++) {
digitalWrite(rsx_pin[i], LOW);
}
}
//--------------------------//
void rsx_output_set(int pin) {
digitalWrite(rsx_pin[pin], rsx_port[pin]);
}

//--END--Sketch-Specific-functions---------------------------------------//
//-----------------------------------------------------------------------//

//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
void client_connect(String httprequest) {
if (client.connect(client_httpserver, client_httpserver_port)) {

} else {

// if you didn't get a connection to the server:
resetfailcounter++;
resetonfail();
}
}

//--------------------------//
void client_disconnect() {
if (!client.connected()) { client.stop(); };
}

void client_data_connect() {
String httprequest = "";

httprequest = "GET ";
httprequest += client_httplocation;
httprequest += "?bid=" + box_mac();
httprequest += " HTTP/1.1\n";
httprequest += "Host: ";
httprequest += client_httpserver_string;
httprequest += "\n";
httprequest += "Connection: close\n";

if (client.connect(server, server_port)) {
client.println(httprequest);
client.println();

} else { // if you didn't get a connection to the server:
rsx_output_low();
for (int x=1; x <= 6; x++) {
rsx_port[x] = HIGH;
}
Serial.print("connection-failed");
}
client_disconnect();
}
void client_data_read () {
String result = "";
char rchar[9];

while(client.connected() && !client.available()) delay(delay_connection);
// read data
while(client.available()) {
char charread = client.read();
result = result + charread;
if (result.endsWith("\n\r") || result.endsWith("\r\n")) {result=""; };
}
// Char to Array
result.toCharArray(rchar, 9);

for (int x=1; x <= 6; x++) {
if (rchar[x]=='0') { rsx_port[x] = LOW; };
if (rchar[x]=='1') { rsx_port[x] = HIGH; };
}
}

//--------------------------//
void http_post(String pagepath, String page, String data) {
String poststring = "";
poststring += "POST " + pagepath + page + "?" + data + " HTTP/1.1" + "\r\n";
poststring += "Host: ";
poststring += client_httpserver_string;
poststring += "\r\n";
poststring += "Content-Type: application/x-www-form-urlencoded\r\n";
poststring += "Connection: close\r\n";
client_connect(poststring);
}

//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
// INIT Functions
// Init pins, default function for setting the pins.
void init_pins() {
// !! Undefinable pins 4(XCK), 10(SS), 0(RS232), 1(RS232)
//-SDCard-[Disable]---[XCK]-//
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

//-Ethernet-[Enable]---[SS]-// (SlaveSelect)
pinMode(10, OUTPUT);
digitalWrite(10, LOW);

//-RESET-[Switch/Relay]-----//
pinMode(resetpin, OUTPUT);
digitalWrite(resetpin, LOW);
}
//--------------------------//
void init_serial() {
Serial.begin(serialbaudrate); // define serial speed
}
//--------------------------//
void eeprom_mac_read() {
for (int i = 0; i < 6; i++) {
mac[i] = EEPROM.read(eeprom_mac[i]);
}
}
//--------------------------//
void eeprom_mac_randomize() {
// Generate mac
for (int i = 0; i < 6; i++) {
mac[i] = TrueRandom.randomByte();
// IEEE defined test mac
mac[0] = 0x02;
mac[5] = 0x01;
EEPROM.write(eeprom_mac[i], mac[i]);
}
EEPROM.write(1, 0x23);
}
//--------------------------//
String box_mac() {
String str;
str = mac[1];
str += mac[2];
str += mac[3];
str += mac[4];
str += mac[5];
return str;
}
int box_mac_int() {
String str;
int str2int;
str = mac[1];
str += mac[2];
str += mac[3];
str += mac[4];
str += mac[5];
str2int = str.toInt();
return str2int;
}
//--------------------------//
// Ethernet
//--------------------------//
void ethernet_start() {
// Init ethernet, Reset if DHCP failed
if (Ethernet.begin(mac) == 0) {
resetfailcounter++;
resetonfail();
} else
{
Ethernet.begin(mac);
}
}
//--------------------------//
String ethernet_ip() {
char ip[16];
IPAddress IPAddr = Ethernet.localIP();
byte oct1 = IPAddr[0];
byte oct2 = IPAddr[1];
byte oct3 = IPAddr[2];
byte oct4 = IPAddr[3];
sprintf(ip, "%d.%d.%d.%d", oct1, oct2, oct3, oct4);
return ip;
}

//--------------------------//
void resetonfail() {
if (ethernet_ip() == "0.0.0.0") { resetfailcounter = resetfailcount; }
if (resetfailcounter >= resetfailcount)
{
digitalWrite(resetpin, HIGH);
delay(300);
digitalWrite(resetpin, LOW);
Serial.print("[[RESET]]");
resetfailcounter = -1;
}
}
//-----------------------------------------------------------------------//


//-----------------------------------------------------------------------//
// Debug functions
//-----------------------------------------------------------------------//
void DEBUG_start() {
Serial.println(".");
}
//--------------------------//
void DEBUG_eeprom_mac_reset() {
EEPROM.write(1, 0xFF);
}
//--------------------------//
void DEBUG_eeprom_mac_read() {
int a = 12;
int value;
for (int i = 0; i < a; i++) {
value = EEPROM.read(i);
Serial.print(value, HEX);
Serial.print("\t");
}
Serial.println();
for (int i = 0; i < a; i++) {
value = EEPROM.read(i);
Serial.print(value);
Serial.print("\t");
}
Serial.println();
}
//--------------------------//
void DEBUG_print_mac() {
char macstr[18];
Serial.print("init mac\t");
snprintf(macstr, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(macstr);
}
//--------------------------//
void DEBUG_print_ip() {
Serial.print("init ip\t");
Serial.println(Ethernet.localIP());
}
void DEBUG_print_boxid() {
Serial.print("init boxid\t");
Serial.println(box_mac());
}
//--------------------------//
void DEBUG_switchstatus(String rin) {
if (rin == "1\n") { Serial.print(" [on] "); Serial.print(resetfailcounter); }
if (rin == "0\n") { Serial.print("[off] "); Serial.print(resetfailcounter); }
// { Serial.print("[nodata-off]"); };
Serial.println();
}
void DEBUG_printoutput () {
for (int x=1; x <=6; x++) {
Serial.print(rsx_port[x]);
}
}
void DEBUG_printoutput_pins() {
for (int i = 1; i <= rsx_pins; i++) {
Serial.print("[");
Serial.print(rsx_pin[i]);
Serial.print(">");
Serial.print(rsx_port[i]);
Serial.print("]");
}
}

//-----------------------------------------------------------------------//

/*

When the network connection fails (either cable disconnect or packet-error) the outputs are overwritten.
While there is no command or function for changing the output high or low.

In the LOOP, 'read_current' and 'read_last' was created to filter network failures.

*/

Advertisement

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: rare output bij 6 relay schakel sketch

Berichtdoor Koepel » 01 Sep 2016, 20:18

Volgens sommigen kun je in een Arduino Uno beter helemaal geen String object gebruiken. Vanwege het kaboom scenario : https://learn.adafruit.com/memories-of-an-arduino/solving-memory-problems
Je gebruikt er best veel.
Bijvoorbeeld de functie ethernet_ip(). Daar worden 16 byte op de stack gedeclareerd. De pointer er naar toe is de return waarde, en de variabelen op de stack vervallen, maar de pointer wordt naar een String object omgezet. Dat is niet fraai, ik weet niet hoe de compiler dat zal doen.
Bij box_mac() ongeveer hetzelfde. Een String object is de return waarde, maar de pointer naar dat object komt te vervallen. Buiten de functie is dat buiten de scope.

Zou je de ChipSelect voor de W5100 hoog willen maken bij het initialiseren ? Als het laag is, dan is het actief, terwijl de Ethernet library dat doet. Dus in de functie init_pins() ook pin 10 hoog maken.

Om het probleem te vinden, kun je veel meer Serial.println() toevoegen. Bijvoorbeeld als de binnenkomende karakters naar het array worden omgezet. Je kunt ervoor en erna de variabelen naar de seriele poort sturen. Je hele code komt dan vol te staan met dat soort dingen, maar dat is normaal. Vaak worden geen afzonderlijke functies gebruikt, maar gewoon in de code:
Code: Alles selecteren
#define DEBUG

#ifdef DEBUG
  Serial.print(F( "temperature = "));
  Serial.println( temperature);
#endif


Ik heb veel moeite om de code door te lezen. De indeling naar functionaliteit is niet zo helder, en de afzonderlijke software-lagen zijn niet goed zichtbaar. Ik hoop dat iemand anders beter kan helpen.

Berichten: 4067
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: rare output bij 6 relay schakel sketch

Berichtdoor shooter » 02 Sep 2016, 11:11

er staat letterlijk if connection fails dan alle outputs naar LOW
paul deelen
shooter@home.nl

Gebruikers-avatar
Berichten: 7
Geregistreerd: 02 Sep 2016, 20:17

Re: rare output bij 6 relay schakel sketch

Berichtdoor Adam69 » 02 Sep 2016, 20:50

Beste CTG,

ergens in het midden van de code (regel 243) staat dit:

Code: Alles selecteren
} else {  // if you didn't get a connection to the server:
rsx_output_low();
for (int x=1; x <= 6; x++) {
rsx_port[x] = HIGH;
//}


Wat hier concreet staat is dat.. als de verbinding mislukt alles op HIGH wordt gezet, dus '111111'.
En dat is wat je vraag is, dus denk ik dat je dit uit moet zetten, en dan zien of het dan wel werkt zoals je wenst.

Code: Alles selecteren
} else {  // if you didn't get a connection to the server:
// rsx_output_low();
// for (int x=1; x <= 6; x++) {
// rsx_port[x] = HIGH;
// }


Dus bij deze 4 regels even '// ' ervoor plaatsen, dan doen die niet meer mee.
Laat maar weten wat de uitkomst is, succes.
Groet,

Adam

Terug naar Overige projecten

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 34 gasten