Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WeMOS - Phone communication #2

Open
leon-thomm opened this issue Oct 11, 2022 · 3 comments
Open

WeMOS - Phone communication #2

leon-thomm opened this issue Oct 11, 2022 · 3 comments

Comments

@leon-thomm
Copy link
Owner

@Tino3141

@Tino3141
Copy link
Collaborator

Socket on Device

How to setup

final server = await ServerSocket.bind("192.168.43.1", 4567); // Provided address is the default address 
                                                              // of a device that is hosting a mobile hotspot
server.listen((client) {
   handleConnection(client);
});

Handle Connection

void handleConnection(Socket client) {
    if (kDebugMode) {
      print('Connection from'
      ' ${client.remoteAddress.address}:${client.remotePort}');
    }

    // listen for events from the client
    client.listen(

    	// handle data from the client
    	(Uint8List data) async {
      
     	 if (kDebugMode) {
     	   print(data);
    	 }
     
     	 final message = String.fromCharCodes(data);
    	},

   	 // handle errors
   	onError: (error) {
    	  if (kDebugMode) {
    	    print(error);
    	  }
    	  client.close();
   	 },

   	 // handle the client closing the connection
   	 onDone: () {
    	  if (kDebugMode) {
    	    print('Client left');
     	 }
     	 client.close();
    	},
  );
}  

@Tino3141
Copy link
Collaborator

IP-Address Issue

For now the IP address is hardcoded into program. Most Android Phones should have the same IP address (192.168.43.1) when hosting their hotspot. However, the IP address can still be different from to phone.

@leon-thomm
Copy link
Owner Author

Because receiving messages from the Phone on the WeMOS did not work reliably using the standard socket api, @Tino3141 and I discontinued these efforts. The problem of attaching the right timestamp was temporarily solved in the following way:

  1. the WeMOS connects to phone wifi
  2. the WeMOS sends reset message
  3. the phone receives reset and resets an internal wemos-alive value
  4. when the WeMOS sends collected data, it sets the timestamp relative to the time at which the reset was sent
  5. when phone receives data, it mutates the timestamp by offsetting it by wemos-alive

We will keep it this way because it works reliably. For publishing a wakeup call we will use MQTT on the WeMOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants