Relay Control Web Interface Timing Problem

Hi im hoping someone can help me with a timing issue im having with a project of mine.

Im new to all this and thou everything works the web server for the relay control loads sometimes and not others, i think its a timing issue.

Code is attached was too long to paste in here.

The first part displaying all the data goes to

I would love to be able to have the realy control page hosted by the linux php server but i cant find any examples on how to do this so have just setup a web server on the aruino mega2560 and it can be found at

code.txt (12.7 KB)

On first glance of your program, I would remove all of the delay(..) you have in each of the routines and use a single timing event in your main loop(), such that each routine gets called when it needs.

This will give you far greater control of how your program works and will be far easier to extend and debug. You don't need to push data up to your server so fast as you check your sensors. Are you wanting alarm setpoints for any of your sensors?

I'm curious about what your application is, as I see you are sensing currents as well and some environmental conditions.

Oh, and welcome to the forum.


First of all thanks for the help removing all those delays and using just one in the loop has made things work alot better.

Eventually I want to use this to control a home automation system. I live on off grid solar hence the current sensors eventually i want to be able to turn the cooling fans for the inverter and solar charge controller box on and off as well as turn lights and air conditioning on and off remotly.

Thanks again to Rockwallaby for your help

The web server runs much better now thou i am finding that i am having to click on a buttom twice before it will execute and then it of course executes twice. Any help with this would be much appreciated..

Also off grid here. I'm in the process of replacing lead acid with LiFeYPO4 and upgrading the Arduino Mega to a DUE to handle more including the solar charge control, to replace a Plasmatronics PL40.

The Arduino also controls a shunt load for my micro-hydro using PID. Exciting stuff right?

If you are planning a GNU/Linux server, you might be interested in something I am developing, a SCADA system that runs on GNU/Linux systems, either Intel 64/32 or ARM systems like Cubieboard or RPi.

If this grabs your interest, take a peek at my website which runs on the system I am actively developing. I have another off grid house over in the states that I am working with and it will also talk with other solar equipment such as charge controllers or inverters that use the Modbus communications protocol.

I would be happy to share and help where I can for system, if you are seeking advice. When I saw your code I was thinking it could be something along the lines of renewable energy or environmental control system.


Cool off grid is so good, and good to see your changing to lithium batteries they are much better from what ive been reading, i to am looking to change over to lithium when finances permit. Better power density, can discharge to lower capacity without shortning the lifespan of the battery very cool. Ive been reading up on these new lithium titanate batteries they look like the future, especially for electric cars.

Im using 3 tristar ts-mppt-60 charge controllers which have the mobius bus with rs425 i got one for my mega but havent got round to setting it up yet. Trying to get everything else working, and as im new to all this its a steep learning curve for me but very addictive and its so rewarding when you get something to work.

And have a TRiMetric tm-2025-a power monitor which has a serial output which i will be looking to read the data from as well.

Would love to see your web site whats the address?

Help, help is invaluble to me at the moment. Im just an electronics tech just getting started with the programming thing (hardware is easy, software a challenge).

Im using an intel i5 board with a 12 volt power supply for my linux php mysql server only draws about 2 amps with a 12 volt 17" LCD (also 2 amps) which i upgraded for a client. (I fix computers for a livin).

Anyways would love to see what your got would be very handy and insperational..

Under my avatar is my web address, or this There is a lot of info about what I do and am doing, but scroll down to where you see the orange button, titled 'hydrosolar', that will take you to the section and load up all the single page web app, which is a full javascript application that does all the work.

It is still in early days with all the software, but importantly the application is fully written in C and does not use the LAMP stack, which makes this application very lightweight and fast.

It has inbuilt Modbus communications and an HTTP server for serving up all HTML, other files and also does AJAX.

My brother in law has a Tri-Star MPPT-45 and I'll be looking to making a system for him in the future. He also is installing the LIFeYPO4 bank as I write this, we pulled out his lead acid bank yesterday. Heavy.

