Define Pin RX, TX Without SoftwareSerial Arduino - arduino

Hi I did 2 diferent codes, then after verifying that they work i try to combine them and I got a problem. I used GSM.h to controle my GSM module, and to controle the GPS I used the SoftwareSerial.h.
I tryed to combine these to codes and this 2 libraries conflit with each other.
Can some one help me?
This is my code
//GSM
#include <GSM.h>
#define PINNUMBER "3805"
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
//N de telefone de envio
char remoteNumber[20]= "914181875";
//Conteudo do SMS
char txtMsg[200]="Tester";
int val = 0;
//GPS
//#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(4, 3);
static void print_float(float val, float invalid, int len, int prec);
int button = 7;
void setup()
{
// initialize serial communications
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
val = digitalRead(button);
if (val == HIGH){
sendSMS();
}
}
void sendSMS(){
Serial.print("Message to mobile number: ");
Serial.println(remoteNumber);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}

I would try using the AltSoftSerial library instead of SoftwareSerial:
https://github.com/PaulStoffregen/AltSoftSerial
The disadvantage of this library is it requires you to use specific pins for serial communication with your GPS hardware, which are different from the pins you're currently using:
// AltSoftSerial always uses these pins:
//
// Board Transmit Receive PWM Unusable
// ----- -------- ------- ------------
// Teensy 3.0 & 3.1 21 20 22
// Teensy 2.0 9 10 (none)
// Teensy++ 2.0 25 4 26, 27
// Arduino Uno 9 8 10
// Arduino Leonardo 5 13 (none)
// Arduino Mega 46 48 44, 45
// Wiring-S 5 6 4
// Sanguino 13 14 12
I don't have the hardware to test but the following code does compile:
//GSM
#include <GSM.h>
#define PINNUMBER "3805"
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
//N de telefone de envio
char remoteNumber[20]= "914181875";
//Conteudo do SMS
char txtMsg[200]="Tester";
int val = 0;
//GPS
#include <AltSoftSerial.h>
AltSoftSerial ss;
#include <TinyGPS.h>
TinyGPS gps;
static void print_float(float val, float invalid, int len, int prec);
int button = 7;
void setup()
{
// initialize serial communications
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
val = digitalRead(button);
if (val == HIGH){
sendSMS();
}
}
void sendSMS(){
Serial.print("Message to mobile number: ");
Serial.println(remoteNumber);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}

Related

Thinger.IO client setup for GPRS enabled ESP32 project

