Pyserial doesn't respond to Arduino - python-2.7

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()

Related

How to control a linear actuator with an ESP32 and a BME280 sensor

I'm trying to program an EPS32 board to open and close a linear actuator depending of the temperature a BME 280 sensor gets. It is for an automated gardening project :)
My code is functional but I would like to perfect it.
The actuator is currently connected with two GPIO pin, one to open and he other to close.
GOALS:
Switch off relays when not used.
Replace the use of delay() with millis()
Currently, the actuator is opening when temperature is above 27c°. Relay is on for 7 seconds which let the time to the actuator to go full course. Then, the relay is switch off.
But if the temperature stay above 27, the "if" part of the loop is going on an on, meaning it turn on for 7 seconds, switch off for 7 seconds and so on... I would like to switch all relays off when not needed to save energy and extend components life.
I suppose there might be something to do with && or even the analogRead() function to get the state of the actuator and apply actions from that but it is too complicated for me.
To open the actuator, I decided to time the course of the actuator and let the relay on for that timing with a delay() function. There is probably a different approach to this ( with analogRead() I suppose) but I find it simple and working. I would like to replace delay() with millis() function in order not to block the rest of the script (I plan to add fans, water valves, a float switch sensor...).
May you show me the way ?
#include <Adafruit_BME280.h>
#include <WiFi.h>
const char* ssid = "#####"; // ESP32 and ESP8266 uses 2.4GHZ wifi only
const char* password = "#####";
//MQTT Setup Start
#include <PubSubClient.h>
#define mqtt_server "####.###.##.##"
WiFiClient espClient;
PubSubClient client(espClient);
#define mqttActuator "growShed/Acuator"
#define mqttTemp1 "growShed/temp1"
int actuatorUp = 15; //GPIO de l'actuator
int actuatorDown = 2; //GPIO de l'actuator
int actuatorUpread = (!digitalRead(actuatorUp)); //read state of gpio : on or off
int actuatorDownread = (!digitalRead(actuatorDown)); //read state of gpio : on or off
//MQTT Setup End
Adafruit_BME280 bme1; // I2C
//utiliser les virgules sur les capteurs suivants
float temp1, hum1, pres1;
//gestion des délais entre chaque captations de data
unsigned long millisNow1 = 0; //for delay purposes (laisser 0)
unsigned int sendDelay1 = 2000; //delay before sending sensor info via MQTT
//gestion des délais entre chaque captations de data
unsigned long millisNow3 = 0; //for delay purposes (laisser 0)
unsigned int sendDelay3 = 7000; //delay before sending sensor info via MQTT
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
// begin Wifi connect
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(2000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address (wifi RaspPi) : ");
Serial.println(WiFi.localIP());
//end Wifi connect
client.setServer(mqtt_server, 1883);
//BME
delay(5000);
unsigned status;
status = bme1.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme1.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1);
}
//Acuator
pinMode(actuatorUp, OUTPUT);
digitalWrite(actuatorDown, HIGH); //Begin with actuator switched off
pinMode(actuatorDown, OUTPUT);
digitalWrite(actuatorUp, HIGH); //Begin with actuator switched off
//End Actuator
}
bool getValues() {
temp1 = round(bme1.readTemperature()); //rounded values
actuatorUpread = (digitalRead(actuatorUp)); //inverted value as the relay is inverted
Serial.print("BME 1 Temperature = ");
Serial.print(temp1);
Serial.println(" °C");
Serial.print("État de l'actuator haut = ");
Serial.println(actuatorUpread);
Serial.print("État de l'actuator bas = ");
Serial.print(actuatorDownread);
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
int counter = 0;
while (!client.connected()) {
if (counter==5){
ESP.restart();
}
counter+=1;
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("growTentController")) {
Serial.println("connected");
} else {
Serial.println("failed, rc=");
Serial.println(client.state());
Serial.println(" try again in 5 seconds");
// Wait 10 seconds before retrying
delay(5000);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()){
reconnect();
}
if (millis() > millisNow1 + sendDelay1){
if (getValues()) {
client.publish(mqttTemp1, String(temp1).c_str(),true);
client.publish(mqttActuator, String(actuatorUpread).c_str(),true);
millisNow1 = millis();
}
}
//Acuator moving according to BME280 temperature sensor
temp1 = bme1.readTemperature();
if (millis() > millisNow3 + sendDelay3){
if ((temp1 > 27)){
digitalWrite(actuatorUp, LOW);
delay(7000); // approx the time it take to execute the full course of the acuator.
digitalWrite(actuatorUp, HIGH);
}
else {
digitalWrite(actuatorDown, LOW);
delay(7000); // approx the time it take to execute the full course of the acuator
digitalWrite(actuatorDown, HIGH);
}
millisNow3 = millis();
}
}
I think this is what you want to do. This uses a state variable to keep track of what state the actuator is in.
When the actuator is closed, the loop will check the temperature and, if the temperature goes above 27 degrees, it will start opening the actuator. Then the loop will simply monitor the time using millis() until the actuator is open - so you can put other code in the loop for other sensors.
The same happens if the actuator is open, it will close the actuator when the temperature is lower than 26 degrees (kept a degree difference here to stop the code constantly triggering if the temperature hovers around 27 degrees).
Currently this will read the temperature on every iteration of the loop whilst the actuator is either open or closed - so I've put a small delay at the end of the loop to restrict this.
Please note - I have not tested any of the changes in the code as I don't have the hardware to hand. But it compiles, and should give you an idea of how you can use a state model with millis() to handle this sort of thing without delaying the loop.
#include <Adafruit_BME280.h>
#include <WiFi.h>
const char* ssid = "#####"; // ESP32 and ESP8266 uses 2.4GHZ wifi only
const char* password = "#####";
//MQTT Setup Start
#include <PubSubClient.h>
#define mqtt_server "####.###.##.##"
WiFiClient espClient;
PubSubClient client(espClient);
#define mqttActuator "growShed/Acuator"
#define mqttTemp1 "growShed/temp1"
int actuatorUp = 15; //GPIO de l'actuator
int actuatorDown = 2; //GPIO de l'actuator
int actuatorUpread = (!digitalRead(actuatorUp)); //read state of gpio : on or off
int actuatorDownread = (!digitalRead(actuatorDown)); //read state of gpio : on or off
//MQTT Setup End
Adafruit_BME280 bme1; // I2C
//utiliser les virgules sur les capteurs suivants
float temp1, hum1, pres1;
//gestion des délais entre chaque captations de data
unsigned long millisNow1 = 0; //for delay purposes (laisser 0)
unsigned int sendDelay1 = 2000; //delay before sending sensor info via MQTT
//gestion des délais entre chaque captations de data
unsigned long millisNow3 = 0; //for delay purposes (laisser 0)
unsigned int sendDelay3 = 7000; //delay before sending sensor info via MQTT
// The states the actuator can be in.
enum ActuatorState
{
OPENING,
OPEN,
CLOSING,
CLOSED
};
ActuatorState actuatorState;
// When opening or closing the actuator - this is the time to stop.
unsigned long actuatorEndTime = 0 ;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
// begin Wifi connect
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(2000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address (wifi RaspPi) : ");
Serial.println(WiFi.localIP());
//end Wifi connect
client.setServer(mqtt_server, 1883);
//BME
delay(5000);
unsigned status;
status = bme1.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme1.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1);
}
//Acuator
pinMode(actuatorUp, OUTPUT);
digitalWrite(actuatorDown, HIGH); //Begin with actuator switched off
pinMode(actuatorDown, OUTPUT);
digitalWrite(actuatorUp, HIGH); //Begin with actuator switched off
actuatorState = CLOSED ;
//End Actuator
}
bool getValues() {
temp1 = round(bme1.readTemperature()); //rounded values
actuatorUpread = (digitalRead(actuatorUp)); //inverted value as the relay is inverted
Serial.print("BME 1 Temperature = ");
Serial.print(temp1);
Serial.println(" °C");
Serial.print("État de l'actuator haut = ");
Serial.println(actuatorUpread);
Serial.print("État de l'actuator bas = ");
Serial.print(actuatorDownread);
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
int counter = 0;
while (!client.connected()) {
if (counter==5){
ESP.restart();
}
counter+=1;
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("growTentController")) {
Serial.println("connected");
} else {
Serial.println("failed, rc=");
Serial.println(client.state());
Serial.println(" try again in 5 seconds");
// Wait 10 seconds before retrying
delay(5000);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()){
reconnect();
}
if (millis() > millisNow1 + sendDelay1){
if (getValues()) {
client.publish(mqttTemp1, String(temp1).c_str(),true);
client.publish(mqttActuator, String(actuatorUpread).c_str(),true);
millisNow1 = millis();
}
}
//Acuator moving according to BME280 temperature sensor
switch ( actuatorState ) {
case OPEN:
// Does it need closing (use value lower than 27 so we're not constantly opening and
// closing the actuator if the temperature is floating around 27)
temp1 = bme1.readTemperature();
if ( temp1 < 26 ) {
digitalWrite(actuatorDown, LOW);
actuatorState = CLOSING;
actuatorEndTime = millis() + 7000 ;
}
break;
case CLOSED:
// Does it need opening?
temp1 = bme1.readTemperature();
if ( temp1 > 27 ) {
digitalWrite(actuatorUp, LOW);
actuatorState = OPENING;
actuatorEndTime = millis() + 7000;
}
break ;
case OPENING:
// Is it fully open?
if ( millis() >= actuatorEndTime ) {
digitalWrite(actuatorUp, HIGH);
actuatorState = OPEN ;
}
break ;
case CLOSING:
// Is it fully closed?
if ( millis() >= actuatorEndTime ) {
digitalWrite(actuatorDown, HIGH);
actuatorState = CLOSED ;
}
break ;
}
// small delay to run the loop - and do the temperature check 5 times per second.
delay(200);
}

