codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/************ Module information **************/ int8_t answer; int onModulePin = 2; char aux_str[50]; /************ Xively information **************/ char server[ ] = "api.xively.com"; char port[ ] = "8081"; const char pin[] = "0000"; String TCP_message = "0"; /**********************************************************************************************************************/ /**********************************************************************************************************************/ /********************************** SETUP ************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ void setup() { pinMode(onModulePin, OUTPUT); Serial.begin(115200); Serial.println("\n\nStarting...\n\n"); //Turn on module Serial.println("*******************************************************"); Serial.println("************************* SETUP ***********************"); Serial.println("******************************************************* \n"); Serial.println("Turn on the module"); power_on(); Serial.println("Module ON \n"); delay(3000); //sets the PIN code Serial.println("Sets the PIN code"); snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin); sendATcommand(aux_str, "OK", 2000); Serial.println("PIN code sets \n"); delay(3000); //network registration Serial.println("Network registration"); while ( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 ); //sets APN, user name and password for the registration Serial.println("Authentification to the network"); sendATcommand("AT+CGSOCKCONT=1,\"mmsbouygtel.com\"", "OK", 2000); sendATcommand("AT+CSOCKAUTH=1,1,\"\",\"\"", "OK", 2000); Serial.println("Success authentification"); Serial.println("Success network registration \n"); Serial.println("*******************************************************"); Serial.println("********************* SETUP : DONE ********************"); Serial.println("*******************************************************\n \n"); } void loop() { String coordinates = "2525"; String distance = "28"; TCP_message = "{\"method\": \"put\",\"resource\": \"/feeds/1819176350/\",\"params\": {},\"headers\": {\"X-ApiKey\": \"aLWKMwlAcdlPK9TrLytC1sKrZxWguK2sl0dwHybMFrFTGhvl\"},\"body\": {\"version\": \"1.0.0\",\"datastreams\": [{\"id\": \"Distance\",\"current_value\": \"" + distance + "\"},{\"id\": \"Coordinates\",\"current_value\": \"" + coordinates + "\"}]}}"; send_data(); } /**********************************************************************************************************************/ /**********************************************************************************************************************/ /********************************** SEND DATA TO VIXELY ************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ void send_data() { char message_TCP [500]; TCP_message.toCharArray(message_TCP, 500); // converting TCP_message to char inorder for it to pass through AT command sprintf(aux_str, "AT+NETOPEN=\"TCP\",%s", port); answer = sendATcommand(aux_str, "Network opened", 20000); if (answer == 1) { Serial.println("Network opened"); sprintf(aux_str, "AT+TCPCONNECT=\"%s\",%s", server, port); answer = sendATcommand(aux_str, "Connect ok", 20000); if (answer == 1) { Serial.println("Socket opened"); sprintf(aux_str, "AT+TCPWRITE=%d", strlen(message_TCP)); answer = sendATcommand(aux_str, ">", 20000); if (answer == 1) { sendATcommand(message_TCP, "Send OK", 20000); } sendATcommand("AT+NETCLOSE", "OK", 20000); } else { Serial.println("Error opening the socket"); } } else { Serial.println("Error opening the network"); } } /**********************************************************************************************************************/ /**********************************************************************************************************************/ /********************************** TURN ON MODULE ************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ void power_on() { uint8_t answer = 0; // checks if the module is started answer = sendATcommand("AT", "OK", 2000); if (answer == 0) { // power on pulse digitalWrite(onModulePin, HIGH); delay(3000); digitalWrite(onModulePin, LOW); // waits for an answer from the module while (answer == 0) { // Send AT every two seconds and wait for the answer answer = sendATcommand("AT", "OK", 5000); } } } /**********************************************************************************************************************/ /**********************************************************************************************************************/ /********************************** SEND COMMAND ***********************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) { uint8_t x = 0, answer = 0; char response[100]; unsigned long previous; memset(response, '\0', 100); // Initialize the string delay(100); while ( Serial.available() > 0) Serial.read(); // Clean the input buffer Serial.println(ATcommand); // Send the AT command x = 0; previous = millis(); // this loop waits for the answer do { if (Serial.available() != 0) { response[x] = Serial.read(); x++; // check if the desired answer is in the response of the module if (strstr(response, expected_answer1) != NULL) { answer = 1; } } // Waits for the asnwer with time out } while ((answer == 0) && ((millis() - previous) < timeout)); return answer; }
Private
[
?
]
Run code
Submit