9
9
// those are not available in this platform.
10
10
#![ no_std]
11
11
#![ feature( alloc) ]
12
+ #![ feature( collections) ]
12
13
14
+ #[ macro_use]
13
15
extern crate cc3200;
14
16
extern crate alloc;
15
17
extern crate freertos_rs;
16
18
extern crate freertos_alloc;
17
- extern crate sensorweb_sys;
18
19
19
- use cc3200 :: cc3200 :: { Board , Console , Utils , LedEnum , LedName } ;
20
+ extern crate smallhttp ;
20
21
21
- use alloc:: arc:: Arc ;
22
- use freertos_rs:: { CurrentTask , Duration , Task , Queue } ;
22
+ #[ macro_use]
23
+ extern crate log;
24
+
25
+ #[ macro_use]
26
+ extern crate collections;
27
+
28
+ use cc3200:: cc3200:: { Board , I2C , I2COpenMode , LedEnum } ;
29
+ use cc3200:: format:: * ;
30
+ use cc3200:: simplelink:: { self , SimpleLink } ;
31
+ use cc3200:: socket_channel:: SocketChannel ;
32
+
33
+ use cc3200:: i2c_devices:: TemperatureSensor ;
34
+ use cc3200:: tmp006:: TMP006 ;
35
+
36
+ use core:: str;
37
+
38
+ use freertos_rs:: { CurrentTask , Duration , Task } ;
39
+ use smallhttp:: Client ;
40
+ use smallhttp:: traits:: Channel ;
41
+
42
+ static VERSION : & ' static str = "1.0" ;
43
+
44
+ mod config;
45
+ mod wlan;
46
+
47
+ fn buf_find ( buf : & [ u8 ] , needle : & str ) -> Option < usize > {
48
+ if let Ok ( s) = str:: from_utf8 ( buf) {
49
+ s. find ( needle)
50
+ } else {
51
+ None
52
+ }
53
+ }
54
+
55
+ fn run ( ) -> Result < ( ) , wlan:: Error > {
56
+
57
+ Board :: led_configure ( & [ LedEnum :: LED1 ] ) ;
58
+
59
+ try!( SimpleLink :: start_spawn_task ( ) ) ;
60
+ try!( wlan:: wlan_station_mode ( ) ) ;
61
+
62
+ let i2c = I2C :: open ( I2COpenMode :: MasterModeFst ) . unwrap ( ) ;
63
+ let temp_sensor = TMP006 :: default ( & i2c) . unwrap ( ) ;
64
+
65
+ println ! ( "Will now send {} temperature sensing to the server..." ,
66
+ config:: SENSOR_READING_COUNT ) ;
67
+
68
+ for _ in 0 ..config:: SENSOR_READING_COUNT {
69
+ let temperature = temp_sensor. get_temperature ( ) . unwrap ( ) ;
70
+
71
+ // Format a simple json payload that we'll POST to the server
72
+ let mut buf: [ u8 ; 24 ] = [ b' ' ; 24 ] ;
73
+ let json = b"{ \" temperature\" : @@@@@ }" ;
74
+ buf[ 0 ..json. len ( ) ] . copy_from_slice ( json) ;
75
+ let num_tmpl = "@@@@@" ;
76
+ let num_idx = buf_find ( & buf, num_tmpl) . unwrap ( ) ;
77
+ if format_float_into ( & mut buf[ num_idx..num_idx + num_tmpl. len ( ) ] ,
78
+ temperature,
79
+ 1 /* digit after decimal */ ) {
80
+ info ! ( "Feels like {} C" ,
81
+ str :: from_utf8( & buf[ num_idx..num_idx + num_tmpl. len( ) ] ) . unwrap( ) ) ;
82
+ info ! ( "Sending {}" , str :: from_utf8( & buf) . unwrap( ) ) ;
83
+
84
+ let mut client = Client :: new ( SocketChannel :: new ( ) . unwrap ( ) ) ;
85
+ let response = client. post ( config:: SERVER_URL )
86
+ . open ( )
87
+ . unwrap ( )
88
+ . send ( json)
89
+ . unwrap ( )
90
+ . response ( |_| false ) // Not interested in any header.
91
+ . unwrap ( ) ;
92
+ let mut buffer = [ 0u8 ; 256 ] ;
93
+ info ! ( "Received {}" ,
94
+ response. body. read_string_to_end( & mut buffer) . unwrap( ) ) ;
95
+ } else {
96
+ error ! ( "Failed to format temperature float." ) ;
97
+ }
98
+
99
+
100
+ CurrentTask :: delay ( Duration :: ms ( 1000 ) )
101
+ }
102
+
103
+ // Power off the network processor.
104
+ try!( SimpleLink :: stop ( simplelink:: SL_STOP_TIMEOUT ) ) ;
105
+ Ok ( ( ) )
106
+ }
23
107
24
108
// Conceptually, this is our program "entry point". It's the first thing the microcontroller will
25
109
// execute when it (re)boots. (As far as the linker is concerned the entry point must be named
@@ -33,76 +117,18 @@ pub fn start() -> ! {
33
117
34
118
Board :: init ( ) ;
35
119
36
- Console :: init_term ( ) ;
37
- Console :: clear_term ( ) ;
38
- Console :: message ( "CC3200 Sample code\n " ) ;
39
-
40
- unsafe {
41
- sensorweb_sys:: sensorweb_test_func ( ) ;
42
- }
43
-
44
- let queue = Arc :: new ( Queue :: new ( 10 ) . unwrap ( ) ) ;
45
- let _producer = {
46
- let queue = queue. clone ( ) ;
47
- Task :: new ( )
48
- . name ( "producer" )
49
- . start ( move || {
50
- let msgs = [ "Welcome " , "to " , "CC32xx " , "development !\n " ] ;
51
- loop {
52
- for msg in msgs. iter ( ) {
53
- queue. send ( msg, Duration :: ms ( 15 ) ) . unwrap ( ) ;
54
- CurrentTask :: delay ( Duration :: ms ( 15 ) )
55
- }
56
- CurrentTask :: delay ( Duration :: ms ( 1000 ) )
57
- }
58
- } )
59
- . unwrap ( )
60
- } ;
61
-
62
- let _consumer = {
63
- let queue = queue. clone ( ) ;
64
- Task :: new ( )
65
- . name ( "consumer" )
66
- . start ( move || {
67
- loop {
68
- let msg = queue. receive ( Duration :: ms ( 2000 ) ) . unwrap ( ) ;
69
- Console :: message ( "Received: " ) ;
70
- Console :: message ( msg) ;
71
- Console :: message ( "\n " ) ;
72
- }
73
- } )
74
- . unwrap ( )
75
- } ;
120
+ println ! ( "Welcome to SensorWeb {}" , VERSION ) ;
76
121
77
- let _blinky = {
122
+ let _client = {
78
123
Task :: new ( )
79
- . name ( "blinky" )
124
+ . name ( "client" )
125
+ . stack_size ( 2048 ) // 32-bit words
80
126
. start ( || {
81
- Board :: led_configure ( & [ LedEnum :: LED1 , LedEnum :: LED2 , LedEnum :: LED3 ] ) ;
82
- Board :: led_off ( LedName :: MCU_ALL_LED_IND ) ;
83
- let mut counter = 0 ;
84
- loop {
85
- Board :: led_on ( LedName :: MCU_RED_LED_GPIO ) ;
86
- if counter & 1 != 0 {
87
- Board :: led_on ( LedName :: MCU_ORANGE_LED_GPIO ) ;
88
- } else {
89
- Board :: led_off ( LedName :: MCU_ORANGE_LED_GPIO ) ;
90
- }
91
- if counter & 2 != 0 {
92
- Board :: led_on ( LedName :: MCU_GREEN_LED_GPIO ) ;
93
- } else {
94
- Board :: led_off ( LedName :: MCU_GREEN_LED_GPIO ) ;
95
- }
96
- Utils :: delay ( 1333333 ) ;
97
- Board :: led_off ( LedName :: MCU_RED_LED_GPIO ) ;
98
- Utils :: delay ( 1333333 ) ;
99
- Board :: led_on ( LedName :: MCU_RED_LED_GPIO ) ;
100
- Utils :: delay ( 1333333 ) ;
101
- Board :: led_off ( LedName :: MCU_RED_LED_GPIO ) ;
102
- Utils :: delay ( 1333333 * 6 ) ;
103
-
104
- counter += 1 ;
105
- }
127
+ match run ( ) {
128
+ Ok ( ( ) ) => { println ! ( "sensorweb succeeded" ) ; } ,
129
+ Err ( e) => { println ! ( "sensorweb failed: {:?}" , e) ; } ,
130
+ } ;
131
+ loop { }
106
132
} )
107
133
. unwrap ( )
108
134
} ;
0 commit comments