I've been using the Thinger.io platform for some of my IoT projects (mostly ESP8266 modules) for quite a long time now. The way I implemented it is something similar to that:
#include <ThingerESP8266.h>
#include <ESP8266WIFI.h>
#define USERNAME "username"
#define DEVICE_ID "deviceid"
#define DEVICE_CREDENTIAL "devicecredential"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void connectToWifi() {
...
}
void setup() {
connectToWifi();
}
void loop() {
thing.handle();
}
and it just works. It is good to also mention that I've been using WiFi all the way.
Now I am trying to achieve the same by taking advantage of a controller called TTGO T-Call ESP32. It is GPRS enabled (using the TinyGsmClient.h) and I have inserted a SIM card inside of it which successfully connects to the internet. The issue is that I can not really establish a connection to the Thinger.io platform where my devices are hosted. This is what my code looks like (making a reference to this library example)
// Your GPRS credentials (leave empty, if not needed)
const char apn[] = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password
// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
// Define the serial console for debug prints, if needed
//#define DUMP_AT_COMMANDS
#include <Wire.h>
#include <TinyGsmClient.h>
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
// I2C for SIM800 (to keep it running when powered from battery)
TwoWire I2CPower = TwoWire(0);
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
#define uS_TO_S_FACTOR 1000000UL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 3600 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
#define IP5306_ADDR 0x75
#define IP5306_REG_SYS_CTL0 0x00
bool setPowerBoostKeepOn(int en){
I2CPower.beginTransmission(IP5306_ADDR);
I2CPower.write(IP5306_REG_SYS_CTL0);
if (en) {
I2CPower.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
} else {
I2CPower.write(0x35); // 0x37 is default reg value
}
return I2CPower.endTransmission() == 0;
}
void connectToApn(){
SerialMon.println("Connecting to: internet.vivacom.bg ... ");
while(!modem.gprsConnect(apn, gprsUser, gprsPass))
delay(500);
SerialMon.println("Successfully connected to: internet.vivacom.bg");
}
// #include <ThingerCore32.h> => ArduinoJson.h: No such file or directory
// #include <ThingerESP8266.h> => ESP8266WiFi.h : No such file or directory
#define USERNAME ""
#define DEVICE_ID ""
#define DEVICE_CREDENTIAL ""
#include <ThingerESP32.h>
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
//#include "arduino_secrets.h"
// Server details
const char server[] = "vsh.pp.ua";
const char resource[] = "/TinyGSM/logo.txt";
const int port = 80;
#include <ArduinoHttpClient.h>
HttpClient http(client, server, port);
void setup() {
// Set serial monitor debugging window baud rate to 115200
SerialMon.begin(115200);
// Start I2C communication
I2CPower.begin(I2C_SDA, I2C_SCL, 400000);
// Keep power when running from battery
bool isOk = setPowerBoostKeepOn(1);
SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
// Configure the wake up source as timer wake up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Connect to APN
connectToApn();
}
void loop() {
thing.handle();
SerialMon.println("In the loop ...");
delay(3000);
SerialMon.print(F("Performing HTTP GET request... "));
int err = http.get(resource);
if (err != 0) {
SerialMon.println(F("failed to connect"));
delay(10000);
return;
}
int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
delay(10000);
return;
}
SerialMon.println(F("Response Headers:"));
while (http.headerAvailable()) {
String headerName = http.readHeaderName();
String headerValue = http.readHeaderValue();
SerialMon.println(" " + headerName + " : " + headerValue);
}
int length = http.contentLength();
if (length >= 0) {
SerialMon.print(F("Content length is: "));
SerialMon.println(length);
}
if (http.isResponseChunked()) {
SerialMon.println(F("The response is chunked"));
}
String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);
SerialMon.print(F("Body length is: "));
SerialMon.println(body.length());
// Put ESP32 into deep sleep mode (with timer wake up)
// esp_deep_sleep_start();
}
NOTE: The board I've picked from the Arduino IDE is called ESP32 Wrover Module
It would be better if you ask this question on the thinger community, the thinger.io https://community.thinger.io/ where the thinger devs or community will be listening.
I have some working code, see below, this works with SIM7000E, but it should work OK with SIM800 the code should work the same. I have noticed that you are not using the thinger library (ThingerTinyGSM.h) and this is probably why the device isn't connecting to thinger.
#define THINGER_SERIAL_DEBUG //This will provide debug messages of what thinger
code is trying to do
#define _DISABLE_TLS_ //TLS needs to be disabled if using ESP32 (not sure why, this is a known bug)
// Select your modem:
//#define TINY_GSM_MODEM_SIM800 //Note SimCom docs state that SIM7000e used same commands as SIM800
#define TINY_GSM_MODEM_SIM7000 //Note SimCom docs state that SIM7000e used same commands as SIM800
#define APN_NAME "..."
#define APN_USER "..."
#define APN_PSWD "..."
//Pins for connecting to SIM module using 2nd Serial connection
#define RXD1 16
#define TXD1 17
#include <TinyGsmClient.h>
#include <ThingerTinyGSM.h>
//Thinger credentials
#define USERNAME "...." //Thinger Account User Name
#define DEVICE_ID "...." //Thinger device IC
#define DEVICE_CREDENTIAL "...." //Thinger device credential (password)
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, Serial2);
/*******************************
**** SET-UP **** SET-UP ****
********************************/
void setup() {
// open serial for debugging
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD1, TXD1);
delay(1000);
Serial.println(); Serial.println();
Serial.println("Starting Thinger GSM Test");
delay(1000);
// set APN, you can remove user and password from call if your apn does not require them
thing.setAPN(APN_NAME, APN_USER, APN_PSWD);
////// Thinger resource output example (i.e. reading a sensor value)
thing["Status"] >> [](pson & out) {
out["Timer(ms)"] = millis();
out["device"] = String(DEVICE_ID);
};
}
void loop() {
thing.handle();
}
This is the code which worked for me:
const char apn[] = ""; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org
const char gprsUser[] = ""; // GPRS User
const char gprsPass[] = ""; // GPRS Password
// SIM card PIN (leave empty, if not defined)
const char simPIN[] = "";
// TTGO T-Call pins
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
// Set serial for debug console (to Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to SIM800 module)
#define SerialAT Serial1
// Configure TinyGSM library
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
#include <Wire.h>
#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
// TinyGSM Client for Internet connection
TinyGsmClient client(modem);
void connectToApn(){
SerialMon.println("Connecting to: internet.vivacom.bg ... ");
while(!modem.gprsConnect(apn, gprsUser, gprsPass))
delay(500);
SerialMon.println("Successfully connected to: internet.vivacom.bg");
}
#define USERNAME ""
#define DEVICE_ID ""
#define DEVICE_CREDENTIAL ""
#include <ThingerTinyGSM.h>
ThingerTinyGSM thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL, Serial1);
void setup() {
// Set serial monitor debugging window baud rate to 115200
SerialMon.begin(115200);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
// Set GSM module baud rate and UART pins
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
delay(3000);
// Restart SIM800 module, it takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// Unlock your SIM card with a PIN if needed
if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
modem.simUnlock(simPIN);
}
// Connect to APN
connectToApn();
}
void loop() {
thing.handle();
}

