LinkSprite Camera with TTL Serial

Hello guys, got some extra time for my project and now I can’t seam to make this code work:


import binascii
import serial

nf = open("new1.jpg","wb")
ser = serial.Serial('/dev/cu.usbmodem1411', 9600) # Establish the connection on a specific port

while 1:
    c = ser.read() # Read the newest output from the Arduino
    if not c:
        break
   	print(c)
    nf.write(binascii.a2b_hex(c))


//Close the file
nf.close()

it’s supposed to get the info from particle serial monitor and convert the image by itself, but nothing happens when I run this code, but if I type “particle serial monitor” on the command line, the image starts to appear. Any ideas? @kennethlimcp @ScruffR

I’d expect some 0x00 bytes to be part of your jpg, but you break out of your loop on first occurance of 0x00.

For debugging it might be useful to printout every character read.

It gives me this error:


Traceback (most recent call last):
File “connection.py”, line 11, in
nf.write(binascii.a2b_hex©)
TypeError: Odd-length string

Do you actually know what binascii.a2b_hex() does, or are you just copy-pasting?

The error message is quite straight forward, when you actually understand the use of the functions you are using :wink:

Hello @kennethlimcp my spark has been flashing green, not connecting to ANY network which never happened before, already tried installing the firmware again and nothing works, any ideas?

See this: [Spark core] Common issues

I tried and somehow when I request the serial monitor before I flash the code from the cloud it works fine, but once I flash the code and then ask for the particle serial monitor gives me the error “! serial: No devices available via serial” while the core is still connected…

You might have to repost some of your current code since the code above will most likely not reflect the current state after this long.

It’s not the code, I also tried the example ones like “blink an led” and the same situation happens.

But here it is:

 #define mySerial Serial1

SYSTEM_MODE(MANUAL);

int onOff = 1;
byte ZERO = 0x00;
byte incomingbyte;
long int j=0,k=0,count=0,i=0x0000;
uint8_t MH,ML;
boolean EndFlag=0;

int red = D4; // choose the pin for the LED
int green = D3;   // choose the input pin (for a pushbutton)
int button = A0;
int val = 0;     // variable for reading the pin status
int buz = D5;
int sol = D2;

int val1 = 0; //variable for push-button status scenario 1
int val2 = 0; //variable for push-button status scenario 2
int state = 1; //variable for on/off state
 
TCPClient client;

void SendResetCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x26);
    mySerial.write(ZERO);
}

void SetImageSizeCmd(byte Size)
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x54);
    mySerial.write(0x01);
    mySerial.write(Size);
}

void SetBaudRateCmd(byte baudrate)
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x24);
    mySerial.write(0x03);
    mySerial.write(0x01);
    mySerial.write(baudrate);
}

void SendTakePhotoCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x36);
    mySerial.write(0x01);
    mySerial.write(ZERO);
}

void SendReadDataCmd()
{
    MH=i/0x100;
    ML=i%0x100;
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x32);
    mySerial.write(0x0c);
    mySerial.write(ZERO);
    mySerial.write(0x0a);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(MH);
    mySerial.write(ML);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(ZERO);
    mySerial.write(0x20);
    mySerial.write(ZERO);
    mySerial.write(0x0a);
    i+=0x20;
}

void StopTakePhotoCmd()
{
    mySerial.write(0x56);
    mySerial.write(ZERO);
    mySerial.write(0x36);
    mySerial.write(0x01);
    mySerial.write(0x03);
}

int ledToggle(String command) {
    if (command=="on") {
        //digitalWrite(led1,HIGH);
        onOff = 1;
        return 1;
    }
    else if (command=="off") {
        //digitalWrite(led1,LOW);
        onOff = 0;
        return 0;
    }
    else {
        return -1;
    }
}

void setup()
{
    uint32_t ms = millis();

    Serial.begin(9600);
    
    pinMode(buz, OUTPUT);
    pinMode(red, OUTPUT);  // declare LED as output
    pinMode(green, OUTPUT);
    pinMode(button, INPUT);    // declare pushbutton as input
    pinMode(sol, OUTPUT);
    
    while(!Serial.available() && (millis() - ms < 10000));
    
    //Serial.println("initialization done.");
    //Serial.println("please wait ....");

    mySerial.begin(38400);
    delay(100);
    SendResetCmd();
    delay(2000);
    
    Spark.function("led",ledToggle);
}

