Arduino WiServer relay control - can't get relay to switch - c

I am working on a sketch to help set up a remote door opener, based on the result of a web call. I've got a BlackWidow Arduino running WiServer, and the wifi works fine and I can get a result from my URL. I'm simply returning a 0 or 1 as the content.
The problem is in my loop the relayControlState is always HIGH, and I can't seem to get the loop to make the relay turn off/on.
When I just use a simple "blinker" sketch I can get the relay to work fine, it's only when it's intertwined with my server fetch code that it doesn't work. What am I missing? Code is below. Why does relayControlState not update inside the WiServer.getStatus callback? Is the relay not getting enough juice to switch?
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"monitored"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters
// setup the wireless mode
// infrastructure - connect to AP
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// IP Address for macpro.local
uint8 ip[] = {192,168,1,12};
// End of wireless configuration parameters ----------------------------------------
// A request that gets the aggregate status of the build system
GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html");
const int relayPin = 12;
int relayControlState = HIGH;
// Function that sets pin/light states
// BEWARE: THIS FUNCTION IS CALLED MULTIPLE (2) TIMES PER HTTP REQ
// Hidden call before/after call that returns payload 0, 1, 2, or null
void setRelayControlState(char* data, int len) {
// Serial.print("=========================\n\nLEN:\n");
// Serial.print(len);
if(len > 0) {
Serial.print("\nDATA:");
Serial.print(data[len - 1]);
Serial.print("\n");
// Serial.print("\n\nsetRelayControlState\n\n");
if(data[len - 1] == '0') {
relayControlState = LOW;
Serial.print("SET LOW");
}
if(data[len-1] == '1') {
relayControlState = HIGH;
Serial.print("SET HIGH");
}
} else {
relayControlState = LOW;
}
}
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(57600);
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
// Enable Serial output and ask WiServer to generate log messages (optional)
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getStatus.setReturnFunc(setRelayControlState);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
// Get another update 15s from now
updateTime += 1000 * 5;
getStatus.submit();
}
// Run WiServer
WiServer.server_task();
// turn on light pins based on stored vals
Serial.print("\nrelayControlState: ");
Serial.print(relayControlState);
Serial.print("\n");
digitalWrite(relayPin, relayControlState);
delay(10);
}

This is the code that worked in the end, but it also could have just been inconsistent behavior of the code loading onto the BlackWidow. I started to switch pins - each time I switched to try a new pin, it would work once, but only once, until I started power cycling the arduino. Seems it likes a power down instead of only a reset or new code upload. Still a bit finicky, but a working example of polling a URL for a specific last character. If 1 set pin high for 5.5 seconds. If 0 do nothing.
#include <WiServer.h>
// ---------------------------------------------------------------------------------
// Wireless configuration parameters
// ---------------------------------------------------------------------------------
unsigned char local_ip[] = {192,168,1,10}; // IP address of WiShield 192.168.1.10
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"monitored"}; // max 32 bytes
// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
unsigned char security_type = 3;
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// ---------------------------------------------------------------------------------
// GET REQUEST
// ---------------------------------------------------------------------------------
// IP Address for macpro.local
uint8 ip[] = {192,168,1,12};
// The request URL
GETrequest getStatus(ip, 80, "macpro.local", "/open-says-me/index.html");
const int relayPin = 3;
int relayControlState = LOW;
// ---------------------------------------------------------------------------------
// Callback for WiServer's getStatus
// ---------------------------------------------------------------------------------
void setRelayControlState(char* data, int len) {
Serial.print("[setRelayControlState] last digit of data: ");
Serial.println(data[len-1]);
Serial.print("[setRelayControlState] len: ");
Serial.println(len);
if(len > 0
&& data[len-1] == '1') {
relayControlState = HIGH;
Serial.print("\nSET HIGH FOR 5.5s\n");
digitalWrite(relayPin, HIGH);
delay(5500);
digitalWrite(relayPin, LOW);
}
}
void setup() {
pinMode(relayPin, OUTPUT);
Serial.begin(57600);
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
// Enable Serial output and ask WiServer to generate log messages (optional)
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getStatus.setReturnFunc(setRelayControlState);
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
// Get another update 15s from now
updateTime += 1000 * 15;
getStatus.submit();
Serial.print("end update # ms ");
Serial.println(millis());
}
WiServer.server_task();
delay(100);
}

