所以我注意到我的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,0\r");
// Tell the pH sensor to stop all readings
Serial3.print("E\r");
// 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.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
www.fastrprint(F("Authorization: Basic ")); www.fastrprint(auth_output); www.fastrprint(F("\r\n"));
www.fastrprint(F("Content-Type: application/x-www-form-urlencoded")); www.fastrprint(F("\r\n"));
char len_c[7];
itoa(data.length(), len_c, 10);
www.fastrprint(F("Content-Length: ")); www.fastrprint(len_c); www.fastrprint(F("\r\n"));
// Extra empty line for post arguments
www.fastrprint(F("\r\n"));
char data_c[200];
data.toCharArray(data_c, data.length() + 1);
www.fastrprint(data_c); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
//displayGreen();
} else {
Serial.println(F("Connection failed"));
//displayRed();
}
www.close();
}
void getEC() {
ecSensorString = "";
Serial2.print("r\r"); // 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("R\r"); // Read only 1
char inchar;
while (Serial3.available()) {
inchar = (char)Serial3.read();
if (inchar != '\r') {
phSensorString += inchar;
}
}
Serial.print("getPH:");
Serial.println(phSensorString);
}
答案 0 :(得分:0)
如果您也可以提供原理图,这将有所帮助。事情是如何联系的?
当你写'&34;停止串行&#34;你的意思是你看到的最后一个活动的标志是串口打印输出?您是否尝试在循环内闪烁板载LED()?