My first project


#1

Hi,
I am Bernard from France … I am fan of IOT and Cayenne.
First of all I would like apologize for my broken English.
This is my first project and I must say I used copy and paste but I thing I have learnt a lot.

// ESP8266 --> SCL pin = d1 SDA = d2
// BUILT_IN led ligth when LOW (inversée)

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include "DHT.h"
#include <CayenneMQTTESP8266.h>


char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info.
char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

#define DHTPIN D4

#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_ADS1115 adc(0x48);
float hum, temp, f;
byte restartMcu = 0;
bool online = false;
void setup()
{
  Serial.begin(9600);
  adc.begin();

  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  pinMode(D5, OUTPUT); // relay#1
  pinMode(D7, OUTPUT); // relay#2
  pinMode(D3, OUTPUT);
  digitalWrite(D7, LOW);
  pinMode(D0, OUTPUT);
  digitalWrite(D0, HIGH);
}

void loop() {
  float Voltage;
  long wifiRSSI;
  int lightLevel = 0;
  Cayenne.loop();

  wifiRSSI = WiFi.RSSI();

  // ************************* LDR *****************************
  long val = 0;
  for (int number = 0; number < 10; number++)
  {
    lightLevel = adc.readADC_SingleEnded(0);
    val = val + lightLevel;
    delay(100);
  }
  lightLevel = (val / 10) / 100;
  //************************************************************

  hum = dht.readHumidity();
  temp = dht.readTemperature();


  if (isnan(temp) || isnan(hum))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  //  builtin analog input esp8266
  int sensorValue = analogRead(A0);
  float vBat = sensorValue * (3.2 / 1023.0);

  if (millis() - lastMillis > 10000)
  {
    lastMillis = millis();
    Cayenne.virtualWrite(0, hum);
    Cayenne.virtualWrite(10, temp);
    //Cayenne.virtualWrite(8,Voltage);
    Cayenne.virtualWrite(19, lightLevel);
    Cayenne.virtualWrite(11, wifiRSSI);
    Cayenne.virtualWrite(12, vBat);
    Cayenne.virtualWrite(18, 0, "digital_sensor", "d");
  }
  yield();
}

CAYENNE_CONNECTED()
{
  //online = true;
  digitalWrite(D0, HIGH);
}

CAYENNE_DISCONNECTED()
{
  //online = false;
  digitalWrite(D0, LOW);
}



CAYENNE_IN(5) // light
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
    digitalWrite(D5, HIGH);
  }
  else
  {
    digitalWrite(D5, LOW);
  }
}

CAYENNE_IN(6) // pump
{

  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
    digitalWrite(D7, HIGH);
  }
  else {
    digitalWrite(D7, LOW);
  }
}

CAYENNE_IN(18)
{
  restartMcu = getValue.asInt();

  if (restartMcu == 1)
  {
    //Cayenne.virtualWrite(18, 0, "digital_sensor", "d");
    //Cayenne.loop();
    delay(500);
    ESP.restart();
  }
}

Everything works but RestartMCU works sometimes and sometimes doesn’t work.
What am I doing wrong?
Thank you.
Salutations


#2

Welcome!

Looking at your code I don’t see any issues with the restart function. I have nearly the exact same code for my restart function and it works everytime. I’d try running this code below to pint out whats running. You can see if its being called correctly and find out if its a issue with the ESP.restart() or something else.

CAYENNE_IN(18)
{
  restartMcu = getValue.asInt();
  Serial.println(restartMcu);

  if (restartMcu == 1)
  {
    //Cayenne.virtualWrite(18, 0, "digital_sensor", "d");
    //Cayenne.loop();
    delay(500);

    Serial.println("restart called");
    ESP.restart();
  }
}

#3

Hi,
Thank you vapor :slight_smile:
I just did 10 tries… success … the 11th failed
I have found this on esp8266 github.
Salutations


#4

Thats so weird why it randomly fails to restart. Looks like its a known problem like you found. it stinks but 10/11 isn’t bad lol


#5

This problem has to be putted maybe in the documentation. I also has some headaches with restarting :frowning:


#6

Welcome to the Cayenne forums @Bernard! I am glad you have become a fan of IOT and Cayenne.