1

Dependencies:   ESP8266

main.cpp

Committer:
nikitoslav
Date:
2018-07-02
Revision:
3:24f12bc2b76d
Parent:
2:f149c178dd58
Child:
4:e584e034f9e1

File content as of revision 3:24f12bc2b76d:

#include "mbed.h"
#include <string>
#include "ESP8266.h"

int localOutPort = 49100;
int localInPort = 49101;

Serial console(USBTX,USBRX);
ESP8266 wifi(PF_7,PF_6,localOutPort,localInPort);

const char* ap = "Clapeyron_Industries";
const char* passPhrase = "06737184";

Thread listeningThread;

void onReceive(void);
void processReceivedData(string, char*, int);

int main() {
    console.baud(9600);    
    if (wifi.startup(1) && wifi.connect(ap,passPhrase))
        console.printf("Your IP is: %s\n",wifi.getIPAddress());
    else
        console.printf("Can not connect to the Wi-Fi router\n");
   // wifi.send("privetFromESP8266",17,"192.168.0.103",8000);
    listeningThread.start(onReceive);
}

void onReceive(void) {
    const int maxSize = 100;
    
    char buffer[100];
    string buf = "";
    char IP[16];
    int port;
    int bytes;
    while(1) {
        buf = "";
        bytes = wifi.recv(&buffer,maxSize,IP,&port);
        if (bytes != -1) {
            console.printf("Bytes received: %d; from %s:%d\n",bytes,IP,port);
            for(int i = 0; i < bytes; i++) {
                buf += buffer[i];
            }
            console.printf("Data: %s\n",buf);
            processReceivedData(buf,IP,port);
        }
    }
}

void processReceivedData(string data, char* IP, int port) {
    if (port == localOutPort) {
        string msg = "?HiClientImARobotClapeyron|hardvers|0.1?";
        if (data == "HiRobotClapeyronImAClient") wifi.send(msg.c_str(),msg.length(),IP,localInPort);
    }
}