Smart coffee machine with facial recognition and remote control

Dependencies:   Camera_LS_Y201 EthernetInterface EthernetNetIf HTTPClient SRF05 TextLCD mbed-rtos mbed-src

ethernet.cpp

Committer:
projetmacintel
Date:
2014-01-15
Revision:
0:43669f623d43

File content as of revision 0:43669f623d43:

#include "ethernet.h"

//const char* ECHO_SERVER_ADDRESS = "194.254.15.213/reco/";
const char* ECHO_SERVER_ADDRESS = "192.168.1.40";
const int ECHO_SERVER_PORT = 4242;

EthernetInterface eth;
TCPSocketConnection socket;
HTTPClient http;
int timeOut = HTTP_CLIENT_DEFAULT_TIMEOUT;

bool preparationEthernet()
{
    afficherAuCentreDeLEcran("Preparation :", "Ethernet (1/2)");
    wait(0.5);
    
    if(eth.init() == 0)
    //if( eth.init("192.168.1.67", "255.255.255.0", ECHO_SERVER_ADDRESS) == 0) // Test en local (Sans DHCP)
    {
        afficherAuCentreDeLEcran("Preparation :", "Ethernet (2/2)");
        
        if(eth.connect() == 0)
        {
            afficherAuCentreDeLEcran("Machine", "connectee");
            allumerLed(1);
            return true;
        }
        
        else
            afficherAuCentreDeLEcran("Erreur :", "Ethernet (2/2)");
    }
        
    else
        afficherAuCentreDeLEcran("Erreur :", "Ethernet (1/2)");
    
    wait(2);
    afficherAuCentreDeLEcran("Err ethernet", eth.getMACAddress());
        
    allumerLed(2);
    return false;
}

void deconnexionEthernet()
{
    eth.disconnect();
    afficherAuCentreDeLEcran("Machine", "deconnectee");
}

bool connexionSocket()
{
    /*int i = 0;
    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0)
    {
        printf("Unable to connect to (%s) on port (%d)\r\n\r", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
        wait(1);
        i++;
        if(i>5)
        {
            return 0; // Échec
        }
    }
    return 1;*/
    for(int i = 0 ; i < 4 ; i ++)
    {
        if(socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0)
        {
            afficherAuCentreDeLEcran("Serveur", "injoignable", "sur", ECHO_SERVER_ADDRESS);
            wait(3);
        }
        
        else
            return true;
    }
    
    return false;
}

void deconnexionSocket()
{
    socket.close();
}

bool envoyerRequete(char complement_url[200], char *reponse, int longueur_reponse)
{
    char url[500];
    sprintf(url, "http://asi-12-cafetiere.insa-rouen.fr/mbed/%s", complement_url);
    //sprintf(url, "http://192.168.1.10/mbed/%s", complement_url);
    HTTPResult retour = http.get(url, reponse, longueur_reponse);

    if(retour != HTTP_OK)
    {
        afficherAuCentreDeLEcran("ECHEC", "Envoi requete");
        wait(1);
    }

    return retour == HTTP_OK; // Succès
}

bool envoyerRequete(char complement_url[200])
{
    char reponse_vide[1];
    return envoyerRequete(complement_url, reponse_vide, 1);
}

void envoyerChaineSocket(char *chaine, int taille_chaine, char *reponse, int longueur_reponse_max)
{
    int m;
    socket.send_all(chaine, taille_chaine);
    m = socket.receive(reponse,longueur_reponse_max);
    reponse[m] = '\0';
}