Connect MSP430 Throught Internet (MSP430F2274) with Ethernet Shield

Hi, in this post I will to document my job to send data throught TCP/IP protocol and a texas microcontroller.

The first step is have a Texas Micro MSP430, I used a board manufactured by a mexican company called Scalini http://indscalini.com/ this board is dedicated for academic purposes with integrated leds, push buttons, serial interface, pins in male and female, also a lcd support. The question is: Why do we use an Arduino board and stop keeping the problems about to use an Texas board? The answer is the use of this board for industrial purposes, the main objective is to communicate to cistern with a water well located along 600 meters, where the water pump have a voltage of 440v. This board can approach to level of quality required.

The main microcontroller is MSP430F2274 and for misfortune Energía IDE does not work in this board. Then we need to use Texas Code Composer Studio in C language (basic programming) if you are not so good in this programming language don't worry, basic-noob skills is required only you need understand pointers, structures, macros and flags (assembly language basics).

The board operates in 3.3v, if you want to turn on/off a 5v relay throught internet or with the board alone,only feed that relay with 3.3v and it will works.

Anyway for flash firmware to board we need a "programmer board" like another compatible board, in this case I am using a launchpad board.



The pins used are
  • RST - GREEN
  • TEST - BLUE
  • GROUND - RED (yes I know VCC normally is)
  • VCC - GREY 
If you see, this is a spywire communication.

It is the same way if you are using a Launch Pad or another MSP430 microcontroller family

The next step is connect ENC28J60 Ethernet Shield to Scainni board.
The communication is with SPI (included in library):

ENC28J60-HMSP430F2274PIN IN MSP430F2274 BOARD
GNDGND
VCCRST - 3.3v
P2.0 CSP2.0/ACLK/A0/OA0I0PIN 8
UCB0SOMI MISOP3.2/UCB0SOMI/UCB0SCL PIN 13
UCB0CLK SCKP3.3/UCB0CLK/UCA0STEPIN 14
USB0SIMO MOSIP3.1/UCB0SIMO/UCB0SDAPIN 12





With  Code Composer Studio create a new CCS Project or import my Github project and start it.






In the file main.c you will see the call to the IPStackInit() function, which send arp request to router and initialize mac address and something else, this function is essential for enable communication.

The infinite cycle is dedicated for send data, but in this ocassion we are going to limit to send only once (With CCS debugger).

The url, reply buffer, ping and data must be ignorated.

#include <msp430.h>
#include "ipstack.h"

const char* url = "192.168.3.150";
const char* data = "GET /admin/sensor/inuse/?id=5f3ee97228dab60a888b7128 HTTP/1.1\r\nHost: 192.168.3.150\r\n\r\n";
char reply[] = "SUCCESS";

int main(void){
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  IPstackInit();
 // unsigned char target[] = {192,168,3,1};
  //SendPing(target);

  while(1){
      IPstackHTMLPost(url, data, reply );
      //IPstackIdle();
  }
}

The method IPstackHTMLPost is dedicated for build TCP/IP package and manage ACK-ACK-SYN-FIN process I'm going to try to explain as the best way possible.

We need to configure the MICRO MAC ADDRESS, MICRO IP, ROUTER MAC ADDRESS AND ROUTER IP AND SERVER IP(also server mac address if you can). If you are not available for determine MAC, you will need to programming the part of ARP, the SendArpPacket method can do that, only you need to say him the IP and will retrieve the MAC in sender mac.

Or check the original code from Duncan Pumpkin where he worked with WAN.

The topology used is this:


// MAC address of the enc28j60
const unsigned char deviceMAC[6] = {0x00,0xa0,0xc9,0x14,0xc8,0x00};
// Router MAC is not known at beginning and requires an ARP reply to know.
//Establecer mac del router (access point) a conectar //todo:automatizar esto con ARP.
unsigned char routerMAC[6] = {0xf4,0xdc,0xf9,0xb2,0x83,0xfe};
// IP address of the enc28j60
unsigned char deviceIP[4] = {192,168,3,100};
// IP address of the router
unsigned char routerIP[4] = {192,168,3,1};

