Request help for combining 2 sketches into 1

Dear arduino guru's,

For a few months I have been stuck; I have 2nr. individually good working sketches as part of my home power monitoring project.

I would like to run both in one sketch. Not simultaneous, but alternately
Meaning first complete sketch 1, then start sketch 2, after completion sketch 2, start again sketch 1, and so on...

One would think: "make a function of each sketch and call each function at its turn in the void loop."
This yet has not been successful. The compiling is ok, however the output is fragmented.

I have an Arduino Mega + W5100 shield

Sketch 1: registers my energy (smart) meter's serial communication and sends data to online server (working)
Sketch 2: registers output of various current sensors (working)

Output of Sketch 1 (working):
Emoncms client starting...
My IP address: 10.0.0.160
Server IP address: www.emoncms.org
Activate Smart Meter Port P1... 1
-------------------------------------------------------
/ISk5\2MT382-1004
0-0:96.1.1(5A42455630303531373930399343133)
1-0:1.8.1(07960.781*kWh)
1-0:1.8.2(07172.746*kWh)
1-0:2.8.1(00000.003*kWh)
1-0:2.8.2(00000.000*kWh)
0-0:96.14.0(0001)
1-0:1.7.0(0000.45*kW)
LOW RATE (W): 7960781
HIGH RATE (W): 7172746
ACTUAL CONSUMPTION (W): 45
TOTAL CONSUMPTION (kWh): 5447527.00
TOTAL COST (EUR): 1174.22
1-0:2.7.0(0000.00*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)

Output of Sketch 2 (working):
0.68 12.66 0.66 19.28 0.05
0.58 1.48 0.67 2.22 0.39
0.60 1.63 0.67 2.42 0.37
0.43 8.80 0.46 19.34 0.05
0.05 0.94 0.49 1.94 0.05
0.06 1.04 0.49 2.09 0.06
1.36 8.25 0.40 20.54 0.17
-0.16 3.11 0.49 6.41 -0.05
-0.52 2.46 0.58 4.26 -0.21
0.34 8.95 0.47 19.24 0.04

Output of Sketch 1 & 2 combined:
0.35 9.76 0.59 16.41 0.04
0.58 1.33 0.59 2.23 0.44
0.57 1.49 0.61 2.44 0.38
Connecting... Connection failed!
GET /api/post?apikey=f45ae0cd475a645e2dfa5f8c23ec5f6&json=(LowRateConsumption:0,HighRate Consumption:0,ActualConsumption:0,TotalConsumption:0.00,TotalCost:0.00) HTTP:/1.1
Host:emoncms.org
User-Agent: Arduino-ethernet
Connection: close
---------------------------------------------------------
0.11 6.51 0.40 16.37 0.02
0.04 0.83 0.41 2.01 0.04
-0.38 4.40 0.52 8.38 -0.09
Connecting... Connection failed!
GET /api/post?apikey=f45ae0cd475a645e2dfa5f8c23ec5f6&json=(LowRateConsumption:0,HighRate Consumption:0,ActualConsumption:0,TotalConsumption:0.00,TotalCost:0.00) HTTP:/1.1
Host:emoncms.org
User-Agent: Arduino-ethernet
Connection: close
---------------------------------------------------------
etc.

Please find attached Sketch1 & 2 (working).
Please find attached Sketch 3 (combination of 1 & 2 = not working).

(please note that I put for this example a fake API key)

I hope somebody is more skilled than I am and can explain why the output of the above sketch doesn't show anything from the energy smart meter, but only output from the current sensors + fail to connect to server message. (--> I should be able to fix the fail to connect issue)

Any advise or comment that would give me a new angle will be very welcome guys!
Thank you greatly in advance and please let me know if you have any questions.

Kind regards,
Vernon

Sketch1.ino (5.74 KB)

Sketch2.ino (2.66 KB)

Sketch3.ino (7.74 KB)

I would do a basic debug, at the last line of decodeTelegram() put a serial print "got to end of Debug", and the first line of httprequest "starting request", to see if their not overlapping, if not then it is a http connection issue! then i would say you missed the ethernet startup in the setup()

Just looking quickly:

The statement : httpRequest(); in the loop() of sketch 3 does not come directly from either sketch 1 or sketch 2 in that position. In sketch 1 it is called from function decodeTelegram(). In sketch 2, it does not exist at all.
I would comment out httpRequest(); in the loop() of sketch 3 and see how far it then goes.

Hi T6000 and 6b6gt,

Thank you for your help. I was gladly surprised to see a fine response so quickly.
I have taken both your advice and I experimented some more.

Please see attached my revised sketch attempt, to combine 2 functions in the void loop()

By doing, I discovered the following:

Calling ONLY the decodeTelegram() in the void loop() gives the perfect result below:

/ISk5\2MT382-1004
0-0:96.1.1(5A424556303035313739303939343133)
1-0:1.8.1(08508.460*kWh)
1-0:1.8.2(07776.077*kWh)
1-0:2.8.1(00000.003*kWh)
1-0:2.8.2(00000.000*kWh)
0-0:96.14.0(0001)
1-0:1.7.0(0000.48*kW)
LOW RATE (W): 8508460
HIGH RATE (W): 7776077
ACTUAL CONSUMPTION (W): 48
TOTAL CONSUMPTION (kWh): 6598537.00
TOTAL COST (EUR): 1422.11
1-0:2.7.0(0000.00*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.0()
!starting sending power meter data to server
Connecting... Connected!
GET /api/post?apikey=f45ae0cd475a645e2dfa5f8c23ec5f6&json={LowRateConsumption:8508460,HighRateConsumption:7776077,ActualConsumption:48,TotalConsumption:6598537.00,TotalCost:1422.11} HTTP/1.1
Host:emoncms.org
User-Agent: Arduino-ethernet
Connection: close
----------------------------------------------

When adding some debug text to the void loop() just after the 'decodeTelegram()' function-call (like "test"), the following results showed:

Emoncms client starting...
My IP address: 10.0.0.160
Server IP address: www.emoncms.org
Activate Smart Meter Port P1... 1
0
----------------------------------------------
/test
Itest
Stest
ktest
5test
\test
2test
Mtest
Ttest
3test
8test
2test
-test
1test
0test
0test
4test
test
test
test
test
0test
-test
0test
:test
9test
6test
.test
1test
.test
1test
(test
5test
Atest
4test
2test
4test
5test
5test
6test
3test
0test
3test
0test
3test
5test
3test
1test
3test
7test
3test
9test
3test
0test
3test
9test
3test
9test
3test
4test
3test
1test
3test
3test
)test
test
test
1test
:test
0test
8test
test
.test
7test
*test
1test
.test
0test
Wtest
0test
(test
0test
)test
9test
(test
test
7test
0test
)test
2test
0test
ktest
0test
0test
0test
test
6test
1test
:test
1test
0test
.test
test
test

To me it looks like the function 'decodeTelegram()' requires to 'loop' many times to collect its data before it sends the data to the server.
Perhaps this can be controlled to put some coding of 'decodeTelegram' in a for- or while loop, instead of (ab)using the void loop() for this purpose?

If anybody would like to help me with this obstacle/challenge I would be very grateful!
Many thanks in advance!
Vernon

Sketch4_.ino (10.8 KB)