PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Aug 12, 2020 1:09 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Jul 02, 2016 5:11 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
I have a WiFi AP on my network which transmits a plain text 1860 byte data packet once every second. It's address is 192.168.4.1 It does not respond to requests.

fsockopen("192.168.4.1, 80 ...); gets the data but appears to escape any backslashes which are valid data characters and therefore corrupts the output. stream_socket_client() does the same. I wrote a routine to remove one backslash if there are two, but the data can contain \\ and \\\ as valid content ... so that won't work in all cases.

fopen() expects an URL like http://192.168.4.1, which doesn't work for me. Putting http:// in fsockopen() and stream_socket_client() also causes them to fail.

I can use fopen() to access external websites.

Appreciate any comments on how I can get valid data.

Thanks,
davef


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 6:38 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
What's your code?


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 12:17 pm 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Syntax: [ Download ] [ Hide ]
$fp = fopen("home/sbms_data", 'w');

  do
  {
    $fd = fsockopen("192.168.4.1", 80, $errno, $errstr, 30);
//    $fd = stream_socket_client("192.168.4.1:80", $errno, $errstr, 30);
    if (!$fd)
    {
      echo "ERROR: $errno - $errstr<br />\n";
      echo $errno;
    }

    stream_set_timeout($fd, 10);
    $number_of_bytes = stream_copy_to_stream($fd, $fp);
    fclose($fd);
  } while ($number_of_bytes != 1860);

 fclose($fp);
 


As a first line of defense I check that the packet size is correct, as sometimes it is 3720. Guess it depends on when you open the socket.


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 7:05 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
You are running PHP 5.4 or later, right?

And are you sure that you should be connecting and reconnecting like that? You can't just connect once?


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 8:20 pm 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
5.6.7-1

I will re-visit the connection process.


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 8:39 pm 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
If I move fsockopen() outside of the do loop I can't connect.

Syntax: [ Download ] [ Hide ]
  $fp = fopen($sbms_data, 'w');

   $fd = fsockopen("192.168.4.1", 80, $errno, $errstr, 30);
//  $fd = stream_socket_client("192.168.4.1:80", $errno, $errstr, 30);
  if (!$fd)
  {
    echo "ERROR: $errno - $errstr<br />\n";
    echo $errno;
  }

  do
  {
    stream_set_timeout($fd, 10);
    $number_of_bytes = stream_copy_to_stream($fd, $fp);
//    fclose($fd);
  } while ($number_of_bytes != 1860);

 fclose($fd);
 fclose($fp);
 


Does it matter what is happening on the socket when you try to connect to it? For example, if the source is sending data for 0.1sec each 1 sec could there be a conflict?


Top
 Profile  
 
PostPosted: Sat Jul 02, 2016 10:34 pm 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Looks like it is not a problem with fsockopen(), but how stream_copy_to_stream behaves later on. Moving fsockopen() outside of the while loop I get the socket but don't get the data.

I tried stream_set_blocking to FALSE still no luck.


Syntax: [ Download ] [ Hide ]
  $fp = fopen($sbms_data, 'w');

//  $fd = fsockopen("192.168.4.1", 80, $errno, $errstr, 30);

  echo "Got the socket";

  do
  {
    $fd = fsockopen("192.168.4.1", 80, $errno, $errstr, 30);
    stream_set_timeout($fd, 30);
    $number_of_bytes = stream_copy_to_stream($fd, $fp);
  } while ($number_of_bytes != 1860);

 fclose($fd);
 fclose($fp);

 echo "Got the data";
 


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 12:01 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Another clue. By putting echo $fd after the fsockopen(), while it is in the do/while loop, I always see more than 3 attempts at getting the full 1860 byte data packet.

I then tried reducing $number_of_bytes to > 100 and it makes one attempt but no data is saved to $sbms_data


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 12:18 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
That had me confused, and maybe it's not a matter of me misunderstanding. So here's some questions:

1. Before you said that the router does not "respond to requests". What do you mean by that?
2. For the router to transmit something, it has to send the data somewhere. Is it a broadcasted packet, like to a 25x address? Or do you mean that when you connect to the router that it will send data back automatically?
3. You're connecting to port 80. I know you had something working earlier but are you sure that's right? That's the HTTP port, and what you're describing is not HTTP.
4. Is there documentation for this feature anywhere online?
5. Connecting inside or outside the loop should not matter. When you say you can't connect, do you mean that you're getting that ERROR message now when you weren't earlier?


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 2:46 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Thank you for continuing to drill-down.

1) The WiFi AP is a ESP8266 sending a custom-designed data packet. It has been set up to ignore any requests. A web browser can look at the data and a HTML page, written in Java, is provided which displays the information. The data is send every second and lasts for a small chunk of that time.