The application I'm developing is called Quadlog, and is basically a SCADA system, where you define data tags which is the data you are interested in and it will manage the getting of data from the device you define using TCP/USB/RS-485 from multiple ports, really any network configuration you want.

You then use a web application to be your GUI to the system. Take a look and see if this is fitting in with your ideas.

I use to fix computers many years ago, now I prefer to design, hardware and software.

Oh, all data is now stored in SQLite database, not text files as I mention, I need to update my site text.


All I can say is "WOW" how beautiful that looks and is exactly what im trying to head toward.

If I can get something even a little like that I would be a happy man!

I really am impressed, and it gives me inspiration to keep up with it, seeing what can be done. (Much to my wifes dissmay, she never sees me).

I love the TS-MPPT-60 charge controllers their industructable! Ive had sparks coming out of mine a couple times from not disconnecting the solar array before the battery a couple times. Just reboot it and off it goes.

Thanks for your kind words, there's been a bit of work in there so far and a fair amount left yet to do.

Quadlog, which runs on GNU/Linux is talking to the Arduino using the Modbus protocol, so it is seen just as any other device. Your TriStar will be the same and just need to define what registers you want from it into the tags database and set various options for each tag such as logging, alarm setpoints and so on.

You can have each device being talked to scanned at a different rate, from a few hundred milliseconds to many seconds between each poll.

I'm currently working on all history data being logged to the SQLite3 database now and needs testing, before I cut my live system across to the new update.

With the TRiMetric tm-2025-a power monitor, we should be able to have the Quadlog application talk to it.

Having a small single board computer to do all the HTTP serving, data logging and talking to the various devices such as solar charge controllers and Arduino for other sensors and control is a good way to go.

I understand you have the Intel i5 board with 17" LCD, but, if I understand correctly, that is consuming a total of 4Amps, which is quite a lot, depending on the size of your solar system.

Use is as your development system, but have a think about using a low cost and low power consuming board based on ARM, such as Cubieboard, Beagleboard, RPi (new one) or some other ARM board.

I can run my dual core ARM cubieboard from the USB port of my Mac, it take so little power. It has SATA and Ethernet and many other ports, all configurable.

Well, if you want to run Quadlog, let me know and we can organise it. First I would like to know better your system details and equipment to make sure all things are or can be covered.

There will be a lot for you to do still in terms of the design of web interface layout. But I have a framework set up already where you can pretty well insert stuff into.


Thanks heaps i would really appreiciate that...

Yes just using the i5 for development at the moment the idea was to go to one of those new RPi 2 boards for the server.

Im using a Mega2560 at the moment and would be very interested to try your QudlogSCATA.

All the information for the serial sentences from the TriMetric i have and the MODBUS information for the TS-MPPT-60 just havent got round to even thinking much about them as i have lots to learn.

So I have 9 x 250Watt PV's on 3 TS-MPPT-60's have the TriMetric which monitors power in and out of battery 12Volt Lead ACID Rolls batteries 2000WATT Inverter a big Power Capacitor for surges I have a 15amp 12volt regulator to give me stable 12 volts for the computer etc I have at the moment 2 ACS712 30A Hall Effect current sensors DHT22 Temp/Humidity Sensor RTC and 3amp Stepper motor controller which i want to setup to keep the solar array tracking the sun with 3 LDR's to messure brightness.

At this stage I really just want to be able to view all the data from all the sensors and charge controller and trimetric with a nice GUI similar to what i have done on

Once i get all that working then i want to setup the temperature sensor to monitor the temperature in the box where charge controller and inverter are and have it turn a cooling fan on and off automatically. Monitor charge from battery charger when the generator is running.

Then add things like turning lights on and off (so when on hollidays it looks like someone is home), get the solar sun tracking working (Get more power).

I see you are in Australia John, and somewhere near the coast I am suspecting, from the current temp and reading from your BMP180, possible Vic or near the NSW boarder would be my guess?

I looked over your site :D

