SPI Micro SD Card - error opening textfile - why isn't it working? - arduino

First of all sorry for my bad english and my programming skills... im still a beginner. I have a problem implementing the example code into my project. The example code for Datalogging on my SD-Card works. So there are no wiring faults.. Implementing this working code in my project, the arduino cant find the text data and i dont know why. can anybody help me ?
im working with an arduino nano V3. and a SPI Card Reader.
Here is what happening in the serial monitor:
Initializing SD card...card initialized.
error opening datalog.txt
Here is my Code - sorry for the used German words... but i think they wont disturb.
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // SD KARTE
int pinDHT22 = 2; // Kombisensor
SimpleDHT22 dht22(pinDHT22);
float temperature = 0;
float humidity = 0;
volatile float windgeschwindigkeit = 0;
unsigned long previousMillis = 0;
volatile int Impulscounter = 0; // Impulszähler für Windgeschwindigkeit
unsigned long windmillis = 0;
int a = 0;
File Datenlog;
void wind()
{
Impulscounter = Impulscounter + 1;
if( Impulscounter == 1)
{
windmillis = millis();
}
}
void setup()
{
pinMode(3, INPUT);
lcd.begin();
lcd.backlight();
Serial.begin(9600);
attachInterrupt(1, wind, RISING);
while (!Serial) { // wait for serial port to connect. Needed for native USB port only
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
Datenlog = SD.open("test.txt", FILE_WRITE);
if (Datenlog){
Datenlog.print("Tag "); // ... und die Textdatei anschließend befüllt werden.
Datenlog.print("Datum ");
Datenlog.print("Uhrzeit ");
Datenlog.print("Aussentemperatur ");
Datenlog.print("Aussenfeuchtigkeit ");
Datenlog.print("Windgeschwindigkeit ");
Datenlog.print("Gehaeusetemperatur ");
Datenlog.close();
Serial.print ( "it worked");
}
else {
Serial.println("error opening datalog.txt");
}
}

Related

XIAO BLE with MAX30100 and BLE

When I run MAX30100 by itself on SEEED XIAO BLE SENSE module everything works fine. But when I add BLE into code, MAX30100 stops getting beats when BLE begins. I am guessing this is to do with pox.update() not updating in time. If someone has any idea what I can do please share. Thank you.
My code is here:
//MAX30100 BPM + SPO2 header
#include <MAX30100_PulseOximeter.h>
//BLE header
#include <Arduino.h>
#include <ArduinoBLE.h>
//Communication header
#include <Wire.h>
//MAX30100
PulseOximeter pox;
uint8_t hearty = 0;
uint8_t oxy = 0;
//MAX timer
unsigned long max_prev_millis = 0;
//BLE timer
unsigned long ble_prev_millis = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!pox.begin()) Serial.println(F("MAX30100 error"));
else Serial.println(F("MAX30100 OK!"));
pox.setOnBeatDetectedCallback(onBeatDetected);
// setting the registers for the bluetooth transmit power
uint32_t *TXPOWER = (uint32_t *)0x4000150C;
*TXPOWER &= 0x0;
*TXPOWER |= 0x08;
uint32_t *MODE = (uint32_t *)0x40001510;
*MODE &= 0x0;
*MODE |= 0x05;
BLE.setLocalName("IT'S ME, BLE");
}
void loop() {
// put your main code here, to run repeatedly:
pox.update();
//Heart rate reading every 1 second
if (millis() - max_prev_millis >= 1000){
readMax30100();
}
//Bluetooth upload every 15 seconds
if (millis() - ble_prev_millis >= 15 * 1000) {
BLEUpload();
}
}
void readMax30100(){
hearty = pox.getHeartRate();
oxy = pox.getSpO2();
Serial.print(F("Heart rate:"));
Serial.print(hearty);
Serial.print(F("bpm / SpO2:"));
Serial.print(oxy);
Serial.println(F("%"));
max_prev_millis = millis();
}
void BLEUpload(){
BLE.begin();
BLE.stopAdvertise();
BLEAdvertisingData advData;
advData.setFlags(BLEFlagsBREDRNotSupported | BLEFlagsGeneralDiscoverable);
unsigned char send_data[2]
{
(unsigned char) (hearty),
(unsigned char) (oxy)
};
advData.setAdvertisedServiceData(0x2ACA, send_data, sizeof(send_data));
BLE.setAdvertisingData(advData);
BLE.advertise();
BLE.end();
ble_prev_millis = millis();
}
void onBeatDetected(){
Serial.println(F("Beat!"));
}

