Ciao a tutti, sto svolgendo un progetto nel quale utilizzo un Arduino Mega collegato in seriale con un esp-01.
Lo sketch che gira sull'esp ha il compito di inviare richieste http ad un server. Quando riceve la risposta invia ad Arduino Mega il json contenente i dati ricevuti; e fin qua tutto ok.
Arduino, oltre le altre cose, invia all'esp un json con l'aggiornamento di alcuni dati (che l'esp poi invia al server quando effettua la richiesta http).
Quindi l'esp, prima di inviare la richiesta http, legge il json che dovrebbe ricevere da Arduino.
Ecco il problema, l'esp non riceve niente. Ho provato anche ad inviare dati all'esp direttamente dal suo monitor seriale ma niente da fare.
Vi posto di seguito lo sketch che gira sull'esp:
#include <ESP8266WiFi.h>
#include "SharedContext.h"
#include "MsgService.h"
#include <ArduinoJson.h>
SharedContext* shared;
DynamicJsonBuffer jsonBuffer;
const char* ssid = "SSID";
const char* password = "psw";
const char* host = "host";
void setup() {
shared = new SharedContext();
MsgService.init();
Serial.begin(9600);
delay(10);
// We start by connecting to a WiFi network
MsgService.sendMsg("Connecting to ");
MsgService.sendMsg(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
MsgService.sendMsg(".");
}
MsgService.sendMsg("");
MsgService.sendMsg("WiFi connected");
MsgService.sendMsg("IP address: ");
MsgService.sendMsg(""+WiFi.localIP());
}
void loop() {
delay(1000);
updateData();
MsgService.sendMsg("connecting to ");
MsgService.sendMsg(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
MsgService.sendMsg("connection failed");
// Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/HttpRequestKennel.php";
String data = "usr="+shared->getUsr()+
"&tempAcq="+shared->getTempAcqua()+
"&qntCiboCiotola="+shared->getQntCiboCiotola()+
"&tempKennel="+shared->getTempKennel()+
"&presenzaAnimale="+shared->isPresenzaAnimale()+
"&pesata="+shared->getPesata();
// This will send the request to the server
client.println("POST /HttpRequestKennel.php HTTP/1.1");
client.println("Host: nomeHost.com");
client.println("Accept: */*");
client.println("Content-Length: " + String(data.length()));
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
client.println(data);
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
MsgService.sendMsg(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
String line;
while (client.available()) {
line += client.readStringUntil('\r');
// Serial.print(line);
}
int inizio = line.indexOf("{");
int fine =line.indexOf("}");
String json = line.substring(inizio, fine+1);
MsgService.sendMsg(json);
MsgService.sendMsg("closing connection");
}
void updateData(){
if (MsgService.isMsgAvailable()) {
Msg* msg = MsgService.receiveMsg();
MsgService.sendMsg("MSG RECEIVED");
MsgService.sendMsg(msg->getContent());
if (msg->getContent().substring(0,1) == "{"){
String json = msg->getContent();
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
MsgService.sendMsg("parseObject() failed");
}else{
shared->setTempAcqua(root[String("tA")]);
shared->setQntCiboCiotola(root[String("qC")]);
shared->setTempKennel(root[String("tK")]);
shared->setPresenzaAnimale(root[String("pA")]);
shared->setPesata(root[String("peso")]);
}
}
jsonBuffer.clear();
delete msg;
}
}
Classe che si occupa della ricezione e invio dei dati sulla seriale:
#include "Arduino.h"
#include "MsgService.h"
String content;
MsgServiceClass MsgService;
bool MsgServiceClass::isMsgAvailable(){
Serial.println(msgAvailable);
return msgAvailable;
}
Msg* MsgServiceClass::receiveMsg(){
if (msgAvailable){
Msg* msg = currentMsg;
msgAvailable = false;
currentMsg = NULL;
content = "";
return msg;
} else {
return NULL;
}
}
void MsgServiceClass::init(){
Serial.begin(9600);
content.reserve(256);
content = "";
currentMsg = NULL;
msgAvailable = false;
}
void MsgServiceClass::sendMsg(const String& msg){
Serial.println(msg);
}
void serialEvent() {
delay(50);
/* reading the content */
while (Serial.available()) {
content += (char)Serial.read();
}
MsgService.currentMsg = new Msg(content);
MsgService.msgAvailable = true;
}
bool MsgServiceClass::isMsgAvailable(Pattern& pattern){
return (msgAvailable && pattern.match(*currentMsg));
}
Msg* MsgServiceClass::receiveMsg(Pattern& pattern){
if (msgAvailable && pattern.match(*currentMsg)){
Msg* msg = currentMsg;
msgAvailable = false;
currentMsg = NULL;
content = "";
return msg;
} else {
return NULL;
}
}
Dove sbaglio?