for znrobotics workshop

Dependencies:   DHT22 HTTPClient SDFileSystem WIZnet_Library mbed

Fork of archlink_Temperture_dweetio by Kevin Lee

This program is for seeed arch link, using wiznet w550 ethernet interface. not compatible with mbed ethernet interface.

main.cpp

Committer:
menggang
Date:
2016-06-16
Revision:
5:35f80d88820f
Parent:
4:81af9a9f7844
Child:
6:dbf5cf5ca7b1

File content as of revision 5:35f80d88820f:

/* This is just a demo by Kevin Lee */
#include "mbed.h"
#include "SDFileSystem.h"
#include "WIZnetInterface.h"
#include "HTTPClient.h"
#include "DHT22.h"

#define USE_DHCP 0
#define LOOPBACKPORT  5000

int W5500_Test(void);

float* temperature_get();         /* Grove - Temperature Sensor V1.2 */
float th[2];

SPI spi(SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0);
WIZnetInterface ethernet(&spi, p24, p17); // Spi ,cs, reset
int ret, dummy, lv = 1;
const char * IP_Addr      = "192.168.1.123";
const char * IP_Subnet    = "255.255.255.0";
const char * IP_Gateway   = "192.168.1.1";
const char * DEVICE_ID    = "933691ed-1f00-4826-953a-d56841f65240";
unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x1C,0xAA,0xCA};

const int time_interval   = 10; // 10 seconds;

// timer event;
Timeout flipper;
void timer_ticked();

DigitalOut myled(LED1);
//Arch Link
Serial pc(USBTX, USBRX);  /* uart */
DHT22 dht22(p6);

AnalogIn   ain(A0);

int main() {
    char buffer[256];
    wait(1);
    pc.baud(9600);
    wait(1);
    
    if(W5500_Test() == 0) {                  // Internet is ok
        printf("W5500 tested OK \r\n");
        
        // start timer tick event.
        flipper.attach(&timer_ticked, timer_interval);
        
        char str[512];
        char post_data[256]= "";
        
        while(1)
        {
            float* th = temperature_get();
            printf("temperature: %.2f -- Humidity: %.2f.\r\n",th[0], th[1]);
            
            pc.printf("Posting message to znrobotics server.\r\n");
            // http://machine.address:port/did/temperature?param1=v1&param2=v2
            sprintf(post_data, "http://192.168.1.222:3000/temperature?deviceid=%s&temperature=%.2f", DEVICE_ID, th[0]);
            
            HTTPClient http;
            ret = http.get(get_msg, str, sizeof(str));
            if(!ret)
            {
                pc.printf("Result: %s\r\n", str);
            }
            else
            {
                pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
            }
            wait(10);
       }
    }
    
    while(1) {
        myled = 1;
        printf("light on\r\n");
        wait(3);
        myled = 0;
        printf("light off\r\n");
        wait(3);
    }
}

void timer_ticked()
{
    
}

int W5500_Test(void)
{
    mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
    wait(1);
    printf("Start to test ethernet!\r\n");
    
    #if USE_DHCP
    printf("use DHCP\r\n");
    ret = ethernet.init(MAC_Addr);
    #else
    printf("do NOT use DHCP\r\n");
    int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
    #endif
    
    if (!ret) {
        pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
        ret = ethernet.connect();
        if (!ret) {
            pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
                      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
            return 0;
        } else {
            pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
            //exit(0);
            return -1;
        }
    } else {
        pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
        //exit(0);
        return -1;
    }
}

float* temperature_get()
{
    int error = 0;
    
    float temp, hum;
    
    error = dht22.sample();
    
    // read successfully
    if (1 == error) {
        // YOUR CODE GOES HERE, read temperature and humidity
        temp    = dht22.getTemperature()/10.0f; //TODO;
        hum     = dht22.getHumidity()/10.0f;  //TODO;
        th[0]   = temp;
        th[1]   = hum;
    // printf("temp: %2.2f  , hum:%2.2f    \r\n",temp,hum);
    } else {
        printf("Error: %d\n", error);
    }
            
    return th;
}