How to fix "aes128_enc_single' was not declared in this scope"

I am connecting a sensor to android device through arduino. I need to encrypt my sensor data and then send it to android device. But when I include aes128_enc_single(key, temp), It gives an error and said "aes128_enc_single' was not declared in this scope "
What should I do for this?
I have included AESLib.h library
My sample code is
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <AESLib.h>
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
SoftwareSerial blue(2,3);
int i = 0;
float temp = 0;
void setup() {
Serial.begin(9600);
blue.begin(9600);
}
void loop() {
i = analogRead(A0);
temp = (i/1024.0)*500;
Serial.println(temp);
aes128_enc_single(key, temp);
Serial.print("encrypted:");
Serial.println(temp);
blue.print("Encrypted Temperature: ");
blue.println(temp);
delay(1000);
}

Porting Arduino Sketch from Ethernet to Wifi

I have a sketch working with an Arduino Uno and an Ethernet Shield - and it's working fine. Now I've gotten my hands on some Arduino Uno WiFi, and I want to port the sketch from ethernet to wifi - but I've run into a wall now. Most of the guide/FAQ/help I can find is for a WiFi Shield, and not a WiFi Arduino, so I'm stuck here.
Below is my (original Ethernet) code. I can post my somewhat modified Wifi code, but I can't even compile it without errors.
// Hartmann fugtighedsmåler v 0.1
// Lavet af Jan Andreasen
// Skriver til DB på FDKTO517
#include <Ethernet.h>
#include <SPI.h>
#include <DHT.h>
#define DHTPIN 2 // Siger sig selv
#define DHTTYPE DHT11 // Typen af sensor.
float h = 0;
float t = 0;
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Macadresse på kortet
IPAddress server(10,16,9,229); // Server adressen på SQL'en
EthernetClient client;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
Ethernet.begin(mac);
dht.begin();
}
void loop() {
readTempHum();
delay(300000); // Loop timer i millis - 5 minutter
}
void get_request(float t, float h) {
Serial.println("Connecting to Client...");
if (client.connect(server, 10080)) {
Serial.println("--> connection ok\n");
client.print("GET /test.php?");
// Placering af PHP script til upload til DB
client.print("t="); // Temp
client.print(t);
client.print("&h="); // Fugtighed
client.print(h);
client.println(" HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println("Connection: close");
client.println();
client.println();
client.stop();
Serial.println("--> finished transmission\n");
} else {
Serial.println("--> connection failed\n");
}
}
void readTempHum() {
h = dht.readHumidity();
t = dht.readTemperature();
{
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%\t");
Serial.print("Temperature:");
Serial.print(t);
Serial.println("*C");
get_request(t,h);
}
}
I've also posted this on the Arduino Forum. I'm sorry if you see my double-post, and I'll post the solution to my problem here as well.
New sketch:
#include <Wire.h>
#include <UnoWiFiDevEd.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
float h = 0;
float t = 0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
const char* connector = "rest";
const char* server = "10.16.9.229";
const char* method = "GET";
const char* resource = "/test.php?t=";
Serial.begin(9600);
Ciao.begin();
dht.begin();
pinMode(2, INPUT);
delay(10000);
}
void loop() {
readTempHum();
// doRequest(connector, server, resource, method);
delay(300000);
}
void doRequest(const char* conn, const char* server, const char* command, const char* method, float t, float h){
CiaoData data = Ciao.write(conn, server, command, method);
}
void readTempHum() {
h = dht.readHumidity();
t = dht.readTemperature();
const char* connector = "rest";
const char* server = "10.16.9.229";
const char* method = "GET";
const char* resource = "/test.php?t=";
{
doRequest(connector, server, resource, method,t,h);
}
}
Now, I've ran into a new problem. The value from the sensor (t and h) are supposed to be output in the HTTP/GET command like this:
test.php?t=1&h=2
But I can't seem to make that work. If I try to define the resource as this
const char* resource = "/test.php?t="+t+"&h="+h;
I get an error (obviously), but if I try to declare it as a string, I the same error again.
Error:
HumidSQL3_Wifi_master:24: error: invalid operands of types 'const char [13]' and 'float' to binary 'operator+'
const char* resource = "/test.php?t="+t+"&h="+h;
Now, I hope that some of you could help me out a bit here :/
If it is the Arduino.org Arduino UNO WiFi Developer Edition, then use WiFi Link with UNO WiFi Serial1 library
Okay - so I made it work. I had to start from scratch, and with the help from Juraj (I'll accept your answer as well) it works now.
Below are the final sketch ("final", as the DHT11 sensor only were for testing purpose, as a proof-of-concept)
// Hartmann fugtighedsmåler v 0.2.2
// Lavet af Jan Andreasen
// Skriver til DB på FDKTO517
// WiFi udgave, testversion
#include <Wire.h>
#include <UnoWiFiDevEd.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define CONNECTOR "rest"
#define SERVER_ADDR "10.16.9.229"
float h = 0;
float t = 0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Ciao.begin();
dht.begin();
pinMode(2, INPUT); // I'm not sure if this is required, just saw it now
delay(10000); // A 10 second delay from start to initialization
}
void loop() {
readTempHum();
delay(300000); // A 5 minute delay between measurements
}
void readTempHum() {
h = dht.readHumidity(); // Reads the humidity from sensor
t = dht.readTemperature(); // Reads the temperature from sensor
String uri = "/test.php?t="; // URL to the PHP-file
uri += t; // Insert the T-value
uri +="&h=";
uri += h; // Insert the H-value
CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, uri); // Make a basic HTTP request to the specified server using REST and the URL specified above
}
Maybe not the prettiest code you've seen, however, it works now.