unsigned char serverIP[4] = {192,168,3,150};

unsigned char serverMAC[6] = {0x94, 0x65, 0x9c, 0xf3, 0xd9, 0xf0};

unsigned char dnsIP[4] = {8,8,8,8};


The core of the project is this function

/*
 * three way handshake (SYN, SYN/ACK, ACK)
 *
 * */

int IPstackHTMLPost(const char* url, const char* data, char* reply)
{  
/*A buffer where is located the packet*/
unsigned char packet[MAXPACKETLEN];

memset(packet,0,sizeof(TCPhdr));//zero the memory as we need all flags = 0

//Set up ethernet/ip/tcp headers
SetupBasicIPPacket( packet, TCPPROTOCOL, serverIP );
//set up tcp header

TCPhdr* tcp = (TCPhdr*)packet;
/*Assign the source port (MSP430) to 58071 (MACRO HTONS CONVERT HOST TO NETWORK)*/
tcp->sourcePort = HTONS(0xe2d7);
/*Assign destiny port to 80(http)*/
tcp->destPort = HTONS(80);
//Seq No and Ack No are zero
tcp->seqNo[0] = 0;
/*DETERMINE THE LENGTH OF HEADER TCPHEADER-IPHEADER/4*/
tcp->hdrLen = (sizeof(TCPhdr)-sizeof(IPhdr))/4;
/*TURN ON THE SYNC FLAG*/
tcp->SYN = 1;
/*ASSIGN THE WINDOWING SIZE TO 200*/
tcp->wndSize = HTONS(200);
//Add in some basic options (are not necessary I think so that is why are commented)
//unsigned char opts[] = { 0x02, 0x04,0x05,0xb4,0x01,0x01,0x04,0x02};
//memcpy(&tcp->options[0],&opts[0],8);
//DETERMINE THE NEW LENGTH OF TCP HEADER
unsigned int len = sizeof(TCPhdr);
/*GENERATE CHECKSUM*/
tcp->ip.len = HTONS(sizeof(TCPhdr)-sizeof(EtherNetII));
int pseudochksum = chksum(TCPPROTOCOL+len-sizeof(IPhdr), tcp->ip.source, 8);
tcp->chksum = HTONS(~(chksum(pseudochksum,packet + sizeof(IPhdr), len-sizeof(IPhdr))));
tcp->ip.chksum = HTONS(~(chksum(0, packet+sizeof(EtherNetII), sizeof(IPhdr)-sizeof(EtherNetII))));

/*SEND FIRST PACKAGE SYN (HELLO PACKAGE)*/
MACWrite(packet,len);
//ack syn/ack
//get the answer from port 80 server (it must be a syn package too)
GetPacket(TCPPROTOCOL, packet);
if(!(tcp->destPort==HTONS(0xe2d7))) return;
/*follow the acknowledge process*/
ackTcp( tcp, len );
/*send the package with acknowledge data configured*/
MACWrite( packet, len );

//Now we must to send the actual data (http protocol data)
//first we need change to a push (PUSH FLAG ON)
tcp->PSH = 1;
//tcp->ip.ident = 0xDEED;
//Now copy in the data in the end of package (according tcp headers)
/*here we can send whatever you want thought TCP/IP PROTOCOL in this case is an HTTP DATA*/
data = "GET /s.php/?s=1500 HTTP/1.1\r\nHost: 192.168.3.150\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nUpgrade-Insecure-Requests: 1\r\n\r\n";
int datalen = strlen(data);
memcpy( packet + sizeof(TCPhdr), data, datalen);
len = sizeof(TCPhdr) + datalen;
/*determine the new length of package*/
tcp->ip.len = HTONS(len-sizeof(EtherNetII));
tcp->chksum = 0;
tcp->ip.chksum = 0;
pseudochksum = chksum(TCPPROTOCOL+len-sizeof(IPhdr),tcp->ip.source,sizeof(deviceIP)*2);
tcp->chksum = HTONS(~(chksum(pseudochksum, packet + sizeof(IPhdr),len-sizeof(IPhdr))));
tcp->ip.chksum = HTONS(~(chksum(0, packet + sizeof(EtherNetII), sizeof(IPhdr)-sizeof(EtherNetII))));
tcp->ACK = 1;

/*SEND THE PACKAGE WITH HTTP WE NEED RESPONSE*/
MACWrite(packet, len);

//if we have response
  GetPacket(TCPPROTOCOL, packet);
if(!(tcp->destPort==HTONS(0xe2d7))) return;//--this line determine if package was resend by server
memcpy( reply, packet + len -7, 7);
ackTcp(tcp, len);
MACWrite( packet, len);

/*Finish communication FIN FLAG ON*/

tcp->PSH=0;
tcp->FIN=1;
tcp->ACK=1;
tcp->chksum = 0;
tcp->ip.chksum = 0;
pseudochksum = chksum(TCPPROTOCOL+len-sizeof(IPhdr), tcp->ip.source, sizeof(deviceIP)*2);
tcp->chksum = HTONS(~(chksum(pseudochksum,packet + sizeof(IPhdr), len-sizeof(IPhdr))));
tcp->ip.chksum = HTONS(~(chksum(0, packet + sizeof(EtherNetII), sizeof(IPhdr)-sizeof(EtherNetII))));

/*SEND PACKAGE WITH FIN*/
MACWrite(packet,len);
  return 0;
}