Related

tiny-aes-c AES CTR 128 cuts off decrypted string in some cases

I've been trying to use the AES CTR 128 from tiny-aes-c (https://github.com/kokke/tiny-AES-c) to encrypt a randomly generated token, and it works, but not all the time. In some cases the retrieved string after encrypting and decrypting is cut off at some point. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "token_auth.h"
#include "aes.h"
uint8_t * create_token() {
static char charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
uint8_t *token = malloc(sizeof(uint8_t) * (TOKEN_LENGTH + 1));
int i = 0;
srand ( time(NULL) );
for (i = 0; i < TOKEN_LENGTH; i++) {
int pos = rand() % (int)(strlen(charset) - 1);
token[i] = (int) charset[pos] - 0;
}
token[TOKEN_LENGTH] = 0;
return token;
}
int main() {
uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
uint8_t iv[16] = { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
uint8_t *in = create_token();
printf("\nInput: %s\nSize: %d", (char *) in, strlen((char *) in));
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, strlen((char *) in));
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, strlen((char *) in));
printf("\nDEC: %s\n", (char *) in);
return 0;
}
TOKEN_LENGTH is 128. As an example of the behavior, the string NM5DlWyYInbeNtEWhBxGCdEjHSv2I6FzTMffJNgudrL2UsYe6zVJMA3wvAyhHeQD18UMXckcF8gBAfPGQNqGqwdW9MgS39w7huVfIgtoqJ212SKSIdBaJP9VErOJAmQT comes out NM5DlWyYInbeNtEWhBxGCdEjHSv2 after being encrypted and decrypted. I'm not really good at C, so it might just well be a problem with something else I've done, but at this point I'm lost. Any ideas? Thanks in advance.
The first call to AES_CTR_xcrypt_buffer encrypts the buffer in place in CTR mode.
The buffer still has the same size (128 in your case), but can contain NUL bytes.
The strlen call in the second call of AES_CTR_xcrypt_buffer for decryption can therefore result in a length < 128 if the buffer contains a NUL byte.
By the way: It works in cases where the encryption does not result in a NUL byte in the buffer.
So if you call it with TOKEN_LENGTH as the length parameter decryption will give the original string again:
AES_CTR_xcrypt_buffer(&ctx, in, TOKEN_LENGTH);

C - CRC32 checksum does not match Wireshark on Ethernet Frame Check Sequence

