Arduino and Raspberry Together


#61

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

image


#62

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

rm cayenne-ser.sh
wget https://raw.githubusercontent.com/myDevicesIoT/Cayenne-Arduino-Library/master/extras/scripts/cayenne-ser.sh

#63

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


#64

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 .


#65

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 cayenne-ser.sh

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


#67

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


#68

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.


#69

Aplogies


#70

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


#71

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


#72

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 cayenne-ser.sh 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.


#73

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


//Variables
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

//Cozir 
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()
{
  //Serial.begin(9600);
  //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) ----
  pinMode(LED_BUILTIN, OUTPUT);

  LastTemperatureCheck = millis();
  LastTemperatureSetCheck = millis();
  LastEggTurnCheck = millis();
  LastSuppVentCheck = millis();
        
  //start serial signaling
  //Serial.begin(9600);
  
  //turn DHT sensors on
  ExternalDHT.begin();
  InternalDHT.begin();
  
  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() {
  Cayenne.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)) {
      return;
    }

    //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)
      {
        digitalWrite(PinRelayHeater,RelayON);
        //board led
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      }
     //HUMIDITY
    
    /************************************************************
    * turn the humidity output pin on/off based on pid output   
    *************************************************************/
    if (InternalHumidity > InternalHumiditySet)
    {
      digitalWrite(PinRelayHumidity, RelayOFF);
    }
    else if (InternalHumidity < InternalHumiditySet)
    {
      digitalWrite(PinRelayHumidity, RelayON);
    }
  }
  
  //TEMPERATURE ADJUSTMENT 
  /************************************************
  * 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;
    }
  }

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

  //VENTILATION ADJUSTMENT 
  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.
CAYENNE_OUT_DEFAULT()
{
  // 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.
CAYENNE_IN(11)
{
  // 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);
  }
}

#74

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

~benny


#75

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


#76

Ok wil try tonight


#77

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

CAYENNE_IN(11)
{
// 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);
}
}


#78

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


#79

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


#80

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