[SOLVED] Is my sketch frozen or something? No serial output at all.

I am creating a sketch to read my electricity meter which has a serial port (P1, Dutch Smart Meter). The meter sends out a telegram which contains the consumption info. I found a sketch that reads the messages from this port with the ‘AltSoftSerial’ library and spits out the figures in a readable way to the serial monitor. This sketch can be found here: http://phoenixinteractive.nl/websitebb/viewtopic.php?f=23&t=37

I want to adapt that sketch so it sends those figures (mEVLT, mEVHT etc.) to a MySQL database. I adapted the sketch to my needs, compiled it, and uploaded it to my Arduino (Duemilanove clone from China). However i get ZERO output from the Serial Monitor on my PC (Linux Mint). If i upload the sketch from the link i provided, it displays everything nicely in the serial monitor.

Can anybody please give some hints where the sketch is wrong? Searching the error in my sketch is impossible for me this way, with no feedback from the Arduino.

Connected to the Arduino is a ENC28J60 Ethernet module, which i provided with his own 3V3 powersupply (because the onboard 3V3 from Arduino is not strong enough)

Findings:

  • Sketch compiles without any errors/warnings
  • Serial monitor is @ 9600 baud
  • I can’t ping the Arduino
  • If i type something in the serial monitor and press Enter, the RX led on the Arduino flickers (So serial connections is OK)
  • An other sketch with serial output works (So Arduino is OK)
  • An other sketch that utilises the ENC28J60 works (So Ethernet hardware is OK)

I think the initalization hangs somewhere. But i can’t figure out why. I just merged a few sketches together to get to this result.

