[SOLVED] Can't get WiFi.ready() and UDP work in Semi-Automatic Mode

I’m trying to use Udp to broadcast message in Semi-Automatic mode. But when I start my core it’s LED gets breathing blue after white.

I couldn’t see any change in LED color.

Here is a piece of my code:

UDP udpClient;

void setup() {

    pinMode(forwardMotor, OUTPUT);
    pinMode(backwardMotor, OUTPUT);
    pinMode(leftMotor, OUTPUT);
    pinMode(rightMotor, OUTPUT);

    WiFi.on();

    Spark.process();

    while (!WiFi.ready()) SPARK_WLAN_Loop();

    // take control of the LED
    RGB.control(true);

    // red, green, blue, 0-255.
    // the following sets the RGB LED to white:
    RGB.color(255, 0, 255);

    // wait one second
    delay(500);

    // scales brightness of all three colors, 0-255.
    // the following sets the RGB LED brightness to 25%:
    RGB.brightness(64);

    // wait one more second
    delay(500);

    // resume normal operation
    RGB.control(false);


    udpClient.begin(udpLocalPort);

    udpClient.beginPacket(udpBroadcastIp, udpRemotePort);
    udpClient.write("asdfg");
    udpClient.endPacket();
}

Ok. I added WiFi.connect() after WiFi.on() and it works now. My bad, sorry.

2 Likes

Glad you got it working @triuman! :wink:

2 Likes

I use this code and doesn’t work. Can you see the problem?

SYSTEM_MODE(SEMI_AUTOMATIC);

UDP udpClient;

void setup() {

WiFi.on();
WiFi.connect();
Spark.process();

while (!WiFi.ready()) SPARK_WLAN_Loop();

// take control of the LED
RGB.control(true);

// red, green, blue, 0-255.
// the following sets the RGB LED to white:
RGB.color(255, 0, 255);

// wait one second
delay(500);

// scales brightness of all three colors, 0-255.
// the following sets the RGB LED brightness to 25%:
RGB.brightness(64);

// wait one more second
delay(500);

// resume normal operation
RGB.control(false);

IPAddress server(8,8,8,8);
udpClient.begin(8888);

udpClient.beginPacket(server, 6789);
udpClient.write("ipbroker");
udpClient.endPacket();

}

Maybe you should explain what your code is trying to do? You are sending packet with the payload “ipbroker” to Google’s DNS server on a port I bet they are not listening on. What did you expect to happen?

Thanks for the response, I try to send this message in my local network on broadcast mode. If I´m not wrong the ip address 255.255.255.255 its the broadcast address for any network. This Ip work in automatic mode, but in semiautomatic doesn’t work. In abstract I want send this packet in broadcast in my local network. Thanks for the attention

This does not match your description.

This is the correct code,if you comment the line “SYSTEM_MODE(SEMI_AUTOMATIC)” the program work and send the packet correctly in broadcast, but if you not comment it, this code don’t work with 255.255.255.255 address.
I change it for a direct address like 192.168.0.3 and sent it correctly in SEMI_AUTOMATIC mode. The problem appear when I want send a broadcast (255.255.255.255) in SEMIAUTOMATIC. Someone have any suggestion?. Thanks for all and sorry for my bad english.

SYSTEM_MODE(SEMI_AUTOMATIC);

UDP udpClient;

void setup() {

    WiFi.on();
    WiFi.connect();
    Spark.process();
    
    while (!WiFi.ready()) SPARK_WLAN_Loop();

    RGB.control(true);
    RGB.color(255, 0, 255);
    delay(500);
    RGB.brightness(64);
    delay(500);
    RGB.control(false);
    
    while(true){
        
       IPAddress server(255,255,255,255);
        udpClient.begin(8888);
        
        udpClient.beginPacket(server, 6789);
        udpClient.write("ipbroker");
        udpClient.endPacket(); 
        delay(500);

    }
    
}

Have you tried Spark.connect()?
You are trying Spark.process() and SPARK_WLAN_Loop() before you actually are connected to the cloud.
This would explain why it works in AUTOMATIC mode, since it’s already connected once you get to this line of code.
I’m still a bit puzzled why it works with your local IP tho’, but maybe broadcasts are treated differently, when cloud is supposed to be connected, but is not.

You have set some LED status reports. Do they show up as expected?
Could you add some D7 (blue LED) blinking within the loop, to find out if the code runs or got stuck.

AFAIK, WiFi is already on in SEMI_AUTOMATK, or not?

Hi all !
I have a similar problem, except that i’m trying to connect to the local wifi, without connecting to the cloud.
My code is posted and my problem is described here : http://community.spark.io/t/new-feature-control-your-connection/6282/54
Is the problem that I’m calling neither Spark.process() and SPARK_WLAN_Loop() ? (Doesn’t seem to work at the moment, but I could have make a mistake calling it)
Thanks in advance !