I am communicating two ESP32 boards which are connected to a Wifi Network. One ESP32 board is the Server and the other is the client. I want to measure the ESP32 CPU utlization on the client ESP32. I have no idea how to do it and have not yet found any useful resources on the internet. Can someone help me with this?
This is the code on server
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "XXXXX";
const char* password = "XXXX";
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED){
delay(200);
Serial.println("Connecting to Wifi...");
}
Serial.println("Connected to Wifi");
Serial.println(WiFi.localIP());
server.on("/test", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Request received from esp32client");
request->send(200, "text/plain", "Hello from ESP32Server to ESP32Client");
});
server.on("/test1", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Request received from PC-Client");
request->send(300, "text/plain", "Hello from ESP32Server to PC");
});
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
}
This is the code on Client
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "XXXXX";
const char* password ="XXXXX";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED){
delay(500);
Serial.println("Connecting to Wifi...");
}
Serial.println("Connected to Wifi Network...");
Serial.println(WiFi.localIP());
}
void loop() {
HTTPClient http;
http.begin("http://192.168.43.35/test");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
delay(30000);
}
Use this function to get CPU load
vTaskGetRunTimeStats(char *buffer);
It tells CPU utilization by currently running task on esp32.
API documentation here.
Related
I am new to Arduino and I am not so familiar with it. I am not sure why the client cannot connect to the server and I have been looking at it for hours. Any solution to this would be a great help.
So I successfully establish a server with the code below. But...
// server.ino
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "WifiName"; // Wifie name
const char* password = "WifiPassword"; // Wifi password
float sensor_value = 0.0;
String Website;
ESP8266WebServer server(80);
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_AP);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while(WiFi.status()!=WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println("Server started at port 80.");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.print("URL=http://");
Serial.println(WiFi.localIP());
server.on("/", handleIndex); //use the top root path report last sensor value
server.on("/update",handleUpdate); // use this route to update sensor value
server.begin();
}
void loop(){
server.handleClient();
}
void handleIndex(){
Website = "<html><body onload ='process()'><div id='div1'>"+ String(sensor_value) +"</div></body></html>";
server.send(200," text/html",Website);
}
void handleUpdate(){
sensor_value = server.arg("value").toFloat();
Serial.println(sensor_value);
server.send(200,"text/plain","Updated");
}
I try to connect a client to the server so it could send data to the server, but a connection between the client and the server cannot be established and I don't know why. I check the IP address and it is all correct I just don't get why the client fails to connect to the server
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <HX711.h>
#include "Wire.h"
#define DOUT D5
#define CLK D6
HX711 scale;
const char* ssid = "WifiName";
const char* password = "Wifipassowrd";
float calibration_factor = 107095;
float val_Weight;
const char* host = "192.168.0.167"; // Server host IP.
const int port = 80;
WiFiClient client;
void setup() {
Serial.begin(115200); //set baud rate
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);//millisecond
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected & connect to client");
}
void loop() {
//connect to the server and send the data as URL parameter
if(client.connect(host, port)){
String url = "update?value=";
url+=String(25.0);
client.print(String("GET /") + url + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
Serial.println("Response: ");
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}else{
Serial.println("fail");
}
}
Any help would be great thanks!!!
Check the passwords, you have a typo.
const char* password = "Wifipassowrd";
Derived from the following code.
I uploaded that code to my ESP8266 and it's all good and okay when I communicate with it with my laptop while my laptop is connected to my network with a LAN cable.
The problem is: when I try to communicate with the ESP with my laptop or phone over Wi-Fi I get ERR_CONNECTION_REFUSED though they rarely work and communicate. I tried another phone another router, and did a factory reset to my router, and all the same.
I know that there is an option in the router that is called AP Isolation and it's been checked and it's disabled.
My question is: What could possibly be the reason for this error ERR_CONNECTION_REFUSED when I communicate with ESP8266 with that code?
If someone could help me I would be pleased as I am stuck in this situation.
The ESP code (same as the link):
#include <ESP8266WiFi.h>
const char* ssid = "*****";
const char* password = "*******";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// prepare a web page to be send to a client (web browser)
// the connection will be closed after completion of the response
// the page will be refreshed automatically every 5 sec
String prepareHtmlPage() {
String htmlPage = String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" +
"Refresh: 5\r\n" + "\r\n" +
"<!DOCTYPE HTML>" + "<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" + "\r\n";
return htmlPage;
}
void loop() {
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client) {
Serial.println("\n[Client connected]");
while (client.connected()) {
// read line by line what the client (web browser) is requesting
if (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n') {
client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disconnected]");
}
}
I hope its not too late and it helps someone in need.
You need to do 2 things
Before WiFi.begin(), you need to add
WiFi.mode(WIFI_STA);
Second, you need to
#include <ESP8266mDNS.h>
in setup()
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
and in loop()
MDNS.update();
Lastly, do not forget to add the following in every server response. Else, you will hit CORS error.
server.sendHeader("Access-Control-Allow-Origin", "*");
Please dont forget to add supporting libraries. Let me know if it works. Demo code would look like the below
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "<your SSID>";
const char* password = "<your WIFI Password>>";
int serverPort = 80;
int boardBaud = 115200;
ESP8266WebServer server(serverPort);
void handleRoot() {
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", "<h1>Hello World</h1>");
}
void handleNotFound() {
if (server.method() == HTTP_OPTIONS)
{
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(204);
}
else
{
server.send(404, "text/plain", "Error");
}
}
void setup(void) {
Serial.begin(boardBaud);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
I tried to Run this code but the connection fails and HTTPCode returns -1 , I'm using WIFI manager and it works sucessfully connected to the network
the link is worked successfully on POSTman and data posted in Database
Hi All, I tried to Run this code but the connection fails and HTTPCode returns -1 , I'm using WIFI manager and it works sucessfully connected to the network
the link is worked successfully on POSTman and data posted in Database
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <Wire.h>
IPAddress staticIP750_100(192,168,1,16);
IPAddress gateway750_100(192,168,1,1);
IPAddress subnet750_100(255,255,255,0);
HTTPClient http;
#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float temp_0;
const char* host = "http://mysite2020.info";
void setup()
{
Serial.begin(115200);
WiFiManager wifimanger ;
DS18B20.begin();
Wire.begin(D2, D1)
readTemp();
WiFi.mode(WIFI_STA);
wifimanger.autoConnect("Inlet Device","12345");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
Serial.println("...");
}
Serial.println("WiFi connected");
WiFi.config(staticIP750_100, gateway750_100, subnet750_100);
WiFi.hostname("Inlet Device") ;
delay(3000);
Serial.println("Welcome To Device No : 1");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Your Gateway is : ");
Serial.println((WiFi.gatewayIP().toString().c_str()));
Serial.println("Your Hostname is : ");
Serial.println((WiFi.hostname()));
delay(3000);
Serial.println("Welcome to my Project!");
delay(3000);
}
void loop() {
Serial.println(host); // Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
sendtemp ();
}
void sendtemp ()
{
temp_0=23.26;
String url = "http://mysite2020.info/Api/insert_mssqlserver.php?Reading=" + String(temp_0);
Serial.println(url);
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
auto httpCode = http.GET();
Serial.println(httpCode); //Print HTTP return code
String payload = http.getString();
Serial.println(payload); //Print request response payload
http.end(); //Close connection Serial.println();
Serial.println("closing connection");
delay(500);
}
I am trying to make a simple esp01 wifi hotspot + a simple webpage with 3 buttons that send ints(1,2,3) over serial when pressed. But the wifi hotspot isn't working.
Here is the code:
#include <ESP8266WiFi>;
#include <WiFiClient>;
#include <ESP8266WebServer>;
const char *ssid = "test";
const char *password = "password";
IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);
WiFiServer server(80);
void setup() {
delay(1000);
Serial.begin(9600);
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ssid, password);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (!client)
{
return;
}
while(!client.available())
{
delay(1);
}
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("/R1") != -1)
{
Serial.println("1");
}else if (request.indexOf("/R2") != -1)
{
Serial.println("2");
}else if (request.indexOf("/R3") != -1)
{
Serial.println("3");
}
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head><title>ESP01 RELAY Control</title></head>");
client.println("<body>");
client.println("<br>");
client.println("<button href=\"/R1\">R:1</button>");
client.println("<button href=\"/R2\">R:2</button>");
client.println("<button href=\"/R3\">R:3</button>");
client.println("<br>");
client.println("<button href=\"/T1\">T:1</button>");
client.println("<button href=\"/T2\">T:2</button>");
client.println("<button href=\"/T3\">T:3</button>");
client.println("</body>");
client.println("</html>");
delay(1);
}
To program an ESP8622 it is best practice to use the Serial.println() command to debug your code. For setting up a working access point (AP) on a ESP8622 module, use the following code;
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char *ssid = "test";
const char *password = "password";
IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);
WiFiServer server(80);
void setup() {
delay(1000);
Serial.begin(9600);
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if(client){
while (client.connected()) {
if(client.available()){
Serial.println("Connected to client");
}
}
// close the connection:
client.stop();
}
}
Hi I am new to arduino programming and I am have an issue. I have successfully managed to display the wifi using esp8266 module i.e when I run my code the esp8266 module creates a wifi. It also asks for password but after that there is no output of successful connection. I am using the method wifi.softAp(username,password) for creation of wifi network. I have written the following code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "Jeet";//Wifi username
const char* password = "wifibin12"; //Wifi password
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
}
void setup(void) {
// put your setup code here, to run once:
Serial.begin(115200);
//WiFi.mode(WIFI_AP);
Serial.print("this is my pass");
Serial.print(password);
WiFi.softAP(ssid, password);
Serial.print("Setting soft-Ap ... ");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server.on("/", handleRoot); //Which routine to handle at root location
server.begin(); //Start server
Serial.println("HTTP server started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
}
When I run the code I get ............. output continuously on serial monitor. Kindly help me fix this issue if anyone knows what I am doing wrong. Suggestions would also be appreciated.
It's getting stuck to while loop. Wifi.status() returns WL_CONNECTED when it is connected to wifi network (to another Access Point). So if you want just get AP to work you should try this:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "Jeet"; //Wifi username
const char* password = "wifibin12"; //Wifi password
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "<h1>hello from esp8266!</h1>");
}
void setup(void) {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("this is my pass");
Serial.print(password);
WiFi.softAP(ssid, password);
Serial.print("Setting soft-Ap ... ");
// Wait for connection
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("AP name ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
server.on("/", handleRoot); //Which routine to handle at root location
server.begin(); //Start server
Serial.println("HTTP server started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
}
And WiFi.localIP() doesn't return AP's local ip. Default is 192.168.4.1.
I recommend looking docs and examples from here: https://github.com/esp8266/Arduino/tree/master/doc/esp8266wifi