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

feat: added support for wireless communication using ESP01 #2561

Open
wants to merge 4 commits into
base: development
Choose a base branch
from

Conversation

AsCress
Copy link
Collaborator

@AsCress AsCress commented Nov 2, 2024

Fixes #2509
Adds support for wireless communication with the PSLab board using the ESP8266 ESP01 chip.

Screenshots / Recordings

WhatsApp Image 2024-12-29 at 2 47 51 PM

2e50f54b-b5b6-424f-a926-9ba1a6ea29c4.MP4

Checklist:

  • No hard coding: I have used resources from strings.xml, dimens.xml and colors.xml without hard coding any value.
  • No end of file edits: No modifications done at end of resource files strings.xml, dimens.xml or colors.xml.
  • Code reformatting: I have reformatted code and fixed indentation in every file included in this pull request.
  • No extra space: My code does not contain any extra lines or extra spaces than the ones that are necessary.

@marcnause This PR is now finally updated with all the changes I made. I'll proceed with creating a guide on how to work with the ESP chip after doing a demo of this with you all.
After that, you would also be able to test this new very cool feature 😎.

Summary by Sourcery

Add support for wireless communication with the PSLab board using the ESP8266 ESP01 chip, replacing the existing HTTP-based communication with a new SocketClient implementation for improved performance.

New Features:

  • Introduce support for wireless communication with the PSLab board using the ESP8266 ESP01 chip.

Enhancements:

  • Replace HttpAsyncTask with SocketClient for handling wireless communication, improving efficiency and reliability.

Summary by Sourcery

Add support for wireless communication with the PSLab board using the ESP8266 ESP-01 module.

New Features:

  • Communicate with PSLab over WiFi using an ESP-01 module.

Tests:

  • Update tests to reflect the new communication method.

Copy link

sourcery-ai bot commented Nov 2, 2024

Reviewer's Guide by Sourcery

This pull request introduces support for wireless communication with the PSLab board using the ESP8266 ESP01 chip. This is achieved by replacing the existing HTTP-based communication with a new SocketClient implementation for improved performance and reliability.

Sequence diagram: Wireless communication flow

sequenceDiagram
    participant App as PSLab App
    participant Socket as SocketClient
    participant ESP as ESP8266
    participant Board as PSLab Board

    App->>Socket: openConnection(ip, port)
    Socket->>ESP: Connect
    App->>Socket: write(data)
    Socket->>ESP: Send data
    ESP->>Board: Forward data
    Board->>ESP: Response
    ESP->>Socket: Data
    Socket->>App: Return data
    App->>Socket: closeConnection()
    Socket->>ESP: Disconnect
Loading

Class diagram: New SocketClient implementation

classDiagram
    class SocketClient {
        -Socket socket
        -OutputStream outputStream
        -InputStream inputStream
        -boolean isConnected
        -byte[] buffer
        -byte[] receivedData
        +getInstance() SocketClient
        +openConnection(String ip, int port)
        +write(byte[] data)
        +read(int bytesToBeRead)
        +closeConnection()
        +isConnected() boolean
        +getReceivedData() byte[]
    }
    note for SocketClient "Singleton pattern for
managing socket connections"

    class PacketHandler {
        -SocketClient socketClient
        +commonRead(int bytesToRead)
        +commonWrite(byte[] data)
        +close()
    }
    PacketHandler --> SocketClient: uses
Loading

File-Level Changes

Change Details Files
Replaced HTTP-based communication with a new SocketClient
  • Replaced HttpAsyncTask with SocketClient for handling wireless communication.
  • Added ExecutorService for managing the socket communication in a separate thread.
  • Modified commonRead and commonWrite methods to use the SocketClient for data transfer.
  • Added a close method to handle closing the socket connection.
  • Updated the logic to check for Wi-Fi connectivity before attempting to communicate with the ESP01 chip.
app/src/main/java/io/pslab/communication/PacketHandler.java
Implemented SocketClient for ESP01 Communication
  • Created a new class SocketClient to manage socket communication with the ESP01.
  • Implemented methods for opening and closing connections, reading and writing data, and checking connection status.
