Arduino and Raspberry Together


What does it mean by newline unexpected , i googled but found mutiple reasons of why .I feel that i am close



Sorry, that was my fault, I put the wrong link in the wget. Use the commands commands below and then try running it.



Looks like something happened , But still nothing on my dashboard , should i update my token ???


Hey Adam , am I close to getting these two to connect , what else would you recommend me to try ? I am getting close to my project and would love to implement my dht22 to read my humidity real time .


Hey man. I was just looking back at some of my old threads to pick up a project I left off with a few months ago. I was having a lot of the same problems that you seem to be having. I think the problem is that you have to cd into the directory where you have the file stored before you can run it. For me it was

cd Downloads/Cayenne-Arduino-Library-master/extras/scripts

If you’re not sure of the complete file path you can
cd Downloads first
then type ls
select the appropriate folder
type ls
and continue selecting folders and typing ls until you find the one where your script is located. Once you have changed directories you should be able to run your script with no problem.

In reading your window from above it looks like you should type

cd Downloads/Cayenne-Arduino-Library/master/extras/scripts and hit enter

then type sudo

I am a newb myselft, but this looks oddly similar to something I went through


Sorry, didn’t see your post. You need to give it execute permissions again with chmod +x


That’s actually just for executing a compiled program that’s in the current directory. “~” is for home. If you use cd ~ it will take you to the home directory.




Thank you Both, it seems that I’m on to something, but the Arduino will not connect. Am I using the correct USB port?


@rsiegel any suggestions? I’m not super familiar with using this script.


Hey guys, my apologies, I offered my help and then lost track of this thread over the weekend.

There might be a simpler way, but what always works for me in this scenario is to verify the USB port from the Arduino IDE. If you don’t already have it installed on your Pi, they do have a Linux ARM version that you can download and install here.

Once that is installed, launch it, and check the Tools menu, Port section.

With your Pi connected it will show the correct USB port where I’ve highlighted in that image. So in my case: /dev/ttyACM0

Open the script in the editor of your choice, and edit the COMM_PORT_LINUX= line with the correct USB port. In my example, I’ve changed it to COMM_PORT_LINUX=/dev/ttyACM0

Save your changes and run the script again. So long as the Arduino has already been flashed with the correct sketch file (with AuthenticationToken replaced with the token when adding the device from your Cayenne dashboard), then it should connect OK.

If it does not, show me the output from the attempted connection again and I’ll continue to help troubleshoot.


Hi all,

Have been exploring the arduino - serial usb - raspberry over the last weeks.

Pushing data towards Cayenne dashboards works fine, however, communication towards the arduino board (eg to switch a relay) fails. Can something like a firewall be blocking? How to debug (cannot debug via serial.print)

This is my code (couldnt get the formatting right)

#include <DHT.h>
#include <Arduino.h>
#include <cozir.h>
#include <SoftwareSerial.h>
#include <CayenneMQTTSerial.h>

// Constants for Arduino relay board
#define RelayON 0
#define RelayOFF 1

//Constants for DHT sensors
#define ExternalDHTPin 2     // what pin is connected to the outside temperature room (OUT)
#define InternalDHTPin 3     // what pin is connected to the inside temperature room (IN)
#define DHTType DHT22   // DHT 22  (AM2302)

//Constants for Arduino relay board switches
#define PinRelayHeater 7
#define PinRelayHeaterVentilator 6
#define PinRelayHumidity 8
#define PinRelayEggTurner 9
#define PinRelaySupplementaryVentilator 12

#define PinPot 2    // select the input pin for the potentiometer
float PotValue;       // variable to store the value coming from the analog resistor

DHT ExternalDHT(ExternalDHTPin, DHTType); //// Initialize DHT sensor EXT for normal 16mhz Arduino
DHT InternalDHT(InternalDHTPin, DHTType); //// Initialize DHT sensor INT for normal 16mhz Arduino

int chk;
float InternalHumidity;  //Stores internal humidity value
float InternalTemperature; //Stores internal temperature value
float ExternalHumidity;  //Stores external humidity value
float ExternalTemperature; //Stores external temperature value

float CozirTemperature;
float CozirHumidity;
int CozirCO2;

// Temperature goals
float ExternalTemperatureSet;  //Setting external temperature value
float InternalTemperatureStart;  //Stores internal humidity value
float InternalTemperatureSet;  //Stores internal humidity value
float InternalHumiditySet; //Stores internal temperature value
int   InternalCO2Set; //Max C02 before ventilation

//Specifiy window sizes for sensors
unsigned long LastTemperatureCheck;
unsigned long LastTemperatureSetCheck;
unsigned long LastEggTurnCheck;
unsigned long LastSuppVentCheck;

//Initialize COZIR
SoftwareSerial nss(10,11);
COZIR czr(nss);

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

