Salut tout le monde
Je rencontre des difficultés avec mon WiShield (Copperhead pour être exact) donc je viens faire appel à votre savoir Je voudrais envoyer une information en WiFi à ma carte Arduino UNO R2 quand j'appui sur une touche de mon clavier. Pour le moment je fais simple, j'ai fais un petit programme en Python qui envoi succesivement les valeurs 1, 2, 3, ..., 6 et un autre programme sur la Arduino qui interprète ces valeurs. La Arduino recoit bien les données puisque dans le "Serial Monitor" je vois les messages mais on dirait qu'elle n'arrive pas à commander les PIN quand le Wishield est branché
J'ai beau chercher, je ne comprends pas mon erreur
Voici les codes :
Serveur Python :
#!/usr/bin/python
import socket
import time
HOST = '192.168.0.100' # The remote host
PORT = 1000 # The same port as used by the server
def sendValue(value):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(str(value) + '\n')
s.close()
sendValue(1)
time.sleep(1)
sendValue("1")
sendValue(2)
time.sleep(1)
sendValue("2")
sendValue(3)
time.sleep(1)
sendValue("3")
sendValue(4)
time.sleep(1)
sendValue("4")
sendValue(5)
time.sleep(1)
sendValue("5")
sendValue(6)
time.sleep(1)
sendValue("6")
Programme Arduino "Socket.pde":
/*
* Socket App
*
* A simple socket application example using the WiShield 1.0
*/
#include <WiShield.h>
//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
192,168,0,100}; // IP address of WiShield
unsigned char gateway_ip[] = {
192,168,0,47}; // router or gateway IP address
unsigned char subnet_mask[] = {
255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
"JPPW"}; // max 32 bytes
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
"****************"}; // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // Key 3
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
char recvChar;
void setup()
{
//set up serial communications
Serial.begin(9600);
pinMode(4, OUTPUT);
//set up global recvChar to indicate nothing received
recvChar = NULL;
WiFi.init();
}
void loop()
{
if(NULL != recvChar) {
Serial.print("Received: ");
Serial.print(recvChar);
Serial.print(" [");
switch(recvChar) {
case '1':
Serial.println("forward]");
//your control code goes here...
digitalWrite(4, HIGH);
Serial.println("Commande envoyée");
delay(300);
break;
case '2':
Serial.println("backward]");
//your control code goes here...
break;
case '3':
Serial.println("turn right]");
//your control code goes here...
break;
case '4':
Serial.println("turn left]");
//your control code goes here...
break;
case '5':
Serial.println("stop]");
//your control code goes here...
break;
default:
Serial.println("unexpected value received]");
//your control code goes here...
break;
}
//we have handled the last data receive so clear recvChar so that we don't handle the same data again.
recvChar = NULL;
}
WiFi.run();
}
Programme Arduino "Socket.c"
/******************************************************************************
Filename: socketapp.c
Description: Simple socket programming example for the WiShield 1.0
******************************************************************************
TCP/IP stack and driver for the WiShield 1.0 wireless devices
Copyright(c) 2009 Async Labs Inc. All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Contact Information:
<asynclabs@asynclabs.com>
Author Date Comment
---------------------------------------------------------------
AsyncLabs 06/06/2009 Initial version
*****************************************************************************/
/*
* This is a short example of how to write uIP applications using
* protosockets.
*/
/*
* We define the application state (struct socket_app_state) in the
* socketapp.h file, so we need to include it here. We also include
* uip.h (since this cannot be included in socketapp.h) and
* <string.h>, since we use the memcpy() function in the code.
*/
#include "socketapp.h"
#include "uip.h"
#include <string.h>
extern char recvChar;
/*
* Declaration of the protosocket function that handles the connection
* (defined at the end of the code).
*/
static int handle_connection(struct socket_app_state *s);
/*---------------------------------------------------------------------------*/
/*
* The initialization function. We must explicitly call this function
* from the system initialization code, some time after uip_init() is
* called.
*/
void socket_app_init(void)
{
/* We start to listen for connections on TCP port 1000. */
uip_listen(HTONS(1000));
}
/*---------------------------------------------------------------------------*/
/*
* In socketapp.h we have defined the UIP_APPCALL macro to
* socket_app_appcall so that this function is uIP's application
* function. This function is called whenever an uIP event occurs
* (e.g. when a new connection is established, new data arrives, sent
* data is acknowledged, data needs to be retransmitted, etc.).
*/
void socket_app_appcall(void)
{
/*
* The uip_conn structure has a field called "appstate" that holds
* the application state of the connection. We make a pointer to
* this to access it easier.
*/
struct socket_app_state *s = &(uip_conn->appstate);
/*
* If a new connection was just established, we should initialize
* the protosocket in our applications' state structure.
*/
if(uip_connected()) {
PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
}
/*
* Finally, we run the protosocket function that actually handles
* the communication. We pass it a pointer to the application state
* of the current connection.
*/
handle_connection(s);
}
/*---------------------------------------------------------------------------*/
/*
* This is the protosocket function that handles the communication. A
* protosocket function must always return an int, but must never
* explicitly return - all return statements are hidden in the PSOCK
* macros.
*/
static int handle_connection(struct socket_app_state *s)
{
if(uip_newdata()) {
PSOCK_BEGIN(&s->p);
PSOCK_READTO(&s->p, '\n');
recvChar = s->inputbuffer[0];
PSOCK_CLOSE(&s->p);
PSOCK_END(&s->p);
}
else {
//if no new data is received set our global into to 0 to indicate no new data
recvChar = NULL;
}
}
/*---------------------------------------------------------------------------*/
Merci d'avance