Ahmed Popović Benjamin Ramić Tviter Projekat

Dependencies:   ESP8266 mbed

Fork of TwitterProjekat by Ahmed Popović

WiFi.h

Committer:
2016US_BenjaminRamic
Date:
2016-05-26
Revision:
2:907b2a247aaa
Parent:
1:51391e2303d7

File content as of revision 2:907b2a247aaa:

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

using namespace std;

class WiFi
{
private:
    char rcv[2048];
    char snd[255];
    ESP8266 * esp;
    Serial * pc;
    
public:
    WiFi(char * ssid, char * pw, Serial * pc)
    {
        this->pc = pc;
        this->pc->baud(115200);
        esp = new ESP8266(PTE0, PTE1, 115200);
        
        SetupModule();
        ConnectToWifi(ssid, pw);
    }
    
    string GetPage(string kveri)
    {
        strcpy(snd, "AT+CIPSTART=0,\"TCP\",\"173.236.171.131\",80");
        esp->SendCMD(snd);
        esp->RcvReply(rcv, 400);
        pc->printf("%s", rcv);
        
        char velChar[10];
        int ukupnaVelicina = 4 + kveri.length() + 35;
        wait(2);
        strcpy(snd, "AT+CIPSEND=0,");
        sprintf(velChar, "%d", ukupnaVelicina);
        strcpy(snd+13, velChar);
        //pc->printf("sad saljem: %s\r\n\r\n", snd);
        esp->SendCMD(snd);
        esp->RcvReply(rcv, 2048);
        //pc->printf("%s", rcv);
        wait(2);
        
        
        strcpy(snd, "GET ");
        strcpy(snd + 4, kveri.c_str());
        strcpy(snd + 4 + kveri.length(), " HTTP/1.1\r\nHost: liliumdev.com\r\n\r\n\0");
        string stringSndBuffer = snd;
        
       /* pc->printf("U bufferu je: \"");
        pc->printf("%s", snd); 
        pc->printf("\", velicina svega je: %d, a nasa je %d\r\n", snd, stringSndBuffer.length(), ukupnaVelicina);*/
        esp->SendCMD(snd);
       /* while(true)
        {
            esp->RcvReply(rcv, 2048);
            pc->printf("%s", rcv);   
        }*/
        
        return "";
    }        
        
    bool SetupModule()
    {
        wait(3);    
        pc->printf("Sending AT\r\n");
        strcpy(snd, "AT");
        esp->SendCMD(snd);
        esp->RcvReply(rcv, 400);
        pc->printf("%s", rcv);
        wait(2);
        pc->printf("Set mode to STA\r\n");
        esp->SetMode(1);
        esp->RcvReply(rcv, 1000);
        pc->printf("%s", rcv);
        
        strcpy(snd, "AT+CIPMUX=1");
        esp->SendCMD(snd);
        esp->RcvReply(rcv, 400);
        
        return true;
    }
    
    bool ConnectToWifi(char * ssid, char * pw)
    {
        pc->printf("Receiving Wifi List\r\n");
        esp->GetList(rcv);
        pc->printf("%s", rcv);
        
        pc->printf("Connecting to AP\r\n");
        esp->Join(ssid, pw);
        esp->RcvReply(rcv, 1000);
        pc->printf("%s", rcv);
        wait(8);
        pc->printf("Getting IP\r\n");
        esp->GetIP(rcv);
        
        pc->printf("%s", rcv);
        
        return true;
    }
    
    void posaljiPoruku(char *poruka, int duzina, char *linkServera)
    {
        //to implement
    }
};