我正在尝试使 GSM 扩展板与 SD 扩展板一起工作,即每次收到消息时都应将其保存到 SD 卡上。然而这似乎不起作用。奇怪的是,错误似乎每次都会在不同的地方出现。有时循环会停止在 sms.flush();但有时它会在 lastMess = ""; 处遇到问题。而且,这些字符经常被打乱并形成奇怪的符号。 我认为问题可能与串行监视器、GSM 扩展板和 SD 卡的通信有关。 SPI/串行协议问题。我还尝试用串行监视器输入替换 SMS 输入,并将其写入 SD,但这带来了更多问题。 预先感谢您!
#include <SPI.h>
#include <SD.h>
File dataFile;
const int chipSelect = 4;
// include the GSM library
#include <GSM.h>
// PIN Number for the SIM
#define PINNUMBER ""
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
// Array to hold the number a SMS is retreived from
char senderNumber[20];
char Content[20];
String lastMess;
char c;
String txtmsg;
String readString;
void setup()
{
// initialize serial communications and wait for port to open:
while (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
//return;
}
Serial.begin(9600);
Serial.println("SMS Messages Receiver");
// connection state
boolean notConnected = true;
// Start GSM connection
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
{
notConnected = false;
}
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("SD initialized");
Serial.println("Waiting for messages");
}
void loop()
{
lastMess = "";
Serial.println("BeginLoop");
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// Read message bytes and print them
while (c = sms.read())
{
Serial.print(c);
String nextChar = String (c);
String Mess = (lastMess + nextChar);
lastMess = Mess;
}
Serial.println("This is the message content:");
Serial.println(lastMess);
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(senderNumber);
dataFile.print(",");
dataFile.print(lastMess);
dataFile.print(",");
dataFile.println("time e.g. 12:00");
dataFile.close();
delay(1000);
Serial.println("Content written to SD");
}
else if (!dataFile)
{
Serial.println("SD not opened");
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.csv");
}
Serial.println("Is SD being skipped?");
}
delay(1000);
Serial.println("EndLoop");
}
答案竟然在arduino的内存中,我得到了以下消息: “可用内存不足,可能会出现稳定性问题。” 这扰乱了字符并引发了问题。减少代码以使用更少的内存后,一切正常!
我正在使用 sim800l EVB,并且遇到了同样的问题。 当我调试以找出未执行的内容时,发现是 isReady() 函数。
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "EmonLib.h"
#include "SIM800L.h"
#include <SPI.h>
#include <SD.h>
// Define pin constants
#define SIM800_RX_PIN 2
#define SIM800_TX_PIN 3
#define SIM800_RST_PIN 7
// Define configuration constants
const int chipSelect = 4;
const char APN[] = "internet";
const char URL[] = "http://api.restful-api.dev/objects";
const char CONTENT_TYPE[] = "application/json";
const char PAYLOAD[] = "{\"name\": \"Apple MacBook Pro 16\", \"data:{\"year\": 2019, \"price\": 1849.99, \"CPU model\": \"Intel Core i9\", \"Hard disk size\": \"1 TB\"}}";
void setup() {
// Initialize LCD display
lcd.init();
lcd.backlight();
// Initialize serial communication for debugging
Serial.begin(115200);
while (!Serial);
initializationOf("GSM");
// Initialize GSM and SIM800L module
initializeGSM();
initializationOf("SD card");
// Initialize SD card module
//initializeSDCard();
// Configure EnergyMonitor
emon1.voltage(A0, 110.75, 1.7);
// Clear LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MEASURED VOLTAGE");
}
void loop() {
// Calculate voltage
emon1.calcVI(20, 2000);
volt = emon1.Vrms;
// Display voltage on LCD
lcd.setCursor(0, 1);
lcd.print("VOLTAGE = ");
lcd.print(volt);
lcd.print("V ");
// Check if counter has reached 5
if (counter >= 5) {
// Perform data upload and reset counter
timeUpForPushToDB();
counter = 0;
}
// Increment counter
counter++;
// Add delay to control loop frequency
delay(500);
}
// Function to initialize GSM and SIM800L module
void initializeGSM() {
// Initialize SoftwareSerial for SIM800 module
SoftwareSerial* serials = new SoftwareSerial(SIM800_RX_PIN, SIM800_TX_PIN);
serials->begin(9600);
delay(1000);
// Initialize SIM800L module
sim800l = new SIM800L((Stream*)serials, SIM800_RST_PIN, 200, 512);
// Perform module setup
setupModule();
}
void initializeSDCard() {
Serial.print("Initializing SD card...");
// See if the card is present and can be initialized
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// Don't do anything more
while (1);
}
Serial.println("card initialized.");
}
void setupModule() {
// Wait until the module is ready to accept AT commands
while (!sim800l->isReady()) {
Serial.println(F("Problem to initialize AT command, retry in 1 sec"));
delay(1000);
}
Serial.println(F("Setup Complete!"));
// Wait for GSM signal
uint8_t signal = sim800l->getSignal();
while (signal <= 0) {
delay(1000);
signal = sim800l->getSignal();
}
Serial.print(F("Signal OK (strength: "));
Serial.print(signal);
Serial.println(F(")"));
delay(1000);
// Wait for operator network registration
NetworkRegistration network = sim800l->getRegistrationStatus();
while (network != REGISTERED_HOME && network != REGISTERED_ROAMING) {
delay(1000);
network = sim800l->getRegistrationStatus();
}
Serial.println(F("Network registration OK"));
delay(1000);
// Setup GPRS configuration
bool success = sim800l->setupGPRS(APN);
while (!success) {
success = sim800l->setupGPRS(APN);
delay(5000);
}
Serial.println(F("GPRS config OK"));
}