How to publish and subscribe to MQTT broker through GSM Module in Arduino/ESP8266?

I am currently working in a project where I have to publish and possibly subscribe to MQTT topic over GSM network (I am using ESP8266 and Ai Thinker A6 GSM Module). So far I am able to publish message to the broker once using MQTT Example code from TinyGSM library,where they pass the GPRS client instance to Pubsub Client class, but when I add a code in loop to publish message continuously every n seconds. The message is published only once or twice and then freezes.
I doubt the mqttclient.loop() function in the void loop(). But, I am just wondering is there any other library which is completely tested, because the TinyGSM developer itself has mentioned this code is not tested in github.
The example code with mqtt publish in loop
// Select your modem:
#define TINY_GSM_MODEM_SIM800
// #define TINY_GSM_MODEM_SIM808
// #define TINY_GSM_MODEM_SIM868
// #define TINY_GSM_MODEM_SIM900
// #define TINY_GSM_MODEM_SIM7000
// #define TINY_GSM_MODEM_SIM5360
// #define TINY_GSM_MODEM_SIM7600
// #define TINY_GSM_MODEM_UBLOX
// #define TINY_GSM_MODEM_SARAR4
// #define TINY_GSM_MODEM_M95
// #define TINY_GSM_MODEM_BG96
// #define TINY_GSM_MODEM_A6
// #define TINY_GSM_MODEM_A7
// #define TINY_GSM_MODEM_M590
// #define TINY_GSM_MODEM_MC60
// #define TINY_GSM_MODEM_MC60E
// #define TINY_GSM_MODEM_ESP8266
// #define TINY_GSM_MODEM_XBEE
// #define TINY_GSM_MODEM_SEQUANS_MONARCH
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
// Add a reception delay - may be needed for a fast processor at a slow baud rate
// #define TINY_GSM_YIELD() { delay(2); }
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "YourAPN";
const char gprsUser[] = "";
const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
const char wifiSSID[] = "YourSSID";
const char wifiPass[] = "YourWiFiPass";
// MQTT details
const char* broker = "broker.hivemq.com";
const char* topicLed = "GsmClientTest/led";
const char* topicInit = "GsmClientTest/init";
const char* topicLedStatus = "GsmClientTest/ledStatus";
#include <TinyGsmClient.h>
#include <PubSubClient.h>
// Just in case someone defined the wrong thing..
#if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS false
#define TINY_GSM_USE_WIFI true
#endif
#if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
#undef TINY_GSM_USE_GPRS
#undef TINY_GSM_USE_WIFI
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#endif
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
PubSubClient mqtt(client);
#define LED_PIN 13
int ledStatus = LOW;
uint32_t lastReconnectAttempt = 0;
void mqttCallback(char* topic, byte* payload, unsigned int len) {
SerialMon.print("Message arrived [");
SerialMon.print(topic);
SerialMon.print("]: ");
SerialMon.write(payload, len);
SerialMon.println();
// Only proceed if incoming message's topic matches
if (String(topic) == topicLed) {
ledStatus = !ledStatus;
digitalWrite(LED_PIN, ledStatus);
mqtt.publish(topicLedStatus, ledStatus ? "1" : "0");
}
}
boolean mqttConnect() {
SerialMon.print("Connecting to ");
SerialMon.print(broker);
// Connect to MQTT Broker
boolean status = mqtt.connect("GsmClientTest");
// Or, if you want to authenticate MQTT:
//boolean status = mqtt.connect("GsmClientName", "mqtt_user", "mqtt_pass");
if (status == false) {
SerialMon.println(" fail");
return false;
}
SerialMon.println(" success");
mqtt.publish(topicInit, "GsmClientTest started");
mqtt.subscribe(topicLed);
return mqtt.connected();
}
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
pinMode(LED_PIN, OUTPUT);
// !!!!!!!!!!!
// Set your reset, enable, power pins here
// !!!!!!!!!!!
SerialMon.println("Wait...");
// Set GSM module baud rate
// TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
SerialAT.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
#if TINY_GSM_USE_GPRS
// Unlock your SIM card with a PIN if needed
if ( GSM_PIN && modem.getSimStatus() != 3 ) {
modem.simUnlock(GSM_PIN);
}
#endif
#if TINY_GSM_USE_WIFI
// Wifi connection parameters must be set before waiting for the network
SerialMon.print(F("Setting SSID/password..."));
if (!modem.networkConnect(wifiSSID, wifiPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
#endif
#if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
// The XBee must run the gprsConnect function BEFORE waiting for network!
modem.gprsConnect(apn, gprsUser, gprsPass);
#endif
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
#if TINY_GSM_USE_GPRS
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif
// MQTT Broker setup
mqtt.setServer(broker, 1883);
mqtt.setCallback(mqttCallback);
}
void loop() {
if (!mqtt.connected()) {
SerialMon.println("=== MQTT NOT CONNECTED ===");
// Reconnect every 10 seconds
uint32_t t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
delay(100);
return;
}
else
{
mqtt.publish("Topic/test","Hi");
delay(1000);
}
mqtt.loop();
}

Working a temprature sensor (LM35) with a GSM module (Sim800L)

I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading
void setup()
{
pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
Serial.begin(9600);
lcd.begin(16,2);
delay(500);
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}
you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> fona.sendSMS(sendto, message)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
while (!Serial);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
Serial.println(F("Initializing....(May take 3 seconds)"));
fonaSS.begin(9600);
if (!fona.begin(fonaSS)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
char sendto[21], message[141];
:
:
//initialize sendto and message
:
:
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
fonaSS.begin(9600);
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}
another way to send sms
you could test the hayes command: for example
void sendsms(){
Serial.println("Sending text message...");
fonaSS.print("AT+CMGF=1\r"); // SMS MODE
delay(100);
// phone number
fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number
delay(100);
// message here
fonaSS.print("Message test \r");
// CTR+Z in mode ASCII, to indicate the end of message
fonaSS.print(char(26));
delay(100);
fonaSS.println();
Serial.println("Text send");
}
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
// you initialisation code
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in degrees Celsius
tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit
lcd.setCursor(0,0);
lcd.print("DegreeC= ");
lcd.print(tempc);
lcd.setCursor(0,1);
lcd.print("Fahrenheit=");
lcd.print(tempf);
delay(1000); //Delay of 1 second for ease of viewing in serial monitor
if (tempc > 30.0) {
SendSms();
}
}
void SendSms() {
char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
char message[141];
sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
//sends the message via SMS
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("error"));
} else {
Serial.println(F("sent!"));
}
}

