Dear all, please, give me some advice!
My board (OLIMEX ESP32-PoE ISO) receives a data packet through UDP (at the moment just a string like "121212") and stores it in an array. I can print the details of the packet with Serial.print, also the packet itself, but when I want to use the received data to do something (e.g. turn on an LED if the 2nd char of the string is "1", and another one if the 2nd char is "2"), the compiler says, it is forbidden to compare a pointer to an integer ([-fpermissive]).
The strange thing is that I'm using char arrays and variables everywhere, because I'm not calculating with these values, they're just markers (they could be e.g. "R" and "G" instead of "1" and "2").
Here is the interesting part of my code:
const int ID = 2;
char packetBuffer[40];
// ...
void loop(){
int packetSize = udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(udp.remotePort());
// read the packet into packetBufffer
udp.read(packetBuffer, packetSize);
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println((String)"ID: " + ID + ", Value: " + packetBuffer[ID]);
// I think I'm OK till this point, but...
if (packetBuffer[ID] == "0") {
digitalWrite(rPin, LOW);
digitalWrite(gPin, LOW);
Serial.println("None");
}
else {
if (packetBuffer[ID] == "1") {
digitalWrite(rPin, LOW);
digitalWrite(gPin, HIGH);
Serial.println("Green");
}
else {
if (packetBuffer[ID] == "2") {
digitalWrite(rPin, HIGH);
digitalWrite(gPin, LOW);
Serial.println("Red");
}
}
}
}
delay(10);
}
Interesting part of the error message:
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
if (packetBuffer[ID] == "2") {
If I make the LED switching part to note, I can upload the sketch to the board and I have this on serial monitor:
15:49:01.195 -> WiFi connected! IP address: 192.168.1.217
15:49:08.051 -> Received packet of size 6
15:49:08.051 -> From 192.168.1.100, port 6555
15:49:08.051 -> Contents:
15:49:08.087 -> 121212
15:49:08.087 -> ID: 2, Value: 1
I was reading a lot about arrays and pointers during last week, but could not figure out, what am I doing wrong... I hope it is something simple, just my brain can't work like a computer ![]()
Here is the full code:
#include <WiFi.h>
#include <WiFiAP.h>
#include <WiFiMulti.h>
#include <WiFiUdp.h>
#include <WiFiScan.h>
#include <ETH.h>
#include <WiFiClient.h>
#include <WiFiSTA.h>
#include <WiFiServer.h>
#include <WiFiType.h>
#include <WiFiGeneric.h>
#define rPin 16
#define gPin 14
// WiFi network name and password:
const char * networkName = "ssid";
const char * networkPswd = "pw";
//IP address to send UDP data to:
// either use the ip address of the server or
// a network broadcast address
const char * udpAddress = "255.255.255.255";
const int udpPort = 6555;
const int ID = 2;
char packetBuffer[40];
//Are we currently connected?
boolean connected = false;
//The udp library class
WiFiUDP udp;
void setup(){
delay(1000);
// Initilize hardware serial:
Serial.begin(115200);
//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);
pinMode(rPin, OUTPUT);
digitalWrite(rPin, LOW);
pinMode(gPin, OUTPUT);
digitalWrite(gPin, LOW);
}
void loop(){
int packetSize = udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = udp.remoteIP();
for (int i = 0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(udp.remotePort());
// read the packet into packetBufffer
udp.read(packetBuffer, packetSize);
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println((String)"ID: " + ID + ", Value: " + packetBuffer[ID]);
// I think I'm OK till this point, but...
/*
switch (packetBuffer[ID]) {
case 0:
digitalWrite(rPin, LOW);
digitalWrite(gPin, LOW);
Serial.println(packetBuffer[ID]);
break;
case 1:
digitalWrite(rPin, LOW);
digitalWrite(gPin, HIGH);
Serial.println(packetBuffer[ID]);
break;
case 2:
digitalWrite(rPin, HIGH);
digitalWrite(gPin, LOW);
Serial.println(packetBuffer[ID]);
break;
}
*/
if (packetBuffer[ID] == "0") {
digitalWrite(rPin, LOW);
digitalWrite(gPin, LOW);
Serial.println("None");
}
else {
if (packetBuffer[ID] == "1") {
digitalWrite(rPin, LOW);
digitalWrite(gPin, HIGH);
Serial.println("Green");
}
else {
if (packetBuffer[ID] == "2") {
digitalWrite(rPin, HIGH);
digitalWrite(gPin, LOW);
Serial.println("Red");
}
}
}
}
delay(10);
}
void connectToWiFi(const char * ssid, const char * pwd){
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
//register event handler
WiFi.onEvent(WiFiEvent);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//wifi event handler
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
udp.begin(WiFi.localIP(),udpPort);
connected = true;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
break;
default: break;
}
}