Whilst my ESP32-C3 is plugged into my Mac using a data cable, the following sketch returns data from the BMP280 to Terminal every 5 seconds, however if I just connect to a USB supply it does not work. There must be something amiss with the wifi part of the code, any help would be appreciated.
Here is the sketch:
//In Terminal use nc 192.168.1.190 8080 note the IP address of the ESP32 may change//
#include <Wire.h>
#include <WiFi.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
// --- Configuration ---
const char* ssid = "*********";
const char* password = "***************";
WiFiServer wifiServer(8080); // Create server on port 80 (look at changing if DHT used at same time)
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
WiFi.setTxPower(WIFI_POWER_8_5dBm);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // IMPORTANT: Copy this IP
// Start Server
wifiServer.begin();
while ( !Serial ) delay(100); // wait for native usb
Serial.println(F("BMP280 test"));
unsigned status;
//status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
status = bmp.begin(0x76);
if (!status) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
WiFiClient client = wifiServer.accept(); // Listen for incoming clients
if (client) {
Serial.println("Mac connected.");
while (client.connected()) {
client.print(F("Temperature = "));
client.print(bmp.readTemperature());
client.println(" °C");
client.print(F("Pressure = "));
client.print(bmp.readPressure()/100);
client.println(" mBar");
client.print(F("Approx altitude = "));
client.print(bmp.readAltitude(1019.45)); /* Adjusted to local forecast! default was 1013.25*/
client.println(" m");
client.println();
delay(2000);
Serial.println("Data sent.");
delay(5000); // Send interval
}
client.stop();
Serial.println("Mac disconnected.");
}
}
The Serial. print needs a full USB cable, data and power. If you want to use external power, then you need to change the Serial to something else. I use an MQTT tool. The prints go to the serial if attached; otherwise, it uses MQTT. I set up a Mosquitto server on an old Pi to receive the messages. If interested in that approach, let me know, and I will post an example.
I do not understand. How does your sketch behave when connected to MAC and how when connected just to USB supply? How do you connect to your ESP32 WiFi server to read the data?
I paired down the sketch and have managed to get it working! I can have the sensor in another room and it will connect to my Mac via WiFi and print out the data from the BMP280 sensor every 5 seconds. This is what I ended up with:
//In Terminal use nc 192.168.1.190 8080 note the IP address of the ESP32 may change//
#include <Wire.h>
#include <WiFi.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
// --- Configuration ---
const char* ssid = "*********";
const char* password = "********************";
WiFiServer wifiServer(8080); // Create server on port 80 (look at changing if DHT used at same time)
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
void setup() {
Serial.begin(115200);
bmp.begin(0x76);
// Connect to WiFi
WiFi.begin(ssid, password);
WiFi.setTxPower(WIFI_POWER_8_5dBm);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // IMPORTANT: Copy this IP
// Start Server
wifiServer.begin();
}
void loop() {
WiFiClient client = wifiServer.accept(); // Listen for incoming clients
if (client) {
Serial.println("Mac connected.");
while (client.connected()) {
client.print(F("Temperature = "));
client.print(bmp.readTemperature());
client.println(" °C");
client.print(F("Pressure = "));
client.print(bmp.readPressure()/100);
client.println(" mBar");
client.print(F("Approx altitude = "));
client.print(bmp.readAltitude(1019.45)); /* Adjusted to local forecast! default was 1013.25*/
client.println(" m");
client.println();
delay(2000);
Serial.println("Data sent.");
delay(5000); // Send interval
}
client.stop();
Serial.println("Mac disconnected.");
}
}
#include <WiFi.h> // esp32 nano
#include <PubSubClient.h>
#include <MqttLogger.h>
#include "wifi_secrets.h"
#include <pins_arduino.h>
const char *ssid = WIFI_SSID;
const char *password = WIFI_PASSWORD;
const char *mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
// Pick one
// default mode is MqttLoggerMode::MqttAndSerialFallback
MqttLogger mqttLogger(client, "mqttlogger/log");
// other available modes:
// MqttLogger mqttLogger(client,"mqttlogger/log",MqttLoggerMode::Both MqttAndSerial);
// MqttLogger mqttLogger(client,"mqttlogger/log",MqttLoggerMode::MqttOnly);
// MqttLogger mqttLogger(client,"mqttlogger/log",MqttLoggerMode::SerialOnly);
void wifiConnect() { // connect to wifi network
mqttLogger.print("Connecting to WiFi: ");
mqttLogger.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
mqttLogger.print(".");
}
mqttLogger.print("WiFi connected: ");
mqttLogger.println(WiFi.localIP());
}
void reconnect() { // establish mqtt client connection
while (!client.connected()) { // Loop until we're reconnected
mqttLogger.print("Attempting MQTT connection..."); // Attempt to connect
if (client.connect("ESP32Logger"))
mqttLogger.println("connected."); // as we have a connection here, this will be the first message published to the mqtt server
else {
mqttLogger.print("failed, rc=");
mqttLogger.print(client.state());
mqttLogger.println(" try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
}
}
}
void setup() {
Serial.begin(115200);
// MqttLogger uses mqtt when available and Serial as a fallback. Before any connection is established,
// MqttLogger works just like Serial
mqttLogger.println("Starting setup..");
wifiConnect(); // connect to wifi
client.setServer(mqtt_server, 1883); // mqtt client
//client.setCallback(mqttIncomingCallback);
}
void loop() {
if (!client.connected()) reconnect(); // here the mqtt connection is established
// with a connection, subsequent print() publish on the mqtt broker, but in a buffered fashion
mqttLogger.print(millis() / 1000);
delay(1000);
mqttLogger.print(" seconds");
delay(1000);
mqttLogger.print(" online");
delay(1000);
mqttLogger.println(); // this finally flushes the buffer and published on the mqtt broker
}
another Telnet example
program blinks a LED transmitting LED state over Telnet - can also change pin number via Telnet
// Blink a LED - enter pin number via Telnet
// adapted from ESP32 example File>Examples>WiFi>WiFiTelnetToSerial
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
const char *ssid = "xxxxxxxx";
const char *password = "zzzzzzzzz";
NetworkServer server(23);
NetworkClient serverClient;
int led = 15; //LED_BUILTIN;
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\nTelnet test - blink LED write/read Telnet client");
Serial.println("\nConnecting to WiFi");
wifiMulti.addAP(ssid, password);
while (1) {
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
break;
} else {
Serial.print('.');
delay(1000);
}
}
// setup Telnet server
server.begin();
server.setNoDelay(true);
Serial.print("Ready! Use 'telnet ");
Serial.print(WiFi.localIP());
Serial.println(" 23' to connect");
while (wifiMulti.run() != WL_CONNECTED) delay(1000); // WiFi OK?
while (!server.hasClient()) delay(1000); //check for a client
serverClient = server.accept(); // accept client is it OK?
if (!serverClient) {
Serial.println("available broken");
}
Serial.print("New client: "); // display client IP address
Serial.println(serverClient.remoteIP());
// setup LED to blink
serverClient.printf("Led to blink %d\n", led);
serverClient.println("enter pin number to blink ? ");
// initialize digital pin LED_BUILTIN as an output.
pinMode(led, OUTPUT);
}
// the loop function blient the LED and also reads a new pin from Telnet
void loop() {
// new pin number entered via Telnet?
if (serverClient.available()) {
int i;
pinMode(led, INPUT);
if (i = serverClient.parseInt()) led = i;
serverClient.printf("Led to blink %d\n", led);
pinMode(led, OUTPUT); // setup new pin
}
// bling LED - sending state to Serial and Telnet
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.print("Led HIGH\n");
serverClient.print("Led HIGH\n");
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
Serial.print("Led LOW\n");
serverClient.print("Led LOW\n");
delay(1000); // wait for a second
}
on reset the ESP32 waits for a Telnet client to connect then start blinking the LED, e.g. serial monitor output
Telnet test - blink LED write/read Telnet client
Connecting to WiFi
WiFi connected IP address: 192.168.1.88
Ready! Use 'telnet 192.168.1.88 23' to connect
New client: 192.168.1.65
Led to blink 15
enter pin number to blink ?
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
Led HIGH
Led LOW
using Teraterm as a Telnet client shows LED state and entering a new pin number
It's something I threw together from a library sample or two. I had a project embeded inside the wall of my RV so it was handy to debug first outside with a USB cable then inside the wall via a Pi4. I never got around to cleaning up the code and making a proper system out of it and the Pi but it will get you started.