所以我注意到我的arduino板在第一个60秒后崩溃(在Serial上暂停),所以我决定随机注释出代码块,看看我是否能识别罪魁祸首。幸运的是,我做到了!
这是我的硬件设置:
Board: Arduino Mega
Pin 16,17: EC sensor
Pin 14,15: pH sensor
Pin 2,3,4: RGB LED
我已经明确地确定了initRGB
和displayGreen()
, displayBlue()
和displayRed()
正在崩溃我的板。为什么?
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <Base64.h>
int RGB_RED = 4;
int RGB_GREEN = 3;
int RGB_BLUE = 2;
String ecSensorString = "";
String phSensorString = "";
...
// Set this on if you want to supply an IP address directly
#define IP_NOT_DOMAIN true
#define PORT 8080
// This corresponds to 192.168.1.117
#define IP_1 192
#define IP_2 168
#define IP_3 1
#define IP_4 117
#define WEBSITE "blah.appspot.com"
#define WEBPAGE "/api/points/new/"
// This is needed for http basic authorization
String serial="serial";
String token="token";
uint32_t ip;
void setup() {
//initRGB();
Serial.begin(9600);
ecSensorString.reserve(30);
phSensorString.reserve(30);
initSensors();
initWireless();
Serial.println("Setup Complete");
}
void loop() {
Serial.println("Loop begins");
Serial.print("Free RAM: ");
Serial.println(getFreeRam(), DEC);
getEC();
getPH();
Serial.println("Sensors reading completed");
createPoint(ecSensorString, phSensorString);
Serial.println("createPoint completed");
delay(10*1000);
Serial.println("delay completed");
}
/*
void initRGB() {
pinMode(RGB_RED, OUTPUT);
pinMode(RGB_GREEN, OUTPUT);
pinMode(RGB_BLUE, OUTPUT);
//displayBlue();
}
void displayBlue() {
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 255);
}
void displayRed() {
analogWrite(RGB_RED, 255);
analogWrite(RGB_GREEN, 0);
analogWrite(RGB_BLUE, 0);
}
void displayGreen() {
analogWrite(RGB_RED, 0);
analogWrite(RGB_GREEN, 255);
analogWrite(RGB_BLUE, 0);
}
*/
void initWireless() {
/* Initialise the module */
if (!cc3000.begin()) {
Serial.println(F("Couldn't begin()! Check your wiring?"));
//displayRed();
while(1);
}
Serial.println(F("Initialization Complete"));
Serial.print(F("Attempting to connect to "));
Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
//displayRed();
while(1);
}
Serial.println(F("Wireless Connected!"));
/* Wait for DHCP to complete */
while (!cc3000.checkDHCP()) {
delay(100); // ToDo: Insert a DHCP timeout!
}
Serial.println(F("DHCP Requested!"));
if (IP_NOT_DOMAIN) {
ip = cc3000.IP2U32(IP_1, IP_2, IP_3, IP_4);
} else {
ip = 0;
// Try looking up the website's IP address
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
}
}
void initSensors() {
Serial2.begin(38400);
Serial3.begin(38400);
// Tell the EC sensors to stop continuous mode
Serial2.print("c,0r");
// Tell the pH sensor to stop all readings
Serial3.print("Er");
// TODO: EC sensor's first reading is always empty, possibly delay?
}
void createPoint(String ec, String ph) {
if (ec && ph) {
postPointToServer("ec=" + ec + "&ph=" + ph);
} else if (ec) {
postPointToServer("ec=" + ec);
} else if (ph) {
postPointToServer("ph=" + ph);
} else {
Serial.println("createPoint: null");
}
}
void postPointToServer(String data) {
Serial.print("postPointToServer:");
Serial.println(data);
// Http Basic Authorization
String auth_raw = serial + ":" + token;
char auth_input[200];
char auth_output[200];
auth_raw.toCharArray(auth_input, auth_raw.length() + 1);
base64_encode(auth_output, auth_input, auth_raw.length());
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, PORT);
if (www.connected()) {
www.fastrprint(F("POST ")); www.fastrprint(WEBPAGE); www.fastrprint(F(" HTTP/1.1rn"));
www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("rn"));
www.fastrprint(F("Authorization: Basic ")); www.fastrprint(auth_output); www.fastrprint(F("rn"));
www.fastrprint(F("Content-Type: application/x-www-form-urlencoded")); www.fastrprint(F("rn"));
char len_c[7];
itoa(data.length(), len_c, 10);
www.fastrprint(F("Content-Length: ")); www.fastrprint(len_c); www.fastrprint(F("rn"));
// Extra empty line for post arguments
www.fastrprint(F("rn"));
char data_c[200];
data.toCharArray(data_c, data.length() + 1);
www.fastrprint(data_c); www.fastrprint(F("rn"));
www.fastrprint(F("rn"));
www.println();
//displayGreen();
} else {
Serial.println(F("Connection failed"));
//displayRed();
}
www.close();
}
void getEC() {
ecSensorString = "";
Serial2.print("rr"); // Read only 1
char inchar;
while (Serial2.available()) {
inchar = (char)Serial2.read();
if (inchar != 'r') {
ecSensorString += inchar;
}
}
Serial.print("getEC:");
Serial.println(ecSensorString);
}
void getPH() {
phSensorString = "";
Serial3.print("Rr"); // Read only 1
char inchar;
while (Serial3.available()) {
inchar = (char)Serial3.read();
if (inchar != 'r') {
phSensorString += inchar;
}
}
Serial.print("getPH:");
Serial.println(phSensorString);
}
如果您能提供原理图就更好了。事物是如何联系在一起的?
当你写"halt on Serial"时,你的意思是你最后看到的任何活动的标志是串口上的打印输出?您是否尝试在循环()内闪烁板载LED ?