Oddly, I just ordered a BMP180 and another AM2303 (DHT22) sensor yesterday to be part of my system to monitor the environment here. Also, I ordered a few more of those ACS712 hall effect sensors.

I have already some larger ACS758-050U, which are uni-directional, 0 - 50 Amps, for monitoring my solar input and inverter current. But presently they are out of circuit and being installed into the new system.

You have a larger PV array than I do 2.25kW vs my piddly 1.2kW system. But I do have the micro-hydro, which is what is powering all these electrons presently.

I will not use my PL40 any longer, but rather, I will make my own, using the Arduino DUE and then control each PV panel with a good low Rdson power MOSFET. I hope to be able to make very fine control by interleaving the control to each MOSFET by individual PWM.

I have a desk full of little circuit boards, including CANbus tranceivers to connect to the BMS for the Lithium bank.

I'm on 24 volts, (25.6V when Lithium are connected) and with 6 lots of 200W panels being wired up in the next week or two. I have 8 of these Winston LiFeYPO4 cells on the floor right now, waiting for me to install. I arranged a pallet load of these cells, 40 in total for 4 separate off grid installations. Hard to beleive such a small battery pack has so much energy.

I too was planning on a PV array tracker, but there is a bit of work in the whole construction, together with the tower to support the structure and a hole of around 1.5m3 to be filled with concrete.

I'll like to let everything sink in to better understand what your needs are and how best to proceed forward. I'll send you a PM later.


no worries and thanks again..

Yeah im in Kempsey NSW well colombatti a bit inland of Kempsey, your lucky to have a hydro our creek only runs about 6 months of the year if we get good rain in summer.

Sounds like you really know your stuff thou...

Im building everything myself from stuff i have lying around the farm, good to use my ingunuwity..

Yes you need a big heap of concrete to support the weight of all the panels, im using 3 poles of 3 panels makes it a bit easier. Using some stepper motors and a worm shaft i made up from some threaded rod i had lying around, seems to work ok thou a little noisy..

I have a nice power plant but really need to get more batteries only got 1000Ah at the moment really need 2000Ah or more. (when finances permit).

I use to make hydros, wind generators, inverters, charge controllers etc when i worked for Rainbow Power Company in Nimbin many years ago. Use to make all the circuit boards transformers and all. Not that we use transformers much anymore with switchmode now.

BMS is very important with Lithium batteries and cell balancers too. The good thing about these TS-MPPT-60's is they can be programed to be used with any type of battery, which was an added bonus when i started looking into going lithium myself.

Like you i have a desk full of circuit boards and components and etching stuff to make my own boards etc. I build a lot of remote control gear planes, cars, boats and lattest project a quad copter all with hkpiolt auto pilot drone stuff, with FPV. Hobbies are good for the soul... Trying to get my son into it...

Anyway was nice talking to you, great to find people with similar interests... and look forward to future coresondance..

Thanks mate....

I must admit thou after talking to you i have convinved myself that im going to have to get a RaspberrPi 2 next week, and ill have to build a 300Amp current sensor for my inverter. (Another fun project).

hi Rockwallaby just wondering what sheilds and modbus modules you would recommend for arduino mega and raspberri pi?

Currently i have one LC Electronics RS485 module for the arduino, will this sufice or is there better more relibale modules you would recommend.

Ill order them next week the same time i order the raspberri pi 2

Im just thinking about it and was wondering how i connect it all up do i use the raspberri pi as master and have the arduino and TS-MPPT-60 both as slaves and the raspberri pi communicates with both reciving from the TS-MPPT-60 and send and recive to the arduino?

Then connect the serial out from my TriMetric to the serial in on the arduino and communicate with the TriMetric from the Raspberri pi via the arduino over the RS485 MODBUS? As well as the relay board DHT22, BMP180 ACS712's etc?

Hi John, just looking at the BMP180 code and alternative library from Sparkfun, rather than Adafruits.