NRF24L01 modules not connecting - hardware or coding issue?

I am trying to transmit data from an ultrasonic sensor to an LCD screen using two NRF24L01 modules connected to two separate Arduino UNOs for a distance sensor for a bike. As I am relatively new to Arduino and initially did not know how to use the NRF24L01 module, I followed a tutorial from https://www.electroniclinic.com/nrf24l01-multiple-transmitters-and-single-receiver-for-sensor-monitoring-using-arduino/
, implemented it into my code, and it is not working. I ensured on multiple occasions that the wiring for the NRF24L01 is correct for the Arduino UNOs and attempted to change radio channels, and no use. For others from the website it appeared to be working, but for me it is not. Both programs compile just fine. Is there an issue with my code or is it possible that my NRF24L01 is nonfunctional?
Here is my code:
Transmitter:
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <RF24.h>
#include <SPI.h>
RF24 radio(5, 4); // radio module, pins 5,4 for (CE, CSN)
RF24Network network(radio);
const uint16_t this_node = 01; //address of arduino
const uint16_t node00 = 00;
//setting up pins for each device
const int buzzerPin = 10;
const int aheadTrigPin = 9;
const int aheadEchoPin = 8;
//initialize variables for ultrasonic sensor and data
unsigned long data[3];
unsigned long aheadDistance;
unsigned long aheadDuration;
void setup() {
Serial.begin(9600);
SPI.begin();
radio.begin();
network.begin(69, this_node);
radio.setDataRate(RF24_2MBPS);
pinMode(buzzerPin, OUTPUT);
pinMode(aheadTrigPin, OUTPUT);
pinMode(aheadEchoPin, INPUT);
}
void loop() {
//clears the "trig" condition for ultrasonic sensor
digitalWrite(aheadTrigPin, LOW);
delayMicroseconds(2);
//releases sound waves from ultrasonic sensor for 10 microseconds
digitalWrite(aheadTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(aheadTrigPin, LOW);
//reads "echo" pin to return sound wave travel time in microseconds
aheadDuration = pulseIn(aheadEchoPin, HIGH);
//calculating distance of ultrasonic sensor, in inches (distance = velocity * time)
aheadDistance = (aheadDuration * 0.034 / 2) * 2.54;
Serial.println(aheadDistance);
//activates buzzer to alert rider if danger values are met
if(aheadDistance <= 100) {
tone(buzzerPin, 255);
}
else {
noTone(buzzerPin);
}
//send data to main component
network.update();
data[0] = aheadDistance;
RF24NetworkHeader header(node00);
bool ok = network.write(header, &data, sizeof(data)); // send data
Serial.println("sent to NRF24 server");
delay(200);
}
The reason I am sending an array as the data is because I am currently working on having another transmitter to send data to a single receiver.
Receiver:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include <RF24.h>
#include <SPI.h>
//sets up timer for the LCD display to constantly run
//sets up OLED screen
LiquidCrystal_I2C display(0x27,20,4);
RF24 radio (10, 9); // CE, CSN, defines new RF24 radio
RF24Network network(radio);
const uint64_t this_node = 00;
//data variables that will be collected from other arduinos
unsigned long data[3];
unsigned long aheadDistance;
unsigned long groundDistance;
unsigned long backAheadDistance;
void setup()
{
Serial.begin(9600);
display.init(); // intialize lcd
display.backlight(); //open backlight of lcd
display.clear();
//initialize starting screen
display.setCursor(0, 0);
//set up radio module (reciever)
SPI.begin();
radio.begin();
network.begin(69, this_node);
radio.setDataRate(RF24_2MBPS);
}
//method to update screen with current data values
void displayDistance() {
display.setCursor(0, 0);
display.print("Bike Sensor");
display.setCursor(0, 1);
display.print("Front: ");
display.print(aheadDistance);
display.print(" in");
display.setCursor(0, 2);
display.print("Ground: ");
display.print(groundDistance);
display.print(" in");
display.setCursor(0, 3);
display.print("Back: ");
display.print(backAheadDistance);
display.print(" in");
display.display();
}
void connectionLost() // activates when main component is unable to connect to others
{
display.clear();
display.setCursor(0, 1);
display.print("Connection");
display.setCursor(0, 2);
display.print("Lost");
display.display();
}
void loop()
{
network.update();
while(network.available()) {
RF24NetworkHeader header;
Serial.println("connection found");
network.read(header, &data, sizeof(data));
Serial.println("data recieved");
if(header.from_node == 01) {
backAheadDistance = data[0];
}
if(header.from_node == 02) {
aheadDistance = data[1];
groundDistance = data[2];
}
displayDistance();
delay(100);
}
connectionLost();
Serial.println("no connection found");
delay(200);
}
//updates OLED screen and actually displays it on the module
It’s possible that your nRF24L01 is dead. I’ve had many. Some fail. I recommend those from EByte especially ML01DP5 and ML01SP4. These are good.
Also there’s a little known hardware issue: never let the FIFO buffers remain full for over 4ms. Of course it’s hard to know. So simply flush them often as a precaution. I have fixed my code this way and now it’s robust.
Good luck!
Malcolm

Read Received via SPI data with arduino mega

There
I am working on one of the project, in that i am trying to read 16-bit data from ADS8681. Currently i get the correct waveform of SCk,MISO,MOSI and SCK on oscilloscope. But I am not able to print data on alphanumeric LCD. With readRegister(long thisRegister, long bytesToRead) also unable to print data. Can anyone suggest me how i print received data?
References which i am using:
1] https://www.arduino.cc/en/Tutorial/LibraryExamples/BarometricPressureSensor#code
Source Code :
#include <SPI.h>
#include <LiquidCrystal.h>
#define SS 53 // CS/CONVST
LiquidCrystal lcd (38,37,36,35,34,33);
void setup()
{
lcd.begin(16,4);
SPI.begin();
digitalWrite(SS,OUTPUT);
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x14, 0x0001); //Write data range selection
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x10, 0x4100); //Write Data_OUT_CTRL
lcd.setCursor(0,0);
lcd.print ("Data");
}
void loop()
{
digitalWrite(SS, HIGH);
delay(10);
digitalWrite(SS,LOW);
writeRegister(0x14, 0x0001); //Write data range selection
delay(100);
}
void readRegister(long thisRegister, long bytesToRead)
{
}
void writeRegister(word thisRegister, word thisValue)
{
}