Copying file bytes from SD Card to FRAM

I'm trying to copy a file from an SD card to an adafruit FRAM module. I'm wondering if I'm going about it the right way. I'm trying to read the file one byte at a time and then write that byte to a specific location on the Fram module.
I've been trying that approach using the sketch below and haven't been successful. I'm wondering if I'm approaching it the right way, and if so, where have I gone wrong with my sketch. Thanks.
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
uint16_t framAddr = 0;
void setup() {
Serial.begin(9600);
// setup SD-card
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println(" failed!");
while(true);
}
Serial.println(" done.");
}
void loop() {
uint16_t count = 0;
File myFile = SD.open("test.txt");
if (!myFile) {
// if the file didn't open, print an error and stop
Serial.println("error opening");
while (true);
}
const int S = 1;
byte buffer[S];
while (myFile.available()) {
// read from the file into buffer
myFile.read(buffer, sizeof(buffer));
Serial.print("0x"); Serial.print(count, HEX); Serial.print(": ");
Serial.println(buffer[count]);
//write fram (address,value)
fram.write8(count,buffer[count]);
}
myFile.close();
while (true) ;
}
I don't familiar with this FRAM, but you don't promote your address.
So the device write to the same address all the time and rewrite the memory.
Hope i help.
yoav

Arduino: loop function runs only once

I'm trying to request temperatures from my DS18B20 sensor to post on plot.ly, but it seems my loop function is only running once; after connecting to plot.ly and creating the graph, the temperature is printed once in the serial monitor and does not seem to continue! Any help is greatly appreciated. Here is my code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <plotly_streaming_cc3000.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WLAN_SSID "wifi"
#define WLAN_PASS "********"
#define WLAN_SECURITY WLAN_SEC_WPA2
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#define nTraces 1
char *tokens[nTraces] = {"token"};
plotly graph("username", "token", tokens, "filename", nTraces);
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!graph.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!graph.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!graph.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
Serial.println("Error setting inactivity timeout!");
}
}
}
void setup() {
graph.maxpoints = 100;
Serial.begin(9600);
sensors.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifi_connect();
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
void loop(void) {
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempFByIndex(0));
graph.plot(millis(), sensors.getTempFByIndex(0), tokens[0]);
delay(500);
}

Resources