/****************************************
* Include Libraries
****************************************/
#include <WiFi.h>
#include <PubSubClient.h>
#define WIFISSID "Fhjf`Use code tags to format code for the forum`R" // Put your Wifi SSID here
#define PASSWORD "11111yn" // Put your WIFI password here
#define TOKEN "BBFF-OgB3moPIyyTgjgY5XpBS8gUOrTxa6X" // Put your `Use code tags to format code for the forum`
Ubidots' TOKEN
#define MQTT_CLIENT_NAME "ECG Monitor" // MQTT client Name, please
enter your own 8-12 alphanumeric character ASCII string;
//it should be a random and unique ascii string and
different from all other devices
/****************************************
* Define Constants
****************************************/
#define VARIABLE_LABEL "sensor" // Assign the variable label
#define DEVICE_LABEL "esp32" // Assig the device label
#define SENSOR A0 // Set the A0 as SENSOR
char mqttBroker[] = "industrial.api.ubidots.com";
char payload[100];
char topic[150];
// Space to store values to send
char str_sensor[10];
/****************************************
* Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Serial.write(payload, length);
Serial.println(topic);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
// Assign the pin as INPUT
pinMode(SENSOR, INPUT);
Serial.println();
Serial.print("Waiting for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) { reconnect();
}
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label
float sensor = analogRead(SENSOR);
/* 4 is minimum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(sensor, 4, 2, str_sensor);
sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value
Serial.println("Publishing data to Ubidots Cloud");
client.publish(topic, payload);
client.loop();
delay(500);
}
Welcome to the forum
Please confirm whether you are using a Nano ESP32 or a generic ESP32 board
I am using esp32-wroom-32d
As you are not using a Nano ESP32 board your topic has been moved to a more generic forum category
Start at the begining, get the SD card working before you try to change your program to write data to an SD file.
Within the ESP32 core there is an example provided called SD_Test, so load that and connect an SD card module of the type below and get it working;
I don't actually understand, I was given this as an assignment and I don't kW more on it
If you mean that code, your teacher is not doing you any favors, so consider dropping the class.
To learn Arduino, start at the beginning, learn how to blink an LED and read a pushbutton, then work your way up.
School project? May be your teacher wants you to study and learn something rather than getting the code written for you from a charitable member (most of us here would not do that anyway).
Actually it's my final year's project and this is where I'm stock, just needed someone to help me with how to include an SD card module here for offline storage. That's all
So first follow the advice in post #5, first check with a basic sketch that the SD card is working and thus connected correctly.
Then maybe you can ask the question;
"I have a working SD card; how do I modify my sketch to write data to a file on the SD card ?"
If you wrote and understand the code posted above in #1, then it should be no problem at all to study the SD library docs, pick an SD library example, get it running, then include the needed bits into your existing code.
If you did not write the posted code, then get started in Arduino by following the advice in post #7.
Are you sure someone else did not write this for you?
You do not have the needed library...
So... "actually" you lie about everything and still expect people to do your work so you can get a final grade. You need to show your attempt with ESP-32 and SD-Card work and describe what you observe, without lying.
[EDIT]
I figured you out... You are not a new programmer or a final year programmer.You are not even a student. You are trying to make other people make a bitcoin miner so you can get rich quick.
I'm no bitcoin miner, I'm a student
See post #11.
I took code from a document
Then follow the advice in post #7. Getting Started tutorials are all over the web, and even included in the Arduino IDE.
Alternatively, you could post on the Jobs and Paid Consultancy forum section, and hire someone to write code for you. Your teacher may frown on that, though.
I need someone to hire
#include <WiFi.h>
#include <PubSubClient.h>
#include <SD.h>
#define WIFISSID "Faiz’s XR"
#define PASSWORD "11111111"
#define TOKEN "BBFF-OgB3moPIyyTgjgY5XpBS8gUOrTxa6X"
#define VARIABLE_LABEL "sensor"
#define DEVICE_LABEL "esp32"
#define SENSOR A0
#define SD_CS 5 // Change to your SD card CS pin
WiFiClient ubidots;
PubSubClient client(ubidots);
File dataFile;
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
pinMode(SENSOR, INPUT);
// Initialize SD card
if (!SD.begin(SD_CS)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialized.");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setServer("industrial.api.ubidots.com", 1883);
}
void reconnect() {
while (!client.connected()) {
client.connect("ECG Monitor", TOKEN, "");
delay(2000);
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
float sensorValue = analogRead(SENSOR);
String payload = String("{\"") + VARIABLE_LABEL + "\":{\"value\":" + sensorValue + "}}";
// Publish to Ubidots
client.publish(("/v1.6/devices/" + String(DEVICE_LABEL)).c_str(), payload.c_str());
// Store data to SD card
if (dataFile) {
dataFile.println(String(sensorValue));
dataFile.close();
} else {
Serial.println("Error opening file for writing");
}
client.loop();
delay(500);
}
I have moved your topic to the paid part of the forum where you can ask for paid help.
It would help if you could indicate your budget and timescales.