Go Down

Topic: Serial communicatoin problem using Onion and Arduino (Read 292 times) previous topic - next topic

Actioncow

Aug 09, 2018, 02:21 pm Last Edit: Aug 09, 2018, 02:22 pm by Actioncow
Dears,
After several weeks of struggling I would like to seek help from the talented people in this forum. I have an Onion Omega 2+ microcontroller on an Arduino Dock 2 and an ethernet shield. I am using python and pyserial to interface between the arduino board and the OS on the Onion, but the communication is very flawed. So flawed to be honest, that only one or two characters are going through and I can only see some indicatoin of messages while using the screen command to troubleshoot. I tried to write a message to the Onions /dev/ttyS1 with echo, nothing came up while I was screening it. I also put serial message into a loop on Arduino, and I got about 5 characters on the screen of the Onion. Below is the code for the Arduino Board:
Code: [Select]

#include <cactus_io_AM2302.h>;
    #include <stdio.h>
   
    // defines pins numbers
    const int trigPin = 9;
    const int echoPin = 10;
   
   #define AM2302_PIN 7
   AM2302 dht(AM2302_PIN);

   
    // defines variables
    long duration;
    int distance;
    int del;
   
    void setup() {
    Serial.begin(9600); // Starts the serial communication
    del=1;
    }







// void called 1
    void read_ultrasonic_sensor(){
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    delayMicroseconds(1000);
      // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    //String D="read_ultrasonic_sensor:"+String(distance);
    Serial.println(distance + "*");
    //Serial.println(distance);
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
   
    }

    void read_temperature_sensor(){
     dht.begin();
     delay(2500);
     dht.readHumidity();
     dht.readTemperature();
     Serial.print(dht.humidity);
     Serial.print(" %");
     Serial.print(dht.temperature_C);
     Serial.println(" C*");
    }



   
    void loop() {
      if (Serial.available() >0) {
      String voidname;
      del=del+1;
      if(del>10){
      voidname= Serial.readString();  //gets one byte from serial buffer
      del=1;
      }
      Serial.print("#");

      if (voidname=="read_ultrasonic_sensor"){
        read_ultrasonic_sensor();
        }
      if (voidname=="read_temperature_sensor"){
        read_temperature_sensor();
        }
      }
    }


And the output of screen /dev/ttyS1 9600 is the following after 2 minutes:

###########

My expectation would have been a rolling mass of "#" signals.

Thank you very much for your help in advance!
Regards,
Cow

PaulS

Quote
I have an Onion Omega 2+ microcontroller on an Arduino Dock 2
What the heck is an "Arduino Dock 2"?

Quote
I am using python and pyserial to interface between the arduino board and the OS on the Onion
What the hell does "to interface" mean? How are you using python on the Arduino?

Most of your comments are either obvious to even a clueless newbie or wrong. Get rid of the useless ones, and fix the ones that are wrong (which probably will make you investigate the functions and figure out what is wrong with your code).

Code: [Select]
      String voidname;
What do you call digitalRead()? An int, because it returns an int?

They are FUNCTIONS, not VOIDS.
The art of getting good answers lies in asking good questions.

Actioncow

Hi,
Thanks for taking the time to compile your reply, regardless of the tone it has. To answer your questions which are apparently not obvious to anyone other than me, the arduino dock is an Arduino Uno R3 with a slot for an Onion Omega microcontroller. By saying "to interface" I meant "communicate between the Arduino Uno R3 board and the Onion Omega microcontroller". Python is being used on the operating system of the Onion Omega, which runs LEDE.
I am sorry for naming my string variable something that has "void" in its name. I am using voids because in my understanding a function has a return value, however what I am doing produces no returns in the code.

Thank you for your answer again!
Regards,
Cow

PaulS

Quote
I am using voids because in my understanding a function has a return value
A function has a return TYPE. That type can be void.

A function does not have to return a VALUE.

Quote
the arduino dock is an Arduino Uno R3 with a slot for an Onion Omega microcontroller.
I still don't understand, and none of your links work.
The art of getting good answers lies in asking good questions.

Actioncow

Dear PaulS,
I have not linked anything in my previous posts, that might be why you are unable to open any links. The issue is that when I send a value over serial, the receiving end sees incomplete- or no data whatsoever. I am not sure why.
I have also highlighted that the microcontroller could talk to itself using serial, and that is also seemingly misbehaving, as in not being able to monitor its own serial port, however it is able to send data over to the Arduino board without any issue. So currently I am not sure if improper usage of serial communication is the issue or firmware issue on the Arduino board, OR some issue with the microcontroller.

Regards,
Cow

ieee488

So currently I am not sure if improper usage of serial communication is the issue or firmware issue on the Arduino board, OR some issue with the microcontroller.

Don't know.

I doubt many people have the Onion or the Arduino Dock 2.

You have the hardware, so you need to figure it out.

.

Actioncow

Dears,
Thanks for the input, I'll try to figure it out then.
Regards,
Cow

ieee488

If it was me, I would start with a very simple sketch for the Arduino.
Also, avoid using String in Arduino.
Use, character byte arrays., e.g. char bytes[20] 

PaulS

Quote
I have not linked anything in my previous posts
That was a hint.
The art of getting good answers lies in asking good questions.

Robin2

This Python - Arduino demo may be of interest.

Also see Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Actioncow

Dear Robin2,
Thank you very much fo rth ecomprehensive guide on how to receive data on Arduino. However sadly enough I have the issue the other way around. My micro controller has issues reading the arduinos replies from serial. I will check and most probably implement the codes you have posted, so I thank you very much for those.
Regards,
Cow

Robin2

Thank you very much fo rth ecomprehensive guide on how to receive data on Arduino. However sadly enough I have the issue the other way around. My micro controller has issues reading the arduinos replies from serial.
Maybe I misunderstood but I got the impression that your "micro controller" is using Python - hence my link to my Python demo.

Is the micro-controller also using Linux?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Actioncow

Dear Robin2,
Apologies, totally missed the files at the bottom of your message in the python tutorial. Checking them now. As for the misunderstanding, I call the Onion Omega 2+ as "microcontroller" and the arduino dock as the "Arduino". Apologies.
Regards,
Cow

Actioncow

Dear Robin2,
I have checked your code for python and Arduino at the port you linked, downloaded the codes, and uploaded them to the respective devices without modification, although I have no leds nor servos. I ran the code and the python on the Omega was just waiting for the ready signal from arduino. I reuploaded the arduino code to reboot it, and it wrote its ready string to serial, but still nothing happened.
I am getting more desperate thinking there is some hardware issue with the Omega.
Regards,
Cow

Robin2

Post the two programs that YOU have uploaded to your two devices. And tell us in as much detail as possible what heppens when you run the programs.

You did not answer my earlier question about whether your micro controller uses Linux.

A link to a webpage that provdes background info about the micro controller would be a big help.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up