Skip to content

Newbie Const Char * question #2168

@bphick

Description

@bphick

As a total neophyte newbie I spent a week trying to get my code to work in multiple locations. I’m sure a more experienced person will find this an easy fix and any help would be very much appreciated. Be nice, I’m a newbie…

In January, this code would work with a warning about “const char ” not being good code… but it compiled and worked great. Alas, now I need to upgrade a couple of Adafruit ESP8266 Huzzah’s with this code but it won’t compile. I put the whole code here, but have highlighted the offending parts of the code between 2 sections marked by something like: *// _______________________ NEED HELP STARTING HERE __________________” or ENDING similar.**

It says “Cannot convert ‘String’ to Const Char* in assignment:

Arduino: 1.6.9 (Windows 7), Board: "Adafruit HUZZAH ESP8266, 80 MHz, 115200, 4M (3M SPIFFS)"

C:\Users\Owner\Documents\Arduino\Huzzah_Sensor_MultiWiFiLocation_Unit_wPIR_Photo_1_1_16\Huzzah_Sensor_MultiWiFiLocation_Unit_wPIR_Photo_1_1_16.ino: In function 'void setup()':

Huzzah_Sensor_MultiWiFiLocation_Unit_wPIR_Photo_1_1_16:146: error: cannot convert 'String' to 'const char*' in assignment

 WLAN_SSID = WiFi.SSID();

           ^

exit status 1
cannot convert 'String' to 'const char*' in assignment


/* Starts here************************
PIR and Light sensor added to this version. Publishes data to 4 feeds every 10 sec.
and also looks for up to 6 WiFi Networks to log into.

Adafruit ESP8266 Sensor Module

Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution

Modified for multi-location by Bryan Hickman, a hobbyist and a wantabie.
****************************************************/

// Libraries

include <ESP8266WiFi.h>

include "Adafruit_MQTT.h"

include "Adafruit_MQTT_Client.h"

include "DHT.h"

// DHT 22 sensor

define DHTPIN 5

define DHTTYPE DHT22

**// _______________________ NEED HELP STARTING HERE __________________

// WiFi parameters
const char * WLAN_SSID;
const char * WLAN_PASS;

 // WiFi's and followed by their passwords

const char * myWLAN_SSIDStrings[]={"Location 1", "Location2", "Location3", "Location4", "Location5","Location6"}; // I tried the unprotected xfinitywifi router with no security password, and while it connects to the web, it won't connect to Adafruit.IO
const char * myWLAN_PASSStrings[]={"PW_Location1", " PW_Location2", " PW_Location3"," PW_Location4"," PW_Location5"," PW_Location6"};
// Particulars on what you said regarding the WiFi Arrays above.
int nbrOfWiFis = 6; // ******Number of WiFi's to look for sequentially
int StartOnWiFiNo = 3; //Which one do you want it to start on before sequentially looking through the WiFi Array. Helps if you know location where unit will spend majority of its time.

// _________ * NEED HELP ABOVE HERE and a similarly marked section BELOW___________*

// Adafruit IO

define AIO_SERVER "io.adafruit.com"

define AIO_SERVERPORT 1883

define AIO_USERNAME "My_UserName"

define AIO_KEY "MyAIO_Key"

// Setup Analog Reading (Photoresistor)

int photocellPin = A0; // the cell and pulldowns are connected to "A"
int photocellReading = 0; // the analog reading from the analog resistor divider
int Brightness = 0; // the Mapped analog reading from the analog resistor divider

// Setup the PIR
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Store the MQTT server, client ID, username, and password in flash memory.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;

// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
//const char MQTT_CLIENTID[] PROGMEM = AIO_KEY DATE TIME;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
//Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);/****************************** Feeds ***************************************/
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

// Setup feeds for temperature, humidity, PIR & Ambient Brightness state

const char Light_FEED[] PROGMEM = AIO_USERNAME "/feeds/Light";
Adafruit_MQTT_Publish Light = Adafruit_MQTT_Publish(&mqtt, Light_FEED);

const char PIR_FEED[] PROGMEM = AIO_USERNAME "/feeds/PIR";
Adafruit_MQTT_Publish PIR = Adafruit_MQTT_Publish(&mqtt, PIR_FEED);

const char TEMPERATURE_FEED[] PROGMEM = AIO_USERNAME "/feeds/temperature";
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, TEMPERATURE_FEED);

