Send data from Electron to my server directly

Hello,

I am using Particle Electron and started learning on it recently.

I am looking for efficient way to send data from Electron directly to my server.

I tried to search through forum but could not understand the best way to do it.

Can someone advice me on this?

Thanks
Mahesh

You can look at TCPServer, TCPClient or UDP objects, which can all be found in the docs
https://docs.particle.io/reference/firmware/electron/

Let us know if you have any issues with the development, @MaheshGulavani

Thanks for your response.

@ScruffR, I did refer the docs, but was looking for some tutorial which guides on the steps required to achieve this.

Regards
Mahesh

Hi Will,

I didn’t get your question. Do you mean, Particle can support development services?

Thanks
Mahesh

Hello,

OK, I am trying to make my Electron as TCPServer and want to write an App on my laptop as TCPClient.
To do this, as a first step, I have made following code on Electron.
By this I am getting Public IP of Electron which it sends over to serial port.
When I get the Public IP, I try to ping it from my laptop but it gives request timed out.
Also I tried doing telnet using Putty, it is also returning network time out error.
Can someone help me here?

    // telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;
void handler(const char *topic, const char *data) {
    Serial.println("received " + String(topic) + ": " + String(data));
}

void setup()
{
  // start listening for clients
  server.begin();

  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) Particle.process();
    //Serial.println(Cellular.localIP());
  for(int i=0;i<5;i++) {
        Serial.println("waiting... " + String(5 - i));
        delay(1000);
    }

    Particle.subscribe("particle/", handler);
    Particle.publish("particle/device/ip");
  
}

void loop()
{
  if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      server.write(client.read());
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}`

`

You essentially cannot use TCPServer on an Electron.

The problem is that the public IP address provided by your mobile provider does not dedicate that IP address to your Electron; it’s shared with many people using port mapping (NAT). There is no way to configure an incoming listening port mapping with your mobile provider, so basically it won’t work.

Thanks @rickkas7 for your reply.

Can you advise on how do I achieve the direct communication between my remote laptop to and fro Electron without using Particle Cloud?
This is because I have to send more than 255 bytes of data/second to my remote server from Electron.

Regards
Mahesh

You’ll likely run into a similar problem in the reverse direction, depending on the network your laptop is connected to. Actually, there are three problems: one is that the public IP address of your network may change, which is common for home networks. Another is that network address translation (NAT) is used so the router must be configured to port forward incoming connection to your laptop. And the third is that there may be one or more firewalls preventing any off this from working. These problems can be worked around, but they’re situation dependent and not specific to the Particle. It’s the generic problem of running an Internet accessible server on your own computer. Also, there are some security issues when you do this.

The other solutions include using a server on the Internet, or a data ingestion service, like Amazon lambda, maybe also Amazon kinesis, which is designed for this sort of thing.

2 Likes

Hello @rickkas7.

Understand the server side requirement and thank you for helping me get insight in this.

I will be able to arrange a server with static IP in my office.

In that case do you think, I will be able to communicate with Electron directly, where Electron is configured as TCPclient? (with TCPserver application running on server at my office)

Regards
Mahesh

Yes, if you have a static IP address in your office and the firewall is configured to allow access, that should work just fine.

Great!.

Thanks @rickkas7 for your kind help and valuable time on this.

I will proceed with these inputs.

Regards
Mahesh

Hey @MaheshGulavani – is there are a reason you aren’t using the Photon for this project, if it’s indoors and communicating with a server in your office?

I think this might be accomplished much more easily over Wi-Fi.

Hello @will,

Yes, we want to use Electron as the target system will be at a remote location away from office.

Thanks
Mahesh

For communication between servers or other devices (behind NAT) I do use MQTT protocol. With photon modules works well. It should be equal for Electron.

Hello @antdem,

Thanks for your inputs.
This is new to me, I am searching for the information on this on internet.
Got this link-> http://www.instructables.com/id/Spark-Core-Photon-and-CloudMQTT

Did an initial try and got stuck at setting up “Mosquitto” on my laptop :disappointed:
…working on solving it.

If you have any reference to any tutorial, kindly share.

Regards
Mahesh

On what operating system you’re testing? Mosquitto (message broker) after the start should be reached on the standard tcp port 1883. There are also sandboxes for testing of MQTT, such iot.eclipse.org … http://iot.eclipse.org/getting-started

Hi @antdem,

I am testing on Win7. I will go through the link you provided.

Regards
Mahesh

In windows there is some dependence on download (some dll) … such as pthreads and openssl …

Thanks @antdem,

I was able to test “Mosquitto” on my laptop :relaxed: with below commands->

mosquitto_sub -d -t hello/world

and

mosquitto_pub -d -t hello/world -m "Hello, MQTT. This is my first message."

Regards
Mahesh