-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenedettelli-nxt2wifi-test5.c
227 lines (198 loc) · 5.96 KB
/
benedettelli-nxt2wifi-test5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma config(Sensor, S1, HTBM, sensorI2CCustom)
#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/*
* $Id: benedettelli-nxt2wifi-test5.c 133 2013-03-10 15:15:38Z xander $
*/
/**
* benedettelli-nxt2wifi.h provides an API for Daniele's NXT2WiFi Sensor. This program
* demonstrates how to use that API.
*
* Changelog:
* - 0.1: Initial release
*
* Credits:
* - Big thanks to Daniele Benedettelli for providing me with the hardware necessary to write and test this.
*
* License: You may use this code as you wish, provided you give credit where it's due.
*
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER.
* Xander Soldaat (xander_at_botbench.com)
* 22 November 2012
* version 0.1
*/
#define __RS485_DEBUG__ 1
#include "drivers/common.h"
#include "drivers/timer.h"
#include "drivers/hitechnic-barometer.h"
#include "drivers/benedettelli-nxt2wifi.h"
long dataTemp = 0;
long dataPress = 0;
long txbytes = 0;
long rxbytes = 0;
string IPaddress = "0.0.0.0";
string connStatus = "disconnected";
// hack
// char *tweethost = "arduino-tweet.appspot.com";
char *line1 = "POST http://arduino-tweet.appspot.com/update HTTP/1.0\nContent-Length: ";
//char *line2 = "Content-Length: %d\n";
char *line3 = "token=30675988-XmplwBFpIfkBV3WwCW5wL63P1d3uOICOLrmkMNw";
char *line4 = "&status=Hello from NXT2WIFI in Rotterdam";
//char *line4 = "&status=%d";
//char *line5 = " people have pressed the button in the past 5 minutes. The current temp is ";
//char *line6 = "%2.1fC.";
// tHugeByteArray data;
tHugeByteArray payloadData;
task updateScreen()
{
while(true)
{
nxtDisplayTextLine(0, "Stat: %s", connStatus);
nxtDisplayTextLine(1, "%s",IPaddress);
nxtDisplayTextLine(2, "-------------------");
nxtDisplayTextLine(3, "Temp : %2.2f", (float)dataTemp/100.0);
nxtDisplayTextLine(4, "Press: %d", dataPress);
nxtDisplayTextLine(5, "Light:");
nxtDisplayTextLine(6, "Sound:");
nxtDisplayTextLine(7, "RX/TX: %d/%d", rxbytes, txbytes);
wait1Msec(100);
}
}
// Constantly read the sensors
task pollSensors()
{
while (true)
{
dataPress = HTBMreadMInHg(HTBM);
wait1Msec(100);
dataTemp = HTBMreadTemp(HTBM) * 100;
wait1Msec(1000);
}
}
bool tweet()
{
char *data;
//char *line1 = "POST http://arduino-tweet.appspot.com/update HTTP/1.0\nContent-Length: ";
//char *line2 = "Content-Length: %d\n";
//char *line3 = "token=30675988-XmplwBFpIfkBV3WwCW5wL63P1d3uOICOLrmkMNw";
//char *line4 = "&status=Hello from NXT2WIFI in Prague";
writeDebugStream(line1);
memset(payloadData, 0, sizeof(tHugeByteArray));
memcpy(payloadData, line1, strlen(line1));
N2WTCPWrite(1, payloadData, strlen(line1));
sprintf(data, "%d, %d\n", strlen(line3), strlen(line4));
writeDebugStream(data);
memset(payloadData, 0, sizeof(tHugeByteArray));
memcpy(payloadData, data, strlen(data));
N2WTCPWrite(1, payloadData, strlen(data));
writeDebugStream(line3);
memset(payloadData, 0, sizeof(tHugeByteArray));
memcpy(payloadData, line3, strlen(line3));
N2WTCPWrite(1, payloadData, strlen(line3));
writeDebugStream(line4);
memset(payloadData, 0, sizeof(tHugeByteArray));
memcpy(payloadData, line4, strlen(line4));
N2WTCPWrite(1, payloadData, strlen(line4));
return true;
}
task main ()
{
StartTask(pollSensors);
StartTask(updateScreen);
N2WsetDebug(true);
wait1Msec(100);
//wait1Msec(1000);
//StopAllTasks();
// initialise the port, etc
RS485initLib();
wait1Msec(100);
N2WsetDebug(true);
wait1Msec(100);
// Disconnect if already connected
N2WDisconnect();
wait1Msec(500);
// if a custom profile exists, use that instead
if (!N2WCustomExist())
{
StopTask(updateScreen);
wait1Msec(50);
eraseDisplay();
PlaySound(soundException);
nxtDisplayCenteredBigTextLine(1, "ERROR");
nxtDisplayTextLine(3, "No custom profile");
nxtDisplayTextLine(4, "configured!!");
while(true) EndTimeSlice();
}
N2WLoad();
wait1Msec(100);
N2WConnect(true);
connStatus = "connecting";
while (!N2WConnected())
wait1Msec(1000);
connStatus = "connected";
PlaySound(soundBeepBeep);
wait1Msec(15000);
N2WgetIP(IPaddress);
wait1Msec(1000);
N2WTCPClose(0);
wait1Msec(1000);
if (!N2WTCPOpenClient(1, "arduino-tweet.appspot.com", 80))
writeDebugStreamLine("Err open port");
tweet();
//if (!N2WUDPOpenServer(1, 161))
// writeDebugStreamLine("Err open port");
//while (true)
//{
// avail = N2WUDPAvail(1);
// if (avail > 0)
// {
// rxbytes += avail;
// N2WchillOut();
// N2WUDPRead(1, avail);
// if (SNMPdecode(reqID, oid))
// {
// writeDebugStreamLine("------------------");
// for (int i = 0; i < sizeof(oid); i++)
// {
// writeDebugStream("0x%02x ", oid[i] & 0xFF);
// }
// writeDebugStreamLine("");
// for (int i = 0; i < sizeof(oidTree); i++)
// {
// writeDebugStream("0x%02x ", oidTree[i] & 0xFF);
// }
// writeDebugStreamLine("\n------------------");
// if (memcmp(oid, oidName, 8) == 0)
// {
// getFriendlyName(dataString);
// totsize = genSNMPreply(reqID, communityName, oid, 8, dataString);
// N2WUDPWrite(1, data, totsize);
// txbytes += totsize;
// }
// else if (memcmp(oid, oidTree, 4) == 0)
// {
// dataString = "unknown";
// switch(oid[4])
// {
// case oidTemp: sensorData = dataTemp; break;
// case oidPress: sensorData = dataPress; break;
// case oidSound: sensorData = dataSound; break;
// case oidLight: sensorData = dataLight; break;
// }
// totsize = genSNMPreply(reqID, communityName, oid, 5, sensorData);
// N2WUDPWrite(1, data, totsize);
// txbytes += totsize;
// }
// else
// {
// writeDebugStreamLine("unknown oid");
// }
// }
// else
// {
// writeDebugStreamLine("Invalid packet");
// }
// }
// wait1Msec(100);
//}
}