Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of MBED_SERVER_SETUP by
main.cpp
- Committer:
- muyiq
- Date:
- 2016-06-17
- Revision:
- 5:1056f0dfb3ba
- Parent:
- 4:03a5f1c0e49a
- Child:
- 6:01a64a6bc702
File content as of revision 5:1056f0dfb3ba:
#include "mbed.h"
//#include "TSISensor.h"
#define DEBUG
#define INFOMESSAGES
#define WARNMESSAGES
#define ERRMESSAGES
#define FUNCNAME "MAIN"
#ifdef NoDEBUG
#define DBG(x, ...) pc.printf(" ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
#else
#define DBG(x, ...)
#endif
#ifdef ERRMESSAGES
#define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
#else
#define ERR(x, ...)
#endif
#ifdef WARNMESSAGES
#define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
#else
#define WARN(x, ...)
#endif
#ifdef INFOMESSAGES
#define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
#else
#define INFO(x, ...)
#endif
#define BUFF_SIZE 1024
// PIN DEFS NOW FOR OLD FIRMWARE VERSION OF THE KL25Z
Serial pc(USBTX, USBRX);
//RawSerial dev(D1, D0);
Serial dev(PTE22,PTE23); //tx,rx
DigitalOut led1(LED1);
DigitalOut led4(LED3); // CHANGE TO LED 3
DigitalOut reset(PTB0,1);
PwmOut led(LED_GREEN);
// TSISensor tsi;
volatile int state=0;
volatile int ready=0;
char ipAddress[20];
char macAddress[32];
char pedometer_channel[1];
char *buffer;
unsigned int bufferPnt=0;
void dev_recv()
{
char c;
int count = 0;
led1 = !led1;
if(bufferPnt==0) {
memset(buffer,0,BUFF_SIZE);
}
while(dev.readable()) {
c = (char)dev.getc();
#ifdef DEBUG
pc.putc(c);
#endif
buffer[bufferPnt]=c;
bufferPnt++;
if (bufferPnt>1000) {
ready=1;
}
// if ((c==0x0a)||(c==0x0d)){
// ready=1;
// }else
if (c==0x0a) {
if (bufferPnt>1) {
if (buffer[bufferPnt -2]==0x0d) {
ready=1;
break;
}
}
}
if (!dev.readable()) {
wait_us(10);
}
}
}
void pc_recv()
{
char c;
led4 = !led4;
while(pc.readable()) {
c=(char)pc.getc();
dev.putc(c);
pc.putc(c);
if(c==13) {
dev.putc(10);
pc.putc(10);
}
}
}
char * OKResponse(char *test, const char *pattern)
{
char *p= strstr(test,pattern);
if (p==NULL) {
// DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
return NULL;
} else {
// DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p);
}
return p;
}
int main()
{
float touch;
buffer=(char *)calloc(BUFF_SIZE,1);
reset=0;
wait(3.0);
reset=1;
int counter=0;
pc.baud(115200);
dev.baud(115200);
pc.attach(&pc_recv, Serial::RxIrq);
dev.attach(&dev_recv, Serial::RxIrq);
pc.printf("Start up\n\r");
char * resp=NULL;
char *dummy;
pc.printf("Here \n\r");
while(1) {
if (ready) {
ready=0;
bufferPnt=0;
INFO("[%d],##%s##",state,buffer);
switch (state) {
case 0: {
resp=OKResponse(buffer,"WIFI GOT IP");
if (resp!=NULL) {
wait(1);
dev.printf("AT\r\n");
state++;
}
break;
}
case 1:
case 2: {
resp=OKResponse(buffer,"OK");
if (resp!=NULL) {
dev.printf("AT\r\n");
state++;
}
break;
}
case 3: {
resp=OKResponse(buffer,"OK");
if (resp!=NULL) {
dev.printf("AT+RST\r\n");
state++;
}
break;
}
case 4: {
resp=OKResponse(buffer,"WIFI GOT IP");
if (resp!=NULL) {
dev.printf("AT+CWMODE=1\r\n");
state++;
}
break;
}
case 5: {
resp=OKResponse(buffer,"OK");
if (resp!=NULL) {
dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n");
state++;
}
break;
}
case 6: {
resp=OKResponse(buffer,"OK");
if (resp!=NULL) {
wait(1);
dev.printf("AT+CIFSR\r\n");
state++;
}
break;
}
case 7: {
resp=OKResponse(buffer,"+CIFSR:STAIP,");
if (resp!=NULL) {
char *strt = strtok(buffer,"\"");
strcpy(ipAddress,strtok(NULL,"\""));
strtok(NULL,"\"");
strcpy(macAddress,strtok(NULL,"\""));
INFO("mac Address = %s", macAddress);
INFO("IP Address = %s", ipAddress);
dev.printf("AT+CIPMUX=1\r\n");
state++;
}
break;
}
case 8: { //initialize server
resp=OKResponse(buffer,"OK");
if (resp!=NULL) {
INFO("Ready");
dev.printf("AT+CIPSERVER=1,8080\r\n");
state++;
}
}
break;
case 9: { //gets pedometer channel
resp=OKResponse(buffer,"PEDOMETER");
if (resp!=NULL) {
INFO("Finding pedometer channel2");
char *strt = strtok(buffer,",");
strcpy(pedometer_channel,strtok(NULL,","));
strtok(NULL,",");
strcpy(macAddress,strtok(NULL,","));
INFO("filler = %s", macAddress);
INFO("pedometer_channel = %s", pedometer_channel);
// pedometer_channel = strtol(ipAddress, NULL, 10);
// INFO("Pedometer Channel = %s", ipAddress);
state++;
}
}
case 10: {
if (resp!=NULL) {
INFO("Testing connection");
state++;
dev.printf("AT+CIPSEND=%s,6\r\n",pedometer_channel);
}
break;
}
case 11: {
resp=OKResponse(buffer,">");
if (resp!=NULL) {
INFO("SENDING");
state++;
dev.printf("Hi ed!\r\n");
state=9;
}
}
break;
}
}
}
//__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY
}
//}
