AxedaGo-mbed Demo - WIZwiki_W7500 or Platform+ W5500 (WIZnet) + Sensor (Photosell), This code tested on FRDM-KL25Z, LPCXpresso8324-MAX, NUCLEO_F103RB and WIZwiki_W7500

Dependencies:   AxedaGo-mbedNXP WIZnetInterface mbed

Fork of AxedaGo-mbed_WIZnetInterface by Soohwan Kim

Committer:
embeddist
Date:
Mon Jun 22 09:12:51 2015 +0000
Revision:
9:63548379bdc4
Parent:
8:4652dbf6461d
tested dhcp client and ethernet_link

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AxedaCorp 0:ab4e84579da0 1 #include "mbed.h"
AxedaCorp 0:ab4e84579da0 2 #include "EthernetInterface.h"
AxedaCorp 0:ab4e84579da0 3
embeddist 7:979a654458c7 4 AnalogIn pot1(A0);
embeddist 7:979a654458c7 5 AnalogIn pot2(A1);
embeddist 7:979a654458c7 6
AxedaCorp 0:ab4e84579da0 7 DigitalOut led1(LED1);
AxedaCorp 0:ab4e84579da0 8 DigitalOut led2(LED2);
AxedaCorp 0:ab4e84579da0 9 DigitalOut led3(LED3);
AxedaCorp 0:ab4e84579da0 10 DigitalOut led4(LED4);
embeddist 7:979a654458c7 11
embeddist 7:979a654458c7 12 #ifdef TARGET_WIZwiki_W7500
embeddist 7:979a654458c7 13 EthernetInterface eth;
embeddist 7:979a654458c7 14 #else
embeddist 7:979a654458c7 15
embeddist 7:979a654458c7 16 #if defined(TARGET_NUCLEO_F103RB)
embeddist 7:979a654458c7 17 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
embeddist 7:979a654458c7 18 EthernetInterface eth(&spi, PB_6, PA_9); // spi, cs, reset
embeddist 7:979a654458c7 19 #else //tested on FRDM-KL25Z, LPCXpresso8324-MAX
embeddist 7:979a654458c7 20 SPI spi(D11, D12, D13); // mosi, miso, sclk
embeddist 7:979a654458c7 21 EthernetInterface eth(&spi, D10, D9); // spi, cs, reset
embeddist 7:979a654458c7 22 #endif
embeddist 7:979a654458c7 23
embeddist 7:979a654458c7 24 #endif //#if defined TARGET_WIZwiki_W7500
embeddist 7:979a654458c7 25
embeddist 7:979a654458c7 26 TCPSocketConnection sock;
embeddist 7:979a654458c7 27
AxedaCorp 0:ab4e84579da0 28 int main()
AxedaCorp 0:ab4e84579da0 29 {
embeddist 9:63548379bdc4 30 //uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
embeddist 9:63548379bdc4 31 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x1D, 0xFE, 0x1B};
AxedaCorp 0:ab4e84579da0 32 char *MODEL = "mbed";
embeddist 8:4652dbf6461d 33 char *SERIAL_NUM = "input_your_serial_num";
embeddist 7:979a654458c7 34 float DEADBAND = 0.015;
AxedaCorp 0:ab4e84579da0 35 char* ip;
AxedaCorp 0:ab4e84579da0 36
AxedaCorp 3:359e019da3b9 37 int http_cmd_sz=800;
AxedaCorp 3:359e019da3b9 38 char http_cmd[http_cmd_sz];
AxedaCorp 3:359e019da3b9 39 int buffer_sz=300;
AxedaCorp 3:359e019da3b9 40 char buffer[buffer_sz];
AxedaCorp 0:ab4e84579da0 41 int returnCode = 0;
embeddist 7:979a654458c7 42
AxedaCorp 0:ab4e84579da0 43 led1 = 1;
AxedaCorp 0:ab4e84579da0 44 led2 = 1;
AxedaCorp 0:ab4e84579da0 45 led3 = 1;
AxedaCorp 0:ab4e84579da0 46 led4 = 1;
embeddist 7:979a654458c7 47
AxedaCorp 0:ab4e84579da0 48 printf("initializing Ethernet\r\n");
embeddist 7:979a654458c7 49 eth.init(mac_addr); //Use DHCP
AxedaCorp 0:ab4e84579da0 50 if ( returnCode == 0 )
AxedaCorp 0:ab4e84579da0 51 {
AxedaCorp 0:ab4e84579da0 52 printf(" - Ethernet ready\r\n");
AxedaCorp 0:ab4e84579da0 53 led1 = returnCode;
AxedaCorp 0:ab4e84579da0 54 }
AxedaCorp 0:ab4e84579da0 55 else
AxedaCorp 0:ab4e84579da0 56 {
AxedaCorp 0:ab4e84579da0 57 printf(" - Could not initialize Ethernet - ending\r\n");
AxedaCorp 0:ab4e84579da0 58 return 0;
AxedaCorp 0:ab4e84579da0 59 }
embeddist 9:63548379bdc4 60
embeddist 9:63548379bdc4 61 eth.ethernet_link();
embeddist 9:63548379bdc4 62 printf("eth.ethernet_link() %d \r\n", eth.ethernet_link());
AxedaCorp 0:ab4e84579da0 63
AxedaCorp 0:ab4e84579da0 64 printf("Ethernet.connecting \r\n");
AxedaCorp 0:ab4e84579da0 65 returnCode = eth.connect();
embeddist 9:63548379bdc4 66
AxedaCorp 0:ab4e84579da0 67 printf(" - connecting returned %d \r\n", returnCode);
AxedaCorp 0:ab4e84579da0 68 led2 = returnCode != -1 ? 0: 1;
AxedaCorp 0:ab4e84579da0 69 printf("Trying to get IP address..\r\n");
AxedaCorp 0:ab4e84579da0 70 ip = eth.getIPAddress();
AxedaCorp 0:ab4e84579da0 71 led3 = strlen(ip)<4 ? 1: 0;
AxedaCorp 0:ab4e84579da0 72 printf(" - IP address:%s\r\n", ip);
AxedaCorp 0:ab4e84579da0 73
AxedaCorp 3:359e019da3b9 74 float oil_level = 0.0;
AxedaCorp 4:22d6467147a5 75 float oil_level2= 0.0;
AxedaCorp 0:ab4e84579da0 76 float oldPotVal = -2.0;
AxedaCorp 4:22d6467147a5 77 float oldPotVal2 = -2.0;
AxedaCorp 0:ab4e84579da0 78
AxedaCorp 0:ab4e84579da0 79 while(1) {
AxedaCorp 3:359e019da3b9 80 oil_level = pot1.read();
embeddist 7:979a654458c7 81 oil_level2= pot2.read();
AxedaCorp 0:ab4e84579da0 82
AxedaCorp 4:22d6467147a5 83 if ( abs(oil_level - oldPotVal) < DEADBAND && abs(oil_level2 - oldPotVal2) < DEADBAND)
AxedaCorp 0:ab4e84579da0 84 {
AxedaCorp 0:ab4e84579da0 85 continue;
AxedaCorp 0:ab4e84579da0 86 }
AxedaCorp 0:ab4e84579da0 87 else
AxedaCorp 0:ab4e84579da0 88 {
AxedaCorp 0:ab4e84579da0 89 led4 = 1;
AxedaCorp 3:359e019da3b9 90 oldPotVal = oil_level;
AxedaCorp 4:22d6467147a5 91 oldPotVal2 = oil_level2;
AxedaCorp 4:22d6467147a5 92 printf("Sending Value for well1 %.2f\n\r", oil_level);
AxedaCorp 4:22d6467147a5 93 printf("Sending Value for well2 %.2f\n\r", oil_level2);
AxedaCorp 6:1f7647c5691a 94 sock.connect("toolbox-connect.axeda.com", 80);
AxedaCorp 0:ab4e84579da0 95
AxedaCorp 4:22d6467147a5 96 snprintf(http_cmd, http_cmd_sz, "POST /ammp/data/1/%s!%s HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 65\r\n\r\n{\"data\":[{\"di\":{\"oil_level\":%.2f, \"oil_level2\":%.2f}}]}\r\n\r\n", MODEL, SERIAL_NUM, oil_level, oil_level2);
AxedaCorp 5:1b8ad120cf29 97 sock.send_all(http_cmd, http_cmd_sz-1);
AxedaCorp 0:ab4e84579da0 98
AxedaCorp 5:1b8ad120cf29 99 while ( (returnCode = sock.receive(buffer, buffer_sz-1)) > 0)
AxedaCorp 0:ab4e84579da0 100 {
AxedaCorp 0:ab4e84579da0 101 buffer[returnCode] = '\0';
AxedaCorp 0:ab4e84579da0 102 printf("Received %d chars from server:\n\r%s\n", returnCode, buffer);
AxedaCorp 0:ab4e84579da0 103 }
AxedaCorp 0:ab4e84579da0 104 led4 = returnCode;
AxedaCorp 0:ab4e84579da0 105 sock.close();
AxedaCorp 0:ab4e84579da0 106 }
AxedaCorp 0:ab4e84579da0 107
AxedaCorp 0:ab4e84579da0 108 }
AxedaCorp 0:ab4e84579da0 109
AxedaCorp 0:ab4e84579da0 110 }