Tuesday, May 30, 2023
HomeiOS DevelopmentIOS Swift MQTT will not publish message to Raspberry Pi

IOS Swift MQTT will not publish message to Raspberry Pi


l am fairly new to iOS growth and simply having a tough time attempting to make MQTT publish messages from my cellphone. l want to use the app I’ve constructed to manage the raspberry pi wheels (the step motors extra particularly). I made a button to connect with the robotic.

struct ConnectToRobot: View {
    var physique: some View {
        Button {
            print("Hook up with robotic")
            mqttClient.join()
            
        } label:  {
            Picture(systemName: "circle")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

When l press the button from the app in my cellphone, it connects to the raspberry pi and it exhibits me the message “subscribing”, “subscribed”. Nevertheless, when l press every other buttons on the app, like “ahead” or the rest it appears to not work in any respect. Right here is the code from the RPi:

#For checking the connection standing
def connectionStatus(shopper, userdata, flags, rc):
    print("subscribing")
    mqttClient.subscribe("robotic/transfer")
    print("subscribed")




#motion management
def messageDecoder(shopper, userdata, msg):
    message = msg.payload.decode(encoding='UTF-8')
    
    if message == "f":
        ahead()
        mess2 = msg.payload.decode(encoding='UTF-8')
        
        print("^^^ ahead! ^^^")
        
    elif message == "b":
        backward()
        print("/ backward /")
        
    elif message == "l":
        left()
        print("<- left")
        
    elif message == "r":
        proper()
        print("-> proper")
        
    elif message == "fl":
        diagFL()
        print(" Diagonal Entrance Left")
        
    elif message == "fr":
        diagFR()
        print("/ Diag Entrance Proper")
        
    elif message == "bl":
        diagBL()
        print("/ Diag Again Left")
        
    elif message == "br":
        diagBR()
        print(" Diag again Proper")
        
    elif message == "cw":
        cw()
        print("O Clockwise")

    elif message == "ccw":
        ccw()
        print("O Counter Clockwise")
            
    elif message == "s":
        cease()
       
    else:
        print("?!? Unknown message?!?")


# Arrange calling capabilities to mqttClient
mqttClient.on_connect = connectionStatus
mqttClient.on_message = messageDecoder


# Hook up with the MQTT server & loop ceaselessly.
# CTRL-C will cease this system from working.
mqttClient.join(serverAddress)
mqttClient.loop_forever()

That is my swiftUI code beneath:


import SwiftUI
import CocoaMQTT





let mqttClient = CocoaMQTT(clientID: "Robotic", host: "raspberrypi.native", port: 1883)


struct ContentView: View {
    var physique: some View {
        ZStack {
            VStack{
                HStack {
                    VStack {
                        BackwardLeft()
                        Backward()
                        BackwardRight()
                    }
                    VStack{
                        Left()
                        ConnectToRobot()
                        Proper()
                    }
                    VStack {
                        ForwardLeft()
                        Ahead()
                        ForwardRight()
                    }

                }
            }
        }
    }
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


//First VStack
//Again left
struct BackwardLeft: View {
    var physique: some View {
        Button {
            print("Transfer again left")
        } label:  {
            Picture(systemName: "arrow.up.left.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}


//Backward
struct Backward: View {
    var physique: some View {
        Button (motion: {
        
            mqttClient.publish("robotic/transfer", withString: "b")
            print("Transfer again")
        }, label:  {
            Picture(systemName: "arrow.left.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        })
    }
}


//Again Proper
struct BackwardRight: View {
    var physique: some View {
        Button {
            print("Again proper")
            
        } label:  {
            Picture(systemName: "arrow.down.left.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}


//Second VStack
//Left
struct Left: View {
    var physique: some View {
        Button {
            mqttClient.allowUntrustCACertificate = true
            mqttClient.publish("robotic/transfer", withString: "l")
            print("Transfer left")
        } label:  {
            Picture(systemName: "arrow.up.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

//MQTT Connection
struct ConnectToRobot: View {
    var physique: some View {
        Button {
            print("Hook up with robotic")
            mqttClient.join()
            
        } label:  {
            Picture(systemName: "circle")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

//Proper
struct Proper: View {
    var physique: some View {
        Button {
            print("Transfer proper")
            mqttClient.publish("robotic/transfer", withString: "r")
        } label:  {
            Picture(systemName: "arrow.down.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}




//Third VStack
//Ahead left
struct ForwardLeft: View {
    var physique: some View {
        Button {
            print("Transfer foward left")
        } label:  {
            Picture(systemName: "arrow.up.proper.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

//Ahead
struct Ahead: View {
    var physique: some View {
        Button {
            mqttClient.publish("robotic/transfer", withString: "f")
            print("Transfer ahead")
        } label:  {
            Picture(systemName: "arrow.proper.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

//Ahead proper
struct ForwardRight: View {
    var physique: some View {
        Button {
            print("Transfer prime proper")
        } label:  {
            Picture(systemName: "arrow.down.proper.sq.")
                .renderingMode(.template)
                .resizable()
                .aspectRatio(contentMode: .match)
                .body(width: 100, peak: 100)
                .foregroundColor(.white)
            
        }
    }
}

I’m not certain what’s flawed, as a result of when l press join the message exhibits up on the PI terminal however when l press a course button nothing exhibits up. I’m pondering it may be a connection challenge or one thing l did flawed within the IOS code. Thanks prematurely

I attempted to alter the swiftUI code but it surely did not work

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments