Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SparkFunESP8266 shield #6

Open
braicauc opened this issue Mar 8, 2016 · 10 comments
Open

SparkFunESP8266 shield #6

braicauc opened this issue Mar 8, 2016 · 10 comments

Comments

@braicauc
Copy link

braicauc commented Mar 8, 2016

I have a SparkFunESP8266 and I try to user Socket.io to send and receive instant messages.

I-am using #include <SparkFunESP8266WiFi.h> to connect to my wifi network... works fine ...

I use in my setup section the code:

client.setDataArrivedDelegate(ondata);
   if (!client.connect(server, port)){
        Serial.println(F("Not connected."));
   }

and I receive "Not connected" ...
I open the SocketIOClient.cpp and on the ::connect i see that function ::sendHandshake(hostname); witch works fine and I receive the following answer:

+IPD,0,357:HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 100
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: Arduino
Set-Cookie: io=1bO6tm54TbT7t-QtAAAC
Date: Mon, 07 Mar 2016 04:01:45 GMT
Connection: keep-alive

and the socket.io server detects that there is a new connection ...

The problem comes in the next line of the ::connect on ::readHandshake();

... this section of code:


if (atoi(&databuffer[9]) != 200) {
      while (client.available()) readLine();
      client.stop();
      return false;
   }

always return false and the program stop working.

I use on my server node -v : 4.2.2 and [email protected]

I do not know c++ to work with SocketIOClient.cpp

Can you help me ?

@washo4evr
Copy link
Owner

could you post the code for both esp8266 and node.js please?

@braicauc
Copy link
Author

braicauc commented Mar 9, 2016

SparkFunESP8266Client.cpp

/******************************************************************************
SparkFunESP8266Client.cpp
ESP8266 WiFi Shield Library Client Source File
Jim Lindblom @ SparkFun Electronics
Original Creation Date: June 20, 2015
http://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library

!!! Description Here !!!

Development environment specifics:
    IDE: Arduino 1.6.5
    Hardware Platform: Arduino Uno
    ESP8266 WiFi Shield Version: 1.0

This code is beerware; if you see me (or any other SparkFun employee) at the
local, and you've found our code helpful, please buy us a round!

Distributed as-is; no warranty is given.
******************************************************************************/

#include "SparkFunESP8266WiFi.h"
#include <Arduino.h>
#include "util/ESP8266_AT.h"
#include "SparkFunESP8266Client.h"

ESP8266Client::ESP8266Client()
{
}

ESP8266Client::ESP8266Client(uint8_t sock)
{
    _socket = sock;
}

uint8_t ESP8266Client::status()
{
    return esp8266.status();
}

int ESP8266Client::connect(IPAddress ip, uint16_t port)
{
    return connect(ip, port, 0);
}

int ESP8266Client::connect(const char *host, uint16_t port)
{
    return connect(host, port, 0);
}

int ESP8266Client::connect(String host, uint16_t port, uint32_t keepAlive)
{
    return connect(host.c_str(), port, keepAlive);
}

int ESP8266Client::connect(IPAddress ip, uint16_t port, uint32_t keepAlive) 
{
    char ipAddress[16];
    sprintf(ipAddress, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);

    return connect((const char *)ipAddress, port, keepAlive);
}

int ESP8266Client::connect(const char* host, uint16_t port, uint32_t keepAlive) 
{
    _socket = getFirstSocket();

    if (_socket != ESP8266_SOCK_NOT_AVAIL)
    {
        esp8266._state[_socket] = TAKEN;
        int16_t rsp = esp8266.tcpConnect(_socket, host, port, keepAlive);

        return rsp;
    }
}

size_t ESP8266Client::write(uint8_t c)
{
    return write(&c, 1);
}

size_t ESP8266Client::write(const uint8_t *buf, size_t size)
{
    return esp8266.tcpSend(_socket, buf, size);
}

int ESP8266Client::available()
{
    int available = esp8266.available();
    if (available == 0)
    {
        // Delay for the amount of time it'd take to receive one character
        delayMicroseconds((1 / esp8266._baud) * 10 * 1E6);
        // Check again just to be sure:
        available = esp8266.available();
    }
    return esp8266.available();
}

int ESP8266Client::read()
{
    return esp8266.read();
}

int ESP8266Client::read(uint8_t *buf, size_t size)
{
    if (esp8266.available() < size)
        return 0;

    for (int i=0; i<size; i++)
    {
        buf[i] = esp8266.read();
    }

    return 1;
}

