-
Got it working better
05/13/2015 at 19:41 • 0 commentsWell I did find a work-around to the issue by adding the following lines after the results of the photoresistor and temperature are sent to clear the incoming serial buffer (a delay was also needed):
delay(250); while(Serial.available()) Serial.read();
When I tried things on my Raspberry Pi, the above fix worked but I was still having issues with not getting a response message/data at times. The work-around was putting the the serial interaction in a while loop until I finally get the return data:
if (isset($_POST['button1'])) { while( empty($read)){ $serial->deviceOpen(); $serial->sendMessage("9"); //usleep(200000); $read = $serial->readPort(); $serial->deviceClose(); } }
A side-effect of this work-around is a slightly longer wait to get something such as a temperature value.
-
Working on making things stable
05/08/2015 at 00:58 • 0 commentsMy current task is getting the darn thing stable, in some enviroments it works perfectly; in others, not so much.
Environment 1:
OS CentOS 6.6 (running as a VirtualBox guest)
Apache version: 2.2.15
PHP version: 5.3.3
Result: fairly stable, very rarely do I not get a sensor reading
Environment 2:
OS: CentOS7.1 (tried both a dedicated physical machine and a VirtualBox guest)
Apache version: 2.4.6
PHP version: 5.4.16
Result: When I take a reading of my photoresistor, one or both of my two LED's turns off/on. Sometimes I do not get a sensor reading.
Environment 3:
OS:Raspbian
Apache version:2.2.22
PHP version:5.4.39
Result: When I take a reading of my photoresistor, one or both of my two LED's turns off/on. Sometimes I do not get a sensor reading.I've tested things under Minicom and all works well, which tells me the issue is on the PHP side of things.
Arduino code:
//temp sensor vars boolean neg=0; byte temp=0; float tempb=0; // end byte hybyte=0; int result=0; void setup() { // put your setup code here, to run once: DDRD|=B11000000; //setting pins 20 and 21 as outputs ADMUX=B01000111;//set adc reference and select channel ADCSRA=B10000110;//enable adc and set prescaler to divide by 64 //temp sensor pins PORTC |=B11000000; DDRC |=B11000000;// pc7 as ada and pc6 as sck //end Serial.begin(9600); DS1631startconvert(); } void loop() { // put your main code here, to run repeatedly: //cal = OSCCAL; if (Serial.available()){ int ser = Serial.read(); if (ser == 52){//keyboard key 4 to read the temp sensor DS1631readtemp(); delay(50); tempb=(tempb*(9.0/5.0))+32; Serial.println(tempb,4);//4 decimal places neg=0; temp=0; tempb=0; } else if (ser == 53){//keyboard key 5 to turn on led1 PORTD |=B01000000; delay(50); Serial.println("Object1 enabled"); } else if(ser == 54){//keyboard key 6 to turn off led1 PORTD &=~B01000000; delay(50); Serial.println("Object1 disabled"); } else if(ser == 55){//keyboard key 7 to turn led2 on PORTD |=B10000000; delay(50); Serial.println("Object2 enabled"); } else if(ser == 56){//keyboard key 8 to tourn led2 off PORTD &=~B10000000; delay(50); Serial.println("Object2 disabled"); } else if(ser == 57){//keyboard key 9 to read the photoresistor delay(50); adcread(); Serial.println(result); } } } void adcread(){ ADCSRA |= (1 << ADSC); // start ADC measurement while (ADCSRA & (1<<ADSC)); // wait till conversion complete result=ADCW; } //temp sensor reading void start(){ DDRC|=_BV(DDC7); PORTC|=_BV(DDC7); PORTC|=_BV(DDC6); PORTC&=~_BV(DDC7); PORTC&=~_BV(DDC6); } void stop1(){ DDRC|=_BV(DDC7); PORTC&=~_BV(DDC6); PORTC&=~_BV(DDC7); PORTC|=_BV(DDC6); PORTC|=_BV(DDC7); } void clk(){ PORTC|=_BV(DDC6); PORTC&=~_BV(DDC6); //delay(1); } void DS1631startconvert(){ start(); //send address //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW);//write PORTC&=~_BV(DDC7); clk(); //ignore ack clk(); //start t convert command //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); clk();//ignoring ack stop1(); } void DS1631readtemp(){ start(); //send device address //send address //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW);//write PORTC&=~_BV(DDC7); clk(); //ignore ack clk(); //send read temp command AAh //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); clk(); //ignoring ack again stop1(); start(); //send device address //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC|=_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH); PORTC&=~_BV(DDC7); clk(); //digitalWrite(sda,HIGH);//read PORTC|=_BV(DDC7); clk(); //pinMode(sda,INPUT);//change pin from output to input for readin DDRC&=~_BV(DDC7); byte raw=0; clk(); //read the temp //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+128; neg=1; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+64; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+32; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+16; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+8; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+4; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+2; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ temp=temp+1; } clk(); //doing more significant read /* 0000 .5 1 .25 2 .125 3 .0625 4 */ //pinMode(sda,OUTPUT); DDRC|=_BV(DDC7); //digitalWrite(sda,LOW); PORTC&=~_BV(DDC7); clk(); //pinMode(sda,INPUT); DDRC&=~_BV(DDC7); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ tempb=tempb+.5; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ tempb=tempb+.25; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ tempb=tempb+.125; } clk(); //raw=digitalRead(sda); raw=PINC & _BV(PC7); if(raw==128){ tempb=tempb+.0625; } clk(); clk(); clk(); clk(); clk(); clk(); stop1(); if (neg==1){ temp=temp-1; temp=~(temp); tempb=(tempb+temp)*-1; } else{ tempb=tempb+temp; } }
php code:
<!DOCTYPE html> <head> <link rel="stylesheet" type="text/css" href="main.css"> <title>Cpannel</title></head> <body> <div id="title">Control Panel</div> <div id="panel"> <form method='post'> <input type='submit' name='button1' value='Read Photo' id="buttona" class="button"/> <input type='submit' name='button2' value='Turn off' id="buttonb" class="button"/> <input type='submit' name="button3" value='Read Temp' id="buttonc" class="button"/> <input type='submit' name="button4" value='Clear' id="buttond" class="button"/> </form> <form method='post'> <input type='submit' name='button5' value='Ob1 on' id="buttone" class="button"/> <input type='submit' name='button6' value='Ob1 off' id="buttonf" class="button"/> <input type='submit' name="button7" value='Ob2 on' id="buttong" class="button"/> <input type='submit' name="button8" value='Ob2 off' id="buttonh" class="button"/> </form> </div> <div class="status"> <br />Status:<br /> </div> <?php include "php_serial.class.php"; $serial = new phpSerial; $serial->deviceSet("/dev/ttyUSB0"); $serial->confBaudRate(9600); date_default_timezone_set("UTC"); if (isset($_POST['button1'])) { $serial->deviceOpen(); $serial->sendMessage("9"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } if (isset($_POST['button2'])) { echo "button2 has been pressed"; } if (isset($_POST['button3'])) { $serial->deviceOpen(); $serial->sendMessage("4"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } if (isset($_POST['button4'])) { echo ""; } if (isset($_POST['button5'])) { $serial->deviceOpen(); $serial->sendMessage("5"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } if (isset($_POST['button6'])) { $serial->deviceOpen(); $serial->sendMessage("6"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } if (isset($_POST['button7'])) { $serial->deviceOpen(); $serial->sendMessage("7"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } if (isset($_POST['button8'])) { $serial->deviceOpen(); $serial->sendMessage("8"); $read = $serial->readPort(); echo "<div class='status'>" . $read . "</div>"; $serial->deviceClose(); echo "<div class='status'>" . date("h:i:sa") . "</div>"; } ?> </body> </html>