Thank you dear #kenb4 for the response and sorry for the late reply. I changed the Arduino code as below. The code compiled and uploaded successfully to H7 portenta. Then I successfully connected the android device to the defined ssid. But it seems data is not exchanged well between H7 Portenta and android app via Json.
#include <WiFi.h>
//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "SECRET_SSID"; // your network SSID (name)
char pass[] = "SECRET_PASS"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Access Point Web Server");
// by default the local IP address of will be 192.168.3.1
// you can override it with the following:
// WiFi.config(IPAddress(10, 0, 0, 1));
if(strlen(pass) < 8){
Serial.println("Creating access point failed");
Serial.println("The Wi-Fi password must be at least 8 characters long");
// don't continue
while(true);
}
// print the network name (SSID);
Serial.print("Creating access point named: ");
Serial.println(ssid);
//Create the Access point
status = WiFi.beginAP(ssid,pass);
if(status != WL_AP_LISTENING){
Serial.println("Creating access point failed");
// don't continue
while (true);
}
// wait 10 seconds for connection:
delay(10000);
// start the web server on port 80
server.begin();
// you're connected now, so print out the status
printWiFiStatus();
}
void loop() {
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
// a device has connected to the AP
Serial.println("Device connected to AP");
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println("Device disconnected from AP");
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) {
Serial.println("new client");
while (client.connected()) {
if (client.available()) {
String request_line = client.readStringUntil('\n');
Serial.println(request_line);
while (String header_line = client.readStringUntil('\n')) {
header_line.trim(); // lines should end with \r\n, so remove trailing \r
if (header_line.isEmpty()) {
break; // blank line indicates end of request headers
}
// ignoring headers for now
Serial.println(header_line);
}
String body;
if (request_line.startsWith("GET /sensorval")) {
body = R"({"sensornow":100})"; //TODO replace with actual JSON string
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
} else {
client.println("HTTP/1.1 404 Not Found");
}
client.print("Content-Length: "); client.println(body.length());
client.println("Connection: close");
client.println(); // blank line ends response headers
client.print(body);
delay(1);
break;
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your Wi-Fi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
In android studio side, I have four Java files as below:
first I read the IP from serial in Arduino and updated the IP address in “ServiceGenerator” file to http://192.168.3.1/
public class ServiceGenerator {
public static final String API_BASE_URL = "http://192.168.3.1/";
private static OkHttpClient httpClient = new OkHttpClient.Builder().build();
private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass){
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
In “StoreClient” file, I send “sensorVal” request to Arduino.
public interface StoreClient {
@GET("sensorVal")
Call<Category> getData();
}
Based on the Arduino code, whenever “sensorVal” is requested, then "sensornow" must be sent from Arduino to android studio in JSON format and will be received in “Category” file.
public class Category {
@SerializedName("sensornow")
@Expose
private Integer sensornow;
public Integer getSensornow() {
return sensornow;
}
}
In the mainActivity.java the received "sensornow" is then displayed in a textview named TSensor. If no response received, the textview is set to "failed!!".
So, what is displayed in android app, is always “failed”. Hence, it seems there is a problem in requesting and responding.
Thank you again for your helping me.