Hello everyone.
this is my first time using arduino wifi shield
unfortunately my wifi shield is not official one. mine is phpoc's product
I try to make my arduino as server and upload some sensor values
and python3 will be client, connected to server and get the sensor values
but my codes are not worked
I've searched on google to configure the problems, but i cant.
please read my code and if you know whats wrong. Tell me
I really need your help!
Arduino code
#include <SPI.h>
#include <Phpoc.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(13, 11); // RX 13, TX 11
unsigned char Send_data[4] = {0x11,0x01,0x01,0xED}; // 읽는명령
unsigned char Receive_Buff[16]; // data buffer
unsigned long PCS; // 수량 저장 변수
float ug; // 농도 저장 변수
unsigned char recv_cnt = 0;
PhpocServer server(1234);
boolean alreadyConnected = false; // whether or not the client was connected previously
void setup() {
pinMode(13,INPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
while (!mySerial);
while(!Serial);
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
//Phpoc.begin();
server.begin();
Serial.print("Chat server address : ");
Serial.println(Phpoc.localIP());
}
void loop() {
Send_CMD(); // Send Read Command
while(1)
{
if(mySerial.available()){
Receive_Buff[recv_cnt++] = mySerial.read();
if(recv_cnt ==16){recv_cnt = 0; break;}
}
}
if(Checksum_cal() == Receive_Buff[15]) // CS 확인을 통해 통신 에러 없으면
{
PCS = (unsigned long)Receive_Buff[3]<<24 | (unsigned long)Receive_Buff[4]<<16 | (unsigned long)Receive_Buff[5]<<8| (unsigned long)Receive_Buff[6]; // 수량
ug = (float)PCS*3528/100000; // 농도 변환(이 식은 PM1001 모델만 적용됩니다.)
Serial.write("PCS : ");
Serial.print(PCS);
Serial.write(", ug : ");
Serial.println(ug);
}
else
{
Serial.write("CHECKSUM Error");
}
// wait for a new client:
PhpocClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clear out the transmission buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
if(thisChar=="PCS"){
server.write(PCS);
}
// echo the bytes back to the client:
server.write(PCS);
server.write(ug);
// echo the bytes to the server as well:
Serial.println(PCS);
Serial.println(ug);
}
}
}
void Send_CMD(void) // COMMAND
{
unsigned char i;
for(i=0; i<4; i++)
{
mySerial.write(Send_data[i]);
delay(1); // Don't delete this line !!
}
}
unsigned char Checksum_cal(void) // CHECKSUM
{
unsigned char count, SUM=0;
for(count=0; count<15; count++)
{
SUM += Receive_Buff[count];
}
return 256-SUM;
}
Python3 code
from socket import *
import time
address=('192.168.1.34',1234)
client_socket=socket(AF_INET,SOCK_STREAM)
client_socket.settimeout(1)
while True:
data=input("PCS")
client_socket.sendto(data.encode('utf-8'),address)
try:
rec_data,addr=client_socket.recvfrom(2048)
rec_data=float(rec_data)
print(rec_data)
with open(test.txt,'a')as out:
out.write("PCS:",rec_data + '\n')
except:
pass
time.sleep(2)