Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By balajibv
#65415 Hello

Im unable to parse the payload and always getting into the error section. What am I missing. Im able to connect, receive message, post message, but cant read the payload :(

Code: Select allvoid callback(char* topic, byte* payload, unsigned int length) {
  byte* bytes;
  int i;
  writeOled("Message arrived",oledLine);
  client.publish(outTopic, "Message received");
  Serial.print(topic);
  Serial.print("] ");
  for (i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

 
  // Switch on the LED if an 1 was received as first character
  writeOled("Message Received",oledLine);
  //res = String(result);
  //writeOled(res,oledLine);
  //client.publish(outTopic, result);
  if ((char)payload[0] == '0') {
    digitalWrite(relay_pin, LOW);   // Turn the LED on (Note that LOW is the voltage level
    Serial.println("relay_pin -> LOW");
    relayState = LOW;
    EEPROM.write(0, relayState);    // Write state to EEPROM
    EEPROM.commit();
  writeOled("Garage:Closed",oledLine);
  client.publish(outTopic, "Garage Closed");
  } else if ((char)payload[0] == '1') {
    digitalWrite(relay_pin, HIGH);  // Turn the LED off by making the voltage HIGH
    Serial.println("relay_pin -> HIGH");
    relayState = HIGH;
    EEPROM.write(0, relayState);    // Write state to EEPROM
    EEPROM.commit();
  client.publish(outTopic, "Garage Open");
  writeOled("Garage:OPEN",oledLine);
  } else if ((char)payload[0] == '2') {
    relayState = !relayState;
    digitalWrite(relay_pin, relayState);  // Turn the LED off by making the voltage HIGH
    Serial.print("relay_pin -> switched to ");
    Serial.println(relayState);
    EEPROM.write(0, relayState);    // Write state to EEPROM
    EEPROM.commit();
  client.publish(outTopic, "Dont know what this is...");
  }
  else {
    writeOled("Garage:ERROR",oledLine);
    client.publish(outTopic, "I didnt understand something!");
  }
  writeOled("DONE!!!!",oledLine);
  client.publish(outTopic, "I did something!");
}
User avatar
By Andyd273
#67111
Touliloup wrote:Yes, Amazon AWS is compatible with MQTT, it will speak with your MQTT broker, not the sensor/client directly.

http://docs.aws.amazon.com/iot/latest/d ... ocols.html



Just to make sure I understand, I would set up a local broker on my computer or Raspberry Pi, and send messages to that from AWS, which would forward the messages on to the ESP01?

Learning all of the things I don't know so I can figure out which questions to ask is always part of the challenge.