arduino user input to control on and off (with time)

I want to use bluetooth connection to control, how long something is on. So I read the Serial Input. If it is a number, I take the number and put it into a delay. After it is through, I write something and check again. If the Serial Read is not a number, it should turn off. in The problem is, the led keeps running. Do you see, what is my mistake?
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available()){
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
}
delay(100);
}
Try to use this:
#include <SoftwareSerial.h>
#define ledPin 13
#define rxPin 10
#define txPin 11
SoftwareSerial btSerial(rxPin, txPin);
int btData;
void setup() {
btSerial.begin(9600);
btSerial.println("bluetooth available");
pinMode(ledPin,OUTPUT);
serv.attach(3);
serv2.attach(5);
}
void loop() {
if (btSerial.available())
btData = btSerial.read();
if(isDigit(btData)){
digitalWrite(ledPin,1);
btSerial.println("LED on Pin is on");
delay(btData*10);
}
else {
digitalWrite(ledPin,0);
btSerial.println("LED on Pin is off");
}
delay(100);
}
And the reason is because the enabling and disabling was done only when there is serial data and if you put the if anywhere else it would be checked always.

Arduino with two RC522

To pimp up my Carrera I'm going to build a round counter.
It contains an Arduino Nano, a lcd and 2 rc522 rfid-reader.
The readers share the pins for scd, miso, mosi and have own pins for sda and rst.
Actually I'm not able to get the two readers work together at the same time. Only if the one or the other reader is physically plugged (hardcore!) into the breadbord it works (without code changing). But not together.
It has to be an issue with my code, but where?
(The RFID-Communication ist inpired by the example from [addicore][1]http://www.addicore.com/v/vspfiles/downloadables/Product%20Downloadables/RFID_RC522/RFIDQuickStartGuide.pdf)
Is there anyone who has a hint for me?
#include <AddicoreRFID.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#define uchar unsigned char
#define uint unsigned int
// create AddicoreRFID object to control the RFID module
/////////////////////////////////////////////////////////////////////
//set the pins
/////////////////////////////////////////////////////////////////////
//2 - SCK Digital 13
//3 - MOSI Digital 11
//4 - MISO Digital 12
const int SS1 = 8; //RFID1
const int RST1 = 9;
AddicoreRFID myRFID1 (SS1, RST1);
const int SS2 = 10; //RFID2
const int RST2 = A0;
AddicoreRFID myRFID2 (SS2, RST2);
//Maximum length of the array
#define MAX_LEN 16
//LCD init
// * LCD RS pin to digital pin 7
// * LCD Enable pin to digital pin 6
// * LCD D4 pin to digital pin 5
// * LCD D5 pin to digital pin 4
// * LCD D6 pin to digital pin 3
// * LCD D7 pin to digital pin 2
LiquidCrystal lcd1(7, 6, 5, 4, 3, 2);
//Counter
int a = 0;
int b = 0;
void setup() {
Serial.begin(9600);
//LCD init
init_lcd1();
myRFID1.AddicoreRFID_Init();
myRFID2.AddicoreRFID_Init();
}
void loop() {
uchar i, tmp, checksum1;
uchar status;
uchar str1[MAX_LEN];
uchar str2[MAX_LEN];
///////////// RFID1 ///////////////////
// 0x4400 = Mifare_UltraLight -Tag Type
str1[1] = 0x4400;
//Find tags, return tag type
// Manipuliert str1(!);
//AddicoreRFID::AddicoreRFID_Request(byte reqMode, byte *TagType)
status = myRFID1.AddicoreRFID_Request(PICC_REQIDL, str1);
if (status == MI_OK) {
serial_TagDetect(str1, 1);
}
//Anti-collision, return tag serial number 4 bytes
// Manipuliert str1(!);
status = myRFID1.AddicoreRFID_Anticoll(str1);
if (status == MI_OK) {
serial_TagData(str1);
lcd_counter(str1, lcd1);
//delay(500);
}
myRFID1.AddicoreRFID_Halt(); //Command tag into hibernation
///////////// RFID2 ///////////////////
str2[1] = 0x4400;
//Find tags, return tag type
status = myRFID2.AddicoreRFID_Request(PICC_REQIDL, str2);
if (status == MI_OK) {
serial_TagDetect(str2, 2);
}
//Anti-collision, return tag serial number 4 bytes
status = myRFID2.AddicoreRFID_Anticoll(str1);
if (status == MI_OK) {
serial_TagData(str2);
lcd_counter(str2, lcd1);
// liest sonst nonstop die Tags!
//delay(500);
}
myRFID2.AddicoreRFID_Halt(); //Command tag into hibernation
}
void init_lcd1() {
... inits the lcd ...
}
// Zählt das Auftreten der Tags
void lcd_counter (uchar *str, LiquidCrystal lcd ) {
... output to the lcd ....
}
// Meldet gefundenen Tag auf der Konsole
void serial_TagDetect(uchar *str, int reader) {
if (reader == 1) {
Serial.print("RFID1 tag detected: ");
} else {
Serial.print("RFID2 tag detected: ");
}
Serial.print(str[0], BIN);
Serial.print(" , ");
Serial.print(str[1], BIN); Serial.println(" ");
}
// gibt Tagdaten auf der Konsole aus
void serial_TagData(uchar *str) {
uchar checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
Serial.print("The tag's number is: ");
//Serial.print(2);
Serial.print(str[0]);
Serial.print(" , ");
Serial.print(str[1], BIN);
Serial.print(" , ");
Serial.print(str[2], BIN);
Serial.print(" , ");
Serial.print(str[3], BIN);
Serial.print(" , ");
Serial.print(str[4], BIN);
Serial.print(" , ");
Serial.println(checksum1, BIN);
}
I wonder about that the same(!!) rfid tag has different values depending it gets read from RFID1 or RFID2:
RFID1 tag detected: 1000100 , 0
The tag's number is: 136 , 100 , 11100 , 1101011 , 11111011 , 11111011
RFID2 tag detected: 1000100 , 0
The tag's number is: 68 , 0 , 0 , 0 , 0 , 1000100
I use 10 RC522 together at the same time with Ardunio mega .
Try this example for 2 ncf reader, its work for me. I tested in Arduino Nano.
The readers share the pins for 3,3V, GND. SCD, MISO, MOSI, RST and have own pins for SDA. I don't use the IRQ pin.
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS1_PIN 10 // Configurable, see typical pin layout above
#define SS2_PIN 8 // Configurable, see typical pin layout above
MFRC522 mfrc522_1(SS1_PIN, RST_PIN); // Create MFRC522 instance
MFRC522 mfrc522_2(SS2_PIN, RST_PIN);
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522_1.PCD_Init(); // Init MFRC522
mfrc522_1.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
mfrc522_2.PCD_Init(); // Init MFRC522
mfrc522_2.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
}
void loop() {
// Look for new cards
if ( mfrc522_1.PICC_IsNewCardPresent() || mfrc522_2.PICC_IsNewCardPresent()) {
Serial.println(F("New card..."));
} else {
return;
}
// Select one of the cards
if ( ! mfrc522_1.PICC_ReadCardSerial() && ! mfrc522_2.PICC_ReadCardSerial()) {
Serial.println(F("Read..."));
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522_1.PICC_DumpToSerial(&(mfrc522_1.uid));
mfrc522_2.PICC_DumpToSerial(&(mfrc522_2.uid));
}
//Anti-collision, return tag serial number 4 bytes
status = myRFID2.AddicoreRFID_Anticoll(str1); // <<<< needs to be strl2

Resources