If you watch the code carefully, you will notice that the instructions build step by step the TCP / IP package, certeanly, if you understand this you are located in the most deep part of internet his core, for well, if you are a novice and you do not know anything about networks and especially network fundamentals, you are going to stay lost because this is not easy.This is not arduino and but I can sure you that you will learn much longer that an average arduino developer. Also we must mention the actual arduino libraries developers, they are making an awesome job. But if you are using texas micro, this will work for you.
/*
In this part we are sending a GET request to php server, where the value of the sensor or another this we want to send is established in s variable 
with a value of 500 to 192.168.1.150 server
*/
data = "GET /s.php/?s=1500 HTTP/1.1\r\nHost: 192.168.3.150\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nUpgrade-Insecure-Requests: 1\r\n\r\n";
int datalen = strlen(data);
memcpy( packet + sizeof(TCPhdr), data, datalen);


Next we need to debug code in CCS and run step by step, soon as possible if you are using Wireshark for package monitoring you will see the packages and the data
You need to check the TCP/IP headers and Three way handshake to clearify the process.

In the server I developed an script in php (XAMPP) that when a GET request come, save the data to file.

<?php

 if(isset($_GET["s"])){
  echo "success!";
  $data = $_GET["s"];
  $myfile = fopen("sensordata.txt", "w");
  fwrite($myfile, $data);
  fclose($myfile);
 }

 else echo "nothing";
 

?>

Well finally we have composer studio and flash firmware.   After flash an debug step by step we obtain this:

The response is OK



https://github.com/zesteros/MSP430-Enc28j60-Driver-and-Basic-Web-Client

Comentarios

  1. Casino Poker Rules
    The minimum 카카오 스포츠 limits of poker room limit is $10. Texas Holdem Poker is only 슬롯추천 $10 minimum 원피스 바카라 limit on Texas Holdem Holdem 토토 사이트 검증 tournaments and it's 통장 협박 대처법

    ResponderEliminar
  2. The lowest betting limit for the poker room is $10. Specifically, Texas Hold'em Poker tournaments have a minimum betting limit of $10, with other games having similar restrictions. tech





    ResponderEliminar
  3. The lowest limit for poker room stakes is $10 tech



    ResponderEliminar

Publicar un comentario