combine 3 projects as one

combine 3 projects as one

Postby Danioool » Sat Aug 12, 2017 12:12 am

Hi, I tried to work with 3 projects at one: Web Push/Web Slide/Web Thermometer

I added the remote_thermometer_php to PHPOC, put another line to the "index_php"
(<a href="remote_thermometer.php">Remote Thermometer </a>).

Web Interface works well, i can select different types of controls.
But:
I tried to combine somehow the Arduino codes of all 3 .ino files:
I did not work properly:
Here is the code:
"#include <OneWire.h>
#include <DallasTemperature.h>
#include "SPI.h"
#include "Phpoc.h"

// Data wire is plugged into port 8 on the Arduino
OneWire oneWire(8);
DallasTemperature sensors(&oneWire);

//Slide
char slideName;
int slideValue;
//


PhpocServer server(80);
boolean alreadyConnected = false;

void setup() {
Serial.begin(9600);
while(!Serial)
;

sensors.begin();

Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);

server.beginWebSocket("thermometer");
//
server.beginWebSocket("remote_slide");
server.beginWebSocket("remote_push");
//
Serial.print("WebSocket server address : ");
Serial.println(Phpoc.localIP());
}

void loop() {
// when the client sends the first byte, say hello:
PhpocClient client = server.available();
if (client) {
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();

if(thisChar == 'A')
Serial.println("button A press");
if(thisChar == 'a')
Serial.println("button A release");
if(thisChar == 'B')
Serial.println("button B press");
if(thisChar == 'b')
Serial.println("button B release");
if(thisChar == 'C')
Serial.println("button C press");
if(thisChar == 'c')
Serial.println("button C release");
}


if (client) {
String slideStr = client.readLine();

if(slideStr)
{
slideName = slideStr.charAt(0);
slideValue = slideStr.substring(1).toInt();

Serial.print(slideName);
Serial.print('/');
Serial.println(slideValue);
}

if (client) {
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
String txtMsg = String(temp) + "\r\n";
char buf[txtMsg.length()];
//txtMsg.toCharArray(buf, txtMsg.length());
server.write(buf, txtMsg.length());
delay(300);
}

}

}
}
"
Danioool
 
Posts: 11
Joined: Thu Jul 27, 2017 10:02 am

Re: combine 3 projects as one

Postby Homer » Mon Aug 14, 2017 4:28 pm

In my opinion, in your code, if you call beginWebSocket() three times with three different WS paths, then only one of these WS connections will be listened by the server at port 80. Note that at the same time, there should be only one WS connection assigned to one specific port on the server.
If you want to use multiple WebSocket client-server connections simultaneously, instead of only use one object of Server and Client class, you may use different objects for establishing different connections. I did a similar example that combines Web Remote Slide and Web Remote Push, and I declared like this:

PhpocServer server_slide(80);
PhpocServer server_push(8080);

server_slide.beginWebSocket("remote_slide");
server_push.beginWebSocket("remote_push");

And then use one client object for each server object created:
PhpocClient client_slide = server_slide.available();
PhpocClient client_push = server_push.available();

You may also try to verify the logic of your source code.
I hope this will help.
Homer
 
Posts: 8
Joined: Thu Jul 20, 2017 10:08 am

Re: combine 3 projects as one

Postby Danioool » Wed Aug 23, 2017 4:01 am

Thank you homer,

This was exactly the input I needed.
I'll try asap to give Feedback if it is working.

Daniel
Danioool
 
Posts: 11
Joined: Thu Jul 27, 2017 10:02 am

Re: combine 3 projects as one

Postby Homer » Thu Aug 24, 2017 5:14 pm

You are welcome.

I hope that will help, and from this forum, we can share the knowledge and help each other to improve.
Homer
 
Posts: 8
Joined: Thu Jul 20, 2017 10:08 am


Return to Tip & Talk

Who is online

Users browsing this forum: No registered users and 0 guests

cron