LinkSprite Camera with TTL Serial

OK, thanks for that, fixed the wiring, now I ran @kennethlimcp example code and was able to get this on command line:

Karinas-MacBook-Pro:~ karinamendez$ spark serial monitor
Opening serial monitor for com port: “/dev/cu.usbmodem1421”

but it’s been there for a while and nothing else comes out ):

In my post following Kenneth's code, I gave the clue :wink:
Send in some byte via USB to get the code running, or add a timeout into the while().

Tried that earlier too ):

while (!Serial.available()){
delay(1000);
return;
}

a little rough just to see if it would work but no ): code still doesn’t start running

and btw, the proyect I’m working on is that since the will be connected to the camera and a door solenoid in a wall outside a house, so when someone comes visit, the core send me a pic of the person via wifi and then I tell the core to open the door, how will I be able to send the data over wifi if I can’t make work manually and over the usb serial? ):

Download Coolterm for mac

Once you connect with Serial, you will need to hit any button for the code to run

What you are doing there is not a timeout - as suggested. This way you only skip all the rest of the setup() code after one second.

Try this instead

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

  Serial.begin(921600);
  while(!Serial.available() && (millis() - ms < 10000)); // wait for keypress for max. 10sec

  ...

}

Finally! Got this on the command line:

also replaced the port for a smaller one, I was reading the code and says something about an SD, is that the only way I can transfer this data to see the actual picture?

Thank you guys so much for your help!

You can use a lot of different routes to transfer, but an SD might be good to have for buffering at least.

Once you got the pic read and buffered, you can start the WiFi and transfer the data via TCP or just use wire.


For your intended use, time might become an issue, depending on the img size. 640x400 does take a while to transfer from cam to Core via serial plus publishing it whatever way :wink:

1 Like

I used this python code:

import binascii

f = open ("outputData5.txt","r")
nf = open("binaryData5.jpg","wb")

//Read whole file into data
while 1:
    c = f.readline()
    d = c.strip()
    if not c:
        break
    nf.write(binascii.a2b_hex(d))


//Close the file
f.close()
nf.close()

but then gave me this error:

"Traceback (most recent call last):
  File "convert.py", line 12, in <module>
    nf.write(binascii.a2b_hex(d))
TypeError: Odd-length string"

 please format your code by wrapping it an blocks of

here your code

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)?.