const char HUMIDITY_FEED[] PROGMEM = AIO_USERNAME "/feeds/humidity";
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, HUMIDITY_FEED);

void setup() {

// Photoresistor

// PIR
pinMode(inputPin, INPUT); // declare sensor as input

// Init sensor
dht.begin();

Serial.begin(115200);
Serial.println(F("Adafruit IO Example With PIR and PhotoResistor"));

// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
// _________________________________________________________-

*// _______________________ * NEED HELP BELOW ________________________________

WLAN_SSID = myWLAN_SSIDStrings[StartOnWiFiNo-1];
WLAN_PASS = myWLAN_PASSStrings[StartOnWiFiNo-1];
delay(10);
//___________________________________________________________________________
Serial.print(F("Initial attempt to connect to your identified preferred network: "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(5200);
while (WiFi.status() != WL_CONNECTED) {
// Serial.print(F("."));
Serial.println("_____________________________________________________");
for (int i = 0; i < nbrOfWiFis; i++){
delay(5200);
Serial.print(F("Attempting to connect to "));
WLAN_SSID = (myWLAN_SSIDStrings[i]);
WLAN_PASS = myWLAN_PASSStrings[i];
Serial.println(WLAN_SSID);
// Serial.print(" - ");
// Serial.println(WLAN_PASS);
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(5000);
} else {break;}
}}
Serial.println();
Serial.println(F("WiFi connected"));
WLAN_SSID = WiFi.SSID();
Serial.println(WLAN_SSID);
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());

// connect to adafruit io
connect();

};

void loop() {

//NEED HELP ABOVE_________________________________

// Photoresistor
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
Serial.print("Converted Brightness reading = ");

// We'll have a few threshholds, qualitatively determined
Brightness = map(photocellReading, 0, 1023, 0, 100);
Serial.println(Brightness); // the raw analog reading
if (Brightness < 10) {
Serial.println(" - Dark");
} else if (Brightness < 20) {
Serial.println(" - Dim");
} else if (Brightness < 50) {
Serial.println(" - Light");
} else if (Brightness < 80) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
Serial.println(" _____________________________________________________");

// ___________________________________________________________________
// PIR

val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
delay(150);

if (pirState == LOW) {
  // we have just turned on
  Serial.println("Motion detected!");
  // We only want to print on the output change, not state
  pirState = HIGH;
}

} else {
// digitalWrite(ledPin, LOW); // turn LED OFF
// playTone(0, 0);
// delay(300);
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}

// ___________________________________________________________________

// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}

// Grab the current state of the sensor
int Light_data = (int)Brightness;
int PIR_data = (int)pirState;
int humidity_data = (int)dht.readHumidity();
if (humidity_data > 100)
humidity_data = 100;
float temperature_data = (float)dht.readTemperature();
temperature_data = temperature_data * 9 / 5 + 32;

// Publish data

 if (! Light.publish(Light_data))
Serial.println(F("Failed to publish Light"));

else
Serial.println(F("Light published!"));

if (! PIR.publish(PIR_data))
Serial.println(F("Failed to publish PIR"));
else
Serial.println(F("PIR published!"));

if (! temperature.publish(temperature_data))
Serial.println(F("Failed to publish temperature"));
else
Serial.println(F("Temperature published!"));

if (! humidity.publish(humidity_data))
Serial.println(F("Failed to publish humidity“));
else
Serial.println(F("Humidity published!"));

// Repeat every 10 seconds
delay(10000);

}

// connect to adafruit io via MQTT
void connect() {

Serial.print(F("Connecting to Adafruit IO... "));

int8_t ret;

while ((ret = mqtt.connect()) != 0) {

switch (ret) {
  case 1: Serial.println(F("Wrong protocol")); break;
  case 2: Serial.println(F("ID rejected")); break;
  case 3: Serial.println(F("Server unavail")); break;
  case 4: Serial.println(F("Bad user/pass")); break;
  case 5: Serial.println(F("Not authed")); break;
  case 6: Serial.println(F("Failed to subscribe")); break;
  default: Serial.println(F("Connection failed")); break;
}

if(ret >= 0)
  mqtt.disconnect();

Serial.println(F("Retrying connection..."));
delay(5000);

}

Serial.println(F("Adafruit IO Connected!"));

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions