Online heating system control project

From Wiki2
Revision as of 19:12, 12 January 2013 by Tim (talk | contribs)

back to electronics

online heating system control project

bits and pieces

version 3
php www-data program that creates a crontab and calls a C program that executes root commands to copy it into crontabs and install it
write a php file that PUTS some light on on the AVR
collect data from AVR and store it in mysql
view the saved data on a client using javascript

code working on arduino

DS18B20 readings to json (in serial monitor)=
in../electronics/Arduino/DS18B20Serial_json.ino
key bits:

You need to allocate a buffer for tempstr

float celsius, fahrenheit;
String jsn; 
char tempBuf[32] = {0};

You end the json after you've gone through all the sensors and if you are just starting, create a new json string. Otherwise, Increment to next sensor. <syntaxhighlight>

 if ( !ds.search(addr)) {//this is where it loops to the next address

Serial.println("No more addresses."); Serial.println();

 	jsn = jsn + "}";//so end the json
 	Serial.println(jsn);//and print it out
 	ctr=0;
   ds.reset_search();//if there are no more devices go back to start of list
   delay(250);
   return;
 } else {
 	if (ctr==0){jsn="{";}//must be just beginning, start the json str
 	++ctr;//since we found a device, increment sensor
 }

</syntaxhighlight> The first two bytes of data = ds.read() is the sensor raw temp which gets converted here

 // convert the data to actual temperature
 unsigned int raw = (data[1] << 8) | data[0];
 if (type_s) {
   raw = raw << 3; // 9 bit resolution default
   if (data[7] == 0x10) {
     // count remain gives full 12 bit resolution
     raw = (raw & 0xFFF0) + 12 - data[6];
   }
 } else {
   byte cfg = (data[4] & 0x60);
   if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
   else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
   else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
   // default is 12 bit resolution, 750 ms conversion time
 }
 celsius = (float)raw / 16.0;
 fahrenheit = celsius * 1.8 + 32.0;
 dtostrf(fahrenheit, 5, 1, tempBuf); //turns float to string
 jsn= jsn + "\"sensor"+ ctr + "\": " + tempBuf + ", ";//appends temp to json
JSON from arduino to PHP server
php in /var/www/feeds/getnoise.php, arduino sketch /tim/Documents/electronics/Arduino/phptest/phptest.ino

Encoding pin values in JSON: <syntaxhighlight>

   // last pieces of the HTTP PUT request:
   client.println("Content-Type: application/json");
   client.println("Connection: close");
   client.println();
     
   client.print("{"); 
   // here's the actual content of the PUT request:
   for (int analogChannel = 2; analogChannel < 6; analogChannel++) {
     int sensorReading = analogRead(analogChannel);    
     String schan = "\"sensor" + analogChannel + "\":";
     client.print(schan);
     client.print(sensorReading);
     if (analogChannel != 5) {
         client.print(",");
     }
    }
    client.println("}");

</syntaxhighlight> Reading arduino JSON from PHP server: <syntaxhighlight> <?php $thefile=file_get_contents("php://input"); echo ($thefile); $thearray = json_decode($thefile); print_r($thearray); ?> </syntaxhighlight> Output produced: <syntaxhighlight> {"sensor2":376,"sensor3":349,"sensor4":251,"sensor5":285} stdClass Object (

   [sensor2] => 376
   [sensor3] => 349
   [sensor4] => 251
   [sensor5] => 285

) </syntaxhighlight> <sytaxhighlight> </syntaxhighlight>

version 4 - temp readings beyond serial monitor
a) separate out a function to provide a reading to send to cosm for pin 0(14) and 1(15)
1 one possibility - send to cosm. This is better than having a webclient like thermometer.ino. Get it to Cosm and then later set up an api like cosm
version 3 - onewire demo

Put sample code in function getTemp(). Subsequent calls of the function don't reset the device counter. this is good.

DS18B20wiring.PNG

On serial monitor running DS18x20_Temperature.ino ala the library here. Of note: How it polls each device: <syntaxhighlight>

 if ( !ds.search(addr)) {//this is where it loops to the next address
   Serial.println("No more addresses.");
   Serial.println();
   ds.reset_search();//if there are no more devices go back to start of list
   delay(250);
   return;
 }

</syntaxhighlight>

version 2
Online thermometer creating web page in which you call for the data by clicking a button. Uses design from Practical Arduino in /tim/Documents/electronics/arduino/thermometerthermometer.ino and it runs at http://10.0.1.79
version 1
Take random readings from analog pins start an etehrnet client that sends them out to http://cosm.com (mcktimo 6j) every ten seconds :comtest.ino

final narrative

An Avr takes in temperature readings from multiple locations. At the control room the AVR hub polls temperature and for each zone knows the set temperature, the default temperature and whether or not a zone is ON and how long it has been on. If it doesn't hear from the (linux) scheduler then it falls back to act as a multi-zone thermostat set at the default temperatures. Through a rudimentary display it would allow you to set temperatures for each zone.

The scheduler would take temperature data from the AVR client and would know the schedule. Whenever a schedule change point would come up, it would notify the AVR and instruct it to change the set point. It would do this via chrontab maybe.

The controller would on the client probably an HTML5/javascript client. Here you can read the temperatures and view the schedule. You can override or change the schedule.

possible routes

A cheap circuit like http://www.craig.copperleife.com/tech/thermo/ or more complicated http://www.rentron.com/project01.htm using http://datasheets.maximintegrated.com/en/ds/DS1620.pdf which can be overridden by server program that controls multiple thermostats.

ReST

cosm curl helper
http://api.cosm.com/v2/feeds/83080/datastreams/noise.csv?start=2013-01-11T19:30:00Z&end=2013-01-11T21:00:00Z&interval=0 doesn't work
http://cosm.github.com/cosm-js/tutorial/
accessing-incoming-put-data-from-php
create-a-rest-api-with-php
http://rest.elkstein.org/2008/02/using-rest-in-php.html
http://blog.garethj.com/2009/02/17/building-a-restful-web-application-with-php/
get for my feed
making-reliable-connections-with-arduino
websockets
http://code.google.com/p/phpwebsocket/
start-using-html5-websockets-today
cosm javascript library docs

Cron Jobs

Hudsonvalley#cron_and_backups
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs

parts

DS1620 Digital Thermometer and Thermostat

In a thermostat circuit $2.20 for dip

DS18B20 Maxim 1-wire temperature sensors
DS18B20 datasheet$1.00 each for chip, $3.00 each for waterproof sensor
one-wire protocol
https://arduino-info.wikispaces.com/Brick-Temperature-DS18B20 multiple
OneWire tutorial another better library
color code for encased DS18B20 sensor red=vcc,black=gnd,yellow=data

DS18B20-pinout.jpg