For the shields, I was looking at some RS-485 boards during the week, the problem there is that the Rpi will be of course 3.3 volt logic and the Arduino Mega will be 5volt logic.

If you can have the Arduino near the RPi, then there may not be the need to use RS-485. I have used USB between my Mega and my Cubieboard, and it works well.

But, you will need to connect into the TriStar, which does have a RS-485 port for Modbus. If we connect the TriStar MPPT charge controllers to the RPi via a multidrop RS-485 network, we need to have a suitable RS-485 board for the RPi.

This means a board that can have I/O logic at 3.3volt and a bus level of 5volt. I'll need to look into it, as I possibly need this as well.

Just did a quick search on those LC modules and they are the standard generic sort you can buy on ebay for a few dollars.

I have only found one sort of RS-485 module that works with 3.3 volt logic so far.


Just confirming you use 3 TriStar MPPT-60?

This is because you have 9 lots of 12volt 250Watt panels, giving 2.25kW, which equates to around 187.5Amps at max. Therefore you need 3 of the MPPT-60 units to handle that current.

Your setup is 3 PV panels per pole and tracker, with a total of 3 trackers?

Is this correct?

John wrote:

Im just thinking about it and was wondering how i connect it all up do i use the raspberri pi as master and have the arduino and TS-MPPT-60 both as slaves and the raspberri pi communicates with both reciving from the TS-MPPT-60 and send and recive to the arduino?

Yes, my suggestion is to have the RPi as the master, call it mission control, and it will be connected to the other devices using Modbus or other serial protocol as needed. The TriStar are Modbus slaves and we can easily make your Mega a Modbus slave and get rid of all the ethernet code you currently have.

The Arduino will then become a smart I/O module, and slave to the Rpi. It will have all your sensors, such as ACS712, BMP, DHT and so on connected and we can have it do all the smoothing and sliding window averaging and alarm trip points and so on if you need. Also, the relays will be controlled form here as well, with timers and any support code in the Arduino.

Then connect the serial out from my TriMetric to the serial in on the arduino and communicate with the TriMetric from the Raspberri pi via the arduino over the RS485 MODBUS? As well as the relay board DHT22, BMP180 ACS712's etc?

It is possible to connect the TriMetric to either the Rpi or Arduino with the suitable interface. Do you have a link to the unit you have and the type of serial interface you have?

The RPi will be host to all these devices and have a HTTP interface that will allow you to view and modify what you define. As well as log any data you define to be logged, which can be charted as you have seen with my site.


For your information i have attached copy of the serial communication protcol information for the TriMetric and the MODBUS info for the TS-MPPT-60 which you might find usefull for your brother (you mentioned he had one and that he was looking to setup the QuodlogSCATA system as well)…

TSMPPT.APP_.Modbus.EN_.10.2.pdf (500 KB)

TM2025-TM2030 Technical Information On Serial Data Output_0.pdf (34.6 KB)

yes thats correct and all the TS-MPPT-60's are networked together with a single remote meter to access the combined data from all 3 units or each unit individualy

Yes this is correct and all the TS-MPPt-60’s are networked with a single remote meter which can display the totals from all 3 units or each units data indiviually.

Cant the Mega use either 3.3v or 5v and if this is a problem for the rpi would i be better off with the cubuieboard (i fell the cubieboard may be better as i think i can run debian on that which i cant on the rpi (just i know debian it is what i use and have never used any other version of linux) and the cubiboard has sata which would be handy too.

Ill attach the documents for the trimetric and ts-mppt-60 modbus info

TM2025-TM2030 Technical Information On Serial Data Output_0.pdf (34.6 KB)

TSMPPT.APP_.Modbus.EN_.10.2.pdf (500 KB)

Looking at the TriMatic details, it will interface nicely to the Arduino Mega on another of its async serial ports. The TriMatic is based on 5volt MCU as is the Mega, so a few wires as described on the technical document will see it connected ok.

The next thing is to sort out the RS-485 between RPi and TriStar units, something I need to look into.

Thanks for providing the documents so fast :)