Arduino uno serial connection with esp8266


#1

For this type of connection does it need always open with the cmd run the script?
if i want use power bank to run my arduino uno does it possible because when i close the cmd the cayenne dashboard will show offline for my device


#2

Could you be more clear and paste a piece of your code ? Diagrams ?


#3
/*
This example shows how to connect to Cayenne using a Serial USB connection and send/receive sample data.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

This requires the use of the Serial USB connection so you cannot use the Serial device for
printing messages. If you need to print you can use SoftwareSerial and connect another device
to read messages via the SoftwareSerial pins.

In order for this to work you must run the connection script on the machine the Arduino is connected to.
The scripts are located under the extras\scripts folder in the main library folder. This redirects the traffic
from the Arduino to the Cayenne server.

Steps:
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload this sketch.
3. Launch the connection script as described below for Windows or Linux/OSX.

Windows:
  1. Open the Windows command line (cmd.exe)
  2. Navigate to the scripts folder by typing "cd [path]", e.g.  "cd C:\Users\[YourUserName]\Documents\Arduino\libraries\CayenneMQTT\extras\scripts"
  3. Run the script by typing "cayenne-ser.bat -c COM4" (where COM4 is the Arduino serial port) and hitting Enter

Linux and OSX:
    ./cayenne-ser.sh (may need to run with sudo)
    
You can specify port, baud rate, and server endpoint like this:
    ./cayenne-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>

    For instance :
      ./cayenne-ser.sh -c /dev/ttyACM0 -b 9600 -s mqtt.mydevices.com -p 1883

    Run cayenne-ser.sh -h for more information

    Be sure to select the right serial port (there may be multiple).

ATTENTION!
  Do not use Serial to display any output in this sketch. It will interfere with the Serial
  USB connection. When uploading sketches the Arduino IDE may complain with "programmer is
  not responding" or "Access is denied." You will need to terminate the connection script
  before uploading new sketches since it blocks access to the Serial port. Also make sure 
  the Serial Monitor is disabled in the IDE since that can prevent the Arduino from 
  connecting to the Windows/Linux/OSX machine. If you use Visual Micro for Visual Studio make
  sure Automatic Debugging is disabled. Otherwise the Serial Monitor can interfere with the
  Serial connection.
*/

#include <CayenneMQTTSerial.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 7
#define DHTTYPE DHT22
DHT_Unified dht (DHTPIN, DHTTYPE);

// 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);
  dht.begin();
  sensor_t sensor;
}

void loop() {
  Cayenne.loop();
}

// 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());

  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  Cayenne.celsiusWrite(1, event.temperature);
  dht.humidity().getEvent(&event);
  Cayenne.virtualWrite(2, event.relative_humidity, "rel_hum", "p");
   int analogValue = analogRead(A0);
    int gas = map(analogValue, 0, 1023, 0, 100);
    Cayenne.virtualWrite(3, gas, "analog_sensor", "p");
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
This is the code that i use i perform the step 
Windows:

Open the Windows command line (cmd.exe)
Navigate to the scripts folder by typing “cd [path]”, e.g. “cd C:\Users[YourUserName]\Documents\Arduino\libraries\CayenneMQTT\extras\scripts”
Run the script by typing “cayenne-ser.bat -c COM4” (where COM4 is the Arduino serial port) and hitting Enter 
but when i close the cmd the device will become offline

#4

the code which you are using is for arduino using serial usb. while the title states about arduino uno with esp8266. for which follow this https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ESP8266Shield/ESP8266Shield.ino

yes, because arduino standalone does not have any means to connect to internet and hence it is depended on it for internet connection. otherwise you can use a ethernet shield or a esp8266 for arduino to connect to cayenne.