void loop()
{
    digitalWrite(sol, HIGH);
    SendResetCmd();
    byte a[32];
    int ii;
    //TIMBRE
    
    val = digitalRead(button);  // read input value
    digitalWrite(D7, HIGH);
    val = HIGH;
    
    if (val == HIGH) {         // check if the input is HIGH (button pressed)
        digitalWrite(green, LOW);  // turn LED OFF
        digitalWrite(red, HIGH);
        analogWrite(buz, 176400);
        delay(100);
        analogWrite(buz, 0);
        
        //start taking picture
        //Serial.println("gonna take pic");
        SendResetCmd();
        delay(2000);                            //Wait 2-3 second to send take picture command
        SendTakePhotoCmd();
        delay(1000);
        onOff = 0;
    
        while(mySerial.available()>0)
        {
            incomingbyte=mySerial.read();
        }
    
        while(!EndFlag)
        {
            j=0;
            k=0;
            count=0;
            //mySerial.flush();
            SendReadDataCmd();
            delay(20);
            while(mySerial.available()>0)
            {
                incomingbyte=mySerial.read();
                k++;
                delay(1); //250 for regular
                if((k>5)&&(j<32)&&(!EndFlag))
                {
                    a[j]=incomingbyte;
                    if((a[j-1]==0xFF)&&(a[j]==0xD9))     //tell if the picture is finished
                    {
                        EndFlag=1;
                    }
                    j++;
                    count++;
                }
            }
    
            for(j=0;j<count;j++)
            {
                digitalWrite(red, HIGH);  // turn LED ON
                digitalWrite(D7, HIGH);
                if(a[j]<0x10)  Serial.print("0");
                Serial.print(a[j],HEX);           // observe the image through serial port
                //Serial.print(" ");
            }
        }
        Serial.print("+");
        digitalWrite(red, HIGH);  // turn LED ON
        digitalWrite(green, HIGH);  // turn LED ON
        
        //Serial.println("Finished writing data to file");
    
    }else{
        digitalWrite(green, LOW);  // turn LED OFF
        digitalWrite(red, HIGH);
    }   
}

When you say you triec “blink an led”, why would you expect to see the Core via particle serial monitor?

There is no Serial.begin() in that sample, so you won’t find a COM port open.

Just try to flash this

void setup()
{
  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);
  Serial.begin(115200);
}

void loop()
{
  Serial.println(millis());
  digitalWrite(D7, !digitalRead(D7));
  delay(250);
}

And then check if you see serial output (and the blinking LED).


BTW: What OS are you on?

Wasn’t expecting to see data, just was expecting the serial port to be recognized.

OS X Yosemite 10.10.3

You won’t see the port if it’s not opened by the device via Serial.begin().
(At least true for the Core FW 0.3.4, the Photon does open the USB serial by default)

I’ve seen some other things in your code

    //pinMode(button, INPUT);    // declare pushbutton as input
    pinMode(button, INPUT_PULLDOWN);  // required unless you have an 
                                      // external pull down resistor 
                                      // otherwise your pin might float 
                                      // giving you wrong readings

You are using SYSTEM_MODE(MANUAL) but also declaring a Spark.function(), why?
When using a TCPClient in MANUAL, you’d need to call WiFi.connect() to have it work.
How do you flash your code from the cloud when your Core is in MANUAL mode (without connection to the cloud anywhere)?.

Sorry, forgot to comment the Spark.function(), I don’t use it.

I just flash the core via usb with the code in the particle build page, then I ask for the serial monitor’s data with this code:

I just need the serial port problem fixed, everything else works fine.

It might be /dev/tty. if i remember correctly. Can you try using a terminal software to see if the device is listed there?

Still having the same problem, when I ask for “particle serial list” says “no devices available via serial”

Have you tried my short test sketch from above?

Yes, and it gave me this error:

so I replaced it for a “HIGH”, asked for the serial monitor and still said “no device available via serial”

That was a typo, which has been corrected already digitalWrite(D7, !digitalRead(D7));

If this sketch doesn’t work, I’d expect you’ve got a problem with your USB port, USB cable or computer and not the Core.

Is there any other way I could see the serial monitor than the command line?

Maybe @kennethlimcp can help here since I have no clue about Mac.
But I’ve heard about CoolTerm, if this helps.