2) Not sure if I understand. This module just transmits the information in plain text, without any request or set up of the connection.

3) I tried port 25 and it didn't even open the socket. I thought I checked somehow, but port 80 works after a fashion.

4) see 1.

5) I haven't see any error messages in php_error.log related to the connection setup or in that:
Syntax: [ Download ] [ Hide ]
echo "ERROR: $errno - $errstr<br />\n";
statement

I do not have any real PHP networking function experience.


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 3:08 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Found a script:

Syntax: [ Download ] [ Hide ]
$services = array('http', 'ftp', 'ssh', 'telnet', 'imap',
'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www');

foreach ($services as $service) {
    $port = getservbyname($service, 'tcp');
    echo $service . ": " . $port . "<br />\n";
}
 


and got

http: 80

ftp: 21

ssh: 22

telnet: 23

imap: 143

smtp: 25

nicname: 43

gopher: 70

finger: 79

pop3: 110

www: 80


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 4:05 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 4:28 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
I have the code that runs on the unit. He has a link to SBMS100-v01a.zip on his Google+ page. I'll have a look through it, so any suggestions what to look for would be helpful.

Here is what I see:

Syntax: [ Download ] [ Hide ]
<!DOCTYPE html>
<html>
<head>
<title>ElectroDacus</title>
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<meta http-equiv='refresh' content='03'>
<style media='screen' type='text/css'>
meter {-webkit-appearance:none;-moz-appearance:none;appearance:none;width:180px;height:12px;position:absolute;left:10px;box-shadow:1px 2px 3px #ec6;}
.bar{position:absolute;bottom:0px;display:inline-block;}
mx1{border:1px solid #fff;color:#fff;padding:0px 3px}
mn1{border:1px solid #fe9;color:#fe9;padding:0px 3px}
Val{color:#fed;}
txt{color:#a99;}
V5{color:#000;white-space:pre;}
v0{background:#fc4;}
v1{background:#e6a;}
v3{background:#0f0;}
v4{background:#f00;}
v6{background:#01f;}
v7{background:#e9f;}
lt{background: #fa6;}
div{position: relative;float:left;}
div2{position: absolute;color:#fed;line-height: 21px;}
div3{position: relative;height:180px; width:720px;float:left;font-family:Arial,serif;font-weight: bold;font-size:18px;}
div12h{position:absolute;width: 359px;height: 160px;left:0px;color:#300;background: rgba(40,20,0,0.5);}
div1h{position:absolute;width: 179px;height: 160px;left:360px;color:#300;background: rgba(120,90,0,0.6);}
div1m{position:absolute;width: 180px;height: 160px;left:540px;color:#300;background: rgba(30,70,0,0.5);}
div4{position:absolute;width: 236px;height: 22px;bottom:9px;color:#211;background: #fa5;}
div5{position:absolute;background: rgba(120,90,0,0.4);}
button{color:#b50;}
</style>
</head>
<body style='background: #000;' >
<div3 style='height: 135px;'>
<canvas id='Lg' width='70' height='120' style='position:relative; top:11px; left:12px; z-index:2; float: left;'></canvas>
<div2 style='top:12px; left:360px; color:#d92;text-shadow:-1px -2px 1px #fd4,1px 2px 2px #fea;font-size:40px;' id='id'></div2>
<div2 style='width:350px; top:82px; left:90px; color:#be5;float:none;'><div >www.ElectroDacus.com</div>
<div style='color:transparent; -webkit-transform: rotateX(180deg);transform: rotateX(180deg);-ms-transform:rotateX(180deg); text-shadow: 0px 0px 1px #371;'>www.ElectroDacus.com</div></div2>
<div2 id="demo"></div2>
<div2 style='width:350px; top:82px; left:520px;'><button onclick="dload('sbms.txt', localStorage['sbms']);">Save log</button><button onclick="localStorage.removeItem('sbms');localStorage['sbms'] =''";);">Clear log</button></div2>

</div3>
<div3>
<meter id='bat' style='height: 70px; width: 320px; top: 9px;' min='0' low='20' max='100'></meter>
<meter style='height: 50px; left: 332px; top:18px; width: 15px;' min='2' max='8' value='0'></meter>
<div2 style='color:#030;font-size:48px;top:35px;left:120px;text-shadow: -2px -2px 2px #efc;' id='SOC' ></div2>
<div2 style='left:360px;color:#ea8;' id='d2'></div2>
<div2 style='left:430px;' id='d3'></div2>
<div2 style='left:490px;color:#888;' id='d4'></div2>
<div2 style='left:5px;top:92px;color:#a99;line-height: 25px;' id='d12'></div2>
<div2 style='left:510px;' id='mt1'></div2>
</div3>
<div3>
<div2 style='left:5px;color:#ea8;top:22px;'id='d6'></div2>
<div2 style='right:530px;text-align: right;'id='d7'></div2>
<div2 style='right:430px;text-align: right;'id='d8'></div2>
<div2 style='right:220px;text-align: right;'id='d9'></div2>
<div2 style='right:10px;text-align: right;'id='d10'></div2>
<div5 style='width: 720px;top:0px;height:22px;text-align: right;'id='d11'></div5>
<div4 id='mo0'>PV1 & PV2</div4>
<div4 id='mo2' style='left:240px;'>Battery</div4>
<div4 id='mo1' style='left:480px;'>Load</div4>

</div3>
<div3><div style='background: #fb9;width: 720px;height:25px;'></div><div12h id='g0'></div12h><div1h id='g1'></div1h><div1m id='g2'></div1m><div id='ch0' style='width: 360px;height: 135px;'></div></div3>
<div3><div style='background: #fb9;width: 720px;height:25px;'></div><div12h id='g3'></div12h><div1h id='g4'></div1h><div1m id='g5'></div1m><div id='ch1' style='width: 360px;height: 135px;' ></div></div3>
<div3><div style='background: #fb9;width: 720px;height:25px;'></div><div12h id='g6'></div12h><div1h id='g7'></div1h><div1m id='g8'></div1m><div id='ch2' style='width: 360px;height: 135px;' ></div></div3>

<script id='smain2'>
all();
function all(){

var PV1=sessionStorage['PV1'];
var PV2=sessionStorage['PV2'];
var Batp=sessionStorage['Batp'];
var Batn=sessionStorage['Batn'];
var Ld=sessionStorage['Ld'];
var ELd=sessionStorage['ELd'];

var sbms=localStorage['sbmsb'];
var xsbms=localStorage['xsbms'];
var gsbms=localStorage['gsbms'];
var eA=localStorage['eA'];
var eW=localStorage['eW'];
var sbms2=JSON.parse(localStorage['sbms2']);
var sbms1=['','Batt','PV1','PV2','ExtLd','PV1+PV2','Load','ExtLd',localStorage['cap'],localStorage['WA'],localStorage['model']];
var lg1="
#B33A33B##333333B##B33A33B##B33333B##B''''''##A44A544##B44444B##;75444A##144B444##B33333B##444444B##2331$$A##B8:B:8B#";

var c = document.getElementById('Lg');
var ctx = c.getContext('2d');
var r ='</br>'



function htm(id,s){document.getElementById(id).innerHTML =s;};
function pad(n, width, z) {z=z || '0';n=n+'';return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;}
function dcmp(p,s,d){xx=0; for (z=0;z<s;z++){xx = xx + ((d.charCodeAt((p+s-1)-z)-35)*Math.pow(91,z));}return xx;}
function fN(nm){return nm.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 , ')}
htm('id',sbms1[10]);
var SOC='';SOC =dcmp(6,2,sbms);htm('SOC','<b>'+SOC+'%</b>');document.getElementById('bat').value=SOC;

if (sbms2[10]!=1){mos(0);};if (sbms2[11]!=1){mos(1);};function mos(rr){document.getElementById('mo'+rr).style.background='rgba(120,90,0,0.7)';}




drawChart('PV1 ','PV2 ',0,PV1,PV2,'#ch0','#fc4','#e6a',1,2,200);
drawChart('Batp ','Batn ',1,Batp,Batn,'#ch1','#0f0','#f00',1,2,200);
drawChart('Load','ExtLd',2,Ld,ELd,'#ch2','#01f','#e9f',1,2,200);




for (i=0;i<15;i++){setInterval(function(){lg(lg1);},500*i);};

function lg(d){
var k = new Date();
var n = k.getSeconds();
if (n>=40){n=n-40};
if (n>=20){n=n-20};
if (n>12){n=12};
ctx.clearRect(0, 0, 70, 120);
for (y=0;y<9;y++){
for (x=0;x<7;x++){
var pix2=((pad(((d.charCodeAt(y+(12*9))-35).toString(2)),6)).charCodeAt(x))-48;
var pix=((pad(((d.charCodeAt(y+(n*9))-35).toString(2)),6)).charCodeAt(x))-48;
if (pix==1){col1='#be9';col2='rgba(142,204,104,0.'}
else if (pix2==1){col1='#694';col2='rgba(66,104,44,0.'}
else {col1='#361';col2='rgba(42,84,36,0.'}
ctx.fillStyle =col1;
ctx.fillRect(x*10, y*10, 8, <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" />;
if(y>=6){ctx.fillStyle =col2+(((y*2)-2)-8)+')';ctx.fillRect(x*10, (17.3-y)*10, 8, <img src="./images/smilies/icon_cool.gif" alt="8)" title="Cool" />;}
}}}

function drawChart(n,m,k,d1,d2,sl,cl,cl2,p,b,bt)
{
var cht = document.querySelector(sl);
var l=0;
for (i=0;i<240;i++)
{
var h1=(((dcmp(i,1,d1))/180)*bt);
var h2=(((dcmp(i,1,d2))/180)*bt);
var f2 = document.createElement('div');
var f = document.createElement('div');
var f3 = document.createElement('div');
f.setAttribute('class', 'bar');
f2.setAttribute('class', 'bar');
f3.setAttribute('class', 'bar');
f.style.background=cl;
f2.style.background=cl2;
f3.style.background='#322';
f.style.width=b+'px';
f2.style.width=b+'px';
f3.style.width=b+'px';
f.style.height=h1+'%';
f2.style.height=h2+'%';
f3.style.height=(100-(h1+h2))+'%';
f.style.left=l+'px';
f2.style.left=l+'px';
f3.style.left=l+'px';
f2.style.bottom=h1+'%';
f3.style.bottom=h2+h1+'%';
cht.appendChild(f);
cht.appendChild(f2);
cht.appendChild(f3);
l += (b+p);    
}
k=k*3;
var sp='        ';
var dd=3;ss=1000;b0='12h'+sp;
if (sbms1[9]=='W'){dd=1;ss=10};

for (i=0;i<3;i++){if (i==1){n=m='';b0='1h';}if (i==2){n=m='';b0='1m';}htm('g'+(k+i),'<V5> '+b0+'<v'+(k)+'>'+n+'</v'+(k)+'><v'+(k+1)+'>'+m+'</v'+(k+1)+'>'+sp+(dcmp((k+i)*3,3,gsbms)/ss).toFixed(dd)+sbms1[9]+'</V5>');}}


mt8('#mt1');
function mt8(m1) {
var w = new Array();
for (i=0;i<20;i++){w[i]='';}
var bv=pv3=sv=max1=min1=0;
for (x1=0;x1<8;x1++)
{
var n=n1='';  
var cv=dcmp((x1*2)+8,2,sbms)/1000;
if (sbms2[9]==x1+1){min1=cv;n='<mn1>';n1='</mn1>';};
if (sbms2[8]==x1+1){max1=cv;n='<mx1>';n1='</mx1>';};
w[0] +=n+'Cell. '+ (x1+1)+n1+r ;
w[1] +=n+cv.toFixed(3)+n1+r;
if (sbms2[x1]!=1){w[2] +='<txt>V</txt>'+r;}
else {w[2] +='<lt><</lt>'+r;};
bv +=cv;
var mt = document.querySelector(m1);
var x = document.createElement('meter');
x.setAttribute('min',dcmp(5,2,xsbms)/1000);
x.setAttribute('max',dcmp(3,2,xsbms)/1000);
x.setAttribute('value',cv);
x.style.top=((x1*21)+3)+'px'
mt.appendChild(x);
}
for (i=2;i<5;i++){htm('d'+i,w[i-2]);}
for (x1=0;x1<7;x1++)
{
var n2=w[8]=w[9]=w[10]=w[11]='';
var cv=dcmp((x1*3)+29,3,sbms)/1000;
var enW=dcmp(x1*6,6,eW);
var enA=dcmp(x1*6,6,eA);
if (x1==0){n2=sbms.charAt(28);w[8]='[A]'+r;w[9]='[W]'+r;w[10]='[MAh][kAh][Ah][mAh]'+r;w[11]='[MWh][kWh][Wh]'+r;};
if (x1==1||x1==2){pv3 +=cv;};
if (x1==3){sv=cv;}
if (x1==4){cv=pv3;}
if (x1==5){cv=dcmp(0,3,xsbms)/1000;}
if (x1==6){cv=sv;}  
if(x1!=3){
w[3] +=sbms1[x1+1]+r ;
w[4] +=w[8]+n2+cv.toFixed(3)+r;
w[5] +=w[9]+n2+(cv*bv).toFixed(1)+r;
w[6] +=w[10]+fN(enA)+r;
w[7] +=w[11]+fN((enW/10).toFixed(1))+r;
}
   
}  
for (i=6;i<11;i++){htm('d'+i,w[i-3]);}
htm('d'+12,'Type: '+ dcmp(7,1,xsbms)+ ' Cap: '+dcmp(8,3,xsbms)+sbms1[8]+' Status: '+dcmp(56,3,sbms)+r+'SBMS Temp Int: '+ ((dcmp(24,2,sbms)/10)-45).toFixed(1)+'&#8451 Ext: '+ ((dcmp(26,2,sbms)/10)-45).toFixed(1)+'&#8451'+r+'BattVoltage <Val>'+ bv.toFixed(3)+'</Val> V Cell &#916 <Val>'+((max1-min1)*1000).toFixed(0)+'</Val> mV');
}



}
function dload(n,t) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(t));
pom.setAttribute('download', n);
if (document.createEvent) {var event = document.createEvent('MouseEvents');event.initEvent('click', true, true);pom.dispatchEvent(event);}
else {pom.click();}}
//http://192.168.4.1/

</script>

<script type="text/javascript" src="http://192.168.4.1/"></script>
<script>
sessionStorage['PV1']=PV1;
sessionStorage['PV2']=PV2;
sessionStorage['Batp']=Btp;
sessionStorage['Batn']=Btn;
sessionStorage['Ld']=Ld;
sessionStorage['ELd']=ELd;

localStorage['sbmsb']=sbms;
localStorage['xsbms']=xsbms;
localStorage['gsbms']=gsbms;
localStorage['eA']=eA;
localStorage['eW']=eW;
localStorage['cap']=s1[0];
localStorage['WA']=s1[1];
localStorage['model']=s1[2];
localStorage['sbms2']=JSON.stringify(s2);
localStorage['sbms'] =localStorage['sbms']+sbms+'\n';

all();
</script>

</body  >
</html>
 


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 4:42 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
A snippet only because I see the letters "AT..."

Syntax: [ Download ] [ Hide ]
void WiFi_Init(void)
{
            uint16_t jj;
            char command1[] = "AT+RST\r\n";
                char command2[] = "ATE0\r\n";
                char command3[] = "AT\r\n";
                //char command4[] = "AT+RFPOWER=60\r\n";
                char command4[] = "AT+CWSAP_DEF=\"SBMS-100\",\"             \",02,0\r\n";
 


He has been very helpful but doesn't know the specifics of PHP equivalents.

If you drop 192.168.4.1 into Firefox it returns the data with no errors.


Top
 Profile  
 
PostPosted: Sun Jul 03, 2016 4:46 am 
Offline
Forum Newbie

Joined: Sat Jul 02, 2016 5:05 am
Posts: 20
Ah, I see an 80

Syntax: [ Download ] [ Hide ]
void WiFi_set(void)

{

        char *cl1,*cl2,*cl3,*cl4;

    char command1[] = "ATE0\r\n";
    char command2[] = "AT+CWMODE=2\r\n";
    char command3[] = "AT+CIPMUX=1\r\n";
    char command4[] = "AT+CIPSERVER=1,80\r\n";
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group