PHP код:
function GetAnswer($handle, $timetowait)
{
// echo "test";
$currt=time();
$cur="";
$i=0;
$j=0;
for ($data="";($data!="\0")&&(time()-$currt<$timetowait);)
{
$i++;
$data = fread($handle, 1);
if ($data!="")
$j++;
if ($data!="\0")
$cur.=$data;
}
// echo $cur;
// echo $i.";".$j;
return $cur;
}
function SendCommand($handle,$str)
{
fputs($handle,$str);
// echo "command:".$str."<br>";
$a=GetAnswer($handle,2);
$data=str_replace("\n","<br>",$a);
// echo $data."<br>";
}
exec("stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -c
rtscts");
$handle = fopen("/dev/ttyUSB0", "r+"); //format: dio.serial://[device-name]
if (!stream_set_blocking($handle,0))
echo "cant set blocking mode";
if ($handle) {
echo "Initialize port... done<br>";
$a=fread($handle,0);
//echo "before:".$a."<br>";
SendCommand($handle,"at\n");
SendCommand($handle,"info\n");
SendCommand($handle,"scan\n"); // не анализируем вывод - он может быть только ОК.
fputs($handle,"list\n");
$a=GetAnswer($handle,2);
//echo "list:".$a."<br>";
$Lines=explode("\n",$a); // делим на строки
for ($i=0;$i<count($Lines);$i++)
{
// echo $Lines[$i]."<br>";
$Params=explode(";",$Lines[$i]); // делим на параметры
if (count($Params)>=4)
{
$Type=$Params[0]; // тип устройства 1W/pin
$Pin=$Params[1]; // номер пина
$Id=$Params[2]; // id
$ReadParams=$Params[3];
$WriteParams=$Params[4];
...
}
}
Код скетча.
PHP код:
#include <OneWire.h>
// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
#define OneWirePinCount 2
#define OneWirePin0 10
#define OneWirePin1 9
byte OneWirePins[OneWirePinCount];
#define IOPinCount 11
byte IOPins[IOPinCount] ={3,4,5,6,7,8,11,12,13,A2,A3};
#ifdef OneWirePin0
OneWire ds0(OneWirePin0);
#endif
#ifdef OneWirePin1
OneWire ds1(OneWirePin1);
#endif
#ifdef OneWirePin2
OneWire ds2(OneWirePin2);
#endif
#ifdef OneWirePin3
OneWire ds3(OneWirePin3);
#endif
#ifdef OneWirePin4
OneWire ds4(OneWirePin4);
#endif
#define Max1WCount 20
#define MaxCommandLength 20
byte Command[MaxCommandLength];
byte CommandLength=0;
byte at_Command[] PROGMEM = "at";
byte info_Command[] PROGMEM = "info";
byte List_Command[] PROGMEM = "list";
byte Scan_Command[] PROGMEM = "scan";
byte Measure_Command[] PROGMEM = "measure";
byte Read_Command[] PROGMEM = "read";
byte Print_Command[] PROGMEM = "print";
byte Set_Command[] PROGMEM = "set";
byte addr[Max1WCount][9];
int16_t Datas[Max1WCount];
OneWire* dss[OneWirePinCount];
byte IOPin[IOPinCount];
int8_t OneWireDeviceCount;
void setup(void) {
#ifdef OneWirePin0
dss[0]=&ds0;
OneWirePins[0]=OneWirePin0;
#endif
#ifdef OneWirePin1
dss[1]=&ds1;
OneWirePins[1]=OneWirePin1;
#endif
#ifdef OneWirePin2
dss[2]=&ds2;
OneWirePins[2]=OneWirePin2;
#endif
#ifdef OneWirePin3
dss[3]=&ds3;
OneWirePins[3]=OneWirePin3;
#endif
#ifdef OneWirePin4
dss[4]=&ds4;
OneWirePins[4]=OneWirePin4;
#endif
// dss[0]=&ds1;
// dss[1]=&ds2;
OneWireDeviceCount=0;
Serial.begin(115200);
}
void Print1WId(byte *id)
{
for(uint8_t i = 0; i < 8; i++) {
Serial.print(*id >>4, HEX);
Serial.print(*id & 15, HEX);
if (i==0)
Serial.write('.');
id++;
}
}
void Scan(void)
{
OneWire* ds;
OneWireDeviceCount=0;
for (uint8_t i=0;i<OneWirePinCount;i++)
{
ds=dss[i];
ds->reset_search();
for (;ds->search(addr[OneWireDeviceCount]);)
{
if (OneWire::crc8(addr[OneWireDeviceCount], 7) == addr[OneWireDeviceCount][7])
{
addr[OneWireDeviceCount][8]=i;
OneWireDeviceCount++;
}
}
}
}
bool IsPinAnalogWrite(uint8_t Pin)
{
return digitalPinToTimer(Pin)!=NOT_ON_TIMER;
}
bool IsPinAnalogRead(uint8_t pin)
{
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; else pin=255; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; else pin=255;// allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; else pin=255;// allow for channel or pin numbers
#endif
return pin!=255;
}
void List(void)
{
for (byte i=0;i<OneWireDeviceCount;i++)
{
Serial.print("1W;");
Serial.print(OneWirePins[addr[i][8]]);
Serial.print(";");
Print1WId(addr[i]);
byte j=addr[i][0];
if ((j==0x10)||(j==0x22)||(j==0x28))
{
Serial.print(";T;");
}
Serial.println();
}
for (byte i=0;i<IOPinCount;i++)
{
byte pin=IOPins[i];
Serial.print("Pin;");
Serial.print(pin);
Serial.print(";P");
Serial.print(pin);
Serial.print(";M,D,P");
if (IsPinAnalogRead(pin))
Serial.print(",A");
Serial.print(";M,D");
if (IsPinAnalogWrite(pin))
Serial.print(",A");
Serial.println();
}
}
void StartMeasureTemp(void)
{
for (uint8_t j=0;j<OneWirePinCount;j++)
{
OneWire* ds;
ds=dss[j];
ds->reset();
ds->skip();
ds->write(0x44,1); // start conversion, with parasite power on at the end
}
/* for (int8_t j=0;j<Count;j++)
{
OneWire* ds;
ds=dss[addr[j][8]];
ds->reset();
ds->select(addr[j]);
ds->write(0x44,1); // start conversion, with parasite power on at the end
}*/
}
int8_t type_s(uint8_t index)
{
switch (addr[index][0]) {
case 0x10:
return 1;
case 0x28:
return 0;
case 0x22:
return 0;
default:
return -1;
}
}
void ReadTemps(void)
{
byte data[12];
for (int j=0;j<OneWireDeviceCount;j++)
{
OneWire* ds;
ds=dss[addr[j][8]];
ds->reset();
ds->select(addr[j]);
ds->write(0xBE); // Read Scratchpad
for (uint8_t i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds->read();
}
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
if (type_s(j)) {
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
}
Datas[j] = raw;
// fahrenheit = celsius * 1.8 + 32.0;
}
}
void PrintTemps(void)
{
for (int j=0;j<OneWireDeviceCount;j++)
{
float celsius=(float)Datas[j]/16.0;
Print1WId(addr[j]);
Serial.print(";");
Serial.println(celsius);
}
}
bool GetPinState(byte pin)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;
if (port == NOT_A_PIN) return false;
// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
out = portOutputRegister(port);
return *out & bit;
}
bool GetPinMode(byte pin)
{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg;
if (port == NOT_A_PIN) return false;
reg = portModeRegister(port);
return *reg & bit;
// if (mode == INPUT) {
// *reg &= ~bit;
// } else {
// *reg |= bit;
// }
}
void PrintPins()
{
for (byte i=0;i<IOPinCount;i++)
{
byte pin=IOPins[i];
Serial.print("P");
Serial.print(pin);
Serial.print(";");
if (GetPinMode(pin))
// if (true)
Serial.print("1,");
else
Serial.print("0,");
if (GetPinState(pin))
// if (false)
Serial.print("1,");
else
Serial.print("0,");
// if (true)
if (digitalRead(pin)==HIGH)
Serial.print("1");
else
Serial.print("0");
if (IsPinAnalogRead(pin))
{
Serial.print(",");
Serial.print(analogRead(pin));
}
Serial.println();
}
}
bool FindNextString(byte &first, byte &length)
{
for (;(first<CommandLength)&&(Command[first]==' ');first++);
if (first>=CommandLength)
return false;
else
{
for(length=1;(first+length<CommandLength)&&(Command[first+length]!=' ');length++);
return true;
}
}
bool TestSubstr(byte first,byte length,byte* str)
{
byte i;
for (i=0;(i<length)&&(pgm_read_byte(&str[i])==Command[first+i]);i++);
if (i<length)
return false;
else
return (pgm_read_byte(&str[length])==0);
}
byte CharToInt(byte ch)
{
if ((ch>='0')&&(ch<='9'))
return ch-'0';
else
return 255;
}
void ProceedCommand(void)
{
byte first;
uint8_t length;
first=0;
byte Error=0;
if (FindNextString(first,length))
// for (first=0;FindNextString(first,length);first=first+length+1)
{
// Serial.write("Command:");
// for (byte i=0;i<length;i++)
// Serial.write(Command[first+i]);
// Serial.write("\n");
// Serial.write(0x10);
if (TestSubstr(first,length,at_Command))
{
Serial.write("OK\n");
}
else
if (TestSubstr(first,length,info_Command))
{
Serial.write("ver 0.1\n");
Serial.write("OneWire;");
for (byte i=0;i<OneWirePinCount;i++)
{
if (i>0)
Serial.write(";");
Serial.print(OneWirePins[i]);
}
Serial.write("\nIOPins;");
for (byte i=0;i<IOPinCount;i++)
{
if (i>0)
Serial.write(";");
Serial.print(IOPins[i]);
}
Serial.write("\n");
}
else
if (TestSubstr(first,length,Scan_Command))
{
Scan();
Serial.write("OK\n");
}
else
if (TestSubstr(first,length,List_Command))
{
// Serial.write("Nothing to do\n");
List();
}
else
if (TestSubstr(first,length,Measure_Command))
{
StartMeasureTemp();
// delay(750);
Serial.write("OK\n");
}
else
if (TestSubstr(first,length,Print_Command))
{
// Serial.write("OK\n");
PrintTemps();
PrintPins();
}
else
if (TestSubstr(first,length,Read_Command))
{
Serial.write("OK\n");
ReadTemps();
}
else
if (TestSubstr(first,length,Set_Command))
{
first+=length+1;
if (FindNextString(first,length))
{
if ((Command[first]=='P')&&((length==2)||(length==3)))
{
byte p2;
byte p1=CharToInt(Command[first+1]);
if (length==2)
{
p2=p1;
p1=0;
}
else
p2=CharToInt(Command[first+2]);
byte pin,comm,value;
if ((p1==255)||(p2==255))
Error=true;
else
pin=p1*10+p2;
if (!Error)
{
first+=length+1;
if (FindNextString(first,length))
{
if (length!=1)
Error=true;
else
comm=Command[first];
}
else
Error=true;
}
if (!Error)
{
first+=length+1;
if (FindNextString(first,length))
{
if (length>3)
Error=true;
else
{
value=0;
for (byte i2=0;i2<length;i2++)
{
byte w=CharToInt(Command[first+i2]);
if (w==255)
Error=true;
else
value=value*10+w;
}
}
}
else
Error=true;
}
if (!Error)
{
if (comm=='M')
{
if (value==0)
pinMode(pin,INPUT);
else
if (value==1)
pinMode(pin,OUTPUT);
else
Error=true;
}
else
if (comm=='D')
{
if (value==0)
digitalWrite(pin,LOW);
else
if (value==1)
digitalWrite(pin,HIGH);
else
Error=true;
}
else
if (comm=='A')
{
if (IsPinAnalogWrite(pin))
analogWrite(pin,value);
else
Error=true;
}
else
Error=true;
}
Serial.write("OK\n");
}
else
Error=true;
}
else
Error=true;
// Serial.write("OK\n");
// ReadTemps();
}
else
Error=true;
if (Error)
Serial.write("Error/n");
Serial.write((byte)0);
}
}
void loop(void) {
byte i;
byte present = 0;
// Serial.write('a');
// delay(5000);
if (Serial.available())
{
char ch;
ch=Serial.read();
// Serial.write(ch);
if ((ch==10)||(ch==13))
{
if (CommandLength>0)
ProceedCommand();
CommandLength=0;
}
else
if (CommandLength<MaxCommandLength)
{
Command[CommandLength]=ch;
CommandLength++;
}
}
// byte type_s;
/* Scan();
if (Count>0)
{
StartMeasureTemp();
}
delay(750); // maybe 750ms is enough, maybe not
ReadTemps();
PrintTemps();
delay(1000);*/
// Serial.println(" Celsius, ");
// Serial.print(fahrenheit);
// Serial.println(" Fahrenheit");
}