Thursday, May 22, 2014

Arduino Code(1) - Web/Arduino/Adam

The basic tutorial for WebSocket:

The code that I used for WebSocket is from the following site:

Now back to the original TOPIC.

Code for Arduino WebSocket

#include <SPI.h>
#include <Ethernet.h>
#include <WebSocket.h> 

Ones that must be defined
/* Mac Address
  Each Ethernet Shield has different mac address. 
  Check your board.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x25, 0xC4 };

/* IP Address
  Arduino becomes the server, and it needs fixed IP.
  It doesn't necessarily need global IP, it just needs fixed IP.
  Most LAN connections use DHCP, so use an Local IP address that 
  DHCP doesn't use.
     Open command promp and type ipconfig /all
     Check the IP Address for the current PC. 
     Keeping the same Subnet Mask, try pinging IP addresses.
     If you find the IP address that doesn't answer the ping,
     use that IP address.
     If someone around you gets pissed off because their internet 
     doesn't work, change the IP address quickly and quietly before
     he notices you.
byte ip[] = { 192, 168, 100, 11 };

Create WebSocket Callback
  There are 3 Callbacks in WebSocket.h.
  You should be able to guess what they are for just by looking at
  there names.

    typedef void DataCallback(WebSocket &socket, char* socketString, byte frameLength);
    typedef void Callback(WebSocket &socket);

    void registerDataCallback(DataCallback *callback);
      // Called when received Data
    void registerConnectCallback(Callback *callback);
      // when WebSocket is connected
    void registerDisconnectCallback(Callback *callback);
      // when WebSocket is disconnected
WebSocket wsServer;
void onData(WebSocket &socket, char* socketString, byte frameLength) { }
void onConnect(WebSocket &socket) { }
void onDisconnect(WebSocket &socket) { }

Code inside setup()
void setup() {
 Ehternet.begin(mac, ip);
 // Give Ethernet Shield some time to get ready.

Code inside loop()
void loop() {

 if(wsServer.isConnected()) {
  wsServer.send("abc123", 6);

  listen() needs to be called continuously.
  If data is received onData() gets called.
  If you want to send data send(char *str, byte length);

RS-485 Connection Code

#include <SoftwareSerial.h>

Ones that must be defined
SoftwareSerial Soft485(3, 4);
int DE485 = 2;

  Pin 3: RX
  Pin 4: TX
  Pin 2: DE This was declared in 
      RS485 Shield - Web/Arduino/Adam posting.

Code inside setup()
void setup() {
 pinMode(DE485, OUTPUT);

  ADAM-4050 is 9600 bps. (ADAM - Web/Arduino/Adam posting)

No comments:

Post a Comment