void setup()
  //Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
  Cayenne.begin(username, password, clientID);
  //Initialize Pins so relays are inactive at reset
  digitalWrite(PinRelayHeater, RelayOFF);
  digitalWrite(PinRelayHeaterVentilator, RelayOFF);
  digitalWrite(PinRelayHumidity, RelayOFF);
  digitalWrite(PinRelayEggTurner, RelayOFF);
  digitalWrite(PinRelaySupplementaryVentilator, RelayOFF);
  digitalWrite(13, RelayOFF);

  //---( THEN set pins as outputs )----
  pinMode(PinRelayHeater, OUTPUT);    
  pinMode(PinRelayHeaterVentilator, OUTPUT);    
  pinMode(PinRelayHumidity, OUTPUT);  
  pinMode(PinRelayEggTurner, OUTPUT);  
  pinMode(PinRelaySupplementaryVentilator, OUTPUT); 

  //----(on board led) ----

  LastTemperatureCheck = millis();
  LastTemperatureSetCheck = millis();
  LastEggTurnCheck = millis();
  LastSuppVentCheck = millis();
  //start serial signaling
  //turn DHT sensors on
  if (true) {
    // Incubation goals
    ExternalTemperatureSet = 38.8;  //Setting external temperature value
    InternalTemperatureStart = 37.8;  //Stores internal humidity value
    InternalHumiditySet = 65.0; //Stores internal temperature value  
  } else {
    // Hatching goals
    ExternalTemperatureSet = 38.6;  //Setting external temperature value
    InternalTemperatureStart = 37.6;  //Stores internal humidity value
    InternalHumiditySet = 80.0; //Stores internal temperature value

void loop() {
  if (millis() - LastTemperatureCheck > 2000) { //check each 2 seconds
    LastTemperatureCheck = millis();
    //Blink on/off every 2 seconds in the temperature cycle
    if (digitalRead(LED_BUILTIN) == HIGH) {
      digitalWrite(LED_BUILTIN, LOW);
    else {
      digitalWrite(LED_BUILTIN, HIGH);

    //Read data and store it to variables hum and temp
    ExternalHumidity = ExternalDHT.readHumidity();
    ExternalTemperature= ExternalDHT.readTemperature();
    InternalHumidity = InternalDHT.readHumidity();
    InternalTemperature= InternalDHT.readTemperature();
    CozirTemperature = czr.Celsius();
    CozirHumidity = czr.Humidity();
    CozirCO2 = czr.CO2();
    PotValue = (1024.0 - analogRead(PinPot))/512.0; 
    InternalTemperatureSet = InternalTemperatureStart*PotValue;

    if (isnan(ExternalHumidity) || isnan(ExternalTemperature) || isnan(InternalHumidity) || isnan(InternalTemperature)) {

    //Serial.println("Humidity: " + String(ExternalHumidity) + " - " + String(InternalHumidity));
    //Serial.println("Temperature: " + String(ExternalTemperature) + " - " + String(InternalTemperature));
    * turn the temperature output pin on/off based on pid output
    if(ExternalTemperature > ExternalTemperatureSet) 
        digitalWrite(PinRelayHeater, RelayOFF);
        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    else if (ExternalTemperature < ExternalTemperatureSet - 0.2)
        //board led
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    * turn the humidity output pin on/off based on pid output   
    if (InternalHumidity > InternalHumiditySet)
      digitalWrite(PinRelayHumidity, RelayOFF);
    else if (InternalHumidity < InternalHumiditySet)
      digitalWrite(PinRelayHumidity, RelayON);
  * turn the exteral temperature setpoint 
  if (millis()- LastTemperatureSetCheck > 900000) { //time to shift the temperature set 0.2 down or 0.1 up each 1.5 minutes
    // code for temperature set checking
    LastTemperatureSetCheck =  millis();
    if(InternalTemperature > InternalTemperatureSet) {
         ExternalTemperatureSet -=0.2;
    else if (InternalTemperature < InternalTemperatureSet) {
         ExternalTemperatureSet +=0.1;

  if (millis() - LastEggTurnCheck > 21600000) { //turn the eggs each 360 min or 6 hours
    LastEggTurnCheck = millis();
    digitalWrite(PinRelayEggTurner, RelayON);
    digitalWrite(PinRelayEggTurner, RelayOFF);
    //Publish the eggs were turned

  if (millis()- LastSuppVentCheck > 5000) {
    // code for ventilation checking
    LastSuppVentCheck =  millis();
    if (CozirCO2 > InternalCO2Set) {
         digitalWrite(PinRelaySupplementaryVentilator, RelayON);
    else if ((CozirCO2 < InternalCO2Set) || isnan(CozirCO2)) {
         digitalWrite(PinRelaySupplementaryVentilator, RelayOFF);

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // code for Cayenne logging
  Cayenne.celsiusWrite(1, InternalTemperature);
  Cayenne.celsiusWrite(2, ExternalTemperature);
  Cayenne.celsiusWrite(3, InternalTemperatureSet);
  Cayenne.celsiusWrite(4, ExternalTemperatureSet);
  Cayenne.celsiusWrite(5, CozirTemperature);
  Cayenne.virtualWrite(6, InternalHumidity, "rel_hum", "p");
  Cayenne.virtualWrite(7, InternalHumiditySet, "rel_hum", "p");
  Cayenne.virtualWrite(8, CozirHumidity, "rel_hum", "p");
  Cayenne.virtualWrite(9, CozirCO2, "co2", "ppm"); 

// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
// This function is called when data is sent from Cayenne.
  // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  if (getValue.asInt() == 0) {
    digitalWrite(8, HIGH);
  else {
    digitalWrite(8, LOW);


@shramik_salgaonkar or @adam might be able to help…that’s a lot of code! :wink:



if you can send data to cayenne, then there should not be any problem receiving data and turning on the relay on/off.
one error is:

pinMode(8, OUTPUT);.


Ok wil try tonight


also it looks like you have defined pin 8 as #define PinRelayHumidity 8. so change :

// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(PinRelayHumidity , HIGH);
else {
digitalWrite(PinRelayHumidity ,LOW);


Is it a problem to define both as output and using it via cayenne at same time?


why you want to define both. define a single pin and use that in both your control


Tried the suggestion but still fails. Tried same sketch with Arduino YUN then works


Hello @adam @akers.kenneth66 @rsiegel,
It’s been a log time ago I did not post anything. Now I am back. How about to connect arduino to raspberry pi, since the agent version is new?

Thank you.