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.

Revision:
5:35f80d88820f
Parent:
4:81af9a9f7844
Child:
6:dbf5cf5ca7b1
diff -r 81af9a9f7844 -r 35f80d88820f main.cpp
--- a/main.cpp	Mon Jul 06 01:55:07 2015 +0000
+++ b/main.cpp	Thu Jun 16 03:09:43 2016 +0000
@@ -1,70 +1,73 @@
 /* This is just a demo by Kevin Lee */
 #include "mbed.h"
 #include "SDFileSystem.h"
-#include "BLE.h"
 #include "WIZnetInterface.h"
 #include "HTTPClient.h"
-#include "DHT.h"
-
+#include "DHT22.h"
 
 #define USE_DHCP 0
 #define LOOPBACKPORT  5000
+
 int W5500_Test(void);
 
-float temperature_get();         /* Grove - Temperature Sensor V1.2 */
+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.21.247";
+const char * IP_Addr      = "192.168.1.123";
 const char * IP_Subnet    = "255.255.255.0";
-const char * IP_Gateway   = "192.168.21.2";
+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 */
-
-
-DHT dht22(A1,DHT22);
+DHT22 dht22(p6);
 
 AnalogIn   ain(A0);
 
 int main() {
     char buffer[256];
     wait(1);
-    pc.baud(115200);
+    pc.baud(9600);
     wait(1);
-    printf("helloworld\r\n");  
     
     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 get_msg[128]= "";
-    
-    /*
-        http://dweet.io/follow/ArchLink
-    */
-        char nameYouWant[] = "ArchLink";
+        char post_data[256]= "";
+        
         while(1)
         {
-            sprintf(get_msg,"http://dweet.io/dweet/for/%s?Temperature=%.2f",nameYouWant,temperature_get());
-            printf("temperature_get: %.2f",temperature_get());
+            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;
-        
-            pc.printf("Send post message to dweet.io\r\n");
-            pc.printf("msg : %s\r\n",get_msg);
             ret = http.get(get_msg, str, sizeof(str));
             if(!ret)
             {
-            pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
-            pc.printf("Result: %s\r\n", str);
+                pc.printf("Result: %s\r\n", str);
             }
             else
             {
-            pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
+                pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
             }
             wait(10);
        }
@@ -72,14 +75,19 @@
     
     while(1) {
         myled = 1;
-        printf("light1\r\n");
+        printf("light on\r\n");
         wait(3);
         myled = 0;
-        printf("light0\r\n");
+        printf("light off\r\n");
         wait(3);
     }
 }
 
+void timer_ticked()
+{
+    
+}
+
 int W5500_Test(void)
 {
     mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
@@ -113,17 +121,25 @@
     }
 }
 
-
-float temperature_get()
+float* temperature_get()
 {
-    /*
-    float resistance = (float)(1023.0-val)*10000.0/(float)val;
-    float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15;
-    */ 
-    int B = 3975;
-    float val = (ain.read()*1023);
-    float resistance = (float)(1023.0-val)*10000.0/(float)val;
-    float temp = 1/(log(resistance/10000)/B+1/298.15)-273.15;
-    return temp;
-}
-
+    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;
+}
\ No newline at end of file