Arduino WiFi shield + Python

I have recently received the Arduino WiFi shield and have upgraded to the firmware included with the 1.0.5 IDE software. I have it mounted on the Uno R3 board. What I am trying to do is get it to read an analog input (A0) and transmit it, as well as receive, and based on the character it receives, set a digital output on pin #8 (having eventually found out pins 9-13 are used by the WiFi shield!) I am using Putty to communicate with the setup, but this will eventually be replaced using Python to do the communicating using a socket. (I currently have a similar setup using the DFRobot WiFi shields V2.1, but their V2.2 shields are proving to be very erratic/unstable, and hence me trying the Arduino WiFi shield)

I used the WiFiChatServer example as a starting point, and then modified it to suit my purposes. However I can get it to send to a client (Python client or Putty in this case), or receive (from Putty), but I can't seem to get it to do both at once, and hope someone can help me.

The setup is as per the WiFiChatServer example, the loop is as follows:

void loop() {
  tempCelsius = analogRead(tempPin); //in steps of 0 - 1023 over range of 0 to 5V
  dtostrf(tempCelsius,1,0,tempOut); //convert float to string
  server.write(tempOut);

  WiFiClient client = server.available();
  if (client) {
    if (client.available() > 0) {
      thisChar = client.read();
      server.write(thisChar); //Not sure why needed, but doesn't work if not here
      if (thisChar == 'h') 
        digitalWrite(LED_PIN, HIGH);
      else 
        digitalWrite(LED_PIN, LOW);
    }
  }
}

On the Putty console, as expected I see the screen slowly fill up with output received from the Arduino/WiFi but it is very strange: It starts off giving 51 with odd characters between, then settles down to 51, sometimes 52 (I'm using the LM35 component, this is a reading 0 to 1023). See below output:

1û51510û51 511û51511û51'511ý51501û50511ý51515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515151515150515151505151515151515151515151515151505151515151515151515150515151515151515151515151515151515151515151515151515151515151515151515151515151

It is the strange characters between that concern me, but in the Python code I can always do a check on converting the string to integer to ignore the funny characters, and read every 5 seconds, so I'm not too concerned about the continuous stream received from the Arduino. However, having said that, if anyone knows how I can transmit from the Arduino at specified intervals instead, I would appreciate it. The problem is that it also needs to continuously look for user input, the "h" or "l" to set the digital pin 8.

Now here is my real problem, even though I type an "h" or "l" into Putty, amongst the stream received, it never seems to alter the state of pin 8.

Not sure if this is the best way, but below are my changes to the loop procedure, I'm guessing it is mixing receive and transmit data, so I added some delays before running each block within the loop procedure

void loop() {
  cnt++;
  if (cnt > 10000) {
    readOrWrite = 1;
    cnt = 0;
  }
  
  if (readOrWrite==1) {
    tempCelsius = analogRead(tempPin); //in steps of 0 - 1023 over range of 0 to 5V
    dtostrf(tempCelsius,1,0,tempOut); //convert float to string
    server.write(tempOut);
  }

  if (readOrWrite==0) {
    WiFiClient client = server.available();
    if (client) {
      if (client.available() > 0) {
        thisChar = client.read();
        server.write(thisChar); //Not sure why needed, but doesn't work if not here
        if (thisChar == 'h') 
          digitalWrite(LED_PIN, HIGH);
        else if (thisChar == 'l') 
          digitalWrite(LED_PIN, LOW);
      }
    }
  }
  readOrWrite = 0;
}

What device are you sending this to?

  server.write(tempOut);

You are sending this whether there is a client connected or not?

Yes, the Python client should be running on a PC permanently, but even if it stops, I still want the value transmitted. It saves a check if Client connected to the Arduino, because within a short time a client will reconnect anyway. UDP? The way the client is run is through Windows Task Manager, it checks every 10 minutes, if still running, it won't start a new instance, if not running, it will start a new one.

That is not UDP. That is TCP. The docs state that server.write() sends that character or string to every TCP client connected to the w5100. It is not a network broadcast. It sends only to connected TCP clients.

...but even if it stops, I still want the value transmitted.

You mean a UDP broadcast? If that is what you want, this is not the way to do it.

FYI, pin 7 is also used by the wifi shield so avoid it too.