sim800 cme erro2 when i pass at commands through code arduino esp32

I am trying to push sensor data to aws cloud database through sim800 gsm module.
I am able to do it by entering the at commands manually via serial monitor but when i incorporate the at commands in the code to automate the process so I don't have to enter commands manually I am getting cme error 3.
Please guide how to solve or identify the problem.
Code
#include <Arduino.h> //Arduino librabry...optional
#include <SPI.h>//SPI communication library
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>//library with GSM commands
HardwareSerial SerialGSM(1);
TinyGsm modemGSM(SerialGSM);
const int BAUD_RATE = 115200;
//Pin definition for RX and TX GPIO2 and GPIO4 are the pins in ESP32 Wroom
const int RX_PIN = 4, TX_PIN = 2;
//const char *APN = "";
//const char *USER = "";
//const char *PASSWORD = "";
//Initialize the gsm
void setupGSM() {
SerialGSM.begin(BAUD_RATE, SERIAL_8N1, RX_PIN, TX_PIN, false);
delay(3000);
Serial.println(modemGSM.getModemInfo());
delay(1000);
}
String sendAT(String command)
{
String response = "";
SerialGSM.println(command);
// aguardamos até que haja resposta do SIM800L
while(!SerialGSM.available());
response = SerialGSM.readString();
Serial.println(response);
return response;
}
void setup() {
Serial.begin(BAUD_RATE);
Serial.println("Starting the device");
Serial.println("Initializing GSM device");
setupGSM();
delay(5000);
SerialGSM.println("AT+CMEE=1");
}
unsigned long start;
void loop() {
//SerialGSM.println("AT+CMEE?");
sendAT("AT+CMEE?");
// SerialGSM.write("AT+COPS?\r\n");
delay(100);
//SerialGSM.println("AT+CSQ");
sendAT("AT+CSQ");
// if(SerialGSM.available()){
// Serial.write(SerialGSM.read());
// }
delay(100);
// SerialGSM.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
sendAT("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(1000);
// SerialGSM.println("AT+SAPBR=3,1,\"APN\",\"www\"");
sendAT("AT+SAPBR=3,1,\"APN\",\"www\"");
delay(1000);
//SerialGSM.println("AT+SAPBR=1,1");
sendAT("AT+SAPBR=1,1");
delay(2000);
//SerialGSM.println("AT+HTTPINIT");
sendAT("AT+HTTPINIT");
delay(1000);
//SerialGSM.println("AT+HTTPPARA=\"CID\",1");
sendAT("AT+HTTPPARA=\"CID\",1");
delay(1000);
//SerialGSM.println("AT+HTTPPARA=\"URL\",\"http://ec2-18-217-119-145.us-east-2.compute.amazonaws.com:8000/?light=0&var=1&var2=2&var3=3&var4=4\"");
sendAT("AT+HTTPPARA=\"URL\",\"http://ec2-18-217-119-145.us-east-2.compute.amazonaws.com:8000/?light=0&var=1&var2=2&var3=3&var4=4\"");
delay(1000);
//SerialGSM.println("AT+HTTPACTION=0");
sendAT("AT+HTTPACTION=0");
delay(2000);
//start=millis();
//
// while(millis()-start<1000)
// {
// if(SerialGSM.available()){
// Serial.write(SerialGSM.read());
// }
// }
}

Arduino restarts after serial input from Raspberry Pi

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