My code: (for the people who prefer syntax marking, i have also put it on Pastebin: http://pastebin.com/DvdQZSmE )

               #include <AltSoftSerial.h>
        #include <SPI.h>
        #include <UIPEthernet.h>
        // AltSoftSerial always uses these pins:
        //
        // Board          Transmit  Receive   PWM Unusable
        // -----          --------  -------   ------------
        // Teensy 2.0         9        10       (none)
        // Teensy++ 2.0      25         4       26, 27
        // Arduino Uno        9         8         10
        // Arduino Mega      46        48       44, 45
        // Wiring-S           5         6          4
        // Sanguino          13        14         12
         
        AltSoftSerial altSerial;

        byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
        IPAddress ip(192,168,4,7);
        IPAddress server(192,168,4,4);
        EthernetClient client;

        const int requestPin =  4;         
        char input; // incoming serial data (byte)
        bool readnextLine = false;
        #define BUFSIZE 75
        char buffer[BUFSIZE]; //Buffer for serial data to find \n .
        int bufpos = 0;
        long mEVLT = 0; //Meter reading Electrics - consumption low tariff
        long mEVHT = 0; //Meter reading Electrics - consumption high tariff
        long mETLT = 0; //Meter reading Electrics - generated electricity (Solar panels) low tariff
        long mETHT = 0; //Meter reading Electrics - generated electricity (Solar panels) high tariff
        long mEAV = 0;  //Meter reading Electrics - Actual consumption
        long mEAT = 0;  //Meter reading Electrics - Actual generated electricity (Solar panels)
        float mG = 0;   //Meter reading Gas
         
        void setup() {
          Serial.begin(9600);
          delay(1000);
          
          altSerial.begin(9600);
          delay(1000);

          Ethernet.begin(mac, ip);
          delay(1000);
          Serial.print("My IP address: ");
          Serial.println(Ethernet.localIP());
          
          //Set RTS pin high, so smart meter will start sending telegrams
          pinMode(requestPin, OUTPUT);
          digitalWrite(requestPin, HIGH);
        }
         
        void loop() {
        long tl = 0;
        long tld =0;
         
          if (altSerial.available()) {
            input = altSerial.read();
           
            // --- 7 bits setting ---
            input &= ~(1 << 7);
            char inChar = (char)input;
            // --- 7 bits setting ---
         
            //Serial.print(input); //Debug
         
            // Fill buffer up to and including a new line (\n)
            buffer[bufpos] = input&127;
            bufpos++;
         
            if (input == '\n') { // We received a new line (data up to \n)
         
              // 1-0:1.8.1 = Electricity consumption low tariff (DSMR v4.0)
              if (sscanf(buffer,"1-0:1.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) {
                mEVLT = tl * 1000 + tld;
                if (mEVLT > 0) {
                  Serial.print(F("Elektra - meterstand verbruik LAAG tarief (Wh): "));
                  Serial.println(mEVLT);
                  //mEVLT = 0;
                }
              }
         
              // 1-0:1.8.2 = Electricity consumption high tariff (DSMR v4.0)
              if (sscanf(buffer,"1-0:1.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) {
                mEVHT = tl * 1000 + tld;
                if (mEVHT > 0) {
                  Serial.print(F("Elektra - meterstand verbruik HOOG tarief (Wh): "));
                  Serial.println(mEVHT);
                  //mEVHT = 0;
                }
              }
         
              // 1-0:1.7.0 = Electricity consumption actual usage (DSMR v4.0)
              if (sscanf(buffer,"1-0:1.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) {
                mEAV = tl * 1000 + tld * 10;
                if (mEAV > 0) {
                  Serial.print(F("Elektra - actueel verbruik (W): "));
                  Serial.println(mEAV);
                  //mEAV = 0;
                }
              }
         
              // 1-0:2.8.1 = Generated electricity low tariff (DSMR v4.0)
              if (sscanf(buffer,"1-0:2.8.1(%ld%.%ld%*s" , &tl, &tld) >0 ) {
                mETLT = tl * 1000 + tld;
                if (mETLT > 0) {
                  Serial.print(F("Elektra - meterstand teruglevering LAAG tarief (Wh): "));
                  Serial.println(mETLT);
                  //mETLT = 0;
                }
              }
         
              // 1-0:2.8.2 = Generated electricity high tariff (DSMR v4.0)
              if (sscanf(buffer,"1-0:2.8.2(%ld%.%ld%*s" , &tl, &tld) >0 ) {
                mETHT = tl * 1000 + tld;
                if (mETHT > 0) {
                  Serial.print(F("Elektra - meterstand teruglevering HOOG tarief (Wh): "));
                  Serial.println(mETHT);
                  //mETHT = 0;
                }
              }
         
              // 1-0:2.7.0 = Actual generated electricity (DSMR v4.0)
              if (sscanf(buffer,"1-0:2.7.0(%ld.%ld%*s" , &tl , &tld) >0  ) {
                mEAT = tl * 1000 + tld * 10;
                if (mEAT > 0) {
                  Serial.print(F("Elektra - actueel teruglevering (W): "));
                  Serial.println(mEAT);
                  //mEAT = 0;
                }
              }
         
              // 0-1:24.3.0 = Gas (DSMR v4.0)
              if (sscanf(buffer,"0-1:24.3.0(%6ld%4ld%*s" , &tl, &tld) > 0  ) {
                readnextLine = true; // we have to go to the next line
              }
              if (readnextLine){
                if (sscanf(buffer,"(%ld.%ld%*s" , &tl, &tld) >0  ) {
                  mG = float ( tl * 1000 + tld ) / 1000;
                  Serial.print(F("Gas - meterstand (m3): "));
                  Serial.println(mG);
                  Serial.println("");
                  readnextLine = false;
                }
              }
              
              httpRequest();
              // Empty buffer again (whole array)
              for (int i=0; i<75; i++)
              { buffer[i] = 0;}
              bufpos = 0;

              mEVLT = 0;
              mEVHT = 0;
              mEAV = 0;
              mETLT = 0;
              mETHT = 0;
              mEAT = 0;
              delay(1000);
              client.stop();

            }
          }
        }

void httpRequest() {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    client.print("GET /www/slimmemeter/p1.php?mEVLT=");
    client.print(mEVLT);
    client.print("&mEVHT=");
    client.print(mEVHT);
    client.print("&mEAV=");
    client.print(mEAV);
    client.print("&mG=");
    client.print(mG);
    client.println(" HTTP/1.1");
    client.println("Host: 192.168.4.4");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    Serial.println("Connection OK!");

    Serial.print("GET /www/slimmemeter/p1.php?mEVLT=");
    Serial.print(mEVLT);
    Serial.print("&mEVHT=");
    Serial.print(mEVHT);
    Serial.print("&mEAV=");
    Serial.print(mEAV);
    Serial.print("&mG=");
    Serial.print(mG);
    Serial.println(" HTTP/1.1");
    Serial.println("Host: 192.168.4.4");
    Serial.println("User-Agent: arduino-ethernet");
    Serial.println("Connection: close");
    Serial.println();
  } 
  else {
    Serial.println("connection failed");
    client.stop();
  }
}
            char inChar = (char)input;

Cast a char to a char so you can store it in a char. Why is ANY of that crap necessary?

            // Fill buffer up to and including a new line (\n)
            buffer[bufpos] = input&127;
            bufpos++;

Doesn’t matter that the buffer might be full?

              // Empty buffer again (whole array)
              for (int i=0; i<75; i++)
              { buffer[i] = 0;}

More code that illustrates that you don’t understand the concept of “NULL terminated array of chars”. Specifically, you don’t seem to understand the that NULL is a stop sign. There is no value in stuffing 75 stop signs in a row. If you don’t stop at the first one, why would you stop at the 17th?

I think there may be a conflict between AltSoftSerial and SPI over the use of Pin 10.

Maybe regular SoftwareSerial will work.

...R

I think there may be a conflict between AltSoftSerial and SPI over the use of Pin 10.

AltSoftSerial doesn't use pin 10. It uses a timer that is also used by PWM on pin 10 (and 9, but obviously PWM and software serial at the same time won't work, so there is no need to mention that PWM on pin 9 won't work).

PaulS:

            char inChar = (char)input;

Cast a char to a char so you can store it in a char. Why is ANY of that crap necessary?

            // Fill buffer up to and including a new line (\n)

buffer[bufpos] = input&127;
           bufpos++;



Doesn't matter that the buffer might be full?



// Empty buffer again (whole array)
             for (int i=0; i<75; i++)
             { buffer[i] = 0;}



More code that illustrates that you don't understand the concept of "NULL terminated array of chars". Specifically, you don't seem to understand the that NULL is a stop sign. There is no value in stuffing 75 stop signs in a row. If you don't stop at the first one, why would you stop at the 17th?

Thank you for your comment. However that part is not made by me. Also my knowledge of C++ is not that high enough to spot that kind of errors :slight_smile: I will try and see if i can improve it.

Robin2:
I think there may be a conflict between AltSoftSerial and SPI over the use of Pin 10.

Maybe regular SoftwareSerial will work.

…R

First i used SoftwareSerial, but SoftwareSerial can’t cope with the packages coming from the meter. It goes too fast i guess, because message are coming in crippled and incomplete. With AltSoftSerial this was solved. But i guess they are biting eachother…

The UIPEthernet library for ENC28J60 uses pins 10 (CS) , 11 (SI), 12 (SO), 13 (SCK)

PaulS:

I think there may be a conflict between AltSoftSerial and SPI over the use of Pin 10.

AltSoftSerial doesn’t use pin 10. It uses a timer that is also used by PWM on pin 10 (and 9, but obviously PWM and software serial at the same time won’t work, so there is no need to mention that PWM on pin 9 won’t work).

But doesn’t the usage of UIPEthernet on pin 10, bites the PWM of AltSoftSerial?

I have bought a W5100 Ethernet module a week ago, i think i will receive it next week. Are the W5100 and AltSoftSerial safely to use at the same time? Or do they also fight eachother?

I think you need to go back to basics.

Write a short sketch using SoftwareSerial that reads the data from the meter and displays it on the Serial Monitor - and nothing else. When you have that working it will be time to move on.

This will be by far the quickest way to make progress.

...R

Thanks, but i got that part already covered by using an other sketch :slight_smile:

I will wait until i have received the W5100. I am quite annoyed by the ENC28J60, time for some serious hardware now :wink:

I have changed the topictitle to solved, i think the issue lies in the fact that UIPEthernet and AltSoftSerial don't like each other.