I'm using an online CRC-32 calculator to check that my output is correct however it seems that Wireshark has a different expected FCS for the ethernet packet.
message2 is the ethernet frame minus the FCS as seen in Wireshark
#include <stdio.h>
#include <stdint.h>
unsigned int crc32b(unsigned char *message) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (message[i] != 0) {
//printf("%i %x \n\n", i, message[i]);
byte = message[i];
crc = crc ^ byte;
for (j = 7; j >= 0; j--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
int main(void)
{
unsigned char * message = "hello test";
unsigned char * message2 = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
unsigned int res = crc32b(message2);
printf("%x\n", res);
return 0;
}
I've tried using different Polynomials as defined in [1 - subsection CRC-32 IEEE 802.3], however the result does not match Wireshark.
Output using 0xED Polynomial: 0xd81e4af3
Wireshark FCS expected: 0xa8cd3084
I'd really like to code in the FCS for my ethhdr packet, I guess when creating a software packet, the FCS isn't entered by the NIC...
Sources:
[1] - http://crppit.epfl.ch/documentation/Hash_Function/WiKi/Cyclic_redundancy_check.htm
Your implementation is definitively correct (for NUL terminated C strings). It's a maybe a wrong configuration of the network interface. In default mode Wireshark doesn't get a FCS from the network driver. If you use Linux and the driver supports this, then you must enable this with ethtool to get the FCS.
Unfortunately, on my system this only works for receiving frames:
$ ethtool -K eth0 rx-fcs on
See this for details.
I use a slightly different algorithm in my embedded (for AVR microcontrollers) projects and it works perfectly for me:
#define CRC_POLY 0xEDB88320
uint32_t crc32_calc(uint8_t *data, int len)
{
int i, j;
uint32_t crc;
if (!data)
return 0;
if (len < 1)
return 0;
crc = 0xFFFFFFFF;
for (j = 0; j < len; j++) {
crc ^= data[j];
for (i = 0; i < 8; i++) {
crc = (crc & 1) ? ((crc >> 1) ^ CRC_POLY) : (crc >> 1);
}
}
return (crc ^ 0xFFFFFFFF);
}
A real world example:
The Ethernet frame in Wireshark (with ethtool rx-fcs on):
The test with my used implementation:
uint8_t frame[] = { 0x20, 0xcf, 0x30, 0x1a, 0xce, 0xa1, 0x62, 0x38,
0xe0, 0xc2, 0xbd, 0x30, 0x08, 0x06, 0x00, 0x01,
0x08, 0x00 ,0x06 ,0x04 ,0x00 ,0x01 ,0x62 ,0x38,
0xe0 ,0xc2 ,0xbd ,0x30 ,0x0a, 0x2a, 0x2a, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x2a,
0x2a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
printf("0x%x\n", crc32_calc(frame, sizeof(frame)));
The output:
$ ./fcs-test
0x6026b722
$
You can see, Wireshark reports 0x22bf2660 as correct FCS. Here is only a different output because of the byte-order. But the CRC calculation algorithm is correct.
EDIT:
I have modified your code:
uint32_t crc32b(uint8_t *message, int len) {
int i, j;
uint32_t crc, mask;
uint8_t byte;
crc = 0xFFFFFFFF;
for (j = 0; j < len; j++) {
byte = message[j];
crc = crc ^ byte;
for (i = 7; i >= 0; i--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
}
return ~crc;
}
I added a length argument, because your implementation only works correct when message is a NUL terminated C string. If your input is a byte array, then you get a incorrect CRC value.
See the differences (Array and C string):
uint8_t msg_arr[] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x5c, 0xb9, 0x01, 0x7c, 0x5a, 0x53, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
char *msg_str = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
printf("0x%x\n", crc32b(msg_arr, sizeof(msg_arr)));
printf("0x%x\n", crc32b(msg_str, strlen(msg_str)));
Output:
$
0x3422dd71
0xd81e4af3
$
There are a number of problems with your code. There are also plenty of existing implementations you could compare (eg, this one linked from the real Wikipedia page on CRC).
unsigned char * message2 = "aabbccddeeff5cb9017c5a53080000000000000000000000000000";
Are you hoping that this will be the octet sequece 0xAA 0xBB 0xCC ... as you see them in Wireshark? Because that isn't at all what you have.
This string actually contains 0x61 0x61 0x62 0x62 ... (assuming your platform uses ASCII encoding) because it is a character string and not an octet string.
specifically, here: byte = message[i]; you assume the first 8 bits of your message are an octet, and again I assume since you didn't say, that you expected this to be 0xAA. It will actually be 0x61.
If you want this to work correctly, translate each pair of characters into an integer value using strtoul(pair, NULL, 16) or similar.
You have a loop for (j = 7; j >= 0; j--) but never use j inside it. You do use the integer constant 1 in an odd-looking way: is there supposed to be a (1 << j) or something?
I suggest fixing the obvious errors, and then writing some self-contained tests before you try comparing whole frames with Wireshark. The code you posted has some basic errors that should be tested, identified and fixed before you get to this point.
Im not sure about your question, but if you want calculate a checksum of a network packet, you have to deploy the data in it's proper structure.
Please make sure your problem is not related with endianness.
The network byte-order is big-endian, here is the point that the things getting a little bit harder.
Little-Endian mostly used in PCs but may vary by hardware and manufacturer.
2byte integer (16 bit integer) with value 255.
Little Endian: FF00
Big Endian: 00FF
Im not sure what kind of checksum you are trying to match with, but checksum not only for data field, mostly it contains all flags and options, issued at last step thats why the implementation require the corresponding data structure.
About checksums, there are a lot of case when you get an invalid checksum with wireshark, it could cause kernel, virtual adapter, accelerated network, dedicated CPU in your NIC, etc...
Example for TCP header:
/* include/linux/tcp.h */
struct tcphdr {
__u16 source;
__u16 dest;
__u32 seq;
__u32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u16 res1:4,
doff:4,
fin:1,
syn:1,
rst:1,
psh:1,
ack:1,
urg:1,
ece:1,
cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
__u16 doff:4,
res1:4,
cwr:1,
ece:1,
urg:1,
ack:1,
psh:1,
rst:1,
syn:1,
fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
__u16 window;
__u16 check;
__u16 urg_ptr;
};

How to stop I2C communication when you are recieving a data from slave?

I am implementing I2C communication protocol. I am sending 5 bytes of data to a slave device (slave address is 0x48). and Then want to see the response.
I am getting my desired response, but the only problem I am facing is that I am not able to stop this communication.
For example, I send 5 bytes of data, and in response I am expecting 3 bytes of data. I get this data but after that I don't see any stop condition. And due to that I cannot send further data to my slave device.
I am checking my results with logic analyzer, and in logic analyzer it shows unknown after receiving 3 bytes. (Images attached).
It seems like I am not properly starting and stopping the I2C communication.
The code I have written is given below:
#include <msp430.h>
char TXData[5] = { 0x5A, 0xCF, 0x00 }; //Data to be transmitted
//unsigned char TXData[] = { 0x5A, 0x40, 0x09, 0x80, 0x04, 0x00, 0x22, 0x03, 0x41, 0x01, 0x01, 0x04, 0xE5, 0xEF }; // GetMemory APDU
//unsigned char TXData[] = { 0x5A, 0x00, 0x0B, 0x80, 0x04, 0x00, 0x1B, 0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x0A, 0xEE, 0x07 }; // CreateSession APDU
//unsigned char TXData[] = { 0x5A, 0x00, 0x16, 0x00, 0xA4, 0x04, 0x00, 0x10, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x54, 0x53, 0x00, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8, 0xC8 }; // GetVersion APDU
volatile unsigned char RXData[3]; // To save the received data
unsigned char SlaveAddress = 0x48; // slave address
volatile unsigned char TXByteCtr;
volatile unsigned char TxByteCnt = 0;
volatile unsigned char RxByteCnt = 0;
unsigned char Rx; // To enable reception
void Transmit(void);
void Recieve(void);
short crc_ret;
//short final;
unsigned short crc16_x25(char* pData, int length)
{
int i;
unsigned short wCrc = 0xffff;
while (length--) {
wCrc ^= *(unsigned char *)pData++ << 0;
for (i=0; i < 8; i++)
wCrc = wCrc & 0x0001 ? (wCrc >> 1) ^ 0x8408 : wCrc >> 1;
}
return wCrc ^ 0xffff;
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;
//while(1);
// Configure GPIO
P8OUT |= BIT0; // Clear P8.0 output latch
P8DIR |= BIT0;
P7SEL0 |= BIT0 | BIT1;
P7SEL1 &= ~(BIT0 | BIT1);
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Configure USCI_B2 for I2C mode
UCB2CTLW0 |= UCSWRST; // put eUSCI_B in reset state
UCB2CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK; // I2C master mode, SMCLK
UCB2BRW = 0x8; // baudrate = SMCLK / 8
UCB2CTLW0 &= ~UCSWRST; // clear reset register
UCB2IE |= UCTXIE0 | UCNACKIE | UCRXIE0; // transmit, Receive and NACK interrupt enable
crc_ret = crc16_x25(TXData, (sizeof(TXData) - 2)); // Calling CRC function
TXData[3] = (char)crc_ret;
TXData[4] = (char)((crc_ret&0xFF00)>>8);
while(1)
{
__delay_cycles(2000); // Delay between transmissions
UCB2I2CSA = SlaveAddress; // configure slave address
TXByteCtr = 1; // Load TX byte counter
Transmit(); //Implementing the transmit function
__delay_cycles(2000);
Rx = 1;
Recieve(); //Implementing the receive function
__delay_cycles(2000);
}
}
#pragma vector = EUSCI_B2_VECTOR
__interrupt void USCI_B2_ISR(void)
{
switch(__even_in_range(UCB2IV, USCI_I2C_UCBIT9IFG))
{
case USCI_NONE: break; // Vector 0: No interrupts
case USCI_I2C_UCALIFG: break; // Vector 2: ALIFG
case USCI_I2C_UCNACKIFG: // Vector 4: NACKIFG
UCB2CTLW0 |= UCTXSTT; // resend start if NACK
break;
case USCI_I2C_UCSTTIFG: break; // Vector 6: STTIFG
case USCI_I2C_UCSTPIFG: break; // Vector 8: STPIFG
case USCI_I2C_UCRXIFG3: break; // Vector 10: RXIFG3
case USCI_I2C_UCTXIFG3: break; // Vector 12: TXIFG3
case USCI_I2C_UCRXIFG2: break; // Vector 14: RXIFG2
case USCI_I2C_UCTXIFG2: break; // Vector 16: TXIFG2
case USCI_I2C_UCRXIFG1: break; // Vector 18: RXIFG1
case USCI_I2C_UCTXIFG1: break; // Vector 20: TXIFG1
case USCI_I2C_UCRXIFG0: // Vector 22: RXIFG0
if (Rx < 3) // Check RX byte counter
{
RXData[RxByteCnt]= UCB2RXBUF; // Load TX buffer
RxByteCnt++;
Rx++; // Decrement TX byte counter
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
else
{
UCB2CTLW0 |= UCTXSTP; // I2C stop condition
UCB2IFG &= ~UCRXIFG; // Clear USCI_B2 RX int flag
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
break;
case USCI_I2C_UCTXIFG0: // Vector 24: TXIFG0
if (TXByteCtr < (sizeof(TXData) + 1)) // Check TX byte counter
{
UCB2TXBUF = TXData[TxByteCnt]; // Load TX buffer
TxByteCnt++;
TXByteCtr++; // Decrement TX byte counter
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
else
{
TxByteCnt = 0;
UCB2CTLW0 |= UCTXSTP; // I2C stop condition
UCB2IFG &= ~UCTXIFG; // Clear USCI_B2 TX int flag
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
break;
case USCI_I2C_UCBCNTIFG: break; // Vector 26: BCNTIFG
case USCI_I2C_UCCLTOIFG: break; // Vector 28: clock low timeout
case USCI_I2C_UCBIT9IFG: break; // Vector 30: 9th bit
default: break;
}
}
void Transmit(void)
{
while (UCB2CTLW0 & UCTXSTP); // Ensure stop condition got sent
UCB2CTLW0 |= UCTR | UCTXSTT; // I2C TX, start condition
__bis_SR_register(GIE); // Enter LPM0 w/ interrupts
// Remain in LPM0 until all data
// is TX'd
}
void Recieve(void)
{
while (UCB2CTLW0 & UCTXSTP); // Ensure stop condition got se
UCB2CTLW0 &= ~UCTR;
UCB2CTLW0 |= UCTXSTT;
__bis_SR_register(GIE); // Enter LPM0 w/ interrupts//start condition;
// I2C stop condition
UCB2CTLW0 &= ~UCTXSTP;
//UCB2IFG &= ~UCRXIFG;
}
And the transmitted and received data is also attached:
You can see in the received data, that after 3 bytes, there is written "unknown". Due to this unknown thing, I am not able to send further data.
I shall be very thankful if someone can help me out in this.
Thank you

Why can't I see data sent over XBees using Arduino serial monitor?

I'm using 3 Xbees. 2 are configured as router AT and the other one as coordinator AT. The codes on the routers are for reading temperatures from a LM and from 3 OneWire DS18B20 sensors :
#include <SoftwareSerial.h>
float temp;
int tempPin = 0;
SoftwareSerial xbee(4, 3);
void setup()
{
Serial.begin(9600);
xbee.begin(9600);
}
void loop()
{
delay(1000);
temp = analogRead(tempPin);
temp = temp * 0.48828125;
//Serial.print("TEMPRATURE = ");
//Serial.print(temp);
// Serial.print("*C");
//Serial.println();
//delay(1000);
xbee.print("Temp D: "); xbee.print(temp);
delay(1000);
}
and
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 8
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress camera1Temp = { 0x28, 0xF6, 0x7C, 0xA0, 0x05, 0x00, 0x00, 0x0E };
DeviceAddress camera2Temp = { 0x28, 0x2A, 0x61, 0xDD, 0x03, 0x00, 0x00, 0xC3 };
DeviceAddress camera3Temp = { 0x28, 0x52, 0x8D, 0x30, 0x05, 0x00, 0x00, 0x04 };
SoftwareSerial xbee(4, 3); // RX, TX
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(camera1Temp, 10);
sensors.setResolution(camera2Temp, 10);
sensors.setResolution(camera3Temp, 10);
xbee.begin(9600);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Eroare citire temperaturi");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}
void loop(void)
{
delay(2000);
//Serial.print("Citire temperaturi...\n\r");
sensors.requestTemperatures();
/*delay(1000);
Serial.print("Temperatura in camera 1: ");
printTemperature(camera1Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 2: ");
printTemperature(camera2Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 3: ");
printTemperature(camera3Temp);
*/
float temp1 = sensors.getTempC(camera1Temp);
float temp2 = sensors.getTempC(camera2Temp);
float temp3 = sensors.getTempC(camera3Temp);
/*Serial.print(int(temp1*100));
delay(1000);
Serial.println(int(temp2*100));
delay(1000);
Serial.println(int(temp3*100));
*/
//Serial.print("\n\r\n\r");
xbee.print("Temp A: "); xbee.print(temp1);
delay(100);
xbee.print("Temp B: "); xbee.print(temp2);
delay(100);
xbee.print("Temp C: "); xbee.print(temp3);
delay(100);
}
When connecting the Xbee coordinator in XCTU i can see the receiving text and temperatures. But I can't see anything when connecting the xbee coordinator to an arduino and using the following code:
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()>0)
{
Serial.write(Serial.read());
}
}
After this I want to procces the incoming text from the 4 sensors....but I can't see anything on the arduino.
Can you please tell what am I doying wrong?
In your Arduino sketch; I think it's not right to do this :
Serial.write(Serial.read());
See this from Arduino Forum : Help with Serial.Read() getting string.
This sketch may help you :
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void loop()
{
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
// Now do something with the string (but not using ==)
for(int i = 0; i < index ; i++){
Serial.print(inData[i]);
}
}
You have to buffer the data before writing it into Serial Interface .

The use of "if (strcmp(URL, "/") == 0)" in Arduino network sketches

I'm trying to understand the "if (strcmp(URL, "/") == 0)" line in the Arduino sketch below (see the sendMyPage function about halfway down):
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
#define AREF_VOLTAGE 5
const int tmpPin = A0;
int tmpReading = 0;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,31,199}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"MERCURY_7F3F70"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"11163127"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
float testTmp (void){
tmpReading = analogRead (tmpPin);
float voltage = tmpReading * AREF_VOLTAGE;
voltage /= 1023;
float tmpC = (voltage - 0.5) * 100;
return tmpC;
}
// This is our page serving function that generates web pages
**boolean sendMyPage(char* URL)** {
// Check if the requested URL matches "/"
**if (strcmp(URL, "/") == 0) {**
// Use WiServer's print and println functions to write out the page content
float tmpC = testTmp ();
WiServer.print("<html>");
// WiServer.print("Hello World!");
WiServer.print(tmpC);
WiServer.print("</html>");
// URL was recognized
return true;
}
// URL not found
return false;
}
void setup() {
// Initialize WiServer and have it use the sendMyPage function to serve pages
WiServer.init(sendMyPage);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
}
void loop(){
// Run WiServer
WiServer.server_task();
delay(10);
}
It seems that the function accepts a URL as an argument, and sends data to the page using WiSever.print if the URL is valid. But how does 'if (strcmp(URL, "/") == 0)' determine if the URL is good?
I've seen this kind of check before, but don't understand how it works.
Thanks!
The line:
if (strcmp(URL, "/") == 0) {
Is testing to see if the string URL exactly matches the string literal "/". If so, it returns 0.
Note, if you wanted to test to see if the string URL contained "/", then use:
if (strstr(URL, "/") != NULL) {
Regarding But how does 'if (strcmp(URL, "/") == 0)' determine if the URL is good?
It does not. More would be needed than a single character to determine if URL was good, it appears that the comment, as written, is not really expressing what that code block actually does.

Resources