Is er iemand met Arduino kennis die ziet wat ik fout doe?
Ik heb de lichtsensor een tijdje van de electriciteitsmeter afgehaald en dan gaat het wel de hele dag goed. Pas als er pulsjes komen tijdens het verzenden van data of volgens mij ook tijdens het schrijven naar de serial, gaat het mis. Zie bijvoorbeeld deze output van mijn Serial monitor, waarbij ik even elke 10 seconde naar Pachube schrijf om de kans op een puls tijdens het schrijven zo groot mogelijk te maken:
- Code: Selecteer alles
<knip lang stuk log>
disconnecting.
Puls!
Puls!
Submit Data
2
connecting...
HTTP/1.1 200 OK
Date: Thu, 29 Dec 2011 17:23:32 GMT
Content-Type: text/html; charset=utf-8
Connection: close
X-Pachube-Logging-Key: (verwijderd)
X-PachubeRequestId: (verwijderd)
Cache-Control: max-age=0
Content-Length: 1
Age: 0
Vary: Accept-Encoding
disconnecting.
Puls!
Submit Data
1
connecting...
HTTP/1.1 200 OK
Date: Thu, 29 Dec 2011 17:23:42 GMT
Content-Type: text/html; charset=utf-8
Connect
Halverwege het woord Connection: close is hij dus afgebroken. Ik heb inmiddels ook al een Watchdogtimer toegevoegd, maar ook dat werkt niet. Alhoewel ik daar lees dat ik een andere bootloader nodig zou hebben om dit te kunnen gebruiken. Is dat nog steeds zo met de nieuwe R3 van de Mega 2560?
Hierbij mijn sketch:
- Code: Selecteer alles
/*
Energiepulsjes tellen en doorsturen naar Pachube, op basis van voorbeeld Pachube sensor client
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
Op basis van:
http://www.tigoe.net/pcomp/code/category/arduinowiring/873
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <avr/wdt.h>
// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x8A, 0xCA};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(10,0,0,200);
// initialize the library instance:
EthernetClient client;
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const long postingInterval = 10000; //delay between updates to Pachube.com
volatile int state = LOW;
const int sensorPin = 3; // Light sensor that monitors LED pulses from energy meter
const int detectLedPin = 4; // LED indicating that sensor data is received. Toggles at every pulse
const int submitLedPin = 5; // LED indicating data is being submitted.
const int logInterruptPin = 1; // interrupt 0 = pin 2, 1 = pin 3
const int interruptPin = 3; // Pin in use for sensorPin
volatile unsigned long wattSensor = 0; // Counts power pulses in interrupt routine, 1 pulse = 2 watt
unsigned long totalWatts = 0; // Total power used since the sketch started (not in use)
void setup() {
wdt_enable(WDTO_8S); // Reset board after 8 seconds without watchdog reset
// start serial port:
Serial.begin(9600);
pinMode(sensorPin, INPUT);
//digitalWrite(sensorPin, HIGH);
attachInterrupt(logInterruptPin, pulsInterrupt, FALLING); // Generate interrupt after pulse from energy meter
pinMode(detectLedPin, OUTPUT);
pinMode(submitLedPin, OUTPUT);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Configure manually:
Ethernet.begin(mac, ip);
}
//Serial.println(mac);
Serial.println(ip);
Serial.println("Arduino boot completed . . . .");
}
void loop() {
wdt_reset(); // reset watchdog timer
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// blink the LED to show meter pulses
digitalWrite(detectLedPin, state);
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
digitalWrite(submitLedPin, HIGH); // Show with LED that data is being submitted
unsigned long wattSensorCount; //number of watts during this logging interval
uint8_t oldSREG = SREG; // save interrupt register
cli(); // prevent interrupts while accessing the count
wattSensorCount = wattSensor; //get the count from the interrupt handler
wattSensor = 0; //reset the watts count
SREG = oldSREG; // restore interrupts
Serial.println("Submit Data");
Serial.println(wattSensorCount); // Debug information
sendData(wattSensorCount);
digitalWrite(submitLedPin, LOW); // Turn off LED that is indicating that data is being submitted
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void sendData(int thisData) {
// if there's a successful connection:
if (client.connect("www.pachube.com", 80)) {
Serial.println("connecting...");
// send the HTTP PUT request.
// fill in your feed address here:
client.print("PUT /api/<knip>.csv HTTP/1.1\n");
client.print("Host: www.pachube.com\n");
// fill in your Pachube API key here:
client.print("X-PachubeApiKey: <knip>");
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
int thisLength = getLength(thisData);
client.println(thisLength, DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.println(thisData, DEC);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
void pulsInterrupt() // routine called when external interrupt is triggered due to flash from Energy Meter
{
Serial.println("Puls!");
state = !state;
wattSensor = wattSensor + 1; //Update number of pulses, 1 pulse = 1 watt
}
Uiteindelijk is mijn doel om dit te combineren met de NODO, zodat hij ook mooi mijn energieverbruik in de gaten kan houden. Maar als het zelfstandig al niet draait, begin ik nog maar niet aan integreren....
Heeft iemand enig idee waarom dit steeds crasht? Ik weet vrij zeker dat dit met de afhandeling van interrupts te maken heeft, maar wat precies....