I have some ask for using SparkFun ESP8266 WiFi shield with TCP/IP commands
i have question of using arduino uno (sparkfun) + sparkfun wifi shield (esp8266)
i have winsocket server.
and using arduino ide
i didn't find sparkfun esp8266 wifi shield commands.
how can programming client ?
ps. can't using english. sorry ! plz understand. !
i have winsocket server.
code: [select]
#pragma comment(lib, "ws2_32")
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#define serverport 7000
#define messageboxw
void err_quit(char*msg)
{
lpvoid ipmsgbuf;
formatmessage(
format_message_allocate_buffer |
format_message_from_system,
null, wsagetlasterror(),
makelangid(lang_neutral, sublang_default),
(lptstr)&ipmsgbuf, 0, null);
messagebox(null, (lpctstr)ipmsgbuf, msg, mb_iconerror);
localfree(ipmsgbuf);
exit(1);
}
void err_display(char *msg)
{
lpvoid ipmsgbuf;
formatmessage(
format_message_allocate_buffer|
format_message_from_system,
null, wsagetlasterror(),
makelangid(lang_neutral, sublang_default),
(lptstr)&ipmsgbuf, 0, null);
printf("[%s] %s", msg, (char *)ipmsgbuf);
localfree(ipmsgbuf);
}
int main(int argc, char *argv[])
{
int retval;
float s_data, r_data;
wsadata wsa;
if (wsastartup(makeword(2, 2), &wsa) != 0)
return 1;
//socket()
socket listen_sock = socket(af_inet, sock_stream, 0);
if (listen_sock == invalid_socket) err_quit("socket()");
//bind()
sockaddr_in serveraddr;
zeromemory(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = af_inet;
serveraddr.sin_addr.s_addr = htonl(inaddr_any);
serveraddr.sin_port = htons(serverport);
retval = bind(listen_sock, (sockaddr *)&serveraddr, sizeof(serveraddr));
if (retval == socket_error) err_quit("bind()");
//listen()
retval = listen(listen_sock, somaxconn);
if (retval == socket_error) err_quit("listen()");
socket client_sock;
sockaddr_in clientaddr;
int addrlen;
int i;
while (1)
{
//accept()
addrlen = sizeof(clientaddr);
client_sock = accept(listen_sock, (sockaddr *)&clientaddr, &addrlen);
if (client_sock == invalid_socket)
{
err_display("accept()");
break;
}
printf("\n[tcp server] client connect:\n");
while (1)
{
retval = recv(client_sock, (char *)&r_data, sizeof(&r_data), 0);
if (retval == socket_error)
{
err_display("recv()");
break;
}
else if (retval == 0)
break;
printf("recive data =%f \n", r_data);
srand(rand());
s_data = (float)(rand() / 654.0); /**** 0 32767 *****/
retval = send(client_sock,(char*)&s_data, sizeof(&s_data), 0);
printf("send data = %f \n\n", s_data);
if(retval == socket_error)
{
err_display("send()");
break;
}
}//while(1) - client data comm.
//closesocket()
closesocket(client_sock);
printf("[tcp server] client end \n");
}//while(1) - server
//closesocket()
closesocket(listen_sock);
wsacleanup();
return 0;
}
and using arduino ide
code: [select]
#include <softwareserial.h>
#include <sparkfunesp8266wifi.h>
const char myssid[] = "cob";
const char mypsk[] = "*********";
const char* serverip = "192.168.0.**";
int i;
float s_data, r_data;
void setup()
{
int status;
serial.begin(9600);
status = esp8266.begin();
if (status <= 0)
{
serial.println(f("unable communicate shield. looping"));
while(1) ;
}
esp8266.setmode(esp8266_mode_sta); // set wifi mode station
if (esp8266.status() <= 0) // if we're not connected
{
if (esp8266.connect(myssid, mypsk) < 0)
{
serial.println(f("error connecting"));
while (1) ;
}
}
serial.print(f("my ip address is: "));
serial.println(esp8266.localip());
}
i didn't find sparkfun esp8266 wifi shield commands.
how can programming client ?
ps. can't using english. sorry ! plz understand. !
well if want low level can go at commands
otherwise the library here. can see examples or @ public methods exposed esp8266
Arduino Forum > Using Arduino > Programming Questions > I have some ask for using SparkFun ESP8266 WiFi shield with TCP/IP commands
arduino
Comments
Post a Comment