app/src/main/java/io/pslab/communication/SocketClient.java
Updated PSLab Initialization and Connection Handling
  • Modified the ScienceLab constructor to handle both USB and Wi-Fi connections.
  • Updated the isDeviceFound and isConnected methods to account for Wi-Fi connectivity.
  • Added a close method to close the connection when using Wi-Fi.
  • Modified enterBootloader method to handle exceptions during bootloader entry.
app/src/main/java/io/pslab/communication/ScienceLab.java
Updated HomeFragment to Handle Wi-Fi Connection Status
  • Added logic to handle the case where the device is not found but the Wi-Fi is connected.
  • Updated UI elements to reflect the Wi-Fi connection status.
app/src/main/java/io/pslab/fragment/HomeFragment.java
Added UI elements for Wi-Fi Connection
  • Added a menu item to indicate Wi-Fi connection status.
  • Updated the home fragment layout to accommodate Wi-Fi connection status.
  • Added a drawable for the Wi-Fi connected icon.
app/src/main/res/menu/pslab_connectivity_menu.xml
app/src/main/res/layout/home_fragment.xml
app/src/main/res/drawable/ic_wifi_connected.xml
Implemented Wi-Fi Disconnect Handling
  • Created a new broadcast receiver WifiDisconnectReceiver to handle Wi-Fi disconnection events.
  • Implemented logic to close the connection and reset relevant variables when Wi-Fi is disconnected.
app/src/main/java/io/pslab/receivers/WifiDisconnectReceiver.java
Removed Unused Classes and Interfaces
  • Removed unused classes related to HTTP communication, as they are no longer needed after switching to SocketClient.
app/src/main/java/io/pslab/communication/HttpAsyncTask.java
app/src/main/java/io/pslab/communication/HttpHandler.java
app/src/main/java/io/pslab/interfaces/HttpCallback.java
Updated ESP Fragment and Main Activity for Wi-Fi Support
  • Updated the ESPFragment to use the SocketClient for establishing a connection with the ESP01 chip.
  • Modified the MainActivity to handle Wi-Fi connection status and update the UI accordingly.
  • Registered a new broadcast receiver to handle Wi-Fi disconnection events.
app/src/main/java/io/pslab/fragment/ESPFragment.java
app/src/main/java/io/pslab/activity/MainActivity.java
Updated CommunicationHandler and related files
  • Updated the CommunicationHandler to support both USB and Wi-Fi communication.
  • Improved logging messages for better debugging.
app/src/main/java/io/pslab/communication/CommunicationHandler.java

Assessment against linked issues

Issue Objective Addressed Explanation
#2509 Add support for wireless (WiFi) communication between the Android app and PSLab device using ESP-01 module

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@AsCress
Copy link
Collaborator Author

AsCress commented Nov 2, 2024

@CloudyPadmal Could you please provide feedback on the fact that if the lines on which I'm working here are correct.
Many things in this PR are currently done ONLY for testing.
It'd be great if you could have a look and provide your feedback if this resembles something which we want to achieve. 🤣
I've not yet configured this properly with the hardware so that I may be able to test this. I'll do that very soon.
Currently, the app only successfully connects to the ESP chip on the board.

@AsCress AsCress self-assigned this Nov 2, 2024
@AsCress AsCress added the In Progress Developer is working on the problem label Nov 2, 2024
Copy link

github-actions bot commented Nov 2, 2024

@AsCress AsCress marked this pull request as ready for review December 27, 2024 17:17
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @AsCress - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@AsCress AsCress added Status: Review Required Requested reviews from peers and maintainers Feature New addition to the existing app and removed In Progress Developer is working on the problem labels Dec 27, 2024
@marcnause
Copy link
Contributor

@AsCress I have the required hardware, but I don't know what I need to do to get it working (e.g. how to flash firmware to ESP-01, where to get the firmware). Are there instructions available anywhere?

Copy link
Collaborator

@CloudyPadmal CloudyPadmal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a much-needed feature! 💯

The demo looks pretty cool and well done @AsCress

@AsCress
Copy link
Collaborator Author

AsCress commented Dec 29, 2024

This is a much-needed feature! 💯

The demo looks pretty cool and well done @AsCress

@CloudyPadmal Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New addition to the existing app Status: Review Required Requested reviews from peers and maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Support for Wifi (ESP-01) Module in the Android App
3 participants