int ESP8266Client::peek()
{
    return esp8266.peek();
}

void ESP8266Client::flush()
{
    esp8266.flush();
}

void ESP8266Client::stop()
{
    esp8266.close(_socket);
    esp8266._state[_socket] = AVAILABLE;
}

uint8_t ESP8266Client::connected()
{
    // If data is available, assume we're connected. Otherwise,
    // we'll try to send the status query, and will probably end 
    // up timing out if data is still coming in.
    if (_socket == ESP8266_SOCK_NOT_AVAIL)
        return 0;
    else if (available() > 0)
        return 1;
    else if (status() == ESP8266_STATUS_CONNECTED)
        return 1;

    return 0;
}

ESP8266Client::operator bool()
{
    return connected();
}

// Private Methods
uint8_t ESP8266Client::getFirstSocket()
{
    /*
    for (int i = 0; i < ESP8266_MAX_SOCK_NUM; i++) 
    {
        if (esp8266._state[i] == AVAILABLE)
        {
            return i;
        }
    }
    return ESP8266_SOCK_NOT_AVAIL;
    */
    esp8266.updateStatus();
    for (int i = 0; i < ESP8266_MAX_SOCK_NUM; i++) 
    {
        if (esp8266._status.ipstatus[i].linkID == 255)
        {
            return i;
        }
    }
    return ESP8266_SOCK_NOT_AVAIL;
}

arduino.js (node server) ... very simple ... just for test

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );


var app = express();
var server = http.createServer( app );

var io = socket.listen( server );




io.sockets.on( 'connection', function( client ) {

console.log("We have a new client");

client.on( 'message', function( data ) {
    console.log(data.ard);
});


});

server.listen(4540);

@washo4evr
Copy link
Owner

you need to include SocketIOClient.h too at the beginning.
I have not tried such shields myself, Im not sure
I guess you are only doing serial connection between the ESP and Arduino
Another user reported that it does not work with a uno an a ESP8266 shield

I strongly recommend getting a ESP8266 development board.

the node code looks good

@braicauc
Copy link
Author

braicauc commented Mar 9, 2016

My sketch:

#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <SocketIOClient.h>

int connectedWF = 0;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "firma-transport.ro";
int port = 4540;
SocketIOClient client;


extern String RID;
extern String Rname;
extern String Rcontent;
unsigned long previousMillis = 0; 
long interval = 10000; 

void setup() {

   Serial.begin(9600);

   if (esp8266.begin()) {
       Serial.println("ESP8266 ready to go!");
       connectToWifi();
   }
   else {
       Serial.println("Unable to communicate with the ESP8266 :(");
   }

   client.setDataArrivedDelegate(ondata);
   if (!client.connect(server, port)){
        Serial.println(F("Not connected."));
   }


}



void loop() {

   if ( connectedWF == 0 ) {
        connectToWifi();
   }


client.monitor();

delay(2000);

}


void ondata(SocketIOClient client, char *data) {
  Serial.print(data);
}


void connectToWifi() {
  int retVal;
   retVal = esp8266.connect("ssid", "pass");
   if (retVal < 0)
      { 
          Serial.println("Not connected");                   
      }
      else {
        IPAddress myIP = esp8266.localIP(); 
        Serial.println("Connected to internet OK"); 
        connectedWF = 1;         
      }   
}    

@washo4evr
Copy link
Owner

I just read your code.
I think it will be easier if you start from my example and just tweak it to adjust for sparkfun
or you need to add to your code in setup

if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
if (client.connected())
{
client.send("connection", "message", "Connected !!!!"); //you can replace the connection message
}

and
client.send("atime", "message", "Time please?");
inside loop
Keep in mind that another user already tried.

@washo4evr
Copy link
Owner

could you try the new example I just uploaded?

Hello_time_sparkfun.ino
(please adjust port for your socket server)

@washo4evr
Copy link
Owner

does it work?

@braicauc
Copy link
Author

No....

The comunication exists but because I do not know C++ a dont know how to modify the function

readHandshake() to read the headers corect and return true ...

@washo4evr
Copy link
Owner

I dont have a module like yours so it will be hard for me to implement it.
if the function doesnt return true, it means that something went wrong.
it checks for return code 200
you could just remove if (atoi(&databuffer[9]) != 200) {
while (client.available()) readLine();
client.stop();
return false;
} to "disable the check

@braicauc
Copy link
Author

atoi(&databuffer[9]) return 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants