I have this code running on the arduino:
#include <SoftwareSerial.h>
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// pins!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const int delayTime=10;
const int pin_yes_gate=3;
const int pin_no_gate=5;
int switchVal =0;
void setup()
{
Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
pinMode(pin_yes_gate, OUTPUT);
pinMode(pin_no_gate, OUTPUT);
}
void loop()
{
if (switchVal ==1 ){
allow();
}else{
prohibit();
}
if ( Serial.available())
{
String string = Serial.readString();
string.trim();
Serial.print("I received: ");
Serial.print(string);
Serial.print("\n");
if(string.equals("allowed"))
{
switchVal=1;
}else if(string.equals("error"))
{
switchVal=0;
}
}
}
void allow(){
digitalWrite(pin_yes_gate,HIGH);
digitalWrite(pin_no_gate,LOW);
delay(delayTime);
}
void prohibit(){
digitalWrite(pin_yes_gate,LOW);
digitalWrite(pin_no_gate,HIGH);
delay(delayTime);
}
and I'm writing echo allowed >> /dev/ttyACM0 which gives the reaction, but for less than a second, and then the arduino returns to the initial state. This is weird, as it works with the arduino IDE perfectly.
Am I missing something ?
Just solved it, apparently the problem is the
Serial.print("I received: ");
Serial.print(string);
Serial.print("\n");
It errors out if serial is not read. So either removing this, or as in my case I can't change anything on the arduino side run this command on raspi boot tail -f /dev/ttyACM0 & which opens a read port
Related
I am having a problem with my code for arduino m0 (using microchip SAMD21). There are two SPI interfaces, first classic and second with int variable in front of the pin name, int MISO, for instance. Does someone know, how to control this classic SPI interface?
I have also attached my code.
PS: Code stucks in begin function of OZONE2CLICK sensor...
#include "Arduino.h"
#include <MQ131.h>
// include RFM69 library
#include <SPI.h>
// Local
#define PC_BAUDRATE 56700
#define MS_DELAY 0 // Number of milliseconds between data sending and LED signalization
#define LED_DELAY 100
#define Serial SerialUSB
// SD card
#define sd_cs_pin 35 // set SD's chip select pin (according to the circuit)
float PPMO2;
float PPBO2;
float MGM3O2;
float UGM3O2;
const byte pinSS = 2; //cs pin
const byte pinRDY = 12;
const byte pinSCK = 13;
const byte O2Pin = 10;
#define DcPin 8
// SD card file
File file; // SD library variable
// LEDS
#define D13_led_pin 42 // D13 LED
#define M_led_pin 36 // MLED
// Local variables
int idCounter = 1;
bool isBmeOk = true;
bool isSdOk = true;
bool isRadioOk = true;
bool isGpsConnected = true;
void OZONE2CLICKCalibrate ()
{
Serial.println("2");
//MQ131.begin(pinSS, pinRDY, O2Pin, LOW_CONCENTRATION, 10000); //(int _pinCS, int _pinRDY, int _pinPower, MQ131Model _model, int _RL)
Serial.println("99");
Serial.println("Calibration in progress...");
MQ131.calibrate();
Serial.println("Calibration done!");
Serial.print("R0 = ");
Serial.print(MQ131.getR0());
Serial.println(" Ohms");
Serial.print("Time to heat = ");
Serial.print(MQ131.getTimeToRead());
Serial.println(" s");
}
void OZONE2CLICKMeasure ()
{
Serial.println("Sampling...");
MQ131.sample();
Serial.print("Concentration O3 : ");
PPMO2 = MQ131.getO3(PPM);
Serial.print(PPMO2);
Serial.println(" ppm");
Serial.print("Concentration O3 : ");
PPBO2 = MQ131.getO3(PPB);
Serial.print(PPBO2);
Serial.println(" ppb");
Serial.print("Concentration O3 : ");
MGM3O2 = MQ131.getO3(MG_M3);
Serial.print(MGM3O2);
Serial.println(" mg/m3");
Serial.print("Concentration O3 : ");
UGM3O2 = MQ131.getO3(UG_M3);
Serial.print(UGM3O2);
Serial.println(" ug/m3");
}
void setup()
{
Serial.begin(PC_BAUDRATE);
// wait for the Arduino serial (on your PC) to connect
// please, open the Arduino serial console (right top corner)
// note that the port may change after uploading the sketch
// COMMENT OUT FOR USAGE WITHOUT A PC!
// while(!Serial);
Serial.println("openCanSat PRO");
Serial.print("Node ");
Serial.print(MYNODEID,DEC);
Serial.println(" ready");
// begin communication with the BME280 on the previously specified address
// print an error to the serial in case the sensor is not found
if (!bme.begin(BME280_ADDRESS_OPEN_CANSAT))
{
isBmeOk = false;
Serial.println("Could not find a valid BME280 sensor, check wiring!");
return;
}
// begin communication with the INA219
ina219.begin();
// check of Gps is connected
Wire.beginTransmission(0x42); // 42 is addres of GPS
int error = Wire.endTransmission();
if (error != 0)
{
isGpsConnected = false;
}
// begin communication with gps
gps.begin();
// Uncomment when you want to see debug prints from GPS library
// gps.debugPrintOn(57600);
if(!radio.initialize(FREQUENCY, MYNODEID, NETWORKID))
{
isRadioOk = false;
Serial.println("RFM69HW initialization failed!");
}
else
{
radio.setFrequency(FREQUENCYSPECIFIC);
radio.setHighPower(true); // Always use this for RFM69HW
}
pinMode(D13_led_pin, OUTPUT);
}
void loop()
{
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
pinMode(DcPin, OUTPUT);
pinMode(O2Pin, OUTPUT);
digitalWrite(DcPin, HIGH);
digitalWrite(O2Pin, HIGH);
delay(10000);
OZONE2CLICKCalibrate();
OZONE2CLICKMeasure();
}
It looks the code opening the SPI connection is commented out:
MQ131.begin(pinSS, pinRDY, O2Pin, LOW_CONCENTRATION, 10000);
You need to configure the SPI connection to get any data from your device.
Refer to reference code from the manufacturer or library you're using to make sure your programming against it correctly.
Please format your code with predictable spacing. This is pretty hard to read.
Since you're using C++, prefer to use:
constexpr <type> NAME = <value>;
rather than macros:
#define NAME (<value>)
Since this is a bare metal compilation, using return in the setup() or loop() functions does not stop them. You probably want something more like while (true) {}. This will loop the code indefinitely, rather than proceed in a bad state.
i.e.:
void stop_forever() {
Serial.println("fatal error detected, stoping forever.");
while (true) {}
}
// then, use it later:
// ...
if (error) {
stop_forever();
}
// ...
I am trying to make USB serial communication between Arduino and Raspberry Pi 3. However, the Arduino IDE serial monitor outputs correct values, but it does not work finely in code using pyserial. The program is running normally, but there is no output on the output terminal.
Arduino code:
#include <DHT11.h>
int pin=2;
int pin1=4;
DHT11 dht11(pin);
DHT11 dht11(pin1);
void setup()
{
Serial.begin(9600);
while(!Serial) {
; //wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
int sensorValue=analogRead(A0);
float outvoltage = sensorValue * (5.0 / 1023.0);
int Level = 6*outvoltage; //The level of wind speed is proportional to the output voltage.
int err1, err2;
float temp1, temp2, humi1, humi2;
if((err1=dht11.read(humi1, temp1))==0&&(err2=dht12.read(humi2, temp2))==0)
{
Serial.print(Level);
Serial.print(" ");
Serial.print(temp1);
Serial.print(" ");
Serial.print(humi1);
Serial.print(" ");
Serial.print(temp2);
Serial.print(" ");
Serial.print(humi2);
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err1);
Serial.print(err2);
Serial.println();
}
delay(1000); //delay for reread
}
Python code:
import serial
ser=serial.Serial("/dev/cu.usbmodemFD1411",9600)
while True:
print ser.readline()
I have noticed that setup() is executed again and again. I receive the same messages from setup()
Here's the code:
enum flags{
RFID= 1,
LASER_ON= 2,
LASER_OFF= 4,
.
.
.
};
int inByte;
bool fcount = false;
unsigned int count=0;
long average_input=0;
bool cont = false;
int prev=-1;
/**
* Initialize.
*/
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)
// Serial1.begin(19200);
// while (!Serial1);
// SPI.begin(); // Init SPI bus
// mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(laser, OUTPUT);
pinMode(13, OUTPUT);
// Prepare the key (used both as key A and as key B)
// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
// for (byte i = 0; i < 6; i++) {
// key.keyByte[i] = 0xFF;
// }
Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
Serial.print(F("Using key (for A and B):"));
// dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();
Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));
digitalWrite(laser, HIGH);
for(byte i =0; i<10 ; i++){
average_input += analogRead(photocell);
delay(10);
}
Serial.print("total sum=\t");Serial.println(average_input);
average_input /=10;
Serial.print("average=\t");Serial.println(average_input);
digitalWrite(laser, LOW);
}
/**
* Main loop.
*/
void loop() {
if(Serial.available()){
delay(2000);
Serial.println("Serial available");
int input = 0;
while (Serial.available()){
input = input*10 + (Serial.read()-48);
inByte = input;
}
Serial.println(inByte);
}
// if(Serial1.available()){
// delay(100);
// Serial.println("Serial1 available");
// int input = 0;
// while (Serial1.available()){
// input = input*10 + (Serial1.read()-48);
// inByte = input;
// }
// Serial.println(inByte);
// }
switch(inByte){
case RFID:
Serial.println("RFID CHOSEN");
// get_rfid();
break;
case LASER_ON:
digitalWrite(laser, HIGH);
fcount=true;
break;
case LASER_OFF:
digitalWrite(laser, LOW);
fcount=false;
break;
.
.
.
}
if (fcount){
.
.
.
}
As you can see I commented out a lot, just trying to make setup() run only once. As the general goal: I'm trying to connect my Qt UI through QSerialPort. I've found a good solution on similar topic and adopted it. Here's the Qt code:
.
.
.
QSerialPort serial;
QStringList ports;
for (QSerialPortInfo port : QSerialPortInfo::availablePorts()){
qDebug() << port.portName() << port.vendorIdentifier() << port.productIdentifier()
<< port.hasProductIdentifier() << port.hasVendorIdentifier() << port.isBusy();
ports += port.portName();
}
serial.setPortName("cu.usbmodem1471");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
qDebug()<<serial.isOpen()<<endl;
qDebug()<<serial.isWritable()<<endl;
if (serial.isOpen() && serial.isWritable())
{
QByteArray ba("2");
serial.write(ba);
//serial.flush();
qDebug() << "data has been send" << endl;
serial.close();
}
.
.
.
First of all, it shows false false on both isOpen() and isWritable() when the serial monitor is opened. My guess is that Qt is trying to connect/send data to the same Serial as PC getting data on. When the Serial monitor is closed the flags are set to true true and message is sent. However it goes through the same setup process (aka calibrating laser) and not receiving 2 as a flag to turn on laser. Please advice me on Serial communication between PC and Arduino.
The serial port is used exclusively by an application, either SerialMonitor or Qt.
Opening a COM port (both by SerialMonitor or Qt) usually triggers a reset of the Arduino. That's why setup() is running.
This is used to make uploading of new sketches easier.
It's achieved by toggling the Serial control signal DTR.
I'm not a Qt expert to know exactly how to disable this on the PC side. Some terminal programs give you control over that DTR signal.
Possible workarounds:
Open the Serial line only once and keep it open as long as your Qt app is active.
If you have a 10 µF capacitor or similar, plug it into the Arduino between GND and RESET. This should inhibit the automatic reset. (Remove it to upload new sketch)
I want to send data from an end device to a coordinator with XBee and Arduino. But, the Arduino reboots when sending data (sending data was aborted). What could the problem be?
/* Transmit */
#include <SoftwareSerial.h>
#include <XBee.h>
int end = 1;
int alim_XbeeRS = A7;
int RX_XBee = 14;
int TX_XBee = 15;
XBee xbee = XBee();
//Allume le périphérique
void powerOn(SoftwareSerial portcom)
{
portcom.begin(57600);
digitalWrite(alim_XbeeRS, HIGH);
}
void setup ()
{
SoftwareSerial XbeeRS(RX_XBee,TX_XBee);
Serial.begin(57600);
XbeeRS.begin(57600);
pinMode(RX_XBee, INPUT); // Rx
pinMode(TX_XBee, OUTPUT); // Tx
pinMode(alim_XbeeRS, OUTPUT);
powerOn(XbeeRS);
xbee.setSerial(XbeeRS);
delay(5000);
Serial.println("XBee OP");
}
void loop()
{
if (end == 1)
{
Serial.println("sending");
ZBTxRequest _zbTx;
uint8_t payload[] = {'Y','E','S','\0'};
XBeeAddress64 address = XBeeAddress64 (0x13A200,0x4081B77C );
_zbTx = ZBTxRequest(address, payload, sizeof(payload));
Serial.println("sending");
xbee.send(_zbTx); // The program blocks here
}
else
{
Serial.println("waiting");
xbee.readPacket(100);
if (xbee.getResponse().isAvailable())
{
Serial.println("waiting 1");
if( xbee.getResponse().getApiId() == ZB_RX_RESPONSE)
{
Serial.println("waiting 2");
ZBRxResponse _rx;
xbee.getResponse().getZBRxResponse(_rx);
uint8_t* response= new uint8_t[50];
for(int i=0; i<_rx.getDataLength(); i++)
{
response[i] = _rx.getData()[i];
Serial.println(char(response[i]));
}
}
}
}
}
EDIT (additional information):
It doesn't change anything if I change the value type in the payload. About the baud rate, both of XBees are configured to 57600 baud. Here is the XBee's configuration:
ENDEVICE
COORDINATOR
The result from the serial port of this device is:
Finally, I use the Arduino ATmega 1284P. I really have no idea what kind of problem could do this.
There is some trouble :/
First, the default coordinator ADD is 0x0 0x0, so the line where
XBeeAddress64 address = XBeeAddress64 (0x13A200,0x4081B77C );
should be
XBeeAddress64 address = XBeeAddress64 (0x0,0x0 );
Then, is the Xbee at 57600 baud too?
To get an ACK, you can use:
if (xbee.readPacket(1000))
{
if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
{
xbee.getResponse().getZBTxStatusResponse(txStatus);
if (txStatus.getDeliveryStatus() == SUCCESS)
{
//It's sent
}
}
It could be from you payload too. You better use a hexadecimal value or int to be sure of what you send.
EDIT:
I see you don't use the last version of Xctu. Try it and test the direct communication between them to see if you can have direct contact between Coordinator and Routeur/End device.
I have tried to control motor stepper via arduino uno board with slider in visual C++.
but the servo didnot move at all.
here the program at PC side:
void CENVSConfigDlg::OnBnClickedButton1()
{
SetTimer(cTimer1,80,NULL);
}
void CENVSConfigDlg::OnTimer(UINT_PTR ID)
{
if(ID==cTimer1){
DWORD nbytes;
char buf[5];
sprintf(buf, "%d \n", val_test);
/* Open serial port. */
if(!WriteFile( hnd_serial, (void*)buf, 5, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;}
}
and the program in arduino:
#include <Servo.h>
Servo servoMain;
int index=0;
String inputString;
void setup()
{
Serial.begin(9600);
servoMain.attach(9);
}
void loop()
{
int data;
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == 'z')
{
data=stringToInt(inputString);
Serial.println(data); //
inputString="";
servoMain.write(data); //tambahannya
delay(50);
break;
}
if (inChar != 0)
{
inputString += inChar;
}
}
}
int stringToInt(String s)
{
char char_string[s.length()+1];
s.toCharArray(char_string, s.length()+1);
return atoi(char_string);
}
the pc i think is sending the data, but why the motor didnot working? any idea?
First off, does the serial link work at all? The number of ways that an RS232 link can not work, for both hardware and software reasons, is legendary. Unless you can show that the hardware can transfer data, it's pointless to look at your dedicated software. If you have a scope, use that to see if anything is being transmitted and then check that it arrives at the correct pin on the arduino input. If you have no access to a scope, a small LED and a 4.7K resistor can be used to indicate that there is data on the line - it will flicker with the data.