0

I have downloaded code from below linkLink of code. I am trying test code from below. I am getting warning for below code.I couldnot able to find how to sort out

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>


/* ------------------ */
/* SKETCH CREDENTIALS */
/* ------------------ */



const char *deviceId     = "<D56debb6351d9d4e74e000015>";             // * set your device id (will be the MQTT client username)
const char *deviceSecret = "<Y4h+R1NRAxrhFciRhtHLj66ERSv8RExu>";         // * set your device secret (will be the MQTT client password)
const char *outTopic     = "devices/<D56debb6351d9d4e74e000015>/set"; // * MQTT channel where physical updates are published
const char *inTopic      = "devices/<D56debb6351d9d4e74e000015>/get"; // * MQTT channel where lelylan updates are received
const char *clientId     = "<CLIENT-ID>";             // * set a random string (max 23 chars, will be the MQTT client id)


/* ------------ */
/* SKETCH LOGIC */
/* ------------ */

/* Server settings */
byte server[] = { 178, 62, 108, 47 }; // MQTT server address

/* Sample payload published to lelylan */
/* The id is the status property id of the basic light /*
  /* http://lelylan.github.io/types-dashboard-ng/#/types/518be107ef539711af000001/ */
const char *payloadOn  = "{\"properties\":[{\"id\":\"56debb6351d9d4e74e000015\",\"value\":\"on\"}]}";
const char *payloadOff = "{\"properties\":[{\"id\":\"56debb6351d9d4e74e000015\",\"value\":\"off\"}]}";

/* Ethernet configuration */
byte mac[] = { 0xA0, 0xA0, 0xBA, 0xAC, 0xAE, 0x12 };
EthernetClient ethClient;

/* MQTT communication */
void callback(char* topic, byte* payload, unsigned int length); // subscription callback
PubSubClient client(server, 1883, callback, ethClient);         // mqtt client

/* Pins configuration */
int inPin = 2; // button
int outPin = 4; // led

/* Button and led logics */
int state = HIGH;     // current state of the output pin
int reading;          // current reading from the input pin
int previous = LOW;   // previous reading from the input pin
long time = 0;        // the last time the output pin was toggled
long debounce = 200;  // the debounce time, increase if the output flickers

/* arduino setup */
void setup() {
  Serial.begin(9600);
  delay(500);

  Ethernet.begin(mac);
  Serial.print("Connected with IP: ");
  Serial.println(Ethernet.localIP());

  lelylanConnection();      // MQTT server connection
  pinMode(inPin, INPUT);    // button pin setup
  pinMode(outPin, OUTPUT);  // led pin setup
}

/* arduino loop */
void loop() {
  lelylanConnection();

  const char *value;
  reading = digitalRead(inPin);  // read the button state

  // if the input just went from LOW and HIGH and we've waited long enough to ignore
  // any noise on the circuit, toggle the output pin and remember the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == LOW) {
      Serial.println("[PHYSICAL] Led turned on");
      lelylanPublish("on");
      state = HIGH;
    } else {
      Serial.println("[PHYSICAL] Led turned off");
      lelylanPublish("off");
      state = LOW;
    }

    time = millis();
  }

  // effectively update the light status
  digitalWrite(outPin, state);
  previous = reading;
}

/* MQTT server connection */
void lelylanConnection() {
  // add reconnection logics
  if (!client.connected()) {
    // connection to MQTT server
    if (client.connect(clientId, deviceId, deviceSecret)) {
      Serial.println("[PHYSICAL] Successfully connected with MQTT");
      lelylanSubscribe(); // topic subscription
    }
  }
  client.loop();
}

/* MQTT publish */
void lelylanPublish(char *value) {
  if (value == "on")
    client.publish(outTopic, payloadOn); // light on
  else
    client.publish(outTopic, payloadOff); // light off
}

/* MQTT subscribe */
void lelylanSubscribe() {
  client.subscribe(inTopic);
}

/* Receive Lelylan message and confirm the physical change */
void callback(char* topic, byte* payload, unsigned int length) {
  // copu the payload content into a char*
  const char *json;
  json = (char *) malloc(length + 1);
  memcpy(json, payload, length);
  json[length] = "\0";

  // update the physical status and confirm the executed update
  if (String(payloadOn) == String(json)) {
    Serial.println("[LELYLAN] Led turned on");
    lelylanPublish("on");
    state = HIGH;
  } else {
    Serial.println("[LELYLAN] Led turned off");
    lelylanPublish("off");
    state = LOW;
  }

  digitalWrite(outPin, state);
  free(json);
}

Arduino: 1.6.6 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino: In function 'void loop()':

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino:77:26: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   lelylanPublish("on");

                      ^

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino:81:27: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   lelylanPublish("off");

                       ^

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino: In function 'void callback(char*, byte*, unsigned int)':

IOT_Testcode:124: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]

memcpy(json, payload, length);

                           ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:25:0,

             from sketch\IOT_Testcode.ino.cpp:1:

c:\program files (x86)\arduino\hardware\tools\avr\avr\include\string.h:117:14: error:
initializing argument 1 of 'void* memcpy(void*, const void*, size_t)' [-fpermissive]

extern void *memcpy(void *, const void *, size_t);

          ^

IOT_Testcode:125: error: assignment of read-only location '*(json + ((sizetype)length))'

json[length] = "\0";

            ^

IOT_Testcode:125: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino:130:24: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

 lelylanPublish("on");

                    ^

C:\Users\Ajit\Desktop\IOT_Testcode\IOT_Testcode.ino:134:25: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

 lelylanPublish("off");

                     ^

IOT_Testcode:139: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]

free(json);

        ^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:23:0,

             from sketch\IOT_Testcode.ino.cpp:1:

c:\program files (x86)\arduino\hardware\tools\avr\avr\include\stdlib.h:305:13: error:
initializing argument 1 of 'void free(void*)' [-fpermissive]

extern void free(void *__ptr);

         ^

exit status 1 invalid conversion from 'const void*' to 'void*' [-fpermissive]

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

Kindly let me know how to resolve these warning

1
  • 1
    Most of those warnings are caused by the author not having a clue what they are doing. That code is so badly written and broken that you should just throw it away and start from scratch. I can't even begin to list the problems with it here.
    – Majenko
    Mar 9, 2016 at 11:26

1 Answer 1

1

This code was written and tested using an earlier Arduino IDE version, the author's upload of the source files happened over a year ago.

Within the Arduino 1.6.x branch the preprocessor and compiler behaviour have changed (among other things).

Older libraries will require an earlier Arduino IDE version (i.e. the 1.5.x branch) from https://www.arduino.cc/en/Main/OldSoftwareReleases#previous.

Additionally, we have to keep in mind that there are differences between Arduino.cc and Arduino.org. Those two projects split apart